import Foundation import Testing @testable import EquipoKit @MainActor @Suite struct MacPowerTests { @Test func fuzzySearchIsOrderedCaseAndDiacriticInsensitive() { #expect(MacJumpIndex.score("tile", in: "Tile labels")! > MacJumpIndex.score("tile", in: "The icon labels overlap")!) #expect(MacJumpIndex.score("cafe", in: "Café") != nil) #expect(MacJumpIndex.score("abc", in: "cba") == nil) #expect(MacJumpIndex.score("", in: "anything") != nil) } @Test func searchIncludesBriefBranchProjectsAndNeedsWithoutDuplicateTasks() throws { var task = try TaskDetailTests.task(); task.brief = "Fix narrow phones"; task.branch = "equipo/retina" let projects: ThinList = try Fixture.decode("thin/projects") let index = MacJumpIndex(tasks: [task], needs: [task], projects: projects.items ?? []) #expect(index.results("narrow").map(\.target) == [.task(task.id)]) #expect(index.results("retina").first?.target == .task(task.id)) #expect(index.results("").filter { $0.target == .task(task.id) }.count == 1) #expect(index.results("").first { $0.target == .task(task.id) }?.needsYou == true) #expect(index.results("").contains { $0.target == .pane(.conversation) }) #expect(index.results("").contains { if case .project = $0.target { true } else { false } }) } @Test func duplicateSearchUsesTheLatestRevisionAndHidesArchivedProjects() throws { var old = try TaskDetailTests.task(); old.title = "Old title"; old.revision = 1 var latest = old; latest.title = "Latest title"; latest.revision = 2 var projects: ThinList = try Fixture.decode("thin/projects") projects.items?[0].state = "archived" for (tasks, needs) in [([latest], [old]), ([old], [latest])] { let index = MacJumpIndex(tasks: tasks, needs: needs, projects: projects.items ?? []) #expect(index.results("Latest title").first?.title == "Latest title") #expect(index.results("").filter { $0.target == .task(old.id) }.count == 1) #expect(!index.results("").contains { $0.target == .project(projects.items![0].id) }) } } @Test func historyDoesNotRepeatAndNewNavigationDropsForward() { let history = MacNavigationHistory() history.go(.pane(.tasks)); history.go(.task("a")); history.go(.task("a")) #expect(history.back() == .pane(.conversation)) #expect(history.back() == nil) #expect(history.forward() == .pane(.tasks)) #expect(!history.canForward) history.back(); history.go(.task("b")) #expect(history.canForward) #expect(history.forward() == .pane(.tasks)) history.go(.project("p")); #expect(history.forward() == nil) } @Test func paletteAndMenuReuseTaskRulesAndConfirmBeforeWriting() async throws { for action in [MacTaskAction.merge, .cancel] { let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) await detail.load(); detail.loaded?.mergeOffered = true var snapshot: Snapshot = try Fixture.decode("thin/snapshot") snapshot.cards = snapshot.cards?.map { var card = $0; card.state = "answered"; card.revision += 1; return card } detail.session.apply(snapshot) #expect(detail.decision.card == nil) let context = MacTaskContext(detail: detail) #expect(context.actions.contains(action)) await context.perform(action) #expect(await server.writes == 0) #expect(detail.confirmation?.action == (action == .merge ? .merge : .cancel)) await detail.confirm(); #expect(await server.writes == 1) } } @Test func actionsRequireFreshReadOwnerCreatorAndNotBusy() async throws { let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) let context = MacTaskContext(detail: detail) #expect(context.actions.isEmpty) await detail.load(); detail.loaded?.mergeOffered = true #expect(context.actions.contains(.merge)) detail.busy = true; #expect(context.actions.isEmpty) await context.perform(.merge); #expect(detail.confirmation == nil) detail.busy = false var snapshot: Snapshot = try Fixture.decode("thin/snapshot") snapshot.me.address = "other@example.com"; detail.session.apply(snapshot) #expect(!context.actions.contains(.merge)); #expect(!context.actions.contains(.cancel)) #expect(context.actions.contains(.move)) snapshot.me.role = "member"; detail.session.apply(snapshot) #expect(!context.actions.contains(.move)) detail.readFailure = "offline"; #expect(context.actions.isEmpty) } @Test func mergeCardUsesCardConfirmationAndRetryUsesTerminatedGuard() async throws { let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) await detail.load() var snapshot: Snapshot = try Fixture.decode("thin/snapshot") let card = try TaskDecisionTests.card(try #require(detail.task)); snapshot.cards = [card] detail.session.apply(snapshot) let context = MacTaskContext(detail: detail) await context.perform(.merge) #expect(detail.confirmation?.card?.id == card.id) #expect(await server.writes == 0) detail.loaded?.steps?[2].state = "failed"; detail.loaded?.steps?[2].terminated = true #expect(context.actions.contains(.retry)) detail.loaded?.steps?[2].terminated = false #expect(!context.actions.contains(.retry)) } @Test func webActionRequiresAReadableTaskAndUsesTheFixedAllowlistedPath() async throws { let detail = try TaskDetailModelTests.model(TaskDetailServer()) var opened: [URL] = [] let context = MacTaskContext(detail: detail, openWeb: { url, _ in opened.append(url) }) await context.perform(.web); #expect(opened.isEmpty) await context.load(); await context.perform(.web) #expect(opened.map(\.absoluteString) == ["https://task-detail.invalid/tasks/tkqsrho7oq3n"]) detail.busy = true; await context.perform(.web); #expect(opened.count == 1) } @Test func refusedAndStaleContextsCannotActAndNetworkErrorsDoNotCloseAWindow() async throws { let server = TaskDetailServer(), detail = try TaskDetailModelTests.model(server) let context = MacTaskContext(detail: detail) await context.load(); detail.loaded?.mergeOffered = true #expect(context.actions.contains(.merge)) let loaded = detail.loaded await detail.session.refresh() detail.loaded?.revision = 0 #expect(context.actions.isEmpty) detail.loaded = loaded for status in [401, 403, 404] { await server.refuseRead(status); await context.load() #expect(context.refused); #expect(context.actions.isEmpty) detail.readFailure = nil; #expect(context.actions.isEmpty) await context.perform(.cancel); #expect(detail.confirmation == nil) } await server.refuseRead(500); await context.load() #expect(!context.refused); #expect(context.actions.isEmpty) await server.refuseRead(200); await context.load() #expect(!context.actions.isEmpty) detail.session.lifetime.end() #expect(context.actions.isEmpty) } @Test func windowClosesOnlyForTerminalStateOrVerifiedLossOfVisibility() throws { var task = try TaskDetailTests.task() #expect(!MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) #expect(!MacTaskWindowPolicy.shouldClose(task: nil, initialRepo: task.repo, status: 500)) #expect(!MacTaskWindowPolicy.shouldClose(task: task, initialRepo: nil, status: nil)) for status in [401, 403, 404] { #expect(MacTaskWindowPolicy.shouldClose(task: nil, initialRepo: task.repo, status: status)) } task.stage = "cancelled" #expect(MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) task.stage = "done" #expect(!MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) #expect(MacTaskWindowPolicy.shouldClose(task: task, initialRepo: "another/repo", status: nil)) task.moving = true #expect(MacTaskWindowPolicy.shouldClose(task: task, initialRepo: task.repo, status: nil)) } }