import { describe, expect, it } from "vitest"; import { acceptElements, changeScene, commitDraw, createDraw, documentFingerprint, emptyState, MAX_ELEMENTS, restoreState, } from "../src/store.ts"; const rectangle = (id: string, extra: Record = {}) => ({ id, type: "rectangle", x: 1, y: 2, width: 30, height: 40, ...extra, }); const snapshot = (state: unknown) => ({ type: "custom", customType: "canvas", data: { kind: "snapshot", state }, }); describe("canvas state store", () => { it("validates bounded shapes, references, and aggregate element limits", () => { expect(() => acceptElements([rectangle("same"), rectangle("same")]), ).toThrow("Duplicate"); expect(() => acceptElements([ rectangle("arrow", { type: "arrow", startBinding: { elementId: "later" }, }), rectangle("later"), ]), ).not.toThrow(); expect(() => acceptElements([ rectangle("arrow", { type: "arrow", startBinding: { elementId: "missing" }, }), ]), ).toThrow("Dangling"); expect(() => acceptElements([rectangle("bad", { constructor: {} })]), ).toThrow("Unsafe"); expect(() => acceptElements( Array.from({ length: MAX_ELEMENTS + 1 }, (_, index) => rectangle(String(index)), ), ), ).toThrow("1000"); }); it("creates an immutable server-owned request without client preparation", () => { const input = { baseRevision: 2, title: "add", additions: [rectangle("client-id")], }; const draw = createDraw( { ...emptyState(), revision: 2, elements: [rectangle("one")] }, input, ); expect(draw.additions[0]?.id).toBe("client-id"); expect(Object.isFrozen(draw)).toBe(true); expect(Object.isFrozen(draw.additions)).toBe(true); expect(Object.isFrozen(draw.additions[0])).toBe(true); expect(draw).not.toHaveProperty("preparedElements"); expect(draw).not.toHaveProperty("clientId"); expect(() => { (draw.additions[0] as { x: number }).x = 99; }).toThrow(); }); it("commits additions plus generated labels, preserving requested fields", () => { const state = emptyState(); const draw = createDraw(state, { baseRevision: 0, title: "labeled", additions: [ rectangle("shape", { text: "Source", strokeColor: "#123456", label: { text: "Hello" }, }), ], }); const elements = [ { ...draw.additions[0]!, seed: 1, version: 1, boundElements: [{ id: "label", type: "text" }], }, { id: "label", type: "text", x: 2, y: 3, text: "Hello", originalText: "Hello", containerId: "shape", }, ]; const committed = commitDraw(state, draw, elements); expect(committed).toMatchObject({ revision: 1, elements, change: { origin: "agent" }, }); expect(committed.change?.id).toEqual(expect.any(String)); expect(() => commitDraw(state, draw, [{ ...elements[0]!, x: 99 }, elements[1]!]), ).toThrow("requested"); expect(() => commitDraw(state, draw, [ { ...elements[0]!, strokeColor: "#000000" }, elements[1]!, ]), ).toThrow("requested"); }); it("requires exact requested updates and deletions, preserving canonical state on failure", () => { const state = { ...emptyState(), revision: 2, elements: [rectangle("one"), rectangle("gone")], }; const draw = createDraw(state, { baseRevision: 2, title: "edit", additions: [rectangle("new")], updates: [{ id: "one", changes: { opacity: 50 } }], deleteIds: ["gone"], }); const final = [ rectangle("one", { opacity: 50 }), { ...rectangle("gone"), isDeleted: true }, draw.additions[0]!, ]; const before = structuredClone(state); expect(() => commitDraw(state, draw, [{ ...final[0]!, x: 90 }, final[1]!, final[2]!]), ).toThrow("requested edits"); expect(state).toEqual(before); expect(commitDraw(state, draw, final)).toMatchObject({ revision: 3, elements: final, change: { origin: "agent" }, }); }); it("rejects stale commits", () => { const state = { ...emptyState(), elements: [rectangle("one")] }; const draw = createDraw(state, { baseRevision: 0, title: "update", updates: [{ id: "one", changes: { opacity: 40 } }], }); expect(() => commitDraw({ ...state, revision: 1 }, draw, [ rectangle("one", { opacity: 40 }), ]), ).toThrow("Stale"); }); it("remaps Mermaid output IDs and rejects non-generated output", () => { const state = emptyState(); const draw = createDraw(state, { baseRevision: 0, title: "diagram", mermaid: { definition: "flowchart TD", x: 0, y: 0 }, }); const elements = [ rectangle(`${draw.id}-mermaid-0`), rectangle(`${draw.id}-mermaid-1`, { frameId: `${draw.id}-mermaid-0` }), ]; expect(() => commitDraw(state, draw, [rectangle("wrong-mermaid-0")]), ).toThrow("Mermaid element ID"); expect(() => commitDraw(state, draw, [])).toThrow("produced no native"); expect(commitDraw(state, draw, elements).elements).toEqual(elements); }); it("requires complete connected and grouped move/delete sets, including group:f", () => { const connected = { ...emptyState(), elements: [ rectangle("box", { boundElements: [{ id: "arrow", type: "arrow" }] }), rectangle("arrow", { type: "arrow", startBinding: { elementId: "box" }, }), ], }; expect(() => createDraw(connected, { baseRevision: 0, title: "partial move", moves: [{ ids: ["box"], dx: 2, dy: 3 }], }), ).toThrow("complete reference"); expect(() => createDraw(connected, { baseRevision: 0, title: "partial delete", deleteIds: ["box"], }), ).toThrow("complete reference"); expect(() => createDraw(connected, { baseRevision: 0, title: "delete", deleteIds: ["box", "arrow"], }), ).not.toThrow(); const moved = createDraw(connected, { baseRevision: 0, title: "move", moves: [{ ids: ["box", "arrow"], dx: 2, dy: 3 }], }); const final = [ rectangle("box", { x: 3, y: 5, boundElements: [{ id: "arrow", type: "arrow" }], }), rectangle("arrow", { type: "arrow", x: 3, y: 5, startBinding: { elementId: "box" }, }), ]; expect(commitDraw(connected, moved, final).elements).toEqual(final); const grouped = { ...emptyState(), elements: [ rectangle("group:f", { type: "frame" }), rectangle("inside", { frameId: "group:f" }), ], }; expect(() => createDraw(grouped, { baseRevision: 0, title: "partial frame delete", deleteIds: ["inside"], }), ).toThrow("complete reference"); }); it("enforces coordinate and aggregate operation bounds", () => { const elements = Array.from({ length: 101 }, (_, index) => rectangle(`shape-${index}`), ); expect(() => createDraw( { ...emptyState(), elements }, { baseRevision: 0, title: "too many", updates: Array.from({ length: 97 }, (_, index) => ({ id: `shape-${index}`, changes: { opacity: 50 }, })), moves: [{ ids: ["shape-97"], dx: 1, dy: 1 }], relabels: [{ id: "shape-98", text: "new" }], deleteIds: ["shape-99"], additions: [rectangle("new")], }, ), ).toThrow("100 operations"); expect(() => createDraw( { ...emptyState(), elements: [rectangle("edge", { x: 1e7 })] }, { baseRevision: 0, title: "out of bounds", moves: [{ ids: ["edge"], dx: 1, dy: 0 }], }, ), ).toThrow("geometry"); }); it("relabels standalone auto-resizing text only", () => { const text = { id: "text", type: "text", x: 1, y: 2, width: 10, height: 20, text: "old", originalText: "old", baseline: 16, }; expect(() => createDraw( { ...emptyState(), elements: [{ ...text, autoResize: false }] }, { baseRevision: 0, title: "fixed", relabels: [{ id: "text", text: "new" }], }, ), ).toThrow("Fixed-width"); const state = { ...emptyState(), elements: [text] }; const draw = createDraw(state, { baseRevision: 0, title: "relabel", relabels: [{ id: "text", text: "new" }], }); const measured = [ { ...text, text: "new", originalText: "new", width: 30, baseline: 17 }, ]; expect(commitDraw(state, draw, measured).elements).toEqual(measured); expect(() => createDraw( { ...emptyState(), elements: [{ ...text, containerId: "box" }, rectangle("box")], }, { baseRevision: 0, title: "bound", relabels: [{ id: "text", text: "no" }], }, ), ).toThrow("standalone"); }); it("marks changed scenes human-owned and validates base revision", () => { const state = { ...emptyState(), revision: 1, elements: [rectangle("one", { version: 1, updated: 1 })], }; expect( changeScene(state, 1, [rectangle("one", { version: 2, updated: 2 })]), ).toBe(state); const changed = changeScene(state, 1, [rectangle("two")]); expect(changed).toMatchObject({ revision: 2, change: { origin: "human" } }); expect(() => changeScene(state, 0, [])).toThrow("revision"); expect(documentFingerprint([rectangle("one"), rectangle("two")])).not.toBe( documentFingerprint([rectangle("two"), rectangle("one")]), ); }); it("normalizes restored historic snapshots", () => { const restored = restoreState([ snapshot({ revision: 7, elements: [rectangle("one")], annotations: [{ malformed: true }], pendingDraw: { unsafe: true }, messages: [{ text: "ignored" }], }), ]); expect(restored).toEqual({ ...emptyState(), revision: 7, elements: [rectangle("one")], }); expect(restored).not.toHaveProperty("pendingDraw"); }); });