55 lines · 2.0 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { LogBatcher } from "../runtime/log-batcher.ts"; |
| 4 | import { jobRunnerName, runIdFor } from "../runtime/run-id.ts"; |
| 5 | |
| 6 | const SHA = "8f8b47dcf240385c09a95af895ecda6e97734b1e"; |
| 7 | |
| 8 | test("a run ID is stable for one commit and config, and changes with either", async () => { |
| 9 | const id = await runIdFor("acme.web", SHA, "jobs: {}"); |
| 10 | assert.match(id, /^acme-web-8f8b47dcf240-[0-9a-f]{12}$/); |
| 11 | assert.equal(await runIdFor("acme.web", SHA, "jobs: {}"), id); |
| 12 | assert.notEqual(await runIdFor("acme.web", SHA, "jobs: { a: {} }"), id); |
| 13 | assert.notEqual(await runIdFor("acme.web", "1".repeat(40), "jobs: {}"), id); |
| 14 | }); |
| 15 | |
| 16 | test("a run ID fits a Workflow instance ID even for a long repo name", async () => { |
| 17 | const id = await runIdFor("R".repeat(300), SHA, "x"); |
| 18 | assert.ok(id.length <= 100); |
| 19 | assert.match(id, /^[a-z0-9_-]+$/); |
| 20 | assert.equal(jobRunnerName(id, "test"), `${id}/test`); |
| 21 | }); |
| 22 | |
| 23 | test("the batcher keeps order, splits on step changes and flushes the rest", async () => { |
| 24 | const sent: string[] = []; |
| 25 | const batcher = new LogBatcher(async (step, text) => { |
| 26 | await new Promise((resolve) => setTimeout(resolve, Math.random() * 5)); |
| 27 | sent.push(`${step}:${text}`); |
| 28 | }); |
| 29 | batcher.write(0, "clone "); |
| 30 | batcher.write(0, "done\n"); |
| 31 | batcher.write(1, "a\n"); |
| 32 | batcher.write(1, "x".repeat(9000)); |
| 33 | batcher.write(2, "end\n"); |
| 34 | await batcher.flush(); |
| 35 | assert.deepEqual(sent, ["0:clone done\n", `1:a\n${"x".repeat(9000)}`, "2:end\n"]); |
| 36 | }); |
| 37 | |
| 38 | test("a failed send is dropped and later output still goes out", async () => { |
| 39 | const sent: string[] = []; |
| 40 | let calls = 0; |
| 41 | const batcher = new LogBatcher(async (_step, text) => { |
| 42 | if (calls++ === 0) throw new Error("hub unavailable"); |
| 43 | sent.push(text); |
| 44 | }); |
| 45 | const original = console.error; |
| 46 | console.error = () => undefined; |
| 47 | try { |
| 48 | batcher.write(0, "lost\n"); |
| 49 | batcher.write(1, "kept\n"); |
| 50 | await batcher.flush(); |
| 51 | } finally { |
| 52 | console.error = original; |
| 53 | } |
| 54 | assert.deepEqual(sent, ["kept\n"]); |
| 55 | }); |