| 1 | import Foundation |
| 2 | import Observation |
| 3 | import Synchronization |
| 4 | import RastrilloApple |
| 5 | import RastrilloTestSupport |
| 6 | import XCTest |
| 7 | @testable import DocsKit |
| 8 | |
| 9 | /// A workspace's initialiser runs on every body pass of its window, so it |
| 10 | /// must leave alone everything a view watches. Docs 0.1.7 and 0.1.8 restored |
| 11 | /// the account's session from there: the window's sync banner saw the |
| 12 | /// drainer change, re-ran the body, which made a workspace again, which |
| 13 | /// changed the drainer again — the main thread spun at launch for anyone |
| 14 | /// with an account (a beachball on the Mac, the watchdog on iPhone). |
| 15 | @MainActor final class WorkspaceInitTests: XCTestCase { |
| 16 | private func knownAccount(_ transport: @escaping @MainActor () -> NativeTransport?) -> (String, DocsOfflineAccount) { |
| 17 | let id = "workspace-init-\(UUID().uuidString)" |
| 18 | let offline = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: id, |
| 19 | connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied)), |
| 20 | transport: transport) |
| 21 | DocsOfflineAccount.accounts[id] = offline |
| 22 | return (id, offline) |
| 23 | } |
| 24 | |
| 25 | func testLookingUpAKnownAccountChangesNothingAViewWatches() { |
| 26 | let server = ScriptedTransport([]) |
| 27 | let (id, offline) = knownAccount({ server }) |
| 28 | defer { DocsOfflineAccount.accounts[id] = nil } |
| 29 | let changed = Mutex(false) |
| 30 | withObservationTracking { |
| 31 | // What the window reads: the sync banner's status, and the account itself. |
| 32 | _ = offline.drainer.status() |
| 33 | _ = offline.drainer.sessionEnded |
| 34 | _ = offline.drainer.phase |
| 35 | _ = offline.drainer |
| 36 | } onChange: { changed.withLock { $0 = true } } |
| 37 | |
| 38 | for _ in 0..<3 { |
| 39 | let again = DocsOfflineAccount.account(id, origin: URL(string: "https://docs.example")!, fallback: server, transport: { server }) |
| 40 | XCTAssertTrue(again === offline, "one account per ID") |
| 41 | } |
| 42 | XCTAssertFalse(changed.withLock { $0 }, "a lookup made from a view's init must not invalidate the view") |
| 43 | } |
| 44 | |
| 45 | func testBuildingAWorkspaceForAKnownAccountChangesNothingItsWindowWatches() { |
| 46 | let server = ScriptedTransport([]) |
| 47 | let (id, offline) = knownAccount({ server }) |
| 48 | defer { DocsOfflineAccount.accounts[id] = nil } |
| 49 | let client = NativeHTTP(origin: URL(string: "https://docs.example")!) |
| 50 | let changed = Mutex(false) |
| 51 | // As a window's body does: build the workspace (its init reads |
| 52 | // whatever it reads), then build it again on the next pass. |
| 53 | withObservationTracking { |
| 54 | _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) |
| 55 | _ = offline.drainer.status() |
| 56 | } onChange: { changed.withLock { $0 = true } } |
| 57 | for _ in 0..<3 { |
| 58 | _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) |
| 59 | } |
| 60 | XCTAssertFalse(changed.withLock { $0 }, "a workspace's init must not invalidate the window that builds it") |
| 61 | } |
| 62 | |
| 63 | func testResumeRestartsAnOutboxStoppedByTheEndedSession() async throws { |
| 64 | var supplied: NativeTransport? = nil |
| 65 | let (id, offline) = knownAccount({ supplied }) |
| 66 | defer { DocsOfflineAccount.accounts[id] = nil } |
| 67 | try await offline.outbox.enqueue(lane: "doc:k", kind: "update", payload: Data([1])) |
| 68 | let stopped = await offline.drainer.drain() |
| 69 | XCTAssertEqual(stopped, .sessionEnded) |
| 70 | XCTAssertTrue(offline.drainer.sessionEnded) |
| 71 | |
| 72 | // Signing in again mounts a new workspace; it resumes when it appears. |
| 73 | let server = ScriptedTransport([]) |
| 74 | supplied = server |
| 75 | offline.resume(transport: { server }) |
| 76 | XCTAssertFalse(offline.drainer.sessionEnded, "the new session restarts the stopped outbox") |
| 77 | } |
| 78 | } |
| 79 | |