/** * JOBS OUT C3: the storage counting (the storage measure, src/storage_measure.ts, and the row counts' recount, * Store.carryCounts) as a step job on M1's door (src/step_jobs.ts, kind `storage_count`), behind its own per-account * switch, off by default. Off, the alarm's bounded jobs take their steps exactly as before; on, the alarm takes none, * and each run takes one bounded step (the recount: Store.recountChunk rows along the key), the next run asked for * at the bounded jobs' own pace. Invented rows only (test/big_object.ts). */ import { env, runInDurableObject, SELF } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import type { Business } from "../src/business.ts"; import { runStepJob, type StepJobMessage } from "../src/jobs/step_job.ts"; import { personalShape, writeShape } from "./big_object.ts"; import { ADMIN, http, personalObject } from "./personal.ts"; const KIND = "storage_count"; const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug)); const door = (slug: string, path: string, init: RequestInit) => SELF.fetch(`https://jobs.internal/b/${slug}${path}`, init); const messageOf = (slug: string): Promise => runInDurableObject(stub(slug), (instance: Business) => instance.stepJobMessage(KIND)); const jobRow = (slug: string) => runInDurableObject(stub(slug), (_i, state) => state.storage.sql.exec>("SELECT * FROM step_job WHERE kind = ?", KIND).toArray()[0] ?? null); type Plan = { queue: unknown[]; steps?: number; headRows?: number } | null; const planOf = (slug: string): Promise => runInDurableObject(stub(slug), (_i, state) => { const v = state.storage.sql.exec<{ value: string }>("SELECT value FROM meta WHERE key = 'row_counts_plan'").toArray()[0]?.value; return v ? (JSON.parse(v) as Plan) : null; }); const counts = (slug: string) => runInDurableObject(stub(slug), (_i, state) => state.storage.sql.exec("SELECT table_id, day, deleted, n FROM row_counts ORDER BY table_id, day, deleted").toArray()); /** The object's own alarm, run by hand and alone. */ const alarm = (slug: string) => runInDurableObject(stub(slug), async (instance: Business, state) => { await state.storage.deleteAlarm(); await instance.alarm(); }); /** A personal object holding invented rows whose counts are to be taken again (as the live object was: no plan yet). */ async function shaped(slug: string, on: boolean): Promise { await personalObject(slug); if (on) expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, ADMIN)).status).toBe(200); await runInDurableObject(stub(slug), (_i, state) => { writeShape(state.storage.sql, personalShape(1 / 200)); state.storage.sql.exec("DELETE FROM meta WHERE key = 'row_counts_plan'"); }); } describe("JOBS OUT C3: the storage counting as a step job", { timeout: 300_000 }, () => { it("is switched per account by the admin alone, on M1's switch, and is off by default", async () => { const slug = "c3-switch"; const { owner } = await personalObject(slug); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, { authorization: `Bearer ${owner}` })).status).toBe(403); // AUDIT F1/F2: a credential that does not open answers 401 whether the kind is on or off. expect((await door(slug, `/v1/jobs/step/${KIND}`, { method: "POST", headers: { authorization: "CottonJob j1.x.y" } })).status).toBe(401); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: null } }, ADMIN)).status).toBe(200); }); it("off: the alarm takes the recount's steps as before", async () => { const slug = "c3-off"; await shaped(slug, false); await alarm(slug); expect((await planOf(slug))?.steps ?? 0).toBeGreaterThan(0); }); it("on: the alarm takes no step; each run takes one bounded step, the next asked for at the bounded jobs' pace, and the counts are the old path's", async () => { const off = "c3-same-off"; await shaped(off, false); for (let i = 0; i < 500 && (await planOf(off))?.queue.length !== 0; i++) await alarm(off); expect((await planOf(off))?.queue.length).toBe(0); const slug = "c3-same-on"; await shaped(slug, true); await alarm(slug); expect((await planOf(slug))?.steps ?? 0, "with the switch on the alarm takes no recount step").toBe(0); expect((await jobRow(slug))!.sent_at, "the alarm's watchdog asked for a run").not.toBeNull(); const first = await messageOf(slug); let runs = 0; let message = first; for (; runs < 500; runs++) { const before = (await planOf(slug))?.steps ?? 0; expect(await runStepJob({ door }, message)).toBe("ran"); const plan = await planOf(slug); expect(plan!.steps, "one run takes one recount step (Store.recountChunk rows at most)").toBe(before + 1); if (!plan!.queue.length) break; // Rows left: the next run is on the queue at the bounded jobs' pace (5 s, or 60 s while in use), never at once. const row = (await jobRow(slug))!; const wait = Number(row.due_at) - Date.now(); expect(wait).toBeGreaterThan(3_000); expect(wait).toBeLessThanOrEqual(61_000); if (runs === 1) { // The alarm, meanwhile, takes no step of its own. await alarm(slug); expect((await planOf(slug))!.steps).toBe(before + 1); } message = await messageOf(slug); } expect(runs, "the counts take several runs").toBeGreaterThan(3); expect(await counts(slug)).toEqual(await counts(off)); // Done: no run asked for until there is work again. expect((await jobRow(slug))!.due_at).toBeNull(); // A message delivered again runs nothing. expect(await runStepJob({ door }, first)).toBe("gone"); }); });