| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @Suite struct HostedSectionTests { |
| 6 | @Test func hostedNamesDecodeAndOlderStepsStillDecode() throws { |
| 7 | let task: ThinTask = try Fixture.decode("thin/task") |
| 8 | let old = try #require(task.steps?.first) |
| 9 | #expect(old.hosted == nil) |
| 10 | let raw = try JSONSerialization.jsonObject(with: Fixture.data("thin/task")) as! [String: Any] |
| 11 | var body = (raw["steps"] as! [[String: Any]])[0] |
| 12 | body["hosted"] = ["failed": ["check shard 5 of 23", "format"]] |
| 13 | let step = try EquipoJSON.decoder.decode(ThinStep.self, from: JSONSerialization.data(withJSONObject: body)) |
| 14 | #expect(step.hosted?.failed == ["check shard 5 of 23", "format"]) |
| 15 | } |
| 16 | @Test func hostedHeaderIsSummaryAndEveryFailedJobIsMarked() throws { |
| 17 | var step = try #require(try TaskDetailTests.task().steps?.first) |
| 18 | step.gate = "hosted CI"; step.checks = "failed"; step.exitCode = 1 |
| 19 | step.hosted = .init(failed: ["format", "check shard 5 of 23"]) |
| 20 | let tail = "Hosted CI on oficina/equipo at abc123: failed.\nFailed: format; check shard 5 of 23\n\n=== format\nformat failed\n=== check shard 5 of 23\n=== not-a-job\ncheck failed\n" |
| 21 | let log = StepLog(stepId: step.id, attempt: 1, tail: tail) |
| 22 | let sections = CheckSection.split(log, step: step) |
| 23 | #expect(sections.map(\.name) == ["", "format", "check shard 5 of 23"]) |
| 24 | #expect(sections.first?.isSummary == true) |
| 25 | #expect(sections.first?.failed == false) |
| 26 | #expect(sections.filter(\.failed).map(\.name) == ["format", "check shard 5 of 23"]) |
| 27 | #expect(sections.last?.text.contains("=== not-a-job") == true) |
| 28 | #expect(restored(sections) == tail) |
| 29 | step.hosted = nil |
| 30 | #expect(CheckSection.split(log, step: step) == sections) |
| 31 | step.hosted = .init(failed: []) |
| 32 | #expect(CheckSection.split(log, step: step).count == 1) |
| 33 | } |
| 34 | @Test func localMarkersAreConservativeAndSpoofsNeverHideOutput() throws { |
| 35 | var step = try #require(try TaskDetailTests.task().steps?.first) |
| 36 | step.gate = ".amadan/ci.d"; step.checks = "failed"; step.exitCode = 1 |
| 37 | let tail = "before any marker\n=== 10-format\n=== prose from a tool\n=== RUN TestThing\n=== ../secret\n=== 20-check\nspoofed section still visible\n" |
| 38 | let log = StepLog(stepId: step.id, attempt: 1, tail: tail) |
| 39 | let sections = CheckSection.split(log, step: step) |
| 40 | #expect(sections.map(\.name) == ["20-check", "", "10-format"]) |
| 41 | #expect(restored(sections) == tail) |
| 42 | #expect(sections.first?.text.contains("spoofed section still visible") == true) |
| 43 | } |
| 44 | @Test func missingHostedHeaderAndTruncatedLogsRemainVisible() throws { |
| 45 | var step = try #require(try TaskDetailTests.task().steps?.first) |
| 46 | step.gate = "hosted CI"; step.checks = "failed" |
| 47 | let log = StepLog(stepId: step.id, attempt: 1, tail: (0..<300).map { "line \($0)" }.joined(separator: "\n") + "\n=== format\nfailure") |
| 48 | #expect(restored(CheckSection.split(log, step: step)) == log.boundedTail) |
| 49 | step.hosted = .init(failed: ["format"]) |
| 50 | let split = CheckSection.split(log, step: step) |
| 51 | #expect(split.count == 2) |
| 52 | #expect(split.first?.isSummary == false) |
| 53 | #expect(restored(split) == log.boundedTail) |
| 54 | } |
| 55 | @Test func hostedNamesNormalizeWhitespaceAndClipUTF8OnBothSides() throws { |
| 56 | var step = try #require(try TaskDetailTests.task().steps?.first) |
| 57 | step.gate = "hosted CI"; step.checks = "failed" |
| 58 | let long = String(repeating: "é", count: 59) + "abc" |
| 59 | let clipped = String(repeating: "é", count: 59) + "ab" |
| 60 | let cases = [("check shard\t5", "check shard 5", "check shard 5"), |
| 61 | (long, clipped, clipped), (clipped, long, clipped), |
| 62 | ("check shard 5", " check shard\t5 ", "check shard 5"), |
| 63 | (String(repeating: "a", count: 119) + "é", String(repeating: "a", count: 119), String(repeating: "a", count: 119))] |
| 64 | for (marker, stored, expected) in cases { |
| 65 | step.hosted = .init(failed: [stored]) |
| 66 | let tail = "Hosted CI on oficina/equipo: failed.\nFailed: \(stored)\n\n=== \(marker)\nfailure\n" |
| 67 | let log = StepLog(stepId: step.id, attempt: 1, tail: tail) |
| 68 | let sections = CheckSection.split(log, step: step) |
| 69 | #expect(sections.map(\.name) == ["", expected]) |
| 70 | #expect(sections.last?.failed == true) |
| 71 | #expect(restored(sections) == tail) |
| 72 | step.hosted = nil |
| 73 | #expect(CheckSection.split(log, step: step) == sections) |
| 74 | } |
| 75 | } |
| 76 | private func restored(_ sections: [CheckSection]) -> String { |
| 77 | sections.sorted { $0.id < $1.id }.map(\.text).joined(separator: "\n") |
| 78 | } |
| 79 | } |
| 80 | |