Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

235 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { JobLogText } from "@g1t/contracts";
5
6import {
7 type StepLog,
8 appendLog,
9 archiveFiles,
10 blocks,
11 classify,
12 emptyLog,
13 fileName,
14 groupOf,
15 highlight,
16 lastStamp,
17 logRows,
18 readLog,
19 rowKey,
20 searchLog,
21 searchStepLog,
22 splitStamp,
23} from "./log-lines.ts";
24
25/** `text` read as chunks cut at `cuts` (offsets into it), one at a time. */
26function readInChunks(text: string, cuts: number[]): StepLog {
27 let log = emptyLog();
28 let at = 0;
29 for (const cut of [...cuts, text.length]) {
30 log = appendLog(log, text.slice(at, cut));
31 at = cut;
32 }
33 return log;
34}
35
36/** What a log holds, for comparing two ways of reading it. */
37function shape(log: StepLog) {
38 return { blocks: log.blocks, lines: log.lines, blockOf: log.blockOf, group: log.group, lastAt: log.lastAt, hasText: log.hasText };
39}
40
41test("a log read chunk by chunk is the log read at once, wherever the chunks fall", () => {
42 const text = [
43 "2026-10-12T09:41:07.1000000Z Job: test",
44 "2026-10-12T09:41:07.2000000Z ##[group]Run cargo test",
45 "2026-10-12T09:41:07.3000000Z cargo test --locked",
46 "2026-10-12T09:41:07.4000000Z \u001b[32mok\u001b[0m",
47 "2026-10-12T09:41:08.0000000Z ##[endgroup]",
48 "2026-10-12T09:41:09.0000000Z ##[error]Process completed with exit code 101.",
49 "",
50 ].join("\n");
51 const whole = readLog(text);
52 // Every cut, and many cuts, including inside a line and inside an escape code.
53 for (let cut = 0; cut <= text.length; cut++) assert.deepEqual(shape(readInChunks(text, [cut])), shape(whole), `cut at ${cut}`);
54 assert.deepEqual(
55 shape(
56 readInChunks(
57 text,
58 Array.from({ length: text.length }, (_, i) => i),
59 ),
60 ),
61 shape(whole),
62 );
63 assert.equal(whole.lastAt, "2026-10-12T09:41:09.0000000Z");
64 assert.deepEqual(whole.blocks, blocks(text));
65});
66
67test("a group a chunk leaves open holds the next chunk's lines", () => {
68 let log = appendLog(emptyLog(), "before\n##[group]Install\none\n");
69 assert.equal(log.group, 1);
70 log = appendLog(log, "two\n##[endgroup]\nafter\n");
71 assert.deepEqual(
72 log.blocks.map((block) => (block.kind === "line" ? block.line.text : `${block.title}: ${block.lines.map((entry) => entry.line.text).join(",")}`)),
73 ["before", "Install: one,two", "after"],
74 );
75 assert.equal(log.group, null);
76 assert.equal(groupOf(log, 3), 1);
77 assert.equal(groupOf(log, 4), null);
78});
79
80test("a line still being written shows, and is read once it ends", () => {
81 let log = appendLog(emptyLog(), "one\ntw");
82 assert.equal(log.lines.length, 1);
83 assert.deepEqual(log.tail, { line: { kind: "text", text: "tw" }, number: 2 });
84 assert.deepEqual(
85 searchStepLog(log, "TW").map((found) => found.number),
86 [2],
87 );
88 log = appendLog(log, "o\n");
89 assert.equal(log.tail, undefined);
90 assert.deepEqual(
91 log.lines.map((entry) => entry.line.text),
92 ["one", "two"],
93 );
94 // A group's marker cut in two opens the group once it is whole.
95 log = appendLog(log, "##[gro");
96 assert.equal(log.tail, undefined, "half a marker is not a line");
97 log = appendLog(log, "up]Later\nthree\n");
98 assert.equal(groupOf(log, 3), 2);
99 assert.equal(readLog(" \n\n").hasText, false);
100});
101
102test("reading a long log chunk by chunk reads each line once", () => {
103 const line = (n: number) => `2026-10-12T09:41:07.0000000Z ${n % 500 === 0 ? "##[group]Batch" : n % 500 === 20 ? "##[endgroup]" : `test case_${n} ... ok`}\n`;
104 let log = emptyLog();
105 const started = performance.now();
106 for (let chunk = 0; chunk < 500; chunk++) {
107 let text = "";
108 for (let n = chunk * 100; n < chunk * 100 + 100; n++) text += line(n);
109 log = appendLog(log, text);
110 }
111 const took = performance.now() - started;
112 assert.equal(log.lines.length, 50_000 - 200);
113 // Reading the whole text again for every chunk took about 25 s here.
114 assert.ok(took < 2000, `50,000 lines in 500 chunks took ${Math.round(took)} ms`);
115});
116
117test("a log's rows are its lines and groups' titles, open groups' lines under them", () => {
118 const log = readLog("a\n##[group]G\nb\nc\n##[endgroup]\nd\n");
119 const keys = (open: number[]) => logRows(log, new Set(open)).map(rowKey);
120 assert.deepEqual(keys([]), ["L1", "G1", "L4"]);
121 assert.deepEqual(keys([1]), ["L1", "G1", "L2", "L3", "L4"]);
122 assert.deepEqual(logRows(log, new Set())[1], { kind: "group", block: 1, title: "G", at: undefined, open: false, size: 2 });
123 // A search reads a log once a change.
124 assert.equal(searchStepLog(log, "b"), searchStepLog(log, " B "));
125});
126
127const LOG = [
128 "Job: test",
129 "##[group]Run cargo test",
130 "cargo test --locked",
131 "##[endgroup]",
132 "test greet ... FAILED",
133 "##[error]Process completed with exit code 101.",
134].join("\n");
135
136test("a log reads as lines and folded groups, numbered past the group markers", () => {
137 const parsed = blocks(LOG);
138 assert.equal(parsed.length, 4);
139 assert.deepEqual(parsed[1], { kind: "group", title: "Run cargo test", lines: [{ line: { kind: "text", text: "cargo test --locked" }, number: 2 }] });
140 assert.deepEqual(parsed[3], { kind: "line", line: { kind: "error", text: "Process completed with exit code 101." }, number: 4 });
141});
142
143test("search finds lines in any case, inside folded groups too", () => {
144 assert.deepEqual(
145 searchLog(LOG, "CARGO TEST").map((found) => found.number),
146 [2],
147 );
148 assert.deepEqual(
149 searchLog(LOG, "test").map((found) => found.number),
150 [1, 2, 3],
151 );
152 assert.deepEqual(searchLog(LOG, " "), []);
153 // The group's title is not a line of its own.
154 assert.deepEqual(searchLog(LOG, "Run cargo"), []);
155});
156
157test("matches are marked where they fall", () => {
158 assert.deepEqual(highlight("Test a test", "test"), [
159 { text: "Test", match: true },
160 { text: " a ", match: false },
161 { text: "test", match: true },
162 ]);
163 assert.deepEqual(highlight("plain", ""), [{ text: "plain", match: false }]);
164});
165
166test("a run's archive has each job whole and a file per step, named as the API names them", () => {
167 const job = (name: string, chunks: [number, string][], omitted = false): JobLogText => ({
168 jobId: "job_1",
169 name,
170 steps: [{ number: 1, name: "Run cargo test", status: "completed", conclusion: "success", startedAt: null, finishedAt: null }],
171 chunks: chunks.map(([step, text], index) => ({ seq: index + 1, step, text })),
172 done: true,
173 omitted,
174 });
175 const files = archiveFiles([
176 job("test", [
177 [0, "set up\n"],
178 [1, "running\n"],
179 [1, "ok\n"],
180 ]),
181 job("deploy/x", [], true),
182 ]);
183 assert.deepEqual(
184 files.map((file) => file.path),
185 ["1_test.txt", "test/0_Set up job.txt", "test/1_Run cargo test.txt", "2_deploy_x.txt"],
186 );
187 assert.equal(files[0]!.text, "set up\nrunning\nok\n");
188 assert.equal(files[2]!.text, "running\nok\n");
189 assert.match(files[3]!.text, /left out/);
190 assert.equal(fileName("build / a:b"), "build _ a_b");
191 assert.equal(fileName(".."), "job");
192});
193
194test("each line's time comes off the front, as GitHub's logs stamp it, and colours stay beside the plain text", () => {
195 const stamped = [
196 "2026-10-12T09:41:07.1234567Z ##[group]Run cargo build",
197 "2026-10-12T09:41:07.2000000Z \u001b[36;1mcargo build\u001b[0m",
198 "2026-10-12T09:41:07.3000000Z ##[endgroup]",
199 "2026-10-12T09:41:09.0000000Z \u001b[1m\u001b[32m Compiling\u001b[0m g1t v0.2.0",
200 "2026-10-12T09:41:10.0000000Z ##[error]Process completed with exit code 1.",
201 "a line from before lines were stamped",
202 "",
203 ].join("\n");
204 const parsed = blocks(stamped);
205 assert.equal(parsed.length, 4, "a log's last newline adds no line");
206 assert.deepEqual(parsed[0], {
207 kind: "group",
208 title: "Run cargo build",
209 at: "2026-10-12T09:41:07.1234567Z",
210 lines: [{ line: { kind: "text", text: "cargo build", at: "2026-10-12T09:41:07.2000000Z", ansi: "\u001b[36;1mcargo build\u001b[0m" }, number: 1 }],
211 });
212 assert.deepEqual(parsed[1], {
213 kind: "line",
214 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" },
215 number: 2,
216 });
217 assert.deepEqual(parsed[2], {
218 kind: "line",
219 line: { kind: "error", text: "Process completed with exit code 1.", at: "2026-10-12T09:41:10.0000000Z" },
220 number: 3,
221 });
222 assert.deepEqual(parsed[3], { kind: "line", line: { kind: "text", text: "a line from before lines were stamped" }, number: 4 });
223 // Search reads the plain text, never the time or the codes.
224 assert.deepEqual(
225 searchLog(stamped, "compiling g1t").map((found) => found.number),
226 [2],
227 );
228 assert.deepEqual(searchLog(stamped, "2026-10-12"), []);
229 assert.deepEqual(searchLog(stamped, "32m"), []);
230 assert.deepEqual(splitStamp("2026-10-12T09:41:07Z text"), { at: "2026-10-12T09:41:07Z", rest: "text" });
231 assert.deepEqual(splitStamp("2026-10-12 not a stamp"), { rest: "2026-10-12 not a stamp" });
232 assert.deepEqual(classify("##[warning]plain"), { kind: "warning", text: "plain" });
233 assert.equal(lastStamp(stamped), "2026-10-12T09:41:10.0000000Z");
234 assert.equal(lastStamp("no stamps\n"), undefined);
235});