| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @MainActor @Suite struct MacPowerTests { |
| 6 | @Test func fuzzySearchIsOrderedCaseAndDiacriticInsensitive() { |
| 7 | #expect(MacJumpIndex.score("tile", in: "Tile labels")! > MacJumpIndex.score("tile", in: "The icon labels overlap")!) |
| 8 | #expect(MacJumpIndex.score("cafe", in: "Café") != nil) |
| 9 | #expect(MacJumpIndex.score("abc", in: "cba") == nil) |
| 10 | #expect(MacJumpIndex.score("", in: "anything") != nil) |
| 11 | } |
| 12 | @Test func searchIncludesBriefBranchProjectsAndNeedsWithoutDuplicateTasks() throws { |
| 13 | var task = try TaskDetailTests.task(); task.brief = "Fix narrow phones"; task.branch = "equipo/retina" |
| 14 | let projects: ThinList<ThinProject> = try Fixture.decode("thin/projects") |
| 15 | let index = MacJumpIndex(tasks: [task], needs: [task], projects: projects.items ?? []) |
| 16 | #expect(index.results("narrow").map(\.target) == [.task(task.id)]) |
| 17 | #expect(index.results("retina").first?.target == .task(task.id)) |
| 18 | #expect(index.results("").filter { $0.target == .task(task.id) }.count == 1) |
| 19 | #expect(index.results("").first { $0.target == .task(task.id) }?.needsYou == true) |
| 20 | #expect(index.results("").contains { $0.target == .pane(.conversation) }) |
| 21 | #expect(index.results("").contains { if case .project = $0.target { true } else { false } }) |
| 22 | } |
| 23 | @Test func duplicateSearchUsesTheLatestRevisionAndHidesArchivedProjects() throws { |
| 24 | var old = try TaskDetailTests.task(); old.title = "Old title"; old.revision = 1 |
| 25 | var latest = old; latest.title = "Latest title"; latest.revision = 2 |
| 26 | var projects: ThinList<ThinProject> = try Fixture.decode("thin/projects") |
| 27 | projects.items?[0].state = "archived" |
| 28 | for (tasks, needs) in [([latest], [old]), ([old], [latest])] { |
| 29 | let index = MacJumpIndex(tasks: tasks, needs: needs, projects: projects.items ?? []) |
| 30 | #expect(index.results("Latest title").first?.title == "Latest title") |
| 31 | #expect(index.results("").filter { $0.target == .task(old.id) }.count == 1) |
| 32 | #expect(!index.results("").contains { $0.target == .project(projects.items![0].id) }) |
| 33 | } |
| 34 | } |
| 35 | @Test func historyDoesNotRepeatAndNewNavigationDropsForward() { |
| 36 | let history = MacNavigationHistory() |
| 37 | history.go(.pane(.tasks)); history.go(.task("a")); history.go(.task("a")) |
| 38 | #expect(history.back() == .pane(.conversation)) |
| 39 | #expect(history.back() == nil) |
| 40 | #expect(history.forward() == .pane(.tasks)) |
| 41 | #expect(!history.canForward) |
| 42 | history.back(); history.go(.task("b")) |
| 43 | #expect(history.canForward) |
| 44 | #expect(history.forward() == .pane(.tasks)) |
| 45 | history.go(.project("p")); #expect(history.forward() == nil) |
| 46 | } |
| 47 | @Test func paletteAndMenuReuseTaskRulesAndConfirmBeforeWriting() async throws { |
| 48 | for action in [MacTaskAction.merge, .cancel] { |
| 49 | let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) |
| 50 | await detail.load(); detail.loaded?.mergeOffered = true |
| 51 | var snapshot: Snapshot = try Fixture.decode("thin/snapshot") |
| 52 | snapshot.cards = snapshot.cards?.map { var card = $0; card.state = "answered"; card.revision += 1; return card } |
| 53 | detail.session.apply(snapshot) |
| 54 | #expect(detail.decision.card == nil) |
| 55 | let context = MacTaskContext(detail: detail) |
| 56 | #expect(context.actions.contains(action)) |
| 57 | await context.perform(action) |
| 58 | #expect(await server.writes == 0) |
| 59 | #expect(detail.confirmation?.action == (action == .merge ? .merge : .cancel)) |
| 60 | await detail.confirm(); #expect(await server.writes == 1) |
| 61 | } |
| 62 | } |
| 63 | @Test func actionsRequireFreshReadOwnerCreatorAndNotBusy() async throws { |
| 64 | let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) |
| 65 | let context = MacTaskContext(detail: detail) |
| 66 | #expect(context.actions.isEmpty) |
| 67 | await detail.load(); detail.loaded?.mergeOffered = true |
| 68 | #expect(context.actions.contains(.merge)) |
| 69 | detail.busy = true; #expect(context.actions.isEmpty) |
| 70 | await context.perform(.merge); #expect(detail.confirmation == nil) |
| 71 | detail.busy = false |
| 72 | var snapshot: Snapshot = try Fixture.decode("thin/snapshot") |
| 73 | snapshot.me.address = "other@example.com"; detail.session.apply(snapshot) |
| 74 | #expect(!context.actions.contains(.merge)); #expect(!context.actions.contains(.cancel)) |
| 75 | #expect(context.actions.contains(.move)) |
| 76 | snapshot.me.role = "member"; detail.session.apply(snapshot) |
| 77 | #expect(!context.actions.contains(.move)) |
| 78 | detail.readFailure = "offline"; #expect(context.actions.isEmpty) |
| 79 | } |
| 80 | @Test func mergeCardUsesCardConfirmationAndRetryUsesTerminatedGuard() async throws { |
| 81 | let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) |
| 82 | await detail.load() |
| 83 | var snapshot: Snapshot = try Fixture.decode("thin/snapshot") |
| 84 | let card = try TaskDecisionTests.card(try #require(detail.task)); snapshot.cards = [card] |
| 85 | detail.session.apply(snapshot) |
| 86 | let context = MacTaskContext(detail: detail) |
| 87 | await context.perform(.merge) |
| 88 | #expect(detail.confirmation?.card?.id == card.id) |
| 89 | #expect(await server.writes == 0) |
| 90 | detail.loaded?.steps?[2].state = "failed"; detail.loaded?.steps?[2].terminated = true |
| 91 | #expect(context.actions.contains(.retry)) |
| 92 | detail.loaded?.steps?[2].terminated = false |
| 93 | #expect(!context.actions.contains(.retry)) |
| 94 | } |
| 95 | @Test func webActionRequiresAReadableTaskAndUsesTheFixedAllowlistedPath() async throws { |
| 96 | let detail = try TaskDetailModelTests.model(TaskDetailServer()) |
| 97 | var opened: [URL] = [] |
| 98 | let context = MacTaskContext(detail: detail, openWeb: { url, _ in opened.append(url) }) |
| 99 | await context.perform(.web); #expect(opened.isEmpty) |
| 100 | await context.load(); await context.perform(.web) |
| 101 | #expect(opened.map(\.absoluteString) == ["https://task-detail.invalid/tasks/tkqsrho7oq3n"]) |
| 102 | detail.busy = true; await context.perform(.web); #expect(opened.count == 1) |
| 103 | } |
| 104 | @Test func refusedAndStaleContextsCannotActAndNetworkErrorsDoNotCloseAWindow() async throws { |
| 105 | let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) |
| 106 | let context = MacTaskContext(detail: detail) |
| 107 | await context.load(); detail.loaded?.mergeOffered = true |
| 108 | #expect(context.actions.contains(.merge)) |
| 109 | let loaded = detail.loaded |
| 110 | await detail.session.refresh() |
| 111 | detail.loaded?.revision = 0 |
| 112 | #expect(context.actions.isEmpty) |
| 113 | detail.loaded = loaded |
| 114 | for status in [401, 403, 404] { |
| 115 | await server.refuseRead(status); await context.load() |
| 116 | #expect(context.refused); #expect(context.actions.isEmpty) |
| 117 | detail.readFailure = nil; #expect(context.actions.isEmpty) |
| 118 | await context.perform(.cancel); #expect(detail.confirmation == nil) |
| 119 | } |
| 120 | await server.refuseRead(500); await context.load() |
| 121 | #expect(!context.refused); #expect(context.actions.isEmpty) |
| 122 | await server.refuseRead(200); await context.load() |
| 123 | #expect(!context.actions.isEmpty) |
| 124 | detail.session.lifetime.end() |
| 125 | #expect(context.actions.isEmpty) |
| 126 | } |
| 127 | @Test func windowClosesOnlyForTerminalStateOrVerifiedLossOfVisibility() throws { |
| 128 | var task = try TaskDetailTests.task() |
| 129 | #expect(!MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) |
| 130 | #expect(!MacTaskWindowPolicy.shouldClose(task: nil, initialRepo: task.repo, status: 500)) |
| 131 | #expect(!MacTaskWindowPolicy.shouldClose(task: task, initialRepo: nil, status: nil)) |
| 132 | for status in [401, 403, 404] { #expect(MacTaskWindowPolicy.shouldClose(task: nil, initialRepo: task.repo, status: status)) } |
| 133 | task.stage = "cancelled" |
| 134 | #expect(MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) |
| 135 | task.stage = "done" |
| 136 | #expect(!MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) |
| 137 | #expect(MacTaskWindowPolicy.shouldClose(task: task, initialRepo: "another/repo", status: nil)) |
| 138 | task.moving = true |
| 139 | #expect(MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) |
| 140 | } |
| 141 | } |
| 142 | |