Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/unit/next-canvas.test.ts9 * Module : tests10 * Repository : https://git.spboucher.ai/note-spboucher-ai11 * Project Website : https://note.spboucher.ai12 * Created : 2026-11-1913 * Last Modified : 2026-11-1914 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Canvas (SPEC §3.4): a page of type `canvas` + one row, saved with baseVersion (409 with the server copy), validated, in the change45 * feed and in the journal (`canvas.edited`, coalesced). Runs against a *_dev database.46 */47import { test, before, after } from "node:test";48import assert from "node:assert/strict";49import { randomUUID } from "node:crypto";50import { and, eq } from "drizzle-orm";51import { db, schema } from "../../src/db/index";52import { workspace, mk, doc, cleanup, code, kinds, eventsOf } from "../helpers/next";53import { getCanvas, putCanvas } from "../../src/lib/canvas/service";54import { canvasProblem, canvasRefs, EMPTY_CANVAS, CANVAS_LIMITS, type CanvasData } from "../../src/lib/canvas/format";55import { backlinks, softDeletePage, updatePage, getPage } from "../../src/lib/pages/service";5657let ws: string; const PFX = "nx-canvas ·";58before(async () => { ws = await workspace(); }); after(() => cleanup(ws, PFX));59const node = (id: string, extra: Record<string, unknown> = {}) => ({ id, kind: "text", x: 0, y: 0, w: 200, h: 80, z: 0, text: id, ...extra });60const canvas = (nodes: any[], edges: any[] = []): CanvasData => ({ ...EMPTY_CANVAS(), nodes, edges });6162test("creating a page of type canvas creates its canvas: empty document, empty spn-canvas-1, version 1", async () => {63 const p = await mk(ws, `${PFX} new`, undefined, { type: "canvas" });64 assert.equal(p.type, "canvas"); assert.deepEqual(((await getPage(ws, p.id)).content as any).content.map((n: any) => n.type), ["paragraph"], "the page keeps an (empty) ProseMirror document");65 const c = await getCanvas(ws, p.id); assert.deepEqual(c.data, { format: "spn-canvas-1", nodes: [], edges: [], viewport: { x: 0, y: 0, zoom: 1 } }); assert.equal(c.version, 1); assert.ok(c.rev > 0); assert.equal(c.title, `${PFX} new`);66 const [row] = await db.select().from(schema.canvases).where(eq(schema.canvases.pageId, p.id)); assert.equal(row.workspaceId, ws);67 const plain = await mk(ws, `${PFX} not a canvas`, doc("x")); assert.equal(await code(getCanvas(ws, plain.id)), "not_canvas"); assert.equal(await code(getCanvas(ws, randomUUID())), "not_found");68 // a page whose type was set to canvas afterwards gets its row on first read69 await updatePage(ws, plain.id, { type: "canvas" }); assert.equal((await getCanvas(ws, plain.id)).version, 1);70});71test("save with baseVersion: one writer wins, the other gets 409 with the server copy; nothing is overwritten", async () => {72 const p = await mk(ws, `${PFX} cas`, undefined, { type: "canvas" });73 const a = await putCanvas(ws, p.id, canvas([node("n1")]), 1); assert.deepEqual([a.version, a.unchanged], [2, false]); assert.ok(a.rev > 0);74 let err: any; try { await putCanvas(ws, p.id, canvas([node("other")]), 1); } catch (e) { err = e; }75 assert.equal(err.status, 409); assert.equal(err.code, "conflict"); assert.equal(err.extra.server.version, 2); assert.deepEqual(err.extra.server.data.nodes.map((n: any) => n.id), ["n1"]); assert.ok(err.extra.server.updatedAt);76 assert.deepEqual((await getCanvas(ws, p.id)).data.nodes.map((n) => n.id), ["n1"]);77 const same = await putCanvas(ws, p.id, canvas([node("n1")]), 2); assert.deepEqual([same.version, same.unchanged], [2, true], "saving what is stored is a no-op");78 const r = await Promise.all([1, 2, 3, 4].map((i) => putCanvas(ws, p.id, canvas([node("n1"), node(`race${i}`)]), 2).then(() => 200, (e) => e.status)));79 assert.deepEqual([...r].sort(), [200, 409, 409, 409], "four simultaneous saves on the same base: exactly one wins"); assert.equal((await getCanvas(ws, p.id)).version, 3);80 await softDeletePage(ws, p.id); assert.equal(await code(putCanvas(ws, p.id, canvas([]), 3)), "not_found", "a trashed canvas is read-only"); assert.equal((await getCanvas(ws, p.id)).version, 3, "…but still readable");81});82test("the canvas document is validated: format, ids, geometry, edges, references, size", () => {83 assert.equal(canvasProblem(EMPTY_CANVAS()), null);84 const full = canvas([node("a", { kind: "page", pageId: randomUUID() }), node("b", { kind: "shape", shape: "diamond", color: "#3157d5" }), node("c", { kind: "hologram", custom: { x: 1 } })], [{ id: "e1", from: "a", to: "b", label: "mène à", kind: "arrow" }]);85 assert.equal(canvasProblem(full), null, "unknown node kinds and fields are kept");86 const bad = (d: any) => canvasProblem(d);87 assert.match(bad({ ...EMPTY_CANVAS(), format: "v0" })!, /format/); assert.match(bad({ format: "spn-canvas-1", nodes: [], edges: [] })!, /viewport/); assert.match(bad(canvas([node("a"), node("a")]))!, /duplicate node id/);88 assert.match(bad(canvas([{ id: "a", kind: "text", x: 0, y: 0, w: -1, h: 1 }]))!, /finite numbers/); assert.match(bad(canvas([{ id: "a", kind: "text", x: NaN, y: 0, w: 1, h: 1 }]))!, /finite numbers/);89 assert.match(bad(canvas([node("a", { pageId: "nope" })]))!, /pageId must be a uuid/); assert.match(bad(canvas([node("a", { url: "javascript:alert(1)" })]))!, /unsafe/);90 assert.match(bad(canvas([node("a")], [{ id: "e", from: "a", to: "ghost" }]))!, /from and to/); assert.match(bad(canvas([node("a"), node("b")], [{ id: "e", from: "a", to: "b" }, { id: "e", from: "b", to: "a" }]))!, /duplicate edge id/);91 assert.match(bad(canvas(Array.from({ length: CANVAS_LIMITS.nodes + 1 }, (_, i) => node(`n${i}`))))!, /at most 5000 nodes/);92 const p1 = randomUUID(), r1 = randomUUID(), a1 = randomUUID(); assert.deepEqual(canvasRefs(canvas([node("a", { pageId: p1 }), node("b", { pageId: p1 }), node("c", { refId: r1 }), node("d", { attachmentId: a1 })])), { pages: [p1], references: [r1], attachments: [a1] });93});94test("an invalid canvas is refused (400 / 413) and nothing is written", async () => {95 const p = await mk(ws, `${PFX} invalid`, undefined, { type: "canvas" });96 let e: any; try { await putCanvas(ws, p.id, { format: "nope" }, 1); } catch (x) { e = x; } assert.deepEqual([e.status, e.code], [400, "bad_canvas"]);97 try { await putCanvas(ws, p.id, canvas(Array.from({ length: CANVAS_LIMITS.nodes + 1 }, (_, i) => node(`n${i}`))), 1); } catch (x) { e = x; } assert.deepEqual([e.status, e.code], [413, "bad_canvas"]);98 assert.equal((await getCanvas(ws, p.id)).version, 1);99});100test("edits are journaled (coalesced), page cards become backlinks, and the canvas page moves in the feed", async () => {101 const target = await mk(ws, `${PFX} card target`, doc("t")); const p = await mk(ws, `${PFX} journal`, undefined, { type: "canvas" });102 const rev0 = (await getPage(ws, p.id)).rev;103 await putCanvas(ws, p.id, canvas([node("a", { kind: "page", pageId: target.id })]), 1); await putCanvas(ws, p.id, canvas([node("a", { kind: "page", pageId: target.id }), node("b")]), 2);104 const ev = (await eventsOf(p.id)).filter((e) => e.kind === "canvas.edited"); assert.equal(ev.length, 1, "two saves within 10 minutes are one canvas.edited"); assert.deepEqual([ev[0].entityKind, ev[0].data.nodes], ["canvas", 1]);105 assert.deepEqual((await backlinks(ws, target.id)).map((b) => b.id), [p.id], "the canvas is a backlink of the page it shows"); assert.ok((await getPage(ws, p.id)).rev > rev0);106 await putCanvas(ws, p.id, canvas([node("b")]), 3); assert.equal((await backlinks(ws, target.id)).length, 0, "removing the card removes the backlink");107 assert.deepEqual(await kinds(target.id), ["page.created"]);108 assert.equal((await db.select().from(schema.links).where(and(eq(schema.links.fromPageId, p.id), eq(schema.links.kind, "canvas")))).length, 0);109});110