import Foundation import Testing @testable import EquipoKit @Suite struct HostedSectionTests { @Test func hostedNamesDecodeAndOlderStepsStillDecode() throws { let task: ThinTask = try Fixture.decode("thin/task") let old = try #require(task.steps?.first) #expect(old.hosted == nil) let raw = try JSONSerialization.jsonObject(with: Fixture.data("thin/task")) as! [String: Any] var body = (raw["steps"] as! [[String: Any]])[0] body["hosted"] = ["failed": ["check shard 5 of 23", "format"]] let step = try EquipoJSON.decoder.decode(ThinStep.self, from: JSONSerialization.data(withJSONObject: body)) #expect(step.hosted?.failed == ["check shard 5 of 23", "format"]) } @Test func hostedHeaderIsSummaryAndEveryFailedJobIsMarked() throws { var step = try #require(try TaskDetailTests.task().steps?.first) step.gate = "hosted CI"; step.checks = "failed"; step.exitCode = 1 step.hosted = .init(failed: ["format", "check shard 5 of 23"]) 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" let log = StepLog(stepId: step.id, attempt: 1, tail: tail) let sections = CheckSection.split(log, step: step) #expect(sections.map(\.name) == ["", "format", "check shard 5 of 23"]) #expect(sections.first?.isSummary == true) #expect(sections.first?.failed == false) #expect(sections.filter(\.failed).map(\.name) == ["format", "check shard 5 of 23"]) #expect(sections.last?.text.contains("=== not-a-job") == true) #expect(restored(sections) == tail) step.hosted = nil #expect(CheckSection.split(log, step: step) == sections) step.hosted = .init(failed: []) #expect(CheckSection.split(log, step: step).count == 1) } @Test func localMarkersAreConservativeAndSpoofsNeverHideOutput() throws { var step = try #require(try TaskDetailTests.task().steps?.first) step.gate = ".amadan/ci.d"; step.checks = "failed"; step.exitCode = 1 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" let log = StepLog(stepId: step.id, attempt: 1, tail: tail) let sections = CheckSection.split(log, step: step) #expect(sections.map(\.name) == ["20-check", "", "10-format"]) #expect(restored(sections) == tail) #expect(sections.first?.text.contains("spoofed section still visible") == true) } @Test func missingHostedHeaderAndTruncatedLogsRemainVisible() throws { var step = try #require(try TaskDetailTests.task().steps?.first) step.gate = "hosted CI"; step.checks = "failed" let log = StepLog(stepId: step.id, attempt: 1, tail: (0..<300).map { "line \($0)" }.joined(separator: "\n") + "\n=== format\nfailure") #expect(restored(CheckSection.split(log, step: step)) == log.boundedTail) step.hosted = .init(failed: ["format"]) let split = CheckSection.split(log, step: step) #expect(split.count == 2) #expect(split.first?.isSummary == false) #expect(restored(split) == log.boundedTail) } @Test func hostedNamesNormalizeWhitespaceAndClipUTF8OnBothSides() throws { var step = try #require(try TaskDetailTests.task().steps?.first) step.gate = "hosted CI"; step.checks = "failed" let long = String(repeating: "é", count: 59) + "abc" let clipped = String(repeating: "é", count: 59) + "ab" let cases = [("check shard\t5", "check shard 5", "check shard 5"), (long, clipped, clipped), (clipped, long, clipped), ("check shard 5", " check shard\t5 ", "check shard 5"), (String(repeating: "a", count: 119) + "é", String(repeating: "a", count: 119), String(repeating: "a", count: 119))] for (marker, stored, expected) in cases { step.hosted = .init(failed: [stored]) let tail = "Hosted CI on oficina/equipo: failed.\nFailed: \(stored)\n\n=== \(marker)\nfailure\n" let log = StepLog(stepId: step.id, attempt: 1, tail: tail) let sections = CheckSection.split(log, step: step) #expect(sections.map(\.name) == ["", expected]) #expect(sections.last?.failed == true) #expect(restored(sections) == tail) step.hosted = nil #expect(CheckSection.split(log, step: step) == sections) } } private func restored(_ sections: [CheckSection]) -> String { sections.sorted { $0.id < $1.id }.map(\.text).joined(separator: "\n") } }