| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @Suite struct TaskActionAPITests { |
| 6 | static let commands: [(TaskCommand, String, String, String)] = [ |
| 7 | (.merge, "POST", "merge", "{}"), (.cancel, "POST", "cancel", "{}"), (.round, "POST", "round", "{}"), |
| 8 | (.undo(receiptRevision: 72), "POST", "undo-merge", #"{"revision":72}"#), |
| 9 | (.move(repo: "amadan.net/team/other", revision: 9), "POST", "move", #"{"repo":"amadan.net/team/other","revision":9}"#), |
| 10 | (.kind("explore", revision: 9), "POST", "kind", #"{"kind":"explore","revision":9}"#), |
| 11 | (.priority("urgent", by: Date(timeIntervalSince1970: 1_800_000_000), revision: 9), "PUT", "priority", #"{"priority":"urgent","by":"2027-01-15T08:00:00Z","revision":9}"#), |
| 12 | (.priority("normal", by: nil, revision: 10), "PUT", "priority", #"{"priority":"normal","by":null,"revision":10}"#) |
| 13 | ] |
| 14 | @Test func everyActionSendsTheWireRequestWithBearerAndReceiptRevision() async throws { |
| 15 | for (command, method, suffix, json) in Self.commands { |
| 16 | let server = ScriptedTransport() |
| 17 | let path = "/tasks/task%2Fid/" + suffix |
| 18 | server.on(method + " " + path, .success(APIResponse(status: 200, data: suffix == "undo-merge" ? Fixture.data("thin/task") : Data("{}".utf8)))) |
| 19 | let result = try await EquipoClient(transport: server, token: "fixture-bearer").execute(task: "task/id", command) |
| 20 | let request = try #require(server.requests.first) |
| 21 | #expect(request.method == method && request.path == path) |
| 22 | #expect(request.bearer == "fixture-bearer") |
| 23 | let actual = try JSONSerialization.jsonObject(with: try #require(request.body)) as! NSDictionary |
| 24 | let expected = try JSONSerialization.jsonObject(with: Data(json.utf8)) as! NSDictionary |
| 25 | #expect(actual == expected) |
| 26 | #expect((result != nil) == (suffix == "undo-merge")) |
| 27 | } |
| 28 | } |
| 29 | @Test func everyActionPropagatesConflictsAndFailures() async throws { |
| 30 | for (command, method, suffix, _) in Self.commands { |
| 31 | for status in [409, 500] { |
| 32 | let server = ScriptedTransport() |
| 33 | server.on(method + " /tasks/task/" + suffix, .success(APIResponse(status: status, data: Data("{}".utf8)))) |
| 34 | await #expect(throws: EquipoAPIError.status(status)) { try await EquipoClient(transport: server, token: "fixture").execute(task: "task", command) } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | @Test(arguments: ["not_mergeable", "not_undoable", "revision_conflict", "task_ended", "task_merging", "task_busy", "repo_not_allowed", "runner_offline"]) |
| 39 | func conflictsUseTheReviewedCodeWords(_ code: String) { |
| 40 | let response = APIResponse(status: 409, data: Data("{\"code\":\"\(code)\",\"copy_key\":\"api.\(code)\",\"request_id\":\"fixture\"}".utf8)) |
| 41 | #expect(TaskActionFailure.words(EquipoClient.error(response)) == EquipoStrings.text(.init("api." + code))) |
| 42 | } |
| 43 | @Test func unknownAndOfflineFailuresUseReviewedFallbacks() { |
| 44 | #expect(TaskActionFailure.words(EquipoAPIError.offline) == EquipoStrings.text("card.offline")) |
| 45 | #expect(TaskActionFailure.words(URLError(.notConnectedToInternet)) == EquipoStrings.text("card.offline")) |
| 46 | #expect(TaskActionFailure.words(EquipoAPIError.status(500)) == EquipoStrings.text("card.failed")) |
| 47 | #expect(TaskActionFailure.words(EquipoAPIError.status(409)) == EquipoStrings.text("card.changed")) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | @Suite struct CheckSectionTests { |
| 52 | @Test func failedRunnerSectionComesFirstAndTestMarkersStayInTheLog() throws { |
| 53 | let task = try TaskDetailTests.task() |
| 54 | var step = try #require(task.steps?.first); step.specialist = "checks"; step.gate = ".amadan/ci.d"; step.checks = "failed"; step.exitCode = 1 |
| 55 | let log = StepLog(stepId: step.id, attempt: 1, tail: "setup\n=== 10-format\nformat ok\n=== 20-test\n=== RUN TestThing\nFAIL expected 2\n") |
| 56 | let sections = CheckSection.split(log, step: step) |
| 57 | #expect(sections.map(\.name) == ["20-test", "", "10-format"]) |
| 58 | #expect(sections.first?.failed == true) |
| 59 | #expect(sections.first?.text.contains("=== RUN TestThing") == true) |
| 60 | #expect(Set(sections.map(\.id)).count == sections.count) |
| 61 | step.checks = "passed"; step.exitCode = 0 |
| 62 | #expect(CheckSection.split(log, step: step).map(\.name) == ["", "10-format", "20-test"]) |
| 63 | #expect(CheckSection.split(log, step: step).allSatisfy { !$0.failed }) |
| 64 | } |
| 65 | @Test func unmarkedAndBoundedLogsArePreservedWithoutInventedSuccess() throws { |
| 66 | var step = try #require(try TaskDetailTests.task().steps?.first) |
| 67 | step.gate = "make ci"; step.checks = "cannot_run" |
| 68 | let log = StepLog(stepId: step.id, attempt: 1, tail: "=== a line from the test\nno runner markers") |
| 69 | #expect(CheckSection.split(log, step: step).first?.text == log.tail) |
| 70 | #expect(CheckSection.split(log, step: step).first?.failed == false) |
| 71 | step.gate = ".amadan/ci.d"; step.checks = "failed" |
| 72 | let long = StepLog(stepId: step.id, attempt: 1, tail: (0..<400).map { "line \($0)" }.joined(separator: "\n")) |
| 73 | let result = CheckSection.split(long, step: step) |
| 74 | #expect(result.count == 1 && result.first?.failed == true) |
| 75 | #expect(result.first?.text == long.boundedTail) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | extension CheckSectionTests { |
| 80 | @Test func eitherFailureSignalMarksTheLastSection() throws { |
| 81 | var step = try #require(try TaskDetailTests.task().steps?.first) |
| 82 | step.gate = ".amadan/ci.d"; step.checks = "failed"; step.exitCode = nil |
| 83 | let log = StepLog(stepId: step.id, attempt: 1, tail: "=== test\nfailed") |
| 84 | #expect(CheckSection.split(log, step: step).first?.failed == true) |
| 85 | step.checks = ""; step.exitCode = 1 |
| 86 | #expect(CheckSection.split(log, step: step).first?.failed == true) |
| 87 | } |
| 88 | } |
| 89 | |