import Foundation import Testing @testable import EquipoKit /// Merges answered on the device must match the web (parity row R-02, /// must_match): the server's question, the same choices in the same order /// with the same words, the same primary, and the same outcome line. @MainActor @Suite struct MergeCardTests { private func cards() throws -> [ThinCard] { let snapshot: Snapshot = try Fixture.decode("thin/snapshot") return snapshot.cards ?? [] } @Test func anOpenMergeIsAnsweredHereInTheWebsWords() throws { let card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) let view = try #require(Feed.decision(card, task: nil)) #expect(view.open) #expect(view.line == card.question, "the server's question, as the web shows it") #expect(view.choices.map(\.value) == ["merge", "wait_mac"], "the server's choices, in its order") #expect(view.choices.map(\.label) == ["Merge", "Wait for a Mac"]) #expect(view.choices.filter(\.primary).map(\.value) == ["merge"], "Merge is primary, as on the web") #expect(view.note == nil) } @Test func anAnsweredMergeSaysWhatWasChosen() throws { let card = try #require(try cards().first { $0.kind == "merge" && $0.state == "answered" }) let view = try #require(Feed.decision(card, task: nil)) #expect(!view.open) #expect(view.note == "Merging without checks.") var resolved = card; resolved.state = "resolved" #expect(Feed.decision(resolved, task: nil)?.note == "Nothing to decide any more.") } @Test func doesMainShipHasItsOwnPrimaryAndOutcome() throws { var card = try #require(try cards().first { $0.kind == "merge_ships" }) let view = try #require(Feed.decision(card, task: nil)) #expect(view.choices.map(\.label) == ["It ships", "It doesn't"]) #expect(view.choices.filter(\.primary).map(\.value) == ["not_ships"]) card.state = "answered"; card.choice = "ships" #expect(Feed.decision(card, task: nil)?.note == "Merging to main ships to users, so I'll ask before merging without checks.") } @Test func aMergeFromBeforeAnExplorationIsClosed() throws { let card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) let tasks: ThinList = try Fixture.decode("thin/tasks") var task = try #require(tasks.items?.first); task.kind = "explore" let view = try #require(Feed.decision(card, task: task)) #expect(!view.open) #expect(view.note == "An earlier attempt to add this change was dropped when the task became an exploration.") } @Test func anUnknownChoiceSendsYouToTheWeb() throws { var card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) card.options = ["merge", "something_new"] #expect(Feed.decision(card, task: nil) == nil, "a choice with no words is never guessed at") for kind in ["publish", "direction", "merge_policy", "specialist", "question"] { var other = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) other.kind = kind #expect(Feed.decision(other, task: nil) == nil, "\(kind) still opens the web, even with choices that have words") } } @Test func thePreviewOpensThroughTheWebsWrapperOnly() throws { let origin = URL(string: "https://team.example.test")! var card = try #require(try cards().first { $0.kind == "merge" && $0.state == "open" }) #expect(Feed.decision(card, task: nil)?.hasPreview == false, "no preview yet, no link") // Whatever the candidate's own address, the device opens Equipo's wrapper page. for raw in ["https://preview.example.test/x", "https://equipo.example@evil.test/", "javascript:alert(1)"] { card.previewUrl = raw #expect(Feed.decision(card, task: nil)?.hasPreview == true) #expect(Feed.previewURL(card, origin: origin).absoluteString == "https://team.example.test/tasks/\(card.taskId)/preview") } var ships = try #require(try cards().first { $0.kind == "merge_ships" }) ships.previewUrl = "https://preview.example.test/x" #expect(Feed.decision(ships, task: nil)?.hasPreview == false, "the web links a preview only on merge cards") } @Test func aClosedCardShowsOnlyItsOutcome() throws { var card = try #require(try cards().first { $0.kind == "merge" && $0.state == "answered" }) card.previewUrl = "https://preview.example.test/x" let view = try #require(Feed.decision(card, task: nil)) #expect(view.showsOnlyNote, "as chat.html's equipo-card-done: the note and nothing else") #expect(!view.hasPreview) var ships = try #require(try cards().first { $0.kind == "merge_ships" }) ships.options = [] let empty = try #require(Feed.decision(ships, task: nil)) #expect(!empty.open, "a ships card with no choices is not open, as on the web") } @Test func aChangedCardKeepsSayingSoUntilItMovesOn() { // The conflict returns the card at revision 7: the message stays at 7 and goes after it. #expect(AnswerFeedback.stillChanged(conflictAt: 7, now: 7)) #expect(!AnswerFeedback.stillChanged(conflictAt: 7, now: 8)) } }