import { describe, expect, it } from "vitest"; import { PERSONAL as SHIPPED } from "../src/kits.ts"; import { served } from "../src/retired.ts"; import type { DefinitionsAnswer } from "../src/protocol.ts"; import { ADMIN, asStored, http, PERSONAL_TABLES, personalObject } from "./personal.ts"; /** Two tools and a board as the owner's Mac kept them: synthetic, in the definition shape the phone reads. */ const LOGBOOK = { id: "logbook", name: "Logbook", purpose: "Days written down.", picture: { photo: "photo:aa" }, card_stock: "sage", tables: ["note"], instructions: "", automations: [], author: { device: "cotton", name: "Cotton" }, signature: null, views: [] }; const GARDEN = { id: "garden", name: "Garden", tables: [], instructions: "", automations: [], author: { device: "cotton", name: "Cotton" }, signature: null }; const HOME = { format_version: 1, id: "home", parts: [{ kind: "tools", face: "fan" }] }; /** The personal kit as an object is made from: without the tables and tools held as retired (TABLE DROP 1). */ const PERSONAL = served(SHIPPED); /** What a personal object holds before anything is merged into it: the whole kit (src/kit_keep.ts). */ const KIT_IDS = [...PERSONAL.tools.map((t) => String(t.id)), "personal"].sort(); const ids = (docs: Record[]): string[] => docs.map((d) => String(d.id)).sort(); /** The kit's boards with the owner's Home in place of the shell's. */ const BOARDS = PERSONAL.boards.map((b) => (b.id === "home" ? HOME : b)).sort((a, b) => String(a.id).localeCompare(String(b.id))); async function definitions(slug: string, credential: string): Promise { const res = await http(slug, "GET", "/v1/definitions", undefined, { authorization: `Bearer ${credential}` }); expect(res.status, JSON.stringify(res.body)).toBe(200); return res.body; } /** RULE (owner, 26 Oct 2026): the phone's fan draws the owner's own tools, so the object holds them beside the package's, merged by id. */ describe("an admin merges tools and boards into the definitions", () => { const SLUG = "definitions-merge"; let credential = ""; it("adds the tools and boards it is given, keeping the package's tool and every table", async () => { ({ credential } = await personalObject(SLUG)); const before = await definitions(SLUG, credential); expect(before.tables.find((t) => t.id === "film")?.card).toMatchObject({ face: "film.poster", offers: ["film.poster"] }); expect(before.tables.find((t) => t.id === "book")?.card).toMatchObject({ face: "book.hardback" }); expect(ids(before.tools)).toEqual(KIT_IDS); expect(ids(before.boards)).toEqual(ids(PERSONAL.boards)); const tools = await http<{ changed: number; tools: number; version: number }>(SLUG, "POST", "/v1/admin/definitions/tools", { tools: [LOGBOOK, GARDEN] }, ADMIN); expect(tools.status, JSON.stringify(tools.body)).toBe(200); expect(tools.body).toMatchObject({ changed: 2, tools: KIT_IDS.length + 2 }); const boards = await http<{ changed: number; boards: number }>(SLUG, "POST", "/v1/admin/definitions/boards", { boards: [HOME] }, ADMIN); expect(boards.status, JSON.stringify(boards.body)).toBe(200); expect(boards.body).toMatchObject({ changed: 1, boards: PERSONAL.boards.length }); const after = await definitions(SLUG, credential); expect(ids(after.tools)).toEqual([...KIT_IDS, "garden", "logbook"].sort()); expect(after.tools.find((t) => t.id === "logbook")).toEqual(LOGBOOK); expect(after.boards.map(asStored), "Home as stored, the tools that hold no rows named on its tools part").toEqual(BOARDS); expect(after.tables.length).toBe(before.tables.length); expect(after.etag).not.toBe(before.etag); }); it("serves filtered empty lines and their whole-table count declaration from the kit", async () => { const answer = await definitions(SLUG, credential); let checked = 0; for (const tool of answer.tools) { for (const view of (tool.views ?? []) as { id: string; read: Record }[]) { if (!view.read.where && !view.read.section) continue; expect(view.read.empty, `${String(tool.id)}/${view.id}`).toMatchObject({ line: expect.stringMatching(/^empty_views\./), table: true }); checked++; } } expect(checked).toBeGreaterThan(30); }); it("changes nothing and moves no version when given what it holds", async () => { const held = await definitions(SLUG, credential); const tools = await http<{ changed: number; version: number }>(SLUG, "POST", "/v1/admin/definitions/tools", { tools: [LOGBOOK, GARDEN] }, ADMIN); const boards = await http<{ changed: number; version: number }>(SLUG, "POST", "/v1/admin/definitions/boards", { boards: [HOME] }, ADMIN); expect([tools.body.changed, boards.body.changed]).toEqual([0, 0]); expect(boards.body.version).toBe(tools.body.version); expect((await definitions(SLUG, credential)).etag).toBe(held.etag); }); it("replaces a tool by its id and leaves the others", async () => { const renamed = { ...LOGBOOK, name: "Diary" }; const res = await http<{ changed: number; tools: number }>(SLUG, "POST", "/v1/admin/definitions/tools", { tools: [renamed] }, ADMIN); expect(res.body).toMatchObject({ changed: 1, tools: KIT_IDS.length + 2 }); const tools = (await definitions(SLUG, credential)).tools; expect(tools.find((t) => t.id === "logbook")!.name).toBe("Diary"); expect(tools.find((t) => t.id === "garden")).toEqual(GARDEN); }); it("keeps merged tools when the tables are merged, and a merge of tables keeps the tools", async () => { const res = await http(SLUG, "POST", "/v1/admin/definitions/tables", { tables: [PERSONAL_TABLES[0]] }, ADMIN); expect(res.status).toBe(200); expect(ids((await definitions(SLUG, credential)).tools)).toEqual([...KIT_IDS, "garden", "logbook"].sort()); }); it("carries the boards in the export", async () => { const res = await http<{ boards: unknown[] }>(SLUG, "GET", "/v1/admin/export?limit=1", undefined, ADMIN); expect(res.body.boards).toEqual(BOARDS); }); it("refuses a tool with no id or name, automations the engine cannot read, and anyone but the admin", async () => { for (const bad of [{ name: "No id" }, { id: "nameless" }, { ...GARDEN, automations: [{ id: "a" }] }, { ...GARDEN, automations: "none" }]) { const res = await http<{ code: string }>(SLUG, "POST", "/v1/admin/definitions/tools", { tools: [bad] }, ADMIN); expect(res.status, JSON.stringify(bad)).toBe(400); expect(res.body.code).toBe("bad_definition"); } expect((await http(SLUG, "POST", "/v1/admin/definitions/boards", { boards: [{ parts: [] }] }, ADMIN)).status).toBe(400); expect((await http(SLUG, "POST", "/v1/admin/definitions/tools", { tool: [GARDEN] }, ADMIN)).status).toBe(400); expect((await http(SLUG, "POST", "/v1/admin/definitions/tools", { tools: [GARDEN] }, { authorization: `Bearer ${credential}` })).status).not.toBe(200); expect(ids((await definitions(SLUG, credential)).tools)).toEqual([...KIT_IDS, "garden", "logbook"].sort()); }); });