/** * READ COST 4 (live, 28 Nov 2026): on the owner's object the shadows were asked and no row was laid in 3.5 hours. Here * the alarm is as busy as his: the object in use at every alarm (one turn job an alarm), every other bounded job always * owing work, and a pass after the jobs that throws on every alarm. The shadow is still filled, a step on each alarm * that is its time; the alarm is brought round by the request that asked; a throwing pass re-arms the alarm within a * minute; and GET /v1/admin/order-shadow says all of it. The file counts as over the index limit. Synthetic rows only. */ import { env, runInDurableObject } from "cloudflare:test"; import { afterAll, describe, expect, it } from "vitest"; import { Store } from "../src/store.ts"; import { ADMIN, http, personalObject, tool } from "./personal.ts"; const SLUG = "read-cost-alarm"; const BINNED = 20_000; const stub = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG)); const proto = Store.prototype as unknown as { indexTooBig(): boolean }; const tooBig = proto.indexTooBig; type Live = { gentleInTests: boolean; wants: Map; lastRequestAt: number; store: Store; alarm(): Promise; dropMedia(): Promise }; type Got = { shadows: { name: string; ready: boolean; rows: number; steps: number }[]; alarm_at: string | null; alarm_last: { began: string; ended: string | null; ran: string; error: string | null } | null; in_use: boolean; jobs_owed: string[]; shadow_last: { at: string; error: string | null } | null; }; /** The jobs that are always owed on his object: each answers "more" without touching the file. */ const OWING: Record = { measureStorage: true, carryCounts: true, trimChanges: { trimmed: 1, more: true }, trimActs: { trimmed: 1, more: true } }; describe("the shadows fill on an alarm as busy as the owner's (READ COST 4)", { timeout: 600_000 }, () => { afterAll(() => { proto.indexTooBig = tooBig; }); it("fills a step an alarm through every other job owing and a pass that throws, and says so", async () => { proto.indexTooBig = () => true; const { owner } = await personalObject(SLUG, { open: ["note"] }); const now = new Date().toISOString(); await runInDurableObject(stub(), (_i, state) => { state.storage.sql.exec( `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ?) INSERT INTO rows (table_id, id, data, owner, version, deleted, created_at, updated_at) SELECT 'note', 'binned-note-' || i, json_object('title', 'binned ' || i, 'trashed_at', ?), NULL, 1, 0, ?, ? FROM n`, BINNED, now, now, now, ); }); expect((await http(SLUG, "POST", "/v1/admin/order-shadow", { on: true }, ADMIN)).status).toBe(200); // Nothing else wants the alarm soon: as after a deploy, it stands an hour away. await runInDurableObject(stub(), async (_i, s) => s.storage.setAlarm(Date.now() + 3_600_000)); // list_bin asks for the note table's shadow, and the request brings the alarm round (not at its hour). expect((await tool(SLUG, owner, "list_bin", { limit: 5 })).isError).toBe(false); const asked = await http(SLUG, "GET", "/v1/admin/order-shadow", undefined, ADMIN); expect(asked.body.shadows.map((s) => s.name)).toContain("bin_note__trashed_at"); expect(Date.parse(asked.body.alarm_at!)).toBeLessThanOrEqual(Date.now() + 60_000); // Alarms by hand, each as on his object: in use, every other job owing, a pass that throws. Between two, the // shadow's want is reached (the clock has moved on by SHADOW_BUSY_MS). let alarms = 0; let rearmed = Infinity; for (; alarms < 40; alarms++) { const done = await runInDurableObject(stub(), async (instance, s) => { const l = instance as unknown as Live; const store = l.store as unknown as Record; l.gentleInTests = true; l.lastRequestAt = Date.now(); l.wants.set("shadow", 0); const real = new Map(); for (const [name, answer] of Object.entries(OWING)) { real.set(name, store[name]); store[name] = () => answer; } const drop = l.dropMedia; l.dropMedia = () => Promise.reject(new Error("an invented pass that always throws")); await s.storage.deleteAlarm(); try { await l.alarm(); } finally { for (const [name, was] of real) store[name] = was; l.dropMedia = drop; } rearmed = Math.min(rearmed, ((await s.storage.getAlarm()) ?? Infinity) - Date.now()); return !l.store.shadowsFilling(); }); if (done) break; } const filled = await http(SLUG, "GET", "/v1/admin/order-shadow", undefined, ADMIN); console.log(`READ COST 4: ${alarms + 1} alarms; ${JSON.stringify({ ...filled.body, lookups: undefined })}`); const note = filled.body.shadows.find((s) => s.name === "bin_note__trashed_at")!; expect(note.ready).toBe(true); expect(note.rows).toBeGreaterThanOrEqual(BINNED); // 20,000 rows at 2,500 a step, made and found complete: about a dozen alarms, never one in eight. expect(alarms + 1).toBeLessThanOrEqual(BINNED / 2_500 + 4); // Every alarm threw after the jobs, and each one set the next within a minute. expect(rearmed).toBeLessThanOrEqual(60_000); expect(filled.body.alarm_last?.error).toContain("an invented pass that always throws"); expect(filled.body.alarm_last?.ran).toContain("order_shadow"); expect(filled.body.shadow_last?.error).toBeNull(); // list_bin now reads along the shadow: the newest binned note answered. const after = await tool(SLUG, owner, "list_bin", { limit: 5 }); expect(JSON.stringify(after)).toContain("binned-note-"); }); });