import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { PERSONAL } from "../src/kits.ts"; import { ADMIN, answer, http, mint, personalObject, refusal } from "./personal.ts"; import type { DefinitionsAnswer } from "../src/protocol.ts"; const TOOL = { id: "synthetic-garden", name: "Garden", tables: ["note"], instructions: "", automations: [] }; const BOARD = { id: "synthetic-board", parts: [] }; const THEME = { id: "synthetic-theme", name: "Dusk", colors: { ink: "#123456" } }; describe("the owner's definition act", () => { const slug = "definition-act"; let credential = "", owner = ""; const act = (args: Record) => http>(slug, "POST", "/v1/act", { act: "definition", args }, { authorization: `Bearer ${credential}` }); const definitions = async () => (await http(slug, "GET", "/v1/definitions", undefined, { authorization: `Bearer ${credential}` })).body; it("puts tools, boards and themes whole and moves the etag", async () => { ({ credential, owner } = await personalObject(slug)); for (const [kind, definition] of [["tool", TOOL], ["board", BOARD], ["theme", THEME]] as const) { const before = await definitions(); expect(await act({ kind, definition })).toMatchObject({ status: 200, body: { kind, definition } }); const after = await definitions(); expect(after.etag).not.toBe(before.etag); expect(after[`${kind}s`]).toContainEqual(definition); } }); it("takes the phone's whole table but writes only look", async () => { const book = (await definitions()).tables.find((t) => t.id === "book")!; const definition = { ...book, name: "Synthetic books" }; expect(await act({ kind: "table", definition })).toMatchObject({ status: 200, body: { definition } }); const refused = await act({ kind: "table", definition: { ...definition, engine: "other" } }); expect(refused.status).toBe(400); expect(refused.body.message).toBe(await refusal(slug, owner, "update_definition", { table: "book", engine: "other" })); }); it("takes a kit tool sent back whole with the build's view cards it was served, and refuses a changed one", async () => { const birthdays = (await definitions()).tools.find((t) => t.id === "birthdays")!; const views = birthdays.views as Record[]; const carded = views.findIndex((v) => "card" in v); expect(carded, "the kit's birthdays tool names a view card").toBeGreaterThanOrEqual(0); const renamed = { ...birthdays, name: "Synthetic birthdays" }; expect(await act({ kind: "tool", definition: renamed, base: birthdays })).toMatchObject({ status: 200, body: { definition: renamed } }); expect((await definitions()).tools.find((t) => t.id === "birthdays")).toEqual(renamed); const id = String(views[carded]!.id); const recarded = { ...renamed, views: views.map((v, i) => (i === carded ? { ...v, card: { face: "note.page" } } : v)) }; expect((await act({ kind: "tool", definition: recarded })).body.message).toBe(`tool "birthdays"'s view "${id}"'s card is the build's; the kit names it, so it is not written here`); const uncarded = { ...renamed, views: views.map((v, i) => (i === carded ? Object.fromEntries(Object.entries(v).filter(([k]) => k !== "card")) : v)) }; expect((await act({ kind: "tool", definition: uncarded })).body.message).toBe(`tool "birthdays"'s view "${id}"'s card is the build's; the kit names it, so it is not written here`); expect((await definitions()).tools.find((t) => t.id === "birthdays")).toEqual(renamed); }); it("keeps the act's changed as the records it wrote, which a definition write is none of", async () => { const book = (await definitions()).tables.find((t) => t.id === "book")!; for (const [kind, definition] of [["table", { ...book, name: "Synthetic shelf" }], ["theme", { ...THEME, name: "Synthetic dawn" }]] as const) { const res = await act({ kind, definition }); expect(res.status).toBe(200); expect(res.body.changed, kind).toEqual([]); } }); it("writes nothing for a whole the phone sends back unchanged with its keys in another order", async () => { const birthdays = (await definitions()).tools.find((t) => t.id === "birthdays")!; const sorted = (v: unknown): unknown => Array.isArray(v) ? v.map(sorted) : v && typeof v === "object" ? Object.fromEntries(Object.keys(v).sort().reverse().map((k) => [k, sorted((v as Record)[k])])) : v; const before = await definitions(); expect(await act({ kind: "tool", definition: sorted(birthdays), base: birthdays })).toMatchObject({ status: 200, body: { kind: "tool", id: "birthdays" } }); expect((await definitions()).etag).toBe(before.etag); }); it("removes a whole and refuses a kit tool with the MCP line", async () => { expect(await act({ kind: "tool", remove: TOOL.id })).toMatchObject({ status: 200, body: { removed: true } }); const args = { kind: "tool", remove: PERSONAL.tools[0]!.id }; expect((await act(args)).body.message).toBe(await refusal(slug, owner, "update_definition", args)); }); it("refuses taking out a shell board, the app's own screen, with the MCP line", async () => { for (const id of ["settings", "home", "tools", "database"]) { const args = { kind: "board", remove: id }; const message = (await act(args)).body.message; expect(message).toBe(`the board "${id}" is one of the app's shell screens: the kit lays it, so it never leaves an object`); expect(await refusal(slug, owner, "update_definition", args)).toBe(message); expect((await definitions()).boards.map((b) => b.id)).toContain(id); } }); it("refuses the build's card and an engine table with the MCP line", async () => { const book = (await definitions()).tables.find((t) => t.id === "book")!; expect((await act({ kind: "table", definition: { ...book, card: null } })).body.message).toBe(await refusal(slug, owner, "update_definition", { table: "book", card: null })); const table = { id: "synthetic-engine", engine: "synthetic", columns: [], name: "Engine" }; expect((await http(slug, "POST", "/v1/admin/definitions/tables", { tables: [table] }, ADMIN)).status).toBe(200); expect((await act({ kind: "table", definition: { ...table, name: "Other" } })).body.message).toBe(await refusal(slug, owner, "update_definition", { table: table.id, name: "Other" })); }); it("refuses non-owners", async () => { // ADMIN KEYS 1b: the owner's phone mints the writer (a key mints no key); every table open, so the refusal is the // owner rule's, not the list's. const writer = await mint(slug, credential, "synthetic writer", ["read", "write"]); expect(await refusal(slug, writer, "run_act", { act: "definition", args: { kind: "theme", definition: THEME } })).toBe("only the owner writes definitions"); }); it("takes a Home edit whose served baseline carries the computed empty-tools list", async () => { const base = (await definitions()).boards.find((b) => b.id === "home")!; const definition = { id: "home", parts: [{ kind: "head" }, { kind: "tools", size: "small" }] }; expect(await act({ kind: "board", definition, base })).toMatchObject({ status: 200, body: { definition } }); }); it("keeps a Home the phone sends back as served without the empty-tools list the object works out", async () => { const served = (await definitions()).boards.find((b) => b.id === "home")!; const tools = (served.parts as Record[]).find((p) => p.kind === "tools")!; expect(tools.empty, "Home is served naming the tools that hold no rows").toBeInstanceOf(Array); const definition = { ...served, picture: "object-synthetic" }; expect((await act({ kind: "board", definition, base: served })).status).toBe(200); const stored = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_instance, state) => JSON.parse(state.storage.sql.exec<{ json: string }>("SELECT json FROM definitions WHERE id = 'board:home'").one().json) as Record, ); expect(stored.picture).toBe("object-synthetic"); expect((stored.parts as Record[]).find((p) => p.kind === "tools"), "the list is derived, never kept").not.toHaveProperty("empty"); }); it("answers the object's whole version when the phone's baseline is stale", async () => { await act({ kind: "tool", definition: TOOL }); const object = { ...TOOL, name: "Object garden" }; await answer(slug, owner, "update_definition", { kind: "tool", definition: object }); expect(await act({ kind: "tool", definition: { ...TOOL, name: "Phone garden" }, base: TOOL })).toMatchObject({ status: 200, body: { conflict: true, definition: object } }); expect((await definitions()).tools.find((t) => t.id === TOOL.id)).toEqual(object); expect(await act({ kind: "tool", remove: TOOL.id, base: TOOL })).toMatchObject({ status: 200, body: { conflict: true, definition: object } }); }); });