import Foundation import Testing @testable import EquipoKit @Suite struct NeedsRowActionTests { static func stopped() throws -> ThinTask { var task = try TaskDetailTests.task() task.stage = "in_progress"; task.needsYou = "retry" var step = task.steps![0]; step.state = "failed"; step.reason = "push_failed"; step.terminated = true task.steps = [step]; return task } static func action(_ task: ThinTask, cards: [ThinCard] = [], mine: Bool = true, owner: Bool = true, resume: Bool = false) -> NeedsRowAction { .init(task: task, cards: cards, mine: mine, owner: owner, canResume: resume) } @Test func retryIsTheListDecisionNotTheBroaderMenuOffer() throws { var task = try Self.stopped() for need in ["retry", "review_blocked"] { task.needsYou = need; #expect(Self.action(task).kind == .retry) } for need in ["fix_rounds", "merge_conflict", "branch_moved", "machine_removed", "direction", "future", ""] { task.needsYou = need; #expect(Self.action(task).kind == .open) } task.needsYou = "retry"; task.steps![0].terminated = false #expect(Self.action(task).kind == .open) task.steps![0].terminated = true; task.steps![0].state = "done" #expect(Self.action(task).kind == .open) } @Test(arguments: ["other_creator", "member", "done", "cancelled", "merging", "moving", "stopping", "copy_hold", "copy_skip"]) func ownershipAndLifecycleGuardRows(_ state: String) throws { var task = try Self.stopped() if ["done", "cancelled", "merging"].contains(state) { task.stage = state } if state == "moving" { task.moving = true } if state == "stopping" { task.stopping = true } if state == "copy_hold" { task.copyHold = true } if state == "copy_skip" { task.copyCanSkip = true } #expect(Self.action(task, mine: state != "other_creator", owner: state != "member").kind == .open) } @Test func questionRendersAndMergeRequiresTheActualDecisionChoice() throws { var task = try Self.stopped(); task.needsYou = "merge" var merge = try TaskDecisionTests.card(task) var question = try TaskDecisionTests.card(task, kind: "question", options: ["Yes", "No"]) question.id = "question"; question.createdAt = merge.createdAt.addingTimeInterval(1) #expect(Self.action(task, cards: [question]).kind == .question) #expect(Self.action(task, cards: [question]).card?.id == question.id) #expect(Self.action(task, cards: [merge, question]).kind == .merge) task.needsYou = "retry" #expect(Self.action(task, cards: [merge]).kind == .open) task.needsYou = "merge"; merge.options = ["wait"] #expect(Self.action(task, cards: [merge]).kind == .open) merge.options = ["merge", "future"] #expect(Self.action(task, cards: [merge]).kind == .open) merge.state = "answered" #expect(Self.action(task, cards: [merge]).kind == .open) // A menu offer without a merge card is not a list decision. #expect(Self.action(task).kind == .open) } @Test func holdsReuseTheResumeMatchAndRunNowOffer() throws { var task = try Self.stopped(); task.needsYou = "held" task.steps![0].state = "running" task.steps![0].hold = ThinHold(reason: "spend_unknown", connection: "local", provider: "codex", stepRevision: 9) #expect(Self.action(task, resume: false).kind == .open) #expect(Self.action(task, resume: true).kind == .resume) task.steps![0].hold?.reason = "headroom" #expect(Self.action(task, resume: true).kind == .open) task.steps![0].paused = ThinPaused(runNow: ThinRunNow(connection: "connection", name: "Codex")) #expect(Self.action(task).kind == .runNow) #expect(Self.action(task).pause?.runNow?.revision == 9) task.needsYou = "fix_rounds" #expect(Self.action(task, resume: true).kind == .open) task.needsYou = "" #expect(Self.action(task, resume: true).kind == .open) task.needsYou = "held"; task.steps![0].paused = nil let held = try TaskDecisionTests.card(task, kind: "held", options: ["run_now", "wait"]) #expect(Self.action(task, cards: [held]).kind == .open) task.steps![0].paused = ThinPaused(runNow: ThinRunNow(connection: "shown", name: "Codex")) #expect(Self.action(task, cards: [held]).kind == .runNow) #expect(Self.action(task, cards: [held]).card?.id == held.id) task.needsYou = "retry" #expect(Self.action(task, cards: [held]).kind == .open) task.needsYou = "held" var wait = held; wait.options = ["wait"] #expect(Self.action(task, cards: [wait]).kind == .open) wait.options = ["run_now", "future"] #expect(Self.action(task, cards: [wait]).kind == .open) } }