diff --git a/quc/test/runs.test.js b/quc/test/runs.test.js new file mode 100644 index 0000000..3250f5a --- /dev/null +++ b/quc/test/runs.test.js @@ -0,0 +1,51 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createRun, setStatus, addEvent, getRun, getEvents } from "../src/runs.js"; + +const mem = () => { const m = new Map(); return { get: async (k) => m.get(k) ?? null, put: async (k, v) => void m.set(k, v), delete: async (k) => void m.delete(k) }; }; +const input = (visitor = "v1") => ({ repo: "https://github.com/a/b", task: "Fix the flaky date test", visitor }); +const code = (p) => p.then(() => null, (e) => e.code); + +test("a run walks queued -> running -> ended and persists in the store", async () => { + const s = mem(); + const run = await createRun(s, input()); + assert.equal(run.status, "queued"); + assert.equal((await setStatus(s, run.id, "running")).status, "running"); + const done = await setStatus(s, run.id, "ended", "finished"); + assert.deepEqual([done.status, done.outcome], ["ended", "finished"]); + assert.ok(done.startedAt && done.endedAt); + assert.deepEqual(await getRun(s, run.id), done); +}); + +test("bad transitions, outcomes and inputs are refused", async () => { + const s = mem(); + const { id } = await createRun(s, input()); + assert.equal(await code(setStatus(s, id, "ended")), "outcome"); + assert.equal(await code(setStatus(s, id, "queued")), "transition"); + await setStatus(s, id, "ended", "safeworded"); + assert.equal(await code(setStatus(s, id, "running")), "transition"); + assert.equal(await code(setStatus(s, "nope", "running")), "not_found"); + assert.equal(await code(createRun(s, { ...input(), repo: "x" })), "invalid"); + assert.equal(await code(createRun(s, input(""))), "visitor"); +}); + +test("one active run per visitor; ending frees the slot; other visitors are unaffected", async () => { + const s = mem(); + const a = await createRun(s, input("v1")); + assert.equal(await code(createRun(s, input("v1"))), "active"); + await setStatus(s, a.id, "running"); + assert.equal(await code(createRun(s, input("v1"))), "active"); + await createRun(s, input("v2")); + await setStatus(s, a.id, "ended", "unlocked"); + await createRun(s, input("v1")); +}); + +test("events append in order and unknown kinds are refused", async () => { + const s = mem(); + const { id } = await createRun(s, input()); + await addEvent(s, id, "commentary", "reading the tests"); + await addEvent(s, id, "diff", "--- a\n+++ b"); + assert.deepEqual((await getEvents(s, id)).map((e) => [e.seq, e.kind]), [[1, "commentary"], [2, "diff"]]); + assert.equal(await code(addEvent(s, id, "nope", "")), "kind"); + assert.equal(await code(addEvent(s, "nope", "commit", "")), "not_found"); +});