| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import Testing |
| 5 | @testable import EquipoKit |
| 6 | @testable import EquipoMac |
| 7 | |
| 8 | @MainActor @Suite(.serialized) struct MacPowerReviewTests { |
| 9 | @Test func paneNavigationKeepsInspectorAndDraftButClearsQueuedAction() async throws { |
| 10 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 11 | await detail.load() |
| 12 | let workspace = MacWorkspace(app: EquipoAppModel(store: MemorySecretStore()), session: detail.session) |
| 13 | let context = MacTaskContext(detail: detail) |
| 14 | _ = workspace.queuePaletteAction(.cancel, context: context) |
| 15 | workspace.composerDraft.text = "unfinished message" |
| 16 | workspace.go(.pane(.needs)) |
| 17 | let cleared = workspace.pendingAction == nil |
| 18 | #expect(cleared) |
| 19 | #expect(workspace.context === context && workspace.inspector) |
| 20 | workspace.go(.pane(.conversation)) |
| 21 | #expect(workspace.composerDraft.text == "unfinished message") |
| 22 | let first = LiveTranscript(session: detail.session, openTask: { _ in }, draft: workspace.composerDraft) |
| 23 | #expect(first.composer.text == "unfinished message") |
| 24 | first.composer.editor.string = "still writing" |
| 25 | first.composer.textDidChange(Notification(name: NSText.didChangeNotification)) |
| 26 | let second = LiveTranscript(session: detail.session, openTask: { _ in }, draft: workspace.composerDraft) |
| 27 | #expect(second.composer.text == "still writing") |
| 28 | second.composer.submit() |
| 29 | #expect(workspace.composerDraft.text.isEmpty) |
| 30 | } |
| 31 | @Test(arguments: ["notification", "palette", "task-window"]) |
| 32 | func sameTaskReopenUsesVisibleModelAndRevealsHiddenInspector(_ source: String) async throws { |
| 33 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 34 | let workspace = MacWorkspace(app: EquipoAppModel(store: MemorySecretStore()), session: detail.session) |
| 35 | workspace.go(.task(detail.taskID)) |
| 36 | let old = try #require(workspace.context) |
| 37 | await old.load() |
| 38 | if source == "palette" { |
| 39 | let fresh = MacTaskContext(session: detail.session, taskID: detail.taskID) |
| 40 | await fresh.load() |
| 41 | _ = workspace.queuePaletteAction(.merge, context: fresh) |
| 42 | } else if source == "task-window" { |
| 43 | let other = MacWorkspace(app: workspace.app, session: detail.session, taskID: "other") |
| 44 | other.go(.task(detail.taskID)) |
| 45 | workspace.open(try #require(workspace.app.destination)) |
| 46 | } else { |
| 47 | workspace.open(Destination(pane: .tasks, taskID: detail.taskID)) |
| 48 | } |
| 49 | let current = try #require(workspace.context) |
| 50 | let screen = TaskScreen(context: current) |
| 51 | #expect(screen.model === current.detail) |
| 52 | workspace.inspector = false |
| 53 | await workspace.performMenuAction(.merge) |
| 54 | #expect(workspace.inspector) |
| 55 | #expect(screen.model.confirmation?.action == .merge) |
| 56 | } |
| 57 | @Test func replacingSameTaskContextInMountedScreenPresentsItsSheet() async throws { |
| 58 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 59 | let old = MacTaskContext(detail: detail); await old.load() |
| 60 | let host = NSHostingView(rootView: TaskScreen(context: old)) |
| 61 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 820, height: 900), styleMask: [.titled], backing: .buffered, defer: false) |
| 62 | window.isReleasedWhenClosed = false |
| 63 | window.contentView = host; window.makeKeyAndOrderFront(nil) |
| 64 | defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil); window.contentView = nil; window.close() } |
| 65 | try await Task.sleep(for: .milliseconds(150)) |
| 66 | let fresh = MacTaskContext(session: detail.session, taskID: detail.taskID) |
| 67 | #expect(fresh.detail.loaded == nil) |
| 68 | host.rootView = TaskScreen(context: fresh) |
| 69 | host.layoutSubtreeIfNeeded() |
| 70 | for _ in 0..<30 where fresh.detail.loaded == nil { try await Task.sleep(for: .milliseconds(50)) } |
| 71 | #expect(fresh.detail.loaded != nil) |
| 72 | await fresh.perform(.merge) |
| 73 | for _ in 0..<30 where window.attachedSheet == nil { try await Task.sleep(for: .milliseconds(50)) } |
| 74 | #expect(window.attachedSheet != nil) |
| 75 | #expect(old.detail.confirmation == nil) |
| 76 | #expect(fresh.detail.confirmation?.action == .merge) |
| 77 | } |
| 78 | @Test func navigationCancelsQueuedActionEvenWhenInspectorKeepsItsContext() async throws { |
| 79 | let detail = try TaskDetailModelTests.model(TaskDetailServer()); await detail.load() |
| 80 | let workspace = MacWorkspace(app: EquipoAppModel(store: MemorySecretStore()), session: detail.session) |
| 81 | let context = MacTaskContext(detail: detail) |
| 82 | _ = workspace.queuePaletteAction(.cancel, context: context) |
| 83 | let pending = try #require(workspace.pendingAction) |
| 84 | workspace.go(.pane(.needs)) |
| 85 | await workspace.performRoutedAction(pending.action, context: pending.context, navigation: pending.navigation) |
| 86 | #expect(detail.confirmation == nil) |
| 87 | } |
| 88 | @Test func taskWindowActionStaysOnItsOwnTask() async throws { |
| 89 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 90 | let app = EquipoAppModel(store: MemorySecretStore()) |
| 91 | let workspace = MacWorkspace(app: app, session: detail.session, taskID: detail.taskID) |
| 92 | let own = try #require(workspace.context); await own.load() |
| 93 | #expect(!workspace.queuePaletteAction(.cancel, context: own)) |
| 94 | #expect(workspace.pendingAction?.context === own) |
| 95 | #expect(app.destination == nil) |
| 96 | await workspace.performMenuAction(.cancel) |
| 97 | #expect(own.detail.confirmation?.action == .cancel) |
| 98 | } |
| 99 | @Test func briefOnlyMatchesWholeWordsAndTitlesWin() throws { |
| 100 | var title = try TaskDetailTests.task(); title.id = "title"; title.title = "Fix login button"; title.brief = ""; title.project = "zz"; title.repo = "zz"; title.branch = "zz" |
| 101 | var partial = title; partial.id = "partial"; partial.title = "Fix rebuttoning"; partial.brief = "" |
| 102 | var brief = title; brief.id = "brief"; brief.title = "Unrelated"; brief.brief = "button narrow" |
| 103 | let index = MacJumpIndex(tasks: [brief, partial, title], needs: [], projects: []) |
| 104 | #expect(index.results("button").prefix(3).map(\.target) == [.task("title"), .task("partial"), .task("brief")]) |
| 105 | #expect(!index.results("but").contains { $0.target == .task("brief") }) |
| 106 | #expect(!index.results("bn").contains { $0.target == .task("brief") }) |
| 107 | brief.brief = "a button"; brief.title = "XYZ" |
| 108 | #expect(!MacJumpIndex(tasks: [brief], needs: [], projects: []).results("a").contains { $0.target == .task("brief") }) |
| 109 | } |
| 110 | @Test func indexRebuildsForDataChangesButNotQueries() async throws { |
| 111 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 112 | let workspace = MacWorkspace(app: EquipoAppModel(store: MemorySecretStore()), session: detail.session) |
| 113 | _ = workspace.index |
| 114 | let generation = workspace.indexGeneration |
| 115 | _ = workspace.index.results("one"); _ = workspace.index.results("two") |
| 116 | #expect(workspace.indexGeneration == generation) |
| 117 | await detail.session.refresh() |
| 118 | _ = workspace.index.results("Swipe") |
| 119 | #expect(workspace.indexGeneration == generation + 1) |
| 120 | } |
| 121 | @Test func hostedPaletteKeepsFooterAccessibilityLabels() async throws { |
| 122 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 123 | let workspace = MacWorkspace(app: EquipoAppModel(store: MemorySecretStore()), session: detail.session) |
| 124 | let host = NSHostingView(rootView: MacJumpPalette(workspace: workspace).environment(\.accessibilityEnabled, true)) |
| 125 | host.frame = NSRect(x: 0, y: 0, width: 620, height: 520) |
| 126 | let window = NSWindow(contentRect: host.frame, styleMask: [.borderless], backing: .buffered, defer: false) |
| 127 | window.isReleasedWhenClosed = false |
| 128 | window.contentView = host; window.orderBack(nil) |
| 129 | defer { window.orderOut(nil); window.contentView = nil; window.close() } |
| 130 | for _ in 0..<10 { host.layoutSubtreeIfNeeded(); host.displayIfNeeded(); try await Task.sleep(for: .milliseconds(40)) } |
| 131 | // SwiftUI's accessibility elements expose Objective-C selectors without |
| 132 | // necessarily advertising NSAccessibilityProtocol conformance. |
| 133 | func attribute(_ element: NSObject, _ name: String) -> Any? { |
| 134 | let selector = NSSelectorFromString(name) |
| 135 | guard element.responds(to: selector) else { return nil } |
| 136 | return element.perform(selector)?.takeUnretainedValue() |
| 137 | } |
| 138 | var buttons: [String] = [], groups: [String] = [], visited: Set<ObjectIdentifier> = [] |
| 139 | func walk(_ element: NSObject) { |
| 140 | guard visited.insert(ObjectIdentifier(element)).inserted, visited.count < 1000 else { return } |
| 141 | let role = attribute(element, "accessibilityRole") as? String |
| 142 | if let label = attribute(element, "accessibilityLabel") as? String { |
| 143 | if role == "AXButton" { buttons.append(label) } |
| 144 | if role == "AXGroup" { groups.append(label) } |
| 145 | } |
| 146 | for name in ["accessibilityChildren", "accessibilityContents", "accessibilityVisibleChildren", "accessibilityChildrenInNavigationOrder"] { |
| 147 | for child in (attribute(element, name) as? [NSObject]) ?? [] { walk(child) } |
| 148 | } |
| 149 | } |
| 150 | walk(host) |
| 151 | #expect(groups.contains(EquipoStrings.text("native.mac.jump"))) |
| 152 | #expect(buttons.contains(EquipoStrings.text("chat.card.not_now"))) |
| 153 | #expect(buttons.contains(EquipoStrings.text("native.mac.open_window"))) |
| 154 | } |
| 155 | @Test func presentationContractsKeepContextAndWindowCommandWiring() throws { |
| 156 | let sources = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Sources") |
| 157 | let palette = try String(contentsOf: sources.appendingPathComponent("EquipoMac/MacJumpPalette.swift"), encoding: .utf8) |
| 158 | #expect(palette.contains("guard Self.canActivate(firstResponder: NSApp.keyWindow?.firstResponder)")) |
| 159 | let screen = try String(contentsOf: sources.appendingPathComponent("EquipoKit/UI/Screens/TaskDetailScreen.swift"), encoding: .utf8) |
| 160 | #expect(screen.contains("macContext?.detail ?? ownedModel!")) |
| 161 | let app = try String(contentsOf: sources.appendingPathComponent("EquipoMac/EquipoMacApp.swift"), encoding: .utf8) |
| 162 | #expect(app.contains(".restorationBehavior(.disabled)")) |
| 163 | let commands = try String(contentsOf: sources.appendingPathComponent("EquipoMac/MacPowerCommands.swift"), encoding: .utf8) |
| 164 | #expect(commands.contains("performMenuAction(action)")) |
| 165 | } |
| 166 | @Test func markedTextDoesNotActivatePalette() { |
| 167 | let editor = NSTextView() |
| 168 | #expect(MacJumpPalette.canActivate(firstResponder: editor)) |
| 169 | editor.setMarkedText("未", selectedRange: NSRange(location: 0, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 170 | #expect(!MacJumpPalette.canActivate(firstResponder: editor)) |
| 171 | } |
| 172 | } |
| 173 | #endif |
| 174 | |