import { env, runInDurableObject } from "cloudflare:test"; import { beforeAll, describe, expect, it } from "vitest"; import { answer, PERSONAL_TABLES, personalObject } from "./personal.ts"; /** * Schema 145 (7 Nov 2026): the message table's body-or-media CHECK left the * kit's storage. The object holds rows as JSON in one table and runs no * per-table SQL, so it has no CHECK of its own to drop: an object made before * takes the change as the keep lays the kit's message definition (the table is * the build's, under its engine), one definition row. No row is read into the * isolate or rewritten, and the links are not rebuilt (the message table's * link columns did not change). On 800,000 rows (the box, 8 Nov 2026, load * ~30) the first request with its keep and four queued behind it answered in * 13.2 s against 9.1 s with no keep: the keep is ~4 s, mostly each kit * table's row count, in one transaction, and no queued request failed. N here * keeps the suite quick. */ const N = 20_000; const CHECK = "\n CHECK ((deleted_at IS NOT NULL) OR (trim(COALESCE(body, '')) <> '') OR (json_array_length(media) > 0))"; type Doc = Record; type Storage = { constraints: { declaration: string; position: number }[] }; /** The kit's message definition as a 144 object holds it: the CHECK in its storage. */ function message144(): Doc { const doc = structuredClone(PERSONAL_TABLES.find((t) => t.id === "message")!) as Doc; const storage = (doc.tables as Storage[])[0]!; const at = storage.constraints.findIndex((d) => d.declaration.includes("CHECK (direction IN")); expect(at).toBeGreaterThan(-1); storage.constraints.splice(at + 1, 0, { declaration: CHECK, position: storage.constraints[at]!.position + 1 }); storage.constraints.forEach((d, i) => (d.position = storage.constraints[0]!.position + i)); return doc; } const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug)); describe("an object made before schema 145 takes the message storage without its CHECK", () => { const SLUG = "message-check-keep"; let owner = ""; let rowsBefore = ""; beforeAll(async () => { ({ owner } = await personalObject(SLUG)); rowsBefore = await runInDurableObject(stub(SLUG), (_instance, state) => { const sql = state.storage.sql; sql.exec( `INSERT INTO rows (table_id, id, data, version, created_at, updated_at) VALUES ('thread', 'th-synthetic', json('{"kind":"dm","title":"A synthetic chat"}'), 1, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z')`, ); 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, version, created_at, updated_at) SELECT 'message', 'm-' || i, CASE i % 3 WHEN 0 THEN json_object('thread_id', 'th-synthetic', 'direction', 'in', 'channel', 'whatsapp', 'origin', 'imported', 'body', 'synthetic ' || i, 'media', json('[]'), 'meta', json('{}')) WHEN 1 THEN json_object('thread_id', 'th-synthetic', 'direction', 'in', 'channel', 'whatsapp', 'origin', 'imported', 'body', 'conversation.preview.photo', 'media', json('[]'), 'meta', json('{"mediaOnly":"image"}')) ELSE json_object('thread_id', 'th-synthetic', 'direction', 'in', 'channel', 'whatsapp', 'origin', 'imported', 'body', 'Voice message', 'media', json('[]'), 'meta', json('{"mediaOnly":"voice"}')) END, 1, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z' FROM n`, N, ); sql.exec("INSERT INTO links (table_id, id, column, target) SELECT table_id, id, 'thread_id', 'th-synthetic' FROM rows WHERE table_id = 'message'"); sql.exec("UPDATE definitions SET json = ? WHERE id = 'table:message'", JSON.stringify(message144())); sql.exec("UPDATE meta SET value = 'a kit before 145' WHERE key = 'kit_version'"); return sql.exec<{ h: string }>("SELECT count(*) || ':' || sum(length(data)) || ':' || max(version) AS h FROM rows WHERE table_id = 'message'").toArray()[0]!.h; }); }, 600_000); it("lays the kit's message storage on the first request, moves no row and fails no request queued behind it", async () => { const started = performance.now(); // Messages are sealed (e3bca3c99): an assistant reads them only in a window, so the five ask a table clear by design (session: seal/default.json pending; podcast, the open one, left with TABLE DROP 1); the keep runs ahead of any request. const answers = await Promise.all(Array.from({ length: 5 }, () => answer<{ table: string; total: number }>(SLUG, owner, "list_records", { table: "session", limit: 1 }))); const took = performance.now() - started; console.log(`message-check-keep: ${N} message rows, the keep and five queued requests in ${Math.round(took)} ms`); for (const got of answers) expect(got).toMatchObject({ table: "session", total: 0 }); const after = await runInDurableObject(stub(SLUG), (_instance, state) => { const sql = state.storage.sql; const doc = JSON.parse(sql.exec<{ json: string }>("SELECT json FROM definitions WHERE id = 'table:message'").toArray()[0]!.json) as Doc; return { constraints: (doc.tables as Storage[])[0]!.constraints.map((d) => d.declaration), rows: sql.exec<{ h: string }>("SELECT count(*) || ':' || sum(length(data)) || ':' || max(version) AS h FROM rows WHERE table_id = 'message'").toArray()[0]!.h, links: sql.exec<{ n: number }>("SELECT count(*) AS n FROM links WHERE table_id = 'message'").toArray()[0]!.n, kit: sql.exec<{ value: string }>("SELECT value FROM meta WHERE key = 'kit_version'").toArray()[0]!.value, }; }); expect(after.constraints).not.toContain(CHECK); expect(after.constraints.some((d) => d.includes("CHECK (json_type(media) = 'array')"))).toBe(true); expect(after.rows).toBe(rowsBefore); expect(after.links).toBe(N); expect(after.kit).not.toBe("a kit before 145"); }, 600_000); });