| 1 | #if os(macOS) |
| 2 | import Foundation |
| 3 | import Testing |
| 4 | import RastrilloApple |
| 5 | import DocsKit |
| 6 | @testable import DocsMac |
| 7 | |
| 8 | @MainActor @Test func windowsReuseDocumentsWithinAccountAndOriginOnly() { |
| 9 | let windows = DocsWindows() |
| 10 | let first = NativeHTTP(origin: URL(string: "https://first.invalid")!) |
| 11 | let second = NativeHTTP(origin: URL(string: "https://second.invalid")!) |
| 12 | let document: String = "same" |
| 13 | let id = windows.reserve(account: "a", document: document, title: "One", client: first) |
| 14 | #expect(windows.reserve(account: "a", document: document, title: "Renamed", client: first) == id) |
| 15 | #expect(windows.entries.count == 1) |
| 16 | #expect(!windows.canSignOut("a")) |
| 17 | #expect(windows.reserve(account: "b", document: document, title: "Other account", client: first) != id) |
| 18 | #expect(windows.reserve(account: "a", document: document, title: "Other server", client: second) != id) |
| 19 | #expect(windows.entries.count == 3) |
| 20 | windows.setOpen(id, false) |
| 21 | #expect(!windows.canSignOut("a")) |
| 22 | for entry in windows.entries where entry.account == "a" { windows.setOpen(entry.id, false) } |
| 23 | #expect(windows.canSignOut("a")) |
| 24 | windows.updateRecents([(document, "Current title")], account: "a", client: first) |
| 25 | #expect(windows.entries.first(where: { $0.id == id })?.title == "Current title") |
| 26 | #expect(windows.entries.first(where: { $0.id == id })?.isOpen == false) |
| 27 | } |
| 28 | @MainActor @Test func draftWindowsKeepAccountAndIdentityThroughAttachment() { |
| 29 | let windows = DocsWindows() |
| 30 | let client = NativeHTTP(origin: URL(string: "https://draft.invalid")!) |
| 31 | let first = windows.reserveDraft(account: "a", client: client) |
| 32 | let second = windows.reserveDraft(account: "a", client: client) |
| 33 | #expect(first != second) |
| 34 | #expect(!windows.canSignOut("a")) |
| 35 | windows.activeAccount = "b" |
| 36 | windows.attach(first, document: "created", title: "Renamed") |
| 37 | #expect(windows.entries.first(where: { $0.id == first })?.account == "a") |
| 38 | #expect(windows.entries.first(where: { $0.id == first })?.isDraft == false) |
| 39 | #expect(windows.reserve(account: "a", document: "created", title: "Renamed", client: client) == first) |
| 40 | windows.setOpen(second, false) |
| 41 | #expect(!windows.entries.contains(where: { $0.id == second })) |
| 42 | windows.setOpen(first, false) |
| 43 | #expect(windows.canSignOut("a")) |
| 44 | } |
| 45 | #endif |
| 46 | #if os(macOS) |
| 47 | /// Signing out of an Oficina ID ends every one of its teams, so any open |
| 48 | /// document window of any of them blocks it, and Settings asks the same. |
| 49 | @MainActor @Test func oficinaIDSignOutIsBlockedByAnyOfItsTeamsOpenWindows() async { |
| 50 | let windows = DocsWindows() |
| 51 | let client = NativeHTTP(origin: URL(string: "https://tito.docs.oficina.works")!) |
| 52 | let tito = DocsOficinaSession.accountID(identity: "id-paul", team: "t2") |
| 53 | let paul = DocsOficinaSession.accountID(identity: "id-paul", team: "t1") |
| 54 | let id = windows.reserve(account: tito, document: "d", title: "Plan", client: client) |
| 55 | windows.activeAccount = paul |
| 56 | windows.settingsIdentity = "id-paul" |
| 57 | #expect(windows.canSignOut(paul), "the home team's own windows are none") |
| 58 | #expect(windows.hasOpenDocuments(identity: "id-paul"), "but another team of the identity has one") |
| 59 | #expect(!windows.hasOpenDocuments(identity: "id-till")) |
| 60 | #expect(await windows.leaveBlock() == .openDocuments) |
| 61 | windows.setOpen(id, false) |
| 62 | #expect(!windows.hasOpenDocuments(identity: "id-paul")) |
| 63 | // A server account: its own windows only. |
| 64 | windows.settingsIdentity = nil |
| 65 | let server = UUID().uuidString |
| 66 | windows.activeAccount = server |
| 67 | _ = windows.reserve(account: server, document: "e", title: "Notes", client: client) |
| 68 | #expect(await windows.leaveBlock() == .openDocuments) |
| 69 | } |
| 70 | #endif |
| 71 | #if os(macOS) |
| 72 | /// Delete… (spec 2026-11-22 §1): File ▸ Recent forgets the document, and |
| 73 | /// its entry goes when the window closes, with any other closed entry for it. |
| 74 | @MainActor @Test func aDeletedDocumentLeavesRecentAndGoesWithItsWindow() { |
| 75 | let windows = DocsWindows() |
| 76 | let client = NativeHTTP(origin: URL(string: "https://delete.invalid")!) |
| 77 | let open = windows.reserve(account: "a", document: "gone", title: "Old minutes", client: client) |
| 78 | windows.updateRecents([("kept", "Kept"), ("gone", "Old minutes")], account: "a", client: client) |
| 79 | windows.forget(open) |
| 80 | #expect(windows.entries.first(where: { $0.id == open })?.isDeleted == true) |
| 81 | #expect(windows.entries.filter { $0.document == "gone" }.count == 1, "no closed entry for it is left behind") |
| 82 | #expect(!windows.canSignOut("a"), "its window is still open until it closes") |
| 83 | windows.setOpen(open, false) |
| 84 | #expect(!windows.entries.contains { $0.document == "gone" }, "gone with its window") |
| 85 | #expect(windows.entries.contains { $0.document == "kept" }) |
| 86 | #expect(windows.canSignOut("a")) |
| 87 | } |
| 88 | #endif |
| 89 | |