| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @Suite struct TaskDecisionTests { |
| 6 | static func card(_ task: ThinTask, kind: String = "merge", options: [String] = ["merge", "wait"]) throws -> ThinCard { |
| 7 | let answer: Answered = try Fixture.decode("thin/answered") |
| 8 | var card = answer.card; card.taskId = task.id; card.kind = kind; card.state = "open"; card.options = options |
| 9 | return card |
| 10 | } |
| 11 | @Test func onlyCurrentOwnOpenTaskCardsCanBeAnswered() throws { |
| 12 | var task = try TaskDetailTests.task(); let card = try Self.card(task) |
| 13 | let decision = { (t: ThinTask, c: ThinCard, mine: Bool, owner: Bool) in TaskDecision(task: t, cards: [c], mine: mine, owner: owner).card } |
| 14 | #expect(decision(task, card, true, true)?.id == card.id) |
| 15 | #expect(decision(task, card, false, true) == nil) |
| 16 | #expect(decision(task, card, true, false) == nil) |
| 17 | for stage in ["done", "cancelled", "merging"] { |
| 18 | var ended = task; ended.stage = stage; #expect(decision(ended, card, true, true) == nil) |
| 19 | } |
| 20 | task.moving = true; #expect(decision(task, card, true, true) == nil) |
| 21 | task.moving = false; task.stopping = true; #expect(decision(task, card, true, true) == nil) |
| 22 | task.stopping = false; task.copyHold = true; #expect(decision(task, card, true, true) == nil) |
| 23 | task.copyHold = false; task.copyCanSkip = true; #expect(decision(task, card, true, true) == nil) |
| 24 | task.copyCanSkip = false |
| 25 | var stale = card; stale.state = "answered"; #expect(decision(task, stale, true, true) == nil) |
| 26 | stale = card; stale.taskId = "other"; #expect(decision(task, stale, true, true) == nil) |
| 27 | for kind in ["task", "merge_receipt"] { |
| 28 | stale = card; stale.kind = kind; #expect(decision(task, stale, true, true) == nil) |
| 29 | } |
| 30 | task.kind = "explore"; #expect(decision(task, card, true, true) == nil) |
| 31 | } |
| 32 | @Test func decisionChoicesAreAllowlistedAndDirectionNeedsANote() throws { |
| 33 | let task = try TaskDetailTests.task() |
| 34 | let direction = try Self.card(task, kind: "direction", options: ["build", "again", "drop"]) |
| 35 | let d = TaskDecision(task: task, cards: [direction], mine: true, owner: true) |
| 36 | #expect(d.choices.map(\.value) == ["build", "again", "drop"]) |
| 37 | #expect(d.canAnswer("build", note: "")) |
| 38 | #expect(!d.canAnswer("again", note: " \n")) |
| 39 | #expect(d.canAnswer("again", note: "Keep the current layout")) |
| 40 | #expect(!d.canAnswer("again", note: String(repeating: "a", count: 1001))) |
| 41 | #expect(!d.canAnswer("merge", note: "")) |
| 42 | var unknown = direction; unknown.options = ["new_server_option"] |
| 43 | #expect(TaskDecision(task: task, cards: [unknown], mine: true, owner: true).choices.isEmpty) |
| 44 | unknown.kind = "new_card_kind"; unknown.options = ["build"] |
| 45 | #expect(TaskDecision(task: task, cards: [unknown], mine: true, owner: true).choices.isEmpty) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | extension TaskDecisionTests { |
| 50 | @Test func latestOpenCardWinsAndHeldChoicesStayNative() throws { |
| 51 | let task = try TaskDetailTests.task() |
| 52 | var first = try Self.card(task); first.id = "first" |
| 53 | var latest = try Self.card(task, kind: "held", options: ["run_now", "wait"]) |
| 54 | latest.id = "latest"; latest.createdAt = first.createdAt.addingTimeInterval(1) |
| 55 | let decision = TaskDecision(task: task, cards: [latest, first], mine: true, owner: true) |
| 56 | #expect(decision.card?.id == "latest") |
| 57 | #expect(decision.choices.map(\.value) == ["run_now", "wait"]) |
| 58 | var wrong = latest; wrong.kind = "direction"; wrong.options = ["merge"] |
| 59 | #expect(TaskDecision(task: task, cards: [wrong], mine: true, owner: true).choices.isEmpty) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | extension TaskDecisionTests { |
| 64 | @Test func questionsAreFallbackButCannotReplaceTheDecision() throws { |
| 65 | let task = try TaskDetailTests.task() |
| 66 | let merge = try Self.card(task) |
| 67 | var question = try Self.card(task, kind: "question", options: ["Keep it", "Change it"]); question.id = "new-question"; question.createdAt = merge.createdAt.addingTimeInterval(1) |
| 68 | var unknown = question; unknown.id = "new-unknown"; unknown.kind = "future"; unknown.createdAt = question.createdAt.addingTimeInterval(1) |
| 69 | #expect(TaskDecision(task: task, cards: [merge, question, unknown], mine: true, owner: true).card?.id == merge.id) |
| 70 | let fallback = TaskDecision(task: task, cards: [question, unknown], mine: true, owner: true) |
| 71 | #expect(fallback.card?.id == question.id) |
| 72 | #expect(fallback.card?.options == ["Keep it", "Change it"]) |
| 73 | #expect(TaskDecision(task: task, cards: [question], mine: true, owner: true).card?.id == question.id) |
| 74 | question.state = "answered" |
| 75 | #expect(TaskDecision(task: task, cards: [question], mine: true, owner: true).card == nil) |
| 76 | } |
| 77 | @Test func unknownOptionsRequireWholeCardWebFallback() throws { |
| 78 | let task = try TaskDetailTests.task() |
| 79 | for (kind, known) in [("merge", ["merge", "wait"]), ("direction", ["build", "again", "drop"]), ("held", ["run_now", "wait"])] { |
| 80 | let card = try Self.card(task, kind: kind, options: [known[0], "future_option"] + known.dropFirst()) |
| 81 | let d = TaskDecision(task: task, cards: [card], mine: true, owner: true) |
| 82 | #expect(d.card?.id == card.id) |
| 83 | #expect(d.choices.isEmpty) |
| 84 | #expect(!d.canAnswer(known[0], note: "")) |
| 85 | #expect(!d.canAnswer("future_option", note: "")) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |