| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { JobLogText } from "@g1t/contracts"; |
| 5 | |
| 6 | import { archiveFiles, blocks, classify, fileName, highlight, lastStamp, searchLog, splitStamp } from "./log-lines.ts"; |
| 7 | |
| 8 | const LOG = ["Job: test", "##[group]Run cargo test", "cargo test --locked", "##[endgroup]", "test greet ... FAILED", "##[error]Process completed with exit code 101."].join("\n"); |
| 9 | |
| 10 | test("a log reads as lines and folded groups, numbered past the group markers", () => { |
| 11 | const parsed = blocks(LOG); |
| 12 | assert.equal(parsed.length, 4); |
| 13 | assert.deepEqual(parsed[1], { kind: "group", title: "Run cargo test", lines: [{ line: { kind: "text", text: "cargo test --locked" }, number: 2 }] }); |
| 14 | assert.deepEqual(parsed[3], { kind: "line", line: { kind: "error", text: "Process completed with exit code 101." }, number: 4 }); |
| 15 | }); |
| 16 | |
| 17 | test("search finds lines in any case, inside folded groups too", () => { |
| 18 | assert.deepEqual( |
| 19 | searchLog(LOG, "CARGO TEST").map((found) => found.number), |
| 20 | [2], |
| 21 | ); |
| 22 | assert.deepEqual( |
| 23 | searchLog(LOG, "test").map((found) => found.number), |
| 24 | [1, 2, 3], |
| 25 | ); |
| 26 | assert.deepEqual(searchLog(LOG, " "), []); |
| 27 | // The group's title is not a line of its own. |
| 28 | assert.deepEqual(searchLog(LOG, "Run cargo"), []); |
| 29 | }); |
| 30 | |
| 31 | test("matches are marked where they fall", () => { |
| 32 | assert.deepEqual(highlight("Test a test", "test"), [ |
| 33 | { text: "Test", match: true }, |
| 34 | { text: " a ", match: false }, |
| 35 | { text: "test", match: true }, |
| 36 | ]); |
| 37 | assert.deepEqual(highlight("plain", ""), [{ text: "plain", match: false }]); |
| 38 | }); |
| 39 | |
| 40 | test("a run's archive has each job whole and a file per step, named as the API names them", () => { |
| 41 | const job = (name: string, chunks: [number, string][], omitted = false): JobLogText => ({ |
| 42 | jobId: "job_1", |
| 43 | name, |
| 44 | steps: [{ number: 1, name: "Run cargo test", status: "completed", conclusion: "success", startedAt: null, finishedAt: null }], |
| 45 | chunks: chunks.map(([step, text], index) => ({ seq: index + 1, step, text })), |
| 46 | done: true, |
| 47 | omitted, |
| 48 | }); |
| 49 | const files = archiveFiles([job("test", [[0, "set up\n"], [1, "running\n"], [1, "ok\n"]]), job("deploy/x", [], true)]); |
| 50 | assert.deepEqual( |
| 51 | files.map((file) => file.path), |
| 52 | ["1_test.txt", "test/0_Set up job.txt", "test/1_Run cargo test.txt", "2_deploy_x.txt"], |
| 53 | ); |
| 54 | assert.equal(files[0]!.text, "set up\nrunning\nok\n"); |
| 55 | assert.equal(files[2]!.text, "running\nok\n"); |
| 56 | assert.match(files[3]!.text, /left out/); |
| 57 | assert.equal(fileName("build / a:b"), "build _ a_b"); |
| 58 | assert.equal(fileName(".."), "job"); |
| 59 | }); |
| 60 | |
| 61 | test("each line's time comes off the front, as GitHub's logs stamp it, and colours stay beside the plain text", () => { |
| 62 | const stamped = [ |
| 63 | "2026-10-12T09:41:07.1234567Z ##[group]Run cargo build", |
| 64 | "2026-10-12T09:41:07.2000000Z \u001b[36;1mcargo build\u001b[0m", |
| 65 | "2026-10-12T09:41:07.3000000Z ##[endgroup]", |
| 66 | "2026-10-12T09:41:09.0000000Z \u001b[1m\u001b[32m Compiling\u001b[0m g1t v0.2.0", |
| 67 | "2026-10-12T09:41:10.0000000Z ##[error]Process completed with exit code 1.", |
| 68 | "a line from before lines were stamped", |
| 69 | "", |
| 70 | ].join("\n"); |
| 71 | const parsed = blocks(stamped); |
| 72 | assert.equal(parsed.length, 4, "a log's last newline adds no line"); |
| 73 | assert.deepEqual(parsed[0], { |
| 74 | kind: "group", |
| 75 | title: "Run cargo build", |
| 76 | at: "2026-10-12T09:41:07.1234567Z", |
| 77 | lines: [{ line: { kind: "text", text: "cargo build", at: "2026-10-12T09:41:07.2000000Z", ansi: "\u001b[36;1mcargo build\u001b[0m" }, number: 1 }], |
| 78 | }); |
| 79 | assert.deepEqual(parsed[1], { |
| 80 | kind: "line", |
| 81 | line: { kind: "text", text: " Compiling g1t v0.2.0", at: "2026-10-12T09:41:09.0000000Z", ansi: "\u001b[1m\u001b[32m Compiling\u001b[0m g1t v0.2.0" }, |
| 82 | number: 2, |
| 83 | }); |
| 84 | assert.deepEqual(parsed[2], { kind: "line", line: { kind: "error", text: "Process completed with exit code 1.", at: "2026-10-12T09:41:10.0000000Z" }, number: 3 }); |
| 85 | assert.deepEqual(parsed[3], { kind: "line", line: { kind: "text", text: "a line from before lines were stamped" }, number: 4 }); |
| 86 | // Search reads the plain text, never the time or the codes. |
| 87 | assert.deepEqual( |
| 88 | searchLog(stamped, "compiling g1t").map((found) => found.number), |
| 89 | [2], |
| 90 | ); |
| 91 | assert.deepEqual(searchLog(stamped, "2026-10-12"), []); |
| 92 | assert.deepEqual(searchLog(stamped, "32m"), []); |
| 93 | assert.deepEqual(splitStamp("2026-10-12T09:41:07Z text"), { at: "2026-10-12T09:41:07Z", rest: "text" }); |
| 94 | assert.deepEqual(splitStamp("2026-10-12 not a stamp"), { rest: "2026-10-12 not a stamp" }); |
| 95 | assert.deepEqual(classify("##[warning]plain"), { kind: "warning", text: "plain" }); |
| 96 | assert.equal(lastStamp(stamped), "2026-10-12T09:41:10.0000000Z"); |
| 97 | assert.equal(lastStamp("no stamps\n"), undefined); |
| 98 | }); |