import assert from "node:assert/strict"; import { test } from "node:test"; import { LogBatcher } from "../runtime/log-batcher.ts"; import { jobRunnerName, runIdFor } from "../runtime/run-id.ts"; const SHA = "8f8b47dcf240385c09a95af895ecda6e97734b1e"; test("a run ID is stable for one commit and config, and changes with either", async () => { const id = await runIdFor("acme.web", SHA, "jobs: {}"); assert.match(id, /^acme-web-8f8b47dcf240-[0-9a-f]{12}$/); assert.equal(await runIdFor("acme.web", SHA, "jobs: {}"), id); assert.notEqual(await runIdFor("acme.web", SHA, "jobs: { a: {} }"), id); assert.notEqual(await runIdFor("acme.web", "1".repeat(40), "jobs: {}"), id); }); test("a run ID fits a Workflow instance ID even for a long repo name", async () => { const id = await runIdFor("R".repeat(300), SHA, "x"); assert.ok(id.length <= 100); assert.match(id, /^[a-z0-9_-]+$/); assert.equal(jobRunnerName(id, "test"), `${id}/test`); }); test("the batcher keeps order, splits on step changes and flushes the rest", async () => { const sent: string[] = []; const batcher = new LogBatcher(async (step, text) => { await new Promise((resolve) => setTimeout(resolve, Math.random() * 5)); sent.push(`${step}:${text}`); }); batcher.write(0, "clone "); batcher.write(0, "done\n"); batcher.write(1, "a\n"); batcher.write(1, "x".repeat(9000)); batcher.write(2, "end\n"); await batcher.flush(); assert.deepEqual(sent, ["0:clone done\n", `1:a\n${"x".repeat(9000)}`, "2:end\n"]); }); test("a failed send is dropped and later output still goes out", async () => { const sent: string[] = []; let calls = 0; const batcher = new LogBatcher(async (_step, text) => { if (calls++ === 0) throw new Error("hub unavailable"); sent.push(text); }); const original = console.error; console.error = () => undefined; try { batcher.write(0, "lost\n"); batcher.write(1, "kept\n"); await batcher.flush(); } finally { console.error = original; } assert.deepEqual(sent, ["kept\n"]); });