// `umtool build --run` (release 36 lane C): the build route as the CLI asks // it -- started, followed (the log as it grows), cancelled, refused -- against // a fake app on a local port. Synthetic. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import http from "node:http"; import test from "node:test"; import { runBuildOverHttp, umtoolBase } from "./buildRun.mjs"; /** A fake umtool: the build route's POST, GET ?job= and ?cancel=. */ async function fakeApp(handler) { const seen = []; const server = http.createServer(async (req, res) => { let body = ""; for await (const c of req) body += c; const url = new URL(req.url, "http://x"); seen.push({ method: req.method, path: url.pathname, query: Object.fromEntries(url.searchParams), body: body ? JSON.parse(body) : null, auth: req.headers.authorization ?? null }); const [status, json] = handler(seen.at(-1), seen); res.writeHead(status, { "content-type": "application/json" }); res.end(JSON.stringify(json)); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); const base = `http://127.0.0.1:${server.address().port}`; return { base, seen, close: () => new Promise((r) => server.close(r)) }; } test("umtoolBase: --url, else UMTOOL_URL, else the local default; no trailing slash", () => { assert.equal(umtoolBase("http://h:1/", {}), "http://h:1"); assert.equal(umtoolBase(undefined, { UMTOOL_URL: "http://u:2//" }), "http://u:2"); assert.equal(umtoolBase(undefined, {}), "http://localhost:3050"); }); test("runBuildOverHttp: starts the job, prints its log as it grows, ends with its state", async () => { let polls = 0; const app = await fakeApp((r) => { if (r.method === "POST" && !r.query.cancel) return [200, { ok: true, job: { id: "j1", kind: "build reports/x (fast)" }, steps: [{ label: "check" }, { label: "build" }] }]; if (r.method === "GET") { polls += 1; const log = ["line 1", "line 2", "line 3"]; const since = Number(r.query.since); return polls < 3 ? [200, { state: "running", log: log.slice(since, polls), next: polls, nextEvent: polls * 2, error: null }] : [200, { state: "done", log: log.slice(since), next: 3, nextEvent: 6, error: null }]; } return [404, {}]; }); try { const lines = []; const r = await runBuildOverHttp({ base: app.base, project: "reports/x", preset: "final", only: null, token: "tok", pollMs: 5, log: (l) => lines.push(l) }); assert.deepEqual(r, { state: "done", job: "j1", error: null }); assert.deepEqual(lines, ["job j1: build reports/x (fast)", " step: check", " step: build", "line 1", "line 2", "line 3"]); assert.deepEqual(app.seen[0].body, { project: "reports/x", preset: "final" }); assert.equal(app.seen[0].auth, "Bearer tok"); // Each poll asks only for what it has not seen. assert.deepEqual(app.seen.slice(1).map((s) => [s.query.since, s.query.sinceEvent]), [["0", "0"], ["1", "2"], ["2", "4"]]); } finally { await app.close(); } }); test("runBuildOverHttp: a newer deliverable is refused until --replace; a running job is named; Ctrl-C cancels", async () => { const app = await fakeApp((r) => { if (r.method === "POST" && r.query.cancel) return [200, { cancelled: true }]; if (r.method === "POST" && r.query.replace !== "1") return [409, { error: "x.mp4 is newer than the manifest", needsReplace: true }]; if (r.method === "POST") return [200, { ok: true, job: { id: "j2", kind: "build" } }]; return [200, { state: "failed", log: ["** cancel requested"], next: 1, error: "cancelled" }]; }); try { const refused = await runBuildOverHttp({ base: app.base, project: "reports/x", log: () => {} }); assert.deepEqual(refused, { state: "refused", job: null, error: "x.mp4 is newer than the manifest", needsReplace: true }); let cancel = null; const r = await runBuildOverHttp({ base: app.base, project: "reports/x", replace: true, skipFetch: true, only: "c1", pollMs: 5, log: () => {}, onJob: (c) => (cancel = c) }); assert.equal(r.state, "failed"); assert.equal(r.error, "cancelled"); assert.deepEqual(app.seen[1].body, { project: "reports/x", preset: "fast", only: "c1", skipFetch: true }); assert.equal(await cancel(), true); assert.equal(app.seen.at(-1).query.cancel, "j2"); } finally { await app.close(); } const gone = await runBuildOverHttp({ base: "http://127.0.0.1:9", project: "p", log: () => {} }); assert.equal(gone.state, "refused"); assert.match(gone.error, /did not answer/); });