import { env, runInDurableObject } from "cloudflare:test"; import { expect, it } from "vitest"; import { Definitions, parseTable, type TableDef } from "../src/definitions.ts"; import { Engine, type Outcome } from "../src/engine.ts"; import { Store } from "../src/store.ts"; it("takes a write that leaves a link as the row holds it, though the linked row has gone; a new link is still checked", async () => { const stub = env.BUSINESS.get(env.BUSINESS.idFromName("synthetic-held-link")); await runInDurableObject(stub, (_instance, state) => { const store = new Store(state.storage.sql, () => new Date()); const shelf = { id: "garden_shelf", visibility: "owner", columns: [{ key: "label", type: "text" }] }; const page = { id: "garden_page", visibility: "owner", columns: [{ key: "mood", type: "text" }, { key: "shelf", type: "link", link_to: "garden_shelf" }] }; const defs = new Definitions([shelf, page].map(parseTable).filter((t): t is TableDef => t !== null)); store.setLinkColumns(defs.linkColumns()); const engine = new Engine(defs, [], store, { now: "2026-11-16T10:00:00Z", today: "2026-11-16" }); const outcome: Outcome = { changed: [], notices: [] }; const top = engine.create("garden_shelf", { label: "Top" }, 1, outcome); const made = engine.create("garden_page", { mood: "calm", shelf: top.id }, 2, outcome); state.storage.sql.exec("DELETE FROM rows WHERE table_id = 'garden_shelf' AND id = ?", top.id); // The row's own link, sent back as held (a sealing commit sends every clear column), is no new claim. const next = engine.update(made, { mood: "bright", shelf: top.id }, 3, outcome); expect(next.data).toMatchObject({ mood: "bright", shelf: top.id }); expect(() => engine.update(next, { shelf: "synthetic-no-such-shelf" }, 4, outcome)).toThrow(); }); });