Cotton code
76 lines · 4.5 KB · TypeScriptRaw
/** * JOBS OUT S1 (owner priority, 28 Nov 2026; assets/design/shared/JOBS-OUT.md (d)): the order shadows' fill moved off * the account object's alarm to a step job beside it, kind `order_shadow`, behind its own per-account switch * (`jobs: { order_shadow: "on" }`, off by default; off, the alarm fills as READ COST 4 made it). On: a request whose * lookup asks for a shadow puts a run on the queue; the alarm lays no row; the run fills each shadow through the door, * one bounded step a call (GENTLE_SHADOW_ROWS rows of one table along its key, never a whole table in one request). * The file counts as over the index limit. Synthetic rows only. */import { env, runInDurableObject, SELF } from "cloudflare:test";import { afterAll, describe, expect, it } from "vitest";import type { Business } from "../src/business.ts";import { runStepJob } from "../src/jobs/step_job.ts";import { Store } from "../src/store.ts";import { ADMIN, http, personalObject, tool } from "./personal.ts";const SLUG = "step-job-order-shadow";const BINNED = 20_000;/** Rows a step lays at most (business.ts GENTLE_SHADOW_ROWS). */const STEP = 2_500;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);const proto = Store.prototype as unknown as { indexTooBig(): boolean };const tooBig = proto.indexTooBig;type Shadow = { name: string; ready: boolean; rows: number; steps: number };const shadows = async () => (await http<{ shadows: Shadow[] }>(SLUG, "GET", "/v1/admin/order-shadow", undefined, ADMIN)).body.shadows;const jobRow = () => runInDurableObject(stub(), (_i, s) => s.storage.sql.exec<Record<string, SqlStorageValue>>("SELECT * FROM step_job WHERE kind = 'order_shadow'").toArray()[0] ?? null);describe("JOBS OUT S1: the order shadows filled by a step job beside the account object", { timeout: 300_000 }, () => {  afterAll(() => {    proto.indexTooBig = tooBig;  });  it("on: a lookup's request asks for a run, the alarm lays nothing, and the run fills the shadow a bounded step a call", async () => {    proto.indexTooBig = () => true;    const { owner } = await personalObject(SLUG, { open: ["note"] });    const now = new Date().toISOString();    await runInDurableObject(stub(), (_i, s) => {      s.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);    const on = await http(SLUG, "PUT", "/v1/admin/business", { jobs: { order_shadow: "on" } }, ADMIN);    expect(on.status, JSON.stringify(on.body)).toBe(200);    // list_bin asks for the note table's shadow: the request puts a run on the queue.    expect((await tool(SLUG, owner, "list_bin", { limit: 5 })).isError).toBe(false);    expect((await shadows()).map((s) => s.name)).toContain("bin_note__trashed_at");    const row = await jobRow();    expect(row, "a job row").not.toBeNull();    expect(row!.sent_at, "a run was put on the queue").not.toBeNull();    // The alarm lays no row: the fill is the job's.    await runInDurableObject(stub(), (instance: Business) => instance.alarm());    expect((await shadows()).find((s) => s.name === "bin_note__trashed_at")).toMatchObject({ ready: false, rows: 0, steps: 0 });    // The run, through the door, until the object says there is no more.    const message = await runInDurableObject(stub(), (instance: Business) => instance.stepJobMessage("order_shadow"));    expect(await runStepJob({ door }, message)).toBe("ran");    const note = (await shadows()).find((s) => s.name === "bin_note__trashed_at")!;    expect(note.ready).toBe(true);    expect(note.rows).toBeGreaterThanOrEqual(BINNED);    // A step a call, each at most STEP rows of the table: never the whole table in one request.    expect(note.steps).toBeGreaterThanOrEqual(Math.ceil(BINNED / STEP));    const after = await jobRow();    expect(after!.run, "the run ended").toBeNull();    expect(after!.due_at, "nothing left: no next run").toBeNull();    // list_bin reads along the filled shadow.    expect(JSON.stringify(await tool(SLUG, owner, "list_bin", { limit: 5 }))).toContain("binned-note-");  });});