import { env, runInDurableObject } from"cloudflare:test";import { describe, expect, it } from"vitest";importtype { Business } from"../src/business.ts";import { personalShape, watchStatements, writeShape } from"./big_object.ts";import { ADMIN, http, personalObject, TODOS_OPEN } from"./personal.ts";/** * THE ALARM CARRIES THE RECOUNT A FEW BOUNDED STEPS AT A TIME (live, 21 Nov * 2026: the first deploy of the stepped recount bounded the alarm's share by * the clock, which a Worker does not move while its code runs; the alarm ran * the whole recount of the personal object in one transaction, its storage * timed out and the object was reset six times). An alarm now takes at most * RECOUNT_STEPS steps, each in its own transaction, and the admin route says * how far it stands from the plan alone. */const SLUG = "row-counts-alarm";typeProgress = { ready: boolean; planned: boolean; left: { table: string }[]; head_rows: number; steps: number; step_rows: number; last_step: string | null };describe("the alarm's recount", () => { it("takes a few steps an alarm, each its own transaction, and says how far it stands without a row", async () => {await personalObject(SLUG);const stub = env.BUSINESS.get(env.BUSINESS.idFromName(SLUG));const rows = await runInDurableObject(stub, (_i, state) => {const shape = personalShape(1 / 200); writeShape(state.storage.sql, shape);// As the live object was: a basis from before plans. state.storage.sql.exec("DELETE FROM meta WHERE key = 'row_counts_plan'");return shape.reduce((n, t) => n + t.rows, 0); });let alarms = 0;const steps = (sql: SqlStorage): number => (JSON.parse(sql.exec<{ value: string }>("SELECT value FROM meta WHERE key = 'row_counts_plan'").toArray()[0]?.value ?? "{}") as { steps?: number }).steps ?? 0;for (; alarms < 1000; alarms++) {const { read, took, left } = await runInDurableObject(stub, async (instance: Business, state) => {// The alarm the object set is the runtime's to fire too: this one is run by hand, alone.await state.storage.deleteAlarm();const at = steps(state.storage.sql);const run = watchStatements(state.storage.sql, () => instance.alarm());await run.result;const read = run.statements.filter((s) => /FROM rows /.test(s.query)).reduce((n, s) => n + s.rowsRead, 0);const plan = JSON.parse(state.storage.sql.exec<{ value: string }>("SELECT value FROM meta WHERE key = 'row_counts_plan'").one().value) as { queue: unknown[] };return { read, took: steps(state.storage.sql) - at, left: plan.queue.length }; });const p = (await http<Progress>(SLUG, "GET", "/v1/admin/row-counts", undefined, ADMIN)).body; expect(p.planned).toBe(true); expect(took, "an alarm takes at most four steps").toBeLessThanOrEqual(4); expect(read, "an alarm reads at most four steps' rows").toBeLessThanOrEqual(4 * (p.step_rows + 1) + 50);if (!left) break; }const p = (await http<Progress>(SLUG, "GET", "/v1/admin/row-counts", undefined, ADMIN)).body; expect(p.ready, JSON.stringify(p)).toBe(true); expect(alarms, `the ${rows} rows take about the alarms their steps need`).toBeGreaterThan(1); expect(JSON.stringify(p)).not.toMatch(/sealed_enc|s1:/); }, 600_000);});describe("a refused act in the log", () => { it("says its name, table and code, never its ids or data", async () => {const slug = "act-refused-log";await personalObject(slug, { open: TODOS_OPEN });const said: string[] = [];const log = console.log; console.log = (...parts: unknown[]) => void said.push(parts.join(" "));try {const made = await http(slug, "POST", "/v1/act", { act: "create", args: { table: "todo", id: "invented-todo-1", data: { title: "an invented secret title" } } }, ADMIN); expect(made.status, JSON.stringify(made.body)).toBe(200);const again = await http(slug, "POST", "/v1/act", { act: "create", args: { table: "todo", id: "invented-todo-1", data: { title: "an invented secret title" } } }, ADMIN); expect(again.status).toBe(409); } finally { console.log = log; }const line = said.find((l) => l.includes("act refused")); expect(line).toMatch(/act refused act-refused-log: create todo 409 duplicate_id by owner/); expect(line).not.toMatch(/invented-todo-1|secret/); }, 120_000);});