import { describe, expect, it } from "vitest"; import { cardsOf, faceProblem, KNOWN_FACES } from "../src/faces.ts"; import { KITS, PERSONAL } from "../src/kits.ts"; import { served } from "../src/retired.ts"; import type { DefinitionsAnswer } from "../src/protocol.ts"; import { answer, asStored, http, personalObject, refusal } from "./personal.ts"; /** Synthetic definitions in the shapes the phone reads. */ const HOME = { format_version: 1, id: "home", parts: [{ kind: "head" }, { kind: "tools", face: "fan" }] }; const DUSK = { id: "dusk", name: "Dusk", colors: { ink: "#1f2933", paper: "#f5f1ea" } }; const GARDEN = { id: "garden", name: "Garden", tables: ["note"], instructions: "", automations: [] }; /** A personal object holds the whole kit before anything is put (src/kit_keep.ts): the shell's Home among its boards. */ const SHELL_HOME = PERSONAL.boards.find((b) => b.id === "home")!; const homeOf = (defs: DefinitionsAnswer): unknown => asStored(defs.boards.find((b) => (b as { id?: unknown }).id === "home")); /** RULE (owner, 26 Oct 2026): boards and themes are data an assistant puts over Cotton MCP, merged by id, and a phone sees them on its next definitions fetch. */ describe("update_definition puts a tool, a board or a theme whole", () => { const slug = "definition-kinds"; let owner = ""; let credential = ""; async function definitions(): Promise { const res = await http(slug, "GET", "/v1/definitions", undefined, { authorization: `Bearer ${credential}` }); expect(res.status).toBe(200); return res.body; } async function version(): Promise { return (await http<{ version: number }>(slug, "GET", "/v1/sync?since=0", undefined, { authorization: `Bearer ${owner}` })).body.version; } it("puts a board that reaches /v1/definitions, moving the version and the etag", async () => { ({ owner, credential } = await personalObject(slug)); const before = await definitions(); expect(before.boards.map((b) => String((b as { id?: unknown }).id)).sort()).toEqual(PERSONAL.boards.map((b) => String(b.id)).sort()); expect(before.themes).toEqual([]); // A token's first call stamps its use (one write a minute at most); the version is read after it. await answer(slug, owner, "list_tools"); const v = await version(); expect(await answer(slug, owner, "update_definition", { kind: "board", definition: HOME })).toEqual({ kind: "board", id: "home", changed: true, before: SHELL_HOME, definition: HOME }); const after = await definitions(); expect(homeOf(after)).toEqual(HOME); expect(after.boards.length, "the rest of the boards stay").toBe(PERSONAL.boards.length); expect(after.etag).not.toBe(before.etag); expect(await version()).toBe(v + 1); // The same board again writes nothing. expect(await answer(slug, owner, "update_definition", { kind: "board", definition: HOME })).toMatchObject({ changed: false, before: HOME }); expect(await version()).toBe(v + 1); const moved = { ...HOME, parts: [{ kind: "tools", face: "grid" }] }; expect(await answer(slug, owner, "update_definition", { kind: "board", definition: moved })).toMatchObject({ changed: true, before: HOME }); expect(homeOf(await definitions())).toEqual(moved); }); it("puts a theme and a tool the same way, leaving the rest of their kind", async () => { await answer(slug, owner, "update_definition", { kind: "theme", definition: DUSK }); await answer(slug, owner, "update_definition", { kind: "tool", definition: GARDEN }); const defs = await definitions(); expect(defs.themes).toEqual([DUSK]); expect(defs.tools.map((t) => t.id).sort()).toEqual([...served(PERSONAL).tools.map((t) => String(t.id)), "garden", "personal"].sort()); }); it("removes a tool, board or theme the object holds by id, and refuses one the kit still ships", async () => { await answer(slug, owner, "update_definition", { kind: "theme", definition: DUSK }); await answer(slug, owner, "update_definition", { kind: "tool", definition: GARDEN }); expect(await answer(slug, owner, "update_definition", { kind: "tool", remove: "garden" })).toEqual({ kind: "tool", id: "garden", removed: true, before: GARDEN }); expect(await answer(slug, owner, "update_definition", { kind: "theme", remove: "dusk" })).toMatchObject({ removed: true, before: DUSK }); const defs = await definitions(); expect(defs.themes).toEqual([]); expect(defs.tools.map((t) => t.id)).not.toContain("garden"); expect(await refusal(slug, owner, "update_definition", { kind: "tool", remove: "garden" })).toBe('no tool "garden" is held'); const shipped = String(PERSONAL.tools[0]!.id); expect(await refusal(slug, owner, "update_definition", { kind: "tool", remove: shipped })).toBe(`the tool "${shipped}" is one the kit ships: it is the kit's to say, so it leaves the kit before it leaves an object`); expect(await refusal(slug, owner, "update_definition", { kind: "tool", remove: "garden", definition: GARDEN })).toBe('a tool is removed by id: send kind and remove, not "definition"'); }); it("refuses a definition that does not read, and a table look still works as it did", async () => { expect(await refusal(slug, owner, "update_definition", { kind: "board", definition: { parts: [] } })).toBe("the definition is refused: every board needs an id"); expect(await refusal(slug, owner, "update_definition", { kind: "tool", definition: { id: "broken", name: "Broken", automations: [{ when: "x" }] } })).toBe("the definition is refused: the tool broken's automations are each a when and a then"); expect(await refusal(slug, owner, "update_definition", { kind: "theme", definition: DUSK, table: "note" })).toBe('a theme is put whole: send kind and definition, not "table"'); expect(await refusal(slug, owner, "update_definition", { kind: "palette", definition: DUSK })).toBe('kind is table, tool, board or theme; "palette" is not one'); expect(await answer(slug, owner, "update_definition", { kind: "table", table: "book", instructions: "Books, synthetic." })).toMatchObject({ table: "book", changed: ["instructions"] }); }); it.each(["book", "todo"])("refuses every card write on %s because the card is the build's", async (table) => { const before = await definitions(); const v = await version(); for (const card of [{ face: "book.hardback" }, { face: "hologram" }, null]) { expect(await refusal(slug, owner, "update_definition", { table, card, instructions: "Must not be written" })).toBe(`table "${table}"'s card is the build's; the kit names it, so it is not written here`); } expect(await definitions()).toEqual(before); expect(await version()).toBe(v); }); it.each(["garden", "birthdays"])("refuses every view card write on tool %s without changing definitions", async (id) => { const before = await definitions(); const v = await version(); for (const card of [{ face: "note.page" }, { face: "hologram" }, null]) { const definition = { ...GARDEN, id, views: [{ id: "plain", read: { table: "note" } }, { id: "all", card }] }; expect(await refusal(slug, owner, "update_definition", { kind: "tool", definition })).toBe(`tool "${id}"'s view "all"'s card is the build's; the kit names it, so it is not written here`); } expect(await definitions()).toEqual(before); expect(await version()).toBe(v); }); it("accepts tool views without cards and still writes table look beside its card", async () => { const gallery = { ...GARDEN, id: "gallery", views: [{ id: "all", label: "All", read: { table: "note" } }] }; expect(await answer(slug, owner, "update_definition", { kind: "tool", definition: gallery })).toMatchObject({ id: "gallery", changed: true }); expect(await answer(slug, owner, "update_definition", { table: "book", instructions: "Books, again." })).toMatchObject({ changed: ["instructions"] }); }); }); /** RULE (Loom step 5): every kit the Worker bundles names only faces the app draws, so no kit an object is made or kept from carries a card the phone cannot draw. */ describe("the bundled kits name only faces the app draws", () => { it("holds every card of every kit to the known faces", () => { for (const kit of Object.values(KITS)) { for (const doc of [...kit.own, ...kit.tools, ...kit.tables, ...kit.boards]) expect(faceProblem(doc), `${kit.id}/${String(doc.id)}`).toBeNull(); } expect(cardsOf(KITS["personal"]!.tables).length, "the personal kit's tables carry cards").toBeGreaterThan(0); expect(faceProblem({ id: "t", card: { face: "hologram" } })).toContain('"hologram"'); expect(cardsOf(KITS["personal"]!.tables).map((card) => card.face)).toEqual(expect.arrayContaining(["transaction.receipt", "transaction.cheque", "account.statement"])); const money = KITS["personal"]!.tools.find((tool) => tool.id === "money")!; for (const view of money.views as { read: { table: string }; card?: unknown }[]) { if (["transaction", "money_account"].includes(view.read.table)) expect(view.card, "money inherits its table card").toBeUndefined(); } expect(KNOWN_FACES.has("todo.card")).toBe(true); for (const gone of ["todo", "person", "note", "event.board", "event.invitation"]) expect(KNOWN_FACES.has(gone), gone).toBe(false); }); });