/** * JOBS OUT PROOF (coordinator, 28 Nov 2026): the object's half of scripts/jobs-out-proof.ts, on the test pool (a dev * Worker as far as the routes can tell). An invented account is made by POST /v1/admin/dev/invent-personal; for a * kind, POST /v1/admin/dev/jobs-seed lays invented work, and GET /v1/admin/jobs says what is left, the steps its job * ran and what the alarm did of it. Switched on, a run through the door does the work and the alarm none; switched * off, the alarm does new work again. Synthetic rows only. */ import { env, runInDurableObject, SELF } from "cloudflare:test"; import { beforeAll, describe, expect, it } from "vitest"; import type { Business } from "../src/business.ts"; import { runStepJob } from "../src/jobs/step_job.ts"; import { ADMIN, http } from "./personal.ts"; const SLUG = "jobs-proof-routes"; const stub = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG)); const door = (slug: string, path: string, init: RequestInit) => SELF.fetch(`https://jobs.internal/b/${slug}${path}`, init); type Kind = { kind: string; on: boolean; owes: boolean; left: number | boolean | null; job_steps: number; object_did: number }; const kindOf = async (kind: string) => (await http<{ kinds: Kind[] }>(SLUG, "GET", "/v1/admin/jobs", undefined, ADMIN)).body.kinds.find((k) => k.kind === kind)!; const done = (k: Kind) => (typeof k.left === "number" ? k.left === 0 : k.left === null ? !k.owes : k.left === false); const alarm = () => runInDurableObject(stub(), async (instance: Business, s) => { await s.storage.deleteAlarm(); await instance.alarm(); }); /** Send's own tables, as the seed and the pass left them. */ const sendState = () => runInDurableObject(stub(), (_instance: Business, s) => { const all = (q: string) => s.storage.sql.exec(q).toArray() as T[]; return { in: all<{ id: string; state: string; tell: number }>("SELECT id, state, tell FROM send_in WHERE id LIKE 'proof%'"), out: all<{ id: string; state: string }>("SELECT id, state FROM send_out WHERE id LIKE 'proof%'"), holds: all<{ send_id: string }>("SELECT send_id FROM send_hold WHERE send_id LIKE 'proof%'"), copies: all<{ dest: string; done: number }>("SELECT dest, done FROM send_copy WHERE send_id LIKE 'proof%'"), }; }); describe("the jobs proof's routes", { timeout: 600_000 }, () => { beforeAll(async () => { const made = await http<{ personal: boolean; tables: number }>(SLUG, "POST", "/v1/admin/dev/invent-personal", {}, ADMIN); expect(made.status, JSON.stringify(made.body)).toBe(200); expect(made.body.personal).toBe(true); expect(made.body.tables).toBeGreaterThan(10); }); it("answers the admin token only", async () => { expect((await http(SLUG, "GET", "/v1/admin/jobs")).status).toBe(403); }); for (const kind of ["prompts", "changes", "media_drop", "order_shadow", "send"]) { it(`${kind}: the job does the seeded work, the alarm none; off, the alarm again`, async () => { expect((await http(SLUG, "PUT", "/v1/admin/business", { jobs: { [kind]: "on" } }, ADMIN)).status, kind).toBe(200); const from = await kindOf(kind); const seed = await http<{ seeded: boolean; why?: string }>(SLUG, "POST", "/v1/admin/dev/jobs-seed", { kind }, ADMIN); expect(seed.body.seeded, `${kind}: ${seed.body.why ?? ""}`).toBe(true); expect(done(await kindOf(kind)), `${kind}: work laid`).toBe(false); // An alarm beside it does none of it. await alarm(); expect((await kindOf(kind)).object_did, `${kind}: the object did none while on`).toBe(from.object_did); // The run, through the door. const message = await runInDurableObject(stub(), (instance: Business) => instance.stepJobMessage(kind)); expect(await runStepJob({ door }, message), kind).toBe("ran"); const after = await kindOf(kind); expect(after.job_steps, `${kind}: steps`).toBeGreaterThan(from.job_steps); expect(done(after), `${kind}: done, left ${JSON.stringify(after.left)}`).toBe(true); // Off: new work is the alarm's again. expect((await http(SLUG, "PUT", "/v1/admin/business", { jobs: { [kind]: null } }, ADMIN)).status).toBe(200); await http(SLUG, "POST", "/v1/admin/dev/jobs-seed", { kind }, ADMIN); for (let i = 0; i < 20 && !done(await kindOf(kind)); i++) await alarm(); const back = await kindOf(kind); expect(back.object_did, `${kind}: the object took it again`).toBeGreaterThan(from.object_did); expect(done(back), `${kind}: the alarm did it, left ${JSON.stringify(back.left)}`).toBe(true); }); } it("send: the seed's sends ask no one outside, and its pass leaves them closed and copied", async () => { await http(SLUG, "POST", "/v1/admin/dev/jobs-seed", { kind: "send" }, ADMIN); const laid = await sendState(); // No seeded send asks for its receipt to go back: the pass tells no other object anything. expect(laid.in.length).toBeGreaterThan(0); expect(laid.in.every((r) => r.tell === 0)).toBe(true); // More copies than one pass takes, so a run is more than one step. expect(laid.copies.filter((c) => !c.done).length).toBeGreaterThan(8); for (let i = 0; i < 20 && !done(await kindOf("send")); i++) await alarm(); const left = await sendState(); expect(left.copies).toEqual([]); expect(left.in.filter((r) => r.state === "keeping")).toEqual([]); expect(left.in.filter((r) => r.state === "offered")).toEqual([]); expect(left.out.length).toBeGreaterThan(0); expect(left.out.every((r) => r.state === "expired")).toBe(true); expect(left.holds).toEqual([]); // The kept sends' bytes were copied, R2 to R2, inside the invented account's own folder. const listed = await env.MEDIA.list({ prefix: `${SLUG}/files/send-proof-kept-` }); expect(listed.objects.length).toBeGreaterThan(8); }); });