| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | /// Merges answered on the device must match the web (parity row R-02, |
| 6 | /// must_match): the server's question, the same choices in the same order |
| 7 | /// with the same words, the same primary, and the same outcome line. |
| 8 | @MainActor @Suite struct MergeCardTests { |
| 9 | private func cards() throws -> [ThinCard] { |
| 10 | let snapshot: Snapshot = try Fixture.decode("thin/snapshot") |
| 11 | return snapshot.cards ?? [] |
| 12 | } |
| 13 | |
| 14 | @Test func anOpenMergeIsAnsweredHereInTheWebsWords() throws { |
| 15 | let card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) |
| 16 | let view = try #require(Feed.decision(card, task: nil)) |
| 17 | #expect(view.open) |
| 18 | #expect(view.line == card.question, "the server's question, as the web shows it") |
| 19 | #expect(view.choices.map(\.value) == ["merge", "wait_mac"], "the server's choices, in its order") |
| 20 | #expect(view.choices.map(\.label) == ["Merge", "Wait for a Mac"]) |
| 21 | #expect(view.choices.filter(\.primary).map(\.value) == ["merge"], "Merge is primary, as on the web") |
| 22 | #expect(view.note == nil) |
| 23 | } |
| 24 | |
| 25 | @Test func anAnsweredMergeSaysWhatWasChosen() throws { |
| 26 | let card = try #require(try cards().first { $0.kind == "merge" && $0.state == "answered" }) |
| 27 | let view = try #require(Feed.decision(card, task: nil)) |
| 28 | #expect(!view.open) |
| 29 | #expect(view.note == "Merging without checks.") |
| 30 | var resolved = card; resolved.state = "resolved" |
| 31 | #expect(Feed.decision(resolved, task: nil)?.note == "Nothing to decide any more.") |
| 32 | } |
| 33 | |
| 34 | @Test func doesMainShipHasItsOwnPrimaryAndOutcome() throws { |
| 35 | var card = try #require(try cards().first { $0.kind == "merge_ships" }) |
| 36 | let view = try #require(Feed.decision(card, task: nil)) |
| 37 | #expect(view.choices.map(\.label) == ["It ships", "It doesn't"]) |
| 38 | #expect(view.choices.filter(\.primary).map(\.value) == ["not_ships"]) |
| 39 | card.state = "answered"; card.choice = "ships" |
| 40 | #expect(Feed.decision(card, task: nil)?.note == "Merging to main ships to users, so I'll ask before merging without checks.") |
| 41 | } |
| 42 | |
| 43 | @Test func aMergeFromBeforeAnExplorationIsClosed() throws { |
| 44 | let card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) |
| 45 | let tasks: ThinList<ThinTask> = try Fixture.decode("thin/tasks") |
| 46 | var task = try #require(tasks.items?.first); task.kind = "explore" |
| 47 | let view = try #require(Feed.decision(card, task: task)) |
| 48 | #expect(!view.open) |
| 49 | #expect(view.note == "An earlier attempt to add this change was dropped when the task became an exploration.") |
| 50 | } |
| 51 | |
| 52 | @Test func anUnknownChoiceSendsYouToTheWeb() throws { |
| 53 | var card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) |
| 54 | card.options = ["merge", "something_new"] |
| 55 | #expect(Feed.decision(card, task: nil) == nil, "a choice with no words is never guessed at") |
| 56 | for kind in ["publish", "direction", "merge_policy", "specialist", "question"] { |
| 57 | var other = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) |
| 58 | other.kind = kind |
| 59 | #expect(Feed.decision(other, task: nil) == nil, "\(kind) still opens the web, even with choices that have words") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Test func thePreviewOpensThroughTheWebsWrapperOnly() throws { |
| 64 | let origin = URL(string: "https://team.example.test")! |
| 65 | var card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) |
| 66 | #expect(Feed.decision(card, task: nil)?.hasPreview == false, "no preview yet, no link") |
| 67 | // Whatever the candidate's own address, the device opens Equipo's wrapper page. |
| 68 | for raw in ["https://preview.example.test/x", "https://equipo.example@evil.test/", "javascript:alert(1)"] { |
| 69 | card.previewUrl = raw |
| 70 | #expect(Feed.decision(card, task: nil)?.hasPreview == true) |
| 71 | #expect(Feed.previewURL(card, origin: origin).absoluteString == "https://team.example.test/tasks/\(card.taskId)/preview") |
| 72 | } |
| 73 | var ships = try #require(try cards().first { $0.kind == "merge_ships" }) |
| 74 | ships.previewUrl = "https://preview.example.test/x" |
| 75 | #expect(Feed.decision(ships, task: nil)?.hasPreview == false, "the web links a preview only on merge cards") |
| 76 | } |
| 77 | |
| 78 | @Test func aClosedCardShowsOnlyItsOutcome() throws { |
| 79 | var card = try #require(try cards().first { $0.kind == "merge" && $0.state == "answered" }) |
| 80 | card.previewUrl = "https://preview.example.test/x" |
| 81 | let view = try #require(Feed.decision(card, task: nil)) |
| 82 | #expect(view.showsOnlyNote, "as chat.html's equipo-card-done: the note and nothing else") |
| 83 | #expect(!view.hasPreview) |
| 84 | var ships = try #require(try cards().first { $0.kind == "merge_ships" }) |
| 85 | ships.options = [] |
| 86 | let empty = try #require(Feed.decision(ships, task: nil)) |
| 87 | #expect(!empty.open, "a ships card with no choices is not open, as on the web") |
| 88 | } |
| 89 | |
| 90 | @Test func aChangedCardKeepsSayingSoUntilItMovesOn() { |
| 91 | // The conflict returns the card at revision 7: the message stays at 7 and goes after it. |
| 92 | #expect(AnswerFeedback.stillChanged(conflictAt: 7, now: 7)) |
| 93 | #expect(!AnswerFeedback.stillChanged(conflictAt: 7, now: 8)) |
| 94 | } |
| 95 | } |
| 96 | |