import Foundation import Observation import Synchronization import RastrilloApple import RastrilloTestSupport import XCTest @testable import DocsKit /// A workspace's initialiser runs on every body pass of its window, so it /// must leave alone everything a view watches. Docs 0.1.7 and 0.1.8 restored /// the account's session from there: the window's sync banner saw the /// drainer change, re-ran the body, which made a workspace again, which /// changed the drainer again — the main thread spun at launch for anyone /// with an account (a beachball on the Mac, the watchdog on iPhone). @MainActor final class WorkspaceInitTests: XCTestCase { private func knownAccount(_ transport: @escaping @MainActor () -> NativeTransport?) -> (String, DocsOfflineAccount) { let id = "workspace-init-\(UUID().uuidString)" let offline = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: id, connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied)), transport: transport) DocsOfflineAccount.accounts[id] = offline return (id, offline) } func testLookingUpAKnownAccountChangesNothingAViewWatches() { let server = ScriptedTransport([]) let (id, offline) = knownAccount({ server }) defer { DocsOfflineAccount.accounts[id] = nil } let changed = Mutex(false) withObservationTracking { // What the window reads: the sync banner's status, and the account itself. _ = offline.drainer.status() _ = offline.drainer.sessionEnded _ = offline.drainer.phase _ = offline.drainer } onChange: { changed.withLock { $0 = true } } for _ in 0..<3 { let again = DocsOfflineAccount.account(id, origin: URL(string: "https://docs.example")!, fallback: server, transport: { server }) XCTAssertTrue(again === offline, "one account per ID") } XCTAssertFalse(changed.withLock { $0 }, "a lookup made from a view's init must not invalidate the view") } func testBuildingAWorkspaceForAKnownAccountChangesNothingItsWindowWatches() { let server = ScriptedTransport([]) let (id, offline) = knownAccount({ server }) defer { DocsOfflineAccount.accounts[id] = nil } let client = NativeHTTP(origin: URL(string: "https://docs.example")!) let changed = Mutex(false) // As a window's body does: build the workspace (its init reads // whatever it reads), then build it again on the next pass. withObservationTracking { _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) _ = offline.drainer.status() } onChange: { changed.withLock { $0 = true } } for _ in 0..<3 { _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) } XCTAssertFalse(changed.withLock { $0 }, "a workspace's init must not invalidate the window that builds it") } func testResumeRestartsAnOutboxStoppedByTheEndedSession() async throws { var supplied: NativeTransport? = nil let (id, offline) = knownAccount({ supplied }) defer { DocsOfflineAccount.accounts[id] = nil } try await offline.outbox.enqueue(lane: "doc:k", kind: "update", payload: Data([1])) let stopped = await offline.drainer.drain() XCTAssertEqual(stopped, .sessionEnded) XCTAssertTrue(offline.drainer.sessionEnded) // Signing in again mounts a new workspace; it resumes when it appears. let server = ScriptedTransport([]) supplied = server offline.resume(transport: { server }) XCTAssertFalse(offline.drainer.sessionEnded, "the new session restarts the stopped outbox") } }