import Foundation import Testing @testable import EquipoKit @Suite struct TaskDecisionTests { static func card(_ task: ThinTask, kind: String = "merge", options: [String] = ["merge", "wait"]) throws -> ThinCard { let answer: Answered = try Fixture.decode("thin/answered") var card = answer.card; card.taskId = task.id; card.kind = kind; card.state = "open"; card.options = options return card } @Test func onlyCurrentOwnOpenTaskCardsCanBeAnswered() throws { var task = try TaskDetailTests.task(); let card = try Self.card(task) let decision = { (t: ThinTask, c: ThinCard, mine: Bool, owner: Bool) in TaskDecision(task: t, cards: [c], mine: mine, owner: owner).card } #expect(decision(task, card, true, true)?.id == card.id) #expect(decision(task, card, false, true) == nil) #expect(decision(task, card, true, false) == nil) for stage in ["done", "cancelled", "merging"] { var ended = task; ended.stage = stage; #expect(decision(ended, card, true, true) == nil) } task.moving = true; #expect(decision(task, card, true, true) == nil) task.moving = false; task.stopping = true; #expect(decision(task, card, true, true) == nil) task.stopping = false; task.copyHold = true; #expect(decision(task, card, true, true) == nil) task.copyHold = false; task.copyCanSkip = true; #expect(decision(task, card, true, true) == nil) task.copyCanSkip = false var stale = card; stale.state = "answered"; #expect(decision(task, stale, true, true) == nil) stale = card; stale.taskId = "other"; #expect(decision(task, stale, true, true) == nil) for kind in ["task", "merge_receipt"] { stale = card; stale.kind = kind; #expect(decision(task, stale, true, true) == nil) } task.kind = "explore"; #expect(decision(task, card, true, true) == nil) } @Test func decisionChoicesAreAllowlistedAndDirectionNeedsANote() throws { let task = try TaskDetailTests.task() let direction = try Self.card(task, kind: "direction", options: ["build", "again", "drop"]) let d = TaskDecision(task: task, cards: [direction], mine: true, owner: true) #expect(d.choices.map(\.value) == ["build", "again", "drop"]) #expect(d.canAnswer("build", note: "")) #expect(!d.canAnswer("again", note: " \n")) #expect(d.canAnswer("again", note: "Keep the current layout")) #expect(!d.canAnswer("again", note: String(repeating: "a", count: 1001))) #expect(!d.canAnswer("merge", note: "")) var unknown = direction; unknown.options = ["new_server_option"] #expect(TaskDecision(task: task, cards: [unknown], mine: true, owner: true).choices.isEmpty) unknown.kind = "new_card_kind"; unknown.options = ["build"] #expect(TaskDecision(task: task, cards: [unknown], mine: true, owner: true).choices.isEmpty) } } extension TaskDecisionTests { @Test func latestOpenCardWinsAndHeldChoicesStayNative() throws { let task = try TaskDetailTests.task() var first = try Self.card(task); first.id = "first" var latest = try Self.card(task, kind: "held", options: ["run_now", "wait"]) latest.id = "latest"; latest.createdAt = first.createdAt.addingTimeInterval(1) let decision = TaskDecision(task: task, cards: [latest, first], mine: true, owner: true) #expect(decision.card?.id == "latest") #expect(decision.choices.map(\.value) == ["run_now", "wait"]) var wrong = latest; wrong.kind = "direction"; wrong.options = ["merge"] #expect(TaskDecision(task: task, cards: [wrong], mine: true, owner: true).choices.isEmpty) } } extension TaskDecisionTests { @Test func questionsAreFallbackButCannotReplaceTheDecision() throws { let task = try TaskDetailTests.task() let merge = try Self.card(task) var question = try Self.card(task, kind: "question", options: ["Keep it", "Change it"]); question.id = "new-question"; question.createdAt = merge.createdAt.addingTimeInterval(1) var unknown = question; unknown.id = "new-unknown"; unknown.kind = "future"; unknown.createdAt = question.createdAt.addingTimeInterval(1) #expect(TaskDecision(task: task, cards: [merge, question, unknown], mine: true, owner: true).card?.id == merge.id) let fallback = TaskDecision(task: task, cards: [question, unknown], mine: true, owner: true) #expect(fallback.card?.id == question.id) #expect(fallback.card?.options == ["Keep it", "Change it"]) #expect(TaskDecision(task: task, cards: [question], mine: true, owner: true).card?.id == question.id) question.state = "answered" #expect(TaskDecision(task: task, cards: [question], mine: true, owner: true).card == nil) } @Test func unknownOptionsRequireWholeCardWebFallback() throws { let task = try TaskDetailTests.task() for (kind, known) in [("merge", ["merge", "wait"]), ("direction", ["build", "again", "drop"]), ("held", ["run_now", "wait"])] { let card = try Self.card(task, kind: kind, options: [known[0], "future_option"] + known.dropFirst()) let d = TaskDecision(task: task, cards: [card], mine: true, owner: true) #expect(d.card?.id == card.id) #expect(d.choices.isEmpty) #expect(!d.canAnswer(known[0], note: "")) #expect(!d.canAnswer("future_option", note: "")) } } }