| 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 MacWorkspaceTests { |
| 9 | @Test func eachWindowOwnsSelectionAndHistory() async throws { |
| 10 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 11 | let app = EquipoAppModel(store: MemorySecretStore()) |
| 12 | let one = MacWorkspace(app: app, session: detail.session) |
| 13 | let two = MacWorkspace(app: app, session: detail.session) |
| 14 | one.go(.task(detail.taskID)); two.go(.task("other")) |
| 15 | #expect(one.context?.taskID == detail.taskID) |
| 16 | #expect(two.context?.taskID == "other") |
| 17 | one.go(.project("project")); #expect(one.pane == .projects); #expect(one.projectID == "project") |
| 18 | one.back(); #expect(one.context?.taskID == detail.taskID); #expect(one.pane == .conversation) |
| 19 | #expect(!one.history.canBack) |
| 20 | one.forward(); #expect(one.pane == .projects); #expect(one.inspector) |
| 21 | #expect(!one.history.canForward) |
| 22 | one.back(); one.go(.task("another")) |
| 23 | #expect(one.history.canForward) |
| 24 | one.forward(); #expect(one.pane == .projects) |
| 25 | #expect(one.context?.taskID == "another") |
| 26 | one.newMessage(); #expect(one.pane == .conversation); #expect(one.composerFocus == 1) |
| 27 | #expect(two.context?.taskID == "other") |
| 28 | } |
| 29 | @Test func windowsCannotFollowAccountChangesOrRestoredOldSessions() throws { |
| 30 | let session = try TaskDetailModelTests.model(TaskDetailServer()).session |
| 31 | let other = try TaskDetailModelTests.model(TaskDetailServer()).session |
| 32 | let identity = MacTaskWindowID(taskID: "task", session: session) |
| 33 | #expect(identity.matches(session)) |
| 34 | #expect(!identity.matches(other)) |
| 35 | let encoded = try JSONEncoder().encode(identity) |
| 36 | #expect(try JSONDecoder().decode(MacTaskWindowID.self, from: encoded) == identity) |
| 37 | #expect(!String(decoding: encoded, as: UTF8.self).contains("token")) |
| 38 | var changedOrigin = try #require(JSONSerialization.jsonObject(with: encoded) as? [String: Any]) |
| 39 | changedOrigin["origin"] = "https://other-team.invalid" |
| 40 | let changed = try JSONDecoder().decode(MacTaskWindowID.self, from: JSONSerialization.data(withJSONObject: changedOrigin)) |
| 41 | #expect(!changed.matches(session)) |
| 42 | session.lifetime.end() |
| 43 | #expect(!identity.matches(session)) |
| 44 | } |
| 45 | @Test func taskWindowsRouteGoToTheMainWindow() throws { |
| 46 | let session = try TaskDetailModelTests.model(TaskDetailServer()).session |
| 47 | let app = EquipoAppModel(store: MemorySecretStore()) |
| 48 | let window = MacWorkspace(app: app, session: session, taskID: "task") |
| 49 | window.go(.project("project")) |
| 50 | #expect(app.destination?.projectID == "project") |
| 51 | #expect(window.context?.taskID == "task") |
| 52 | #expect(window.mainRequest == 1) |
| 53 | window.newMessage(); #expect(app.destination?.pane == .conversation) |
| 54 | } |
| 55 | @Test func paletteActionForAnotherTaskRoutesWithoutReplacingItsWindow() async throws { |
| 56 | let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) |
| 57 | await detail.load() |
| 58 | let target = MacTaskContext(detail: detail) |
| 59 | let app = EquipoAppModel(store: MemorySecretStore()) |
| 60 | let window = MacWorkspace(app: app, session: detail.session, taskID: "original") |
| 61 | detail.busy = true |
| 62 | #expect(!window.queuePaletteAction(.cancel, context: target)); #expect(app.destination == nil) |
| 63 | detail.busy = false |
| 64 | #expect(window.queuePaletteAction(.cancel, context: target)) |
| 65 | #expect(window.context?.taskID == "original") |
| 66 | #expect(app.destination?.taskID == target.taskID) |
| 67 | #expect(app.destination?.macAction == .cancel) |
| 68 | #expect(await server.writes == 0) |
| 69 | let main = MacWorkspace(app: app, session: detail.session) |
| 70 | #expect(!main.queuePaletteAction(.cancel, context: target)) |
| 71 | #expect(main.pendingAction?.action == .cancel && main.context === target) |
| 72 | await main.performRoutedAction(.cancel, context: target) |
| 73 | #expect(detail.confirmation?.action == .cancel) |
| 74 | #expect(await server.writes == 0) |
| 75 | detail.confirmation = nil; main.go(.task("other")) |
| 76 | await main.performRoutedAction(.cancel, context: target) |
| 77 | #expect(detail.confirmation == nil) |
| 78 | MacWorkspace.newMessage(app: app, focused: nil) |
| 79 | #expect(app.destination?.pane == .conversation && app.destination?.focusComposer == true) |
| 80 | } |
| 81 | @Test func commandReturnSendsButShiftReturnAndMarkedTextDoNot() throws { |
| 82 | let editor = ComposerEditor(); var sends = 0 |
| 83 | editor.onSubmit = { sends += 1 } |
| 84 | let event = try #require(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: .command, timestamp: 0, |
| 85 | windowNumber: 0, context: nil, characters: "\r", charactersIgnoringModifiers: "\r", isARepeat: false, keyCode: 36)) |
| 86 | editor.keyDown(with: event); #expect(sends == 1) |
| 87 | let shifted = try #require(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [.command, .shift], timestamp: 0, |
| 88 | windowNumber: 0, context: nil, characters: "\r", charactersIgnoringModifiers: "\r", isARepeat: false, keyCode: 36)) |
| 89 | editor.keyDown(with: shifted); #expect(sends == 1) |
| 90 | editor.setMarkedText("unfinished", selectedRange: NSRange(location: 0, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 91 | editor.keyDown(with: event); #expect(sends == 1) |
| 92 | } |
| 93 | @Test func focusRequestSurvivesAttachingTheConversationToAWindow() throws { |
| 94 | let session = try TaskDetailModelTests.model(TaskDetailServer()).session |
| 95 | let transcript = LiveTranscript(session: session, openTask: { _ in }) |
| 96 | transcript.focusComposer() |
| 97 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 900, height: 700), styleMask: [.titled], backing: .buffered, defer: false) |
| 98 | window.contentView = transcript |
| 99 | #expect(window.firstResponder === transcript.composer.editor) |
| 100 | window.contentView = nil |
| 101 | } |
| 102 | } |
| 103 | #endif |
| 104 | |