import test from "node:test"; import assert from "node:assert/strict"; import { TOKEN, callGet, callPost, setupOpsCorpus } from "./_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_liveRecordings.test.ts" // // THE LIVE RECORDINGS DOOR (lane live-recording-guard): `get live-recordings` // answers each run from its own row, `live-recording-ack` acknowledges, and // every ops reply carries the attention header while something needs a // person. A temp corpus; the row is written through the one writer. const corpus = await setupOpsCorpus(null); test.after(() => corpus.cleanup()); const SLUG = "demo-live"; await corpus.writeChannelConfig(SLUG, { platform: "youtube", url: "https://www.youtube.com/@Example" }); const { getPaths } = await import("yt-dlp-transcript-common/lib/paths"); const { upsertLiveRecordingRow } = await import("yt-dlp-transcript-common/lib/liveRecordings-server"); await upsertLiveRecordingRow(getPaths(), SLUG, { id: "exampleid01", url: "https://www.youtube.com/watch?v=exampleid01", platform: "youtube", startedAt: new Date().toISOString(), endedAt: new Date().toISOString(), status: "failed", fromStart: true, error: "nothing was recorded — 6 attempts (3 from its start, 3 from the live edge): ERROR: The downloaded file is empty", attempts: 6, mustKeep: true, parts: [], }); const list = (await import("./live-recordings/route")).GET as unknown as Parameters[0]; const ack = (await import("./live-recording-ack/route")).POST as unknown as Parameters[0]; const authed = { authorization: `Bearer ${TOKEN}` }; test("get live-recordings lists the failure, and the reply carries the attention header", async () => { const res = await list(new Request(`http://localhost/api/ops/live-recordings?slug=${SLUG}`, { headers: authed }), { params: Promise.resolve({}) }); assert.equal(res.status, 200); assert.equal(res.headers.get("x-archilyzer-attention"), "live-recordings=1"); const body = (await res.json()) as { needsAttention: number; rows: { id: string; status: string; needsAttention: boolean; check: string }[] }; assert.equal(body.needsAttention, 1); assert.equal(body.rows[0]?.status, "failed"); assert.equal(body.rows[0]?.check, `pnpm ops get live-recordings ${SLUG}/exampleid01`); const bad = await callGet(list, "http://localhost/api/ops/live-recordings?id=exampleid01"); assert.equal(bad.status, 400, "an id needs its slug"); const unknown = await callGet(list, "http://localhost/api/ops/live-recordings?slugs=x"); assert.equal(unknown.status, 400); }); test("live-recording-ack acknowledges it: listed still, counted no more, and the header goes", async () => { const refused = await callPost(ack, { slug: SLUG, id: "nosuchid001" }); assert.equal(refused.status, 400); const traversing = await callPost(ack, { slug: "../../escape", id: "exampleid01" }); assert.equal(traversing.status, 400); assert.match(String(traversing.body.error), /is not a valid channel slug/); const res = await ack( new Request("http://localhost/api/ops/live-recording-ack", { method: "POST", headers: { ...authed, "content-type": "application/json" }, body: JSON.stringify({ slug: SLUG, id: "exampleid01", by: "test" }), }), ); assert.equal(res.status, 200); assert.deepEqual(await res.json(), { ok: true, acknowledged: 1 }); assert.equal(res.headers.get("x-archilyzer-attention"), null); const after = await callGet(list, `http://localhost/api/ops/live-recordings?slug=${SLUG}`); const rows = after.body.rows as { needsAttention: boolean; acknowledgedBy?: string }[]; assert.equal(rows.length, 1); assert.equal(rows[0]?.needsAttention, false); assert.equal(rows[0]?.acknowledgedBy, "test"); }); test("the attention header rides on the GET routes that check their own auth too: get job, get jobs", async () => { await upsertLiveRecordingRow(getPaths(), SLUG, { id: "exampleid02", url: "https://www.youtube.com/watch?v=exampleid02", platform: "youtube", startedAt: new Date().toISOString(), status: "interrupted", stoppedBy: "shutdown", fromStart: true, attempts: 1, mustKeep: true, parts: [], }); (await import("./_attention")).forgetAttention(); const job = (await import("./job/[id]/route")).GET as unknown as (r: Request, c: { params: Promise<{ id: string }> }) => Promise; const res = await job(new Request("http://localhost/api/ops/job/nosuchjob", { headers: authed }), { params: Promise.resolve({ id: "nosuchjob" }) }); assert.equal(res.status, 404); assert.equal(res.headers.get("x-archilyzer-attention"), "live-recordings=1", "a 404 says it too"); const jobs = (await import("./jobs/route")).GET as unknown as (r: Request) => Promise; const list2 = await jobs(new Request("http://localhost/api/ops/jobs?active=1", { headers: authed })); assert.equal(list2.headers.get("x-archilyzer-attention"), "live-recordings=1"); }); test("the header's value says stalled recordings beside those that need a person", async () => { const { attentionHeaderValue } = await import("./_attention"); assert.equal(attentionHeaderValue(null), null); assert.equal(attentionHeaderValue({ needsAttention: 0, stalled: 0 }), null); assert.equal(attentionHeaderValue({ needsAttention: 2, stalled: 0 }), "live-recordings=2"); assert.equal(attentionHeaderValue({ needsAttention: 0, stalled: 1 }), "live-recordings=0, stalled=1"); });