SPB Git

Repository Public

next-canvas.spec.ts 40.5 KB 340 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/e2e/next-canvas.spec.ts9 * Description      : Infinite Canvas, desktop Chromium, scratch database only —10 *                    every card kind created through the interface and11 *                    identical after a reload; two browser contexts editing12 *                    concurrently (three-way merge on 409, conflict review);13 *                    offline draft and reconnect; selection, alignment,14 *                    clipboard, stacking, context menu, minimap, drops,15 *                    exports, entry points; unknown content preserved; viewport16 *                    without version noise; a performance probe with 1 00017 *                    cards (frame times are printed and written to the data18 *                    directory).19 * Module           : tests20 * Repository       : https://git.spboucher.ai/note-spboucher-ai21 * Project Website  : https://note.spboucher.ai22 * Created          : 2026-11-1923 * Last Modified    : 2026-11-1924 *25 * Author           : Simon-Pierre Boucher26 * Position         : Professor of Finance27 * Institution      : Université du Québec en Outaouais (UQO)28 * Organization     : SP Boucher29 * Website          : https://spboucher.ai30 * Git              : https://git.spboucher.ai31 *32 * Preferred Contact: contact@spboucher.ai33 * Development      : dev@spboucher.ai34 * Support          : support@spboucher.ai35 * Security/Admin   : admin@spboucher.ai36 *37 * Copyright (c) 202638 * Simon-Pierre Boucher39 * All rights reserved where applicable. This file is governed by the40 * applicable project license, copyright notices and relevant law.41 * Existing third-party rights and notices remain unaffected.42 *43 * License          : None specified (no license file in this repository)44 *45 * Do not embed credentials, private keys, access tokens,46 * certificates or confidential personal information.47 *48 * =============================================================================49 * END OF SP BOUCHER SOURCE HEADER50 * =============================================================================51 */5253import { test, expect, type Page, type BrowserContext } from "@playwright/test";54import fs from "node:fs";55import os from "node:os";56import path from "node:path";57import { apiCall, docOf, para, PDF, PNG, sql, ORIGIN, STATE } from "./helpers";5859const SHOTS = process.env.CANVAS_SHOTS ?? path.join(os.homedir(), "dev/note-next-c-data/shots");60const made: string[] = [];61const uid = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 6);62type Canvas = { format: string; nodes: any[]; edges: any[]; viewport: any; [k: string]: unknown };63const text = (id: string, x: number, y: number, t = id, extra: Record<string, unknown> = {}) => ({ id, kind: "text", x, y, w: 200, h: 100, z: 0, text: t, ...extra });64async function newCanvas(page: Page, title: string, data?: Partial<Canvas>) {65  const r = await apiCall(page, "POST", "/api/pages", { title: `E2E-canvas ${title} ${uid()}`, type: "canvas" }); expect(r.status).toBe(201); made.push(r.body.id);66  if (data) { const put = await apiCall(page, "PUT", `/api/canvases/${r.body.id}`, { data: { format: "spn-canvas-1", nodes: [], edges: [], viewport: { x: 0, y: 0, zoom: 1 }, ...data }, baseVersion: 1 }); expect(put.status, JSON.stringify(put.body)).toBe(200); }67  return r.body.id as string;68}69const server = async (page: Page, id: string) => (await apiCall(page, "GET", `/api/canvases/${id}`)).body as { data: Canvas; version: number };70const ready = (page: Page) => page.locator("[data-testid='canvas-page'][data-state='ready']");71const open = async (page: Page, id: string) => { await page.goto(`/p/${id}`); await ready(page).waitFor(); await page.locator("[data-testid='canvas-surface']").waitFor(); };72const saved = (page: Page) => expect(page.locator("[data-testid='canvas-status']")).toHaveAttribute("data-status", "saved", { timeout: 20_000 });73const surface = (page: Page) => page.locator("[data-testid='canvas-surface']");74const card = (page: Page, id: string) => page.locator(`.cv-node[data-id="${id}"]`);75const local = <T,>(page: Page, fn: string): Promise<T> => page.evaluate(`(() => { const c = window.__canvas; const s = c.store; return (${fn})(s, c); })()`) as Promise<T>;76const docOfCanvas = (page: Page) => local<Canvas>(page, "(s) => s.doc");77const selection = (page: Page) => local<string[]>(page, "(s) => s.get().selection");78const waitFor = async <T,>(fn: () => Promise<T | null | undefined | false>, ms = 15_000): Promise<T> => { const t0 = Date.now(); for (;;) { const v = await fn(); if (v) return v; if (Date.now() - t0 > ms) throw new Error("condition not met in time"); await new Promise((r) => setTimeout(r, 200)); } };79const waitVersion = (page: Page, id: string, above: number) => waitFor(async () => { const s = await server(page, id); return s.version > above ? s : null; });80/** Types into the card being edited in place, then leaves the field. */81const typeInCard = async (page: Page, value: string, replace = false) => { const f = page.locator(".cv-node[data-editing] textarea.cv-field"); await f.waitFor(); if (replace) await f.fill(value); else await page.keyboard.type(value); await page.keyboard.press("Escape"); await expect(page.locator(".cv-node[data-editing]")).toHaveCount(0); };82const editCard = async (page: Page, id: string, value: string) => { await card(page, id).dblclick(); await typeInCard(page, value, true); };83const center = async (page: Page, id: string) => { const b = (await card(page, id).boundingBox())!; return { x: b.x + b.width / 2, y: b.y + b.height / 2, b }; };84const shot = async (page: Page, name: string) => { fs.mkdirSync(SHOTS, { recursive: true }); await page.screenshot({ path: path.join(SHOTS, `${name}.png`) }); };85const setTheme = (page: Page, theme: "light" | "dark") => page.evaluate((t) => (window as any).__noteUI().setTheme(t), theme);86test.afterAll(async () => { if (made.length) await sql`delete from pages where id = any(${made}::uuid[])`; await sql`delete from pages where title like 'E2E-canvas %' or title = 'New canvas'`; await sql`delete from knowledge_edges where to_id in (select id from "references" where title like 'Canvas reference %')`; await sql`delete from "references" where title like 'Canvas reference %'`; });8788test("create a canvas from the sidebar, add every kind of card, connect two, reload: identical", async ({ page }) => {89  test.setTimeout(150_000);90  await page.goto("/");91  const target = await apiCall(page, "POST", "/api/pages", { title: `E2E-canvas target ${uid()}`, content: docOf(para("The excerpt shown on the page card.")) }); made.push(target.body.id);92  const refTitle = `Canvas reference ${uid()}`; const ref = await apiCall(page, "POST", "/api/research/references", { title: refTitle, authors: ["Ada Lovelace"], year: 1843 }); expect(ref.status).toBe(201);93  await page.getByRole("button", { name: "New canvas" }).first().click();94  await page.waitForURL(/\/p\/[0-9a-f-]{36}/); await ready(page).waitFor(); const id = page.url().split("/p/")[1].slice(0, 36); made.push(id);95  await expect(page.getByText("This canvas is empty")).toBeVisible(); await expect(page.locator("[data-testid='canvas-icon']").first()).toBeVisible();96  const tool = (name: string) => page.locator("[data-testid='canvas-toolbar']").getByRole("button", { name, exact: true });97  const count = (kind: string) => page.locator(`.cv-node[data-kind="${kind}"]`);98  // text (in-place Markdown)99  await tool("Add text").click(); await typeInCard(page, "Hello **canvas**"); await expect(count("text").locator("strong")).toHaveText("canvas");100  // shape101  await tool("Add shape").click(); await page.getByRole("menuitem", { name: "Ellipse" }).click(); await expect(count("shape")).toHaveAttribute("data-shape", "ellipse");102  // page card (search and pick)103  await tool("Add page card").click(); const form = page.locator("[data-testid='canvas-card-form']"); await form.getByLabel("Find a page").fill("E2E-canvas target"); await form.getByRole("option").filter({ hasText: target.body.title }).click(); await form.getByRole("button", { name: "Done" }).click();104  await expect(page.locator("[data-testid='canvas-page-card'] .cv-title")).toContainText(target.body.title); await expect(page.locator("[data-testid='canvas-page-card'] .cv-excerpt")).toContainText("The excerpt shown on the page card.");105  // link106  await tool("Add link").click(); await form.getByLabel("Link address").fill("https://example.org/paper"); await form.getByLabel("Link title").fill("A paper"); await form.getByRole("button", { name: "Done" }).click(); await expect(count("link").locator(".cv-title")).toHaveText("A paper");107  // image + pdf (uploaded as attachments of the canvas page)108  await page.setInputFiles("[data-testid='canvas-file-input']", [{ name: "pixel.png", mimeType: "image/png", buffer: PNG }, { name: "hello.pdf", mimeType: "application/pdf", buffer: PDF }]);109  await expect(count("image").locator("img")).toBeVisible({ timeout: 15_000 }); await expect(page.locator("[data-testid='canvas-pdf'] .cv-pdf-page img")).toBeVisible({ timeout: 30_000 }); await expect(page.locator("[data-testid='canvas-pdf']")).toContainText("1 p.");110  // reference111  await tool("Add reference").click(); await form.getByLabel("Find a reference").fill(refTitle); await form.getByRole("option").filter({ hasText: refTitle }).click(); await form.getByRole("button", { name: "Done" }).click(); await expect(page.locator("[data-testid='canvas-ref-card'] .cv-title")).toHaveText(refTitle);112  // formula (KaTeX)113  await tool("Add formula").click(); await typeInCard(page, "E=mc^2"); await expect(count("math").locator(".katex")).toBeVisible();114  // table (small grid)115  await tool("Add table").click(); await page.keyboard.type("Year"); await page.keyboard.press("Tab"); await page.keyboard.type("Rate"); await page.keyboard.press("Escape"); await expect(count("table").locator("th").first()).toHaveText("Year");116  // freehand ink, directly on the canvas117  await tool("Draw").click(); const sb = (await surface(page).boundingBox())!;118  await page.mouse.move(sb.x + 70, sb.y + 260); await page.mouse.down(); for (let i = 1; i <= 12; i++) await page.mouse.move(sb.x + 70 + i * 10, sb.y + 260 + Math.sin(i / 2) * 30); await page.mouse.up();119  await expect(page.locator("[data-testid='canvas-ink-layer']")).toHaveAttribute("data-strokes", "1"); await page.locator("[data-testid='canvas-ink-bar']").getByRole("button", { name: "Done" }).click();120  await expect(page.locator("[data-testid='canvas-ink']")).toHaveAttribute("data-strokes", "1", { timeout: 15_000 });121  await expect(page.locator(".cv-node")).toHaveCount(10);122  // tidy up (animated grid: no connection yet), then connect the text to the shape by dragging its side handle, and label the connection123  await surface(page).click({ position: { x: 30, y: 120 } }); await tool("Tidy up").click(); await page.waitForTimeout(1200);124  const d0 = await docOfCanvas(page); const overlap = d0.nodes.some((a, i) => d0.nodes.some((b, j) => j > i && a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h)); expect(overlap, "tidy up leaves no overlap").toBe(false);125  const textId = d0.nodes.find((n) => n.kind === "text").id, shapeId = d0.nodes.find((n) => n.kind === "shape").id;126  await card(page, textId).click(); expect(await selection(page)).toEqual([textId]);127  const handle = (await page.locator(".cv-connect[data-connect='bottom']").boundingBox())!; const to = await center(page, shapeId);128  await page.mouse.move(handle.x + handle.width / 2, handle.y + handle.height / 2); await page.mouse.down(); await page.mouse.move(to.x, to.y, { steps: 8 }); await page.mouse.up();129  await expect(page.locator(".cv-edge")).toHaveCount(1); await page.locator("[data-testid='canvas-edge-bar']").getByRole("button", { name: "Edit label" }).click();130  await page.locator(".cv-edge-input").fill("leads to"); await page.keyboard.press("Enter"); await expect(page.locator(".cv-edge-label text")).toHaveText("leads to");131  await saved(page);132  const s1 = await waitFor(async () => { const s = await server(page, id); return s.data.edges[0]?.label === "leads to" ? s : null; });133  expect(new Set(s1.data.nodes.map((n) => n.kind))).toEqual(new Set(["text", "shape", "page", "link", "image", "pdf", "reference", "math", "table", "drawing"]));134  expect(s1.data.edges).toHaveLength(1); expect(s1.data.edges[0]).toMatchObject({ from: textId, to: shapeId, kind: "arrow", label: "leads to" });135  const by = (k: string) => s1.data.nodes.find((n) => n.kind === k);136  expect(by("page").pageId).toBe(target.body.id); expect(by("reference").refId).toBe(ref.body.id); expect(by("math").latex).toBe("E=mc^2"); expect(by("link")).toMatchObject({ url: "https://example.org/paper", text: "A paper" });137  expect(by("table").text.split("\n")[0]).toBe("| Year | Rate |  |"); expect(by("image").attachmentId).toMatch(/^[0-9a-f-]{36}$/); expect(by("pdf").attachmentId).toMatch(/^[0-9a-f-]{36}$/); expect(by("drawing").ink).toMatch(/^[0-9a-f-]{36}$/);138  const ink = await page.request.get(`/api/files/${by("drawing").ink}`); const inkDoc = await ink.json(); expect(inkDoc.format).toBe("spn-ink-1"); expect(inkDoc.strokes).toHaveLength(1); expect(inkDoc.width).toBe(by("drawing").w);139  // the canvas text is the page's plain text (search); page cards are backlinks140  const [row] = await sql`select plain_text from pages where id = ${id}`; expect(row.plain_text).toContain("Hello **canvas**"); expect(row.plain_text).toContain("leads to"); expect(row.plain_text).toContain("E=mc^2");141  const back = (await apiCall(page, "GET", `/api/pages/${target.body.id}`)).body.backlinks; expect(back.map((b: any) => b.id)).toContain(id);142  await shot(page, "desktop-light-all-kinds"); await setTheme(page, "dark"); await page.waitForTimeout(300); await shot(page, "desktop-dark-all-kinds"); await setTheme(page, "light");143  // reload: the same document, the same rendering, and no save caused by merely opening it144  await page.reload(); await ready(page).waitFor(); await expect(page.locator(".cv-node")).toHaveCount(10);145  await expect(page.locator("[data-testid='canvas-pdf'] .cv-pdf-page img")).toBeVisible({ timeout: 30_000 }); await expect(page.locator("[data-testid='canvas-ink']")).toHaveAttribute("data-strokes", "1"); await expect(page.locator(".cv-node[data-kind='math'] .katex")).toBeVisible();146  await expect(page.locator("[data-testid='canvas-page-card'] .cv-title")).toContainText(target.body.title); await expect(page.locator("[data-testid='canvas-ref-card'] .cv-title")).toHaveText(refTitle); await expect(page.locator(".cv-edge-label text")).toHaveText("leads to");147  await page.waitForTimeout(2500); const s2 = await server(page, id); expect(s2.version).toBe(s1.version); expect(s2.data).toEqual(s1.data); expect(await docOfCanvas(page).then((d) => ({ nodes: d.nodes, edges: d.edges }))).toEqual({ nodes: s1.data.nodes, edges: s1.data.edges });148  await apiCall(page, "DELETE", `/api/research/references/${ref.body.id}`);149});150151async function second(browser: import("@playwright/test").Browser): Promise<{ ctx: BrowserContext; page: Page }> { const ctx = await browser.newContext({ storageState: STATE, baseURL: ORIGIN, viewport: { width: 1280, height: 800 } }); return { ctx, page: await ctx.newPage() }; }152const count409 = (page: Page) => { const seen = { n: 0 }; page.on("response", (r) => { if (r.request().method() === "PUT" && r.url().includes("/api/canvases/") && r.status() === 409) seen.n++; }); return seen; };153154test("two sessions edit different cards at the same time: the second save meets a 409, merges, and both edits survive", async ({ page, browser }) => {155  await page.goto("/"); const id = await newCanvas(page, "merge", { nodes: [text("a", 0, 0, "Card A"), text("b", 300, 0, "Card B"), text("c", 600, 0, "Card C")], edges: [{ id: "e1", from: "a", to: "b" }] });156  const B = await second(browser); const conflictsB = count409(B.page);157  await open(page, id); await open(B.page, id); const v0 = (await server(page, id)).version;158  await editCard(page, "a", "Card A — edited in session one"); await saved(page); await waitVersion(page, id, v0);159  // session two still holds the old base: it edits another card, moves a third and adds a connection160  await editCard(B.page, "b", "Card B — edited in session two");161  const c = await center(B.page, "c"); await B.page.mouse.move(c.x, c.y); await B.page.mouse.down(); await B.page.mouse.move(c.x + 40, c.y + 160, { steps: 6 }); await B.page.mouse.up();162  await saved(B.page);163  const s = await waitFor(async () => { const x = await server(page, id); return x.data.nodes.find((n) => n.id === "b").text.includes("session two") ? x : null; });164  expect(conflictsB.n, "session two's save was refused once (409) before the merge").toBeGreaterThanOrEqual(1);165  expect(s.data.nodes.find((n) => n.id === "a").text).toBe("Card A — edited in session one"); expect(s.data.nodes.find((n) => n.id === "b").text).toBe("Card B — edited in session two"); expect(s.data.nodes.find((n) => n.id === "c").y).toBeGreaterThan(100); expect(s.data.edges).toHaveLength(1);166  await expect(card(B.page, "a")).toContainText("edited in session one"); await expect(B.page.locator("[data-testid='canvas-review']")).toHaveCount(0);167  // session one catches up when its window comes back to the front (nothing pending there: plain refresh)168  await page.evaluate(() => window.dispatchEvent(new Event("focus"))); await expect(card(page, "b")).toContainText("edited in session two"); await expect(page.locator("[data-testid='canvas-review']")).toHaveCount(0);169  expect(await docOfCanvas(page).then((d) => d.nodes)).toEqual(s.data.nodes);170  await B.ctx.close();171});172173test("the same card edited in both sessions, and an edit against a deletion: review panel, nothing lost, keep mine / keep theirs", async ({ page, browser }) => {174  await page.goto("/"); const id = await newCanvas(page, "conflict", { nodes: [text("a", 0, 0, "Original A"), text("b", 300, 0, "Original B"), text("c", 0, 200, "Original C")], edges: [] });175  const B = await second(browser); const conflictsB = count409(B.page);176  await open(page, id); await open(B.page, id); const v0 = (await server(page, id)).version;177  await editCard(page, "a", "A from session one"); await card(page, "b").click(); await page.keyboard.press("Delete"); await expect(card(page, "b")).toHaveCount(0); await saved(page); await waitVersion(page, id, v0);178  await editCard(B.page, "a", "A from session two"); await editCard(B.page, "b", "B edited in session two while session one deleted it");179  const review = B.page.locator("[data-testid='canvas-review']"); await expect(review).toBeVisible({ timeout: 20_000 }); expect(conflictsB.n).toBeGreaterThanOrEqual(1);180  const items = review.locator("[data-testid='canvas-conflict']"); await expect(items).toHaveCount(2);181  await expect(review.locator("[data-reason='bothEdited']")).toContainText("A from session two"); await expect(review.locator("[data-reason='bothEdited']")).toContainText("A from session one");182  await expect(review.locator("[data-reason='editedAndDeleted']")).toContainText("Deleted");183  // non-blocking: the canvas is still editable while the panel is open, and this device's versions are on the canvas and on the server184  await editCard(B.page, "c", "C edited during the review"); await saved(B.page);185  let s = await waitFor(async () => { const x = await server(page, id); return x.data.nodes.find((n) => n.id === "c")?.text === "C edited during the review" ? x : null; });186  expect(s.data.nodes.find((n) => n.id === "a").text).toBe("A from session two"); expect(s.data.nodes.find((n) => n.id === "b").text, "edit versus delete keeps the edited card").toContain("B edited in session two");187  fs.mkdirSync(SHOTS, { recursive: true }); await B.page.screenshot({ path: path.join(SHOTS, "desktop-light-conflict-review.png") });188  // keep theirs for A (the other session's text comes back), keep mine for B (the edited card stays)189  await review.locator("[data-reason='bothEdited']").getByRole("button", { name: "Keep theirs" }).click(); await expect(card(B.page, "a")).toContainText("A from session one"); await expect(items).toHaveCount(1);190  await review.locator("[data-reason='editedAndDeleted']").getByRole("button", { name: "Keep mine" }).click(); await expect(review).toHaveCount(0); await saved(B.page);191  s = await waitFor(async () => { const x = await server(page, id); return x.data.nodes.find((n) => n.id === "a").text === "A from session one" ? x : null; });192  expect(s.data.nodes.map((n) => n.id).sort()).toEqual(["a", "b", "c"]);193  await B.ctx.close();194});195196test("offline: edits are kept on the device, sent on reconnect, and a draft survives a closed tab", async ({ page, context }) => {197  await page.goto("/"); const id = await newCanvas(page, "offline", { nodes: [text("a", 0, 0, "Online card")], edges: [] }); await open(page, id); const v0 = (await server(page, id)).version;198  const draft = () => page.evaluate((key) => new Promise<any>((res) => { const r = indexedDB.open("note-local"); r.onsuccess = () => { try { const g = r.result.transaction("drafts").objectStore("drafts").get(key); g.onsuccess = () => res(g.result ?? null); g.onerror = () => res(null); } catch { res(null); } }; r.onerror = () => res(null); }), `canvas:${id}`);199  await context.setOffline(true);200  await page.locator("[data-testid='canvas-toolbar']").getByRole("button", { name: "Add text", exact: true }).click(); await typeInCard(page, "Written offline");201  await expect(page.locator("[data-testid='canvas-status']")).toHaveAttribute("data-status", "offline", { timeout: 20_000 }); await expect(page.locator("[data-testid='canvas-status']")).toContainText("Offline");202  const d = await waitFor(draft); expect(d.baseVersion).toBe(v0); expect(d.patch.data.nodes.map((n: any) => n.text)).toContain("Written offline"); expect(d.patch.base.nodes).toHaveLength(1);203  expect((await server(page, id).catch(() => null))?.version ?? v0).toBe(v0);204  await context.setOffline(false); await page.evaluate(() => window.dispatchEvent(new Event("online"))); await saved(page);205  const s1 = await waitVersion(page, id, v0); expect(s1.data.nodes.map((n) => n.text)).toContain("Written offline"); expect(await draft()).toBeNull();206  // second part: offline again, edit, close the tab before any reconnection207  await context.setOffline(true); await editCard(page, "a", "Changed offline, then the tab was closed"); await waitFor(async () => { const x = await draft(); return x && x.patch.data.nodes.some((n: any) => String(n.text).includes("tab was closed")) ? x : null; });208  await page.close(); await context.setOffline(false);209  const again = await context.newPage(); await open(again, id); await expect(again.getByText("Unsaved changes restored")).toBeVisible({ timeout: 15_000 }); await expect(card(again, "a")).toContainText("Changed offline"); await saved(again);210  const s2 = await waitVersion(again, id, s1.version); expect(s2.data.nodes.find((n) => n.id === "a").text).toBe("Changed offline, then the tab was closed"); expect(s2.data.nodes).toHaveLength(2);211});212213test("selection, alignment, undo, clipboard, stacking, resize, context menu, minimap; unknown content preserved; the viewport alone never creates a version", async ({ page }) => {214  await page.goto("/");215  const alien = { id: "alien", kind: "hologram", x: 700, y: 300, w: 220, h: 90, z: 3, payload: { deep: [1, { k: null }] }, locked: true };216  const id = await newCanvas(page, "interactions", { theme: { grid: true }, nodes: [text("a", 100, 100, "Alpha"), text("b", 380, 160, "Beta", { z: 1 }), text("c", 660, 80, "Gamma", { z: 2, future: [1, 2] }), alien], edges: [{ id: "e1", from: "a", to: "c", weight: 7, kind: "zigzag" }] });217  await open(page, id); const v0 = (await server(page, id)).version; const s = surface(page); const box = (await s.boundingBox())!;218  await expect(card(page, "alien")).toContainText("Unsupported card"); await expect(card(page, "alien")).toContainText("hologram");219  // pan and zoom only: nothing is sent, the view is remembered on this device220  await page.mouse.move(box.x + 500, box.y + 400); await page.mouse.wheel(0, -200); await page.mouse.wheel(0, 100); await page.keyboard.down("Space").catch(() => {}); await page.keyboard.up("Space").catch(() => {});221  await s.focus(); await page.keyboard.press("Shift+Digit1"); await page.waitForTimeout(1800);222  expect((await server(page, id)).version, "a viewport change alone does not create a version").toBe(v0);223  expect(await page.evaluate((k) => JSON.parse(localStorage.getItem(k) ?? "null")?.zoom > 0, `note-canvas-vp-${id}`)).toBe(true);224  // marquee around Alpha and Beta, align top, undo / redo225  const a = await center(page, "a"), b = await center(page, "b");226  await page.mouse.move(a.b.x - 30, a.b.y - 30); await page.mouse.down(); await page.mouse.move(b.b.x + b.b.width + 10, b.b.y + b.b.height + 10, { steps: 6 }); await page.mouse.up();227  expect((await selection(page)).sort()).toEqual(["a", "b"]);228  const bar = page.locator("[data-testid='canvas-selection-bar']"); await bar.getByRole("button", { name: "Align and distribute" }).click(); await page.getByRole("menuitem", { name: "Align top" }).click();229  let d = await docOfCanvas(page); expect(d.nodes.find((n) => n.id === "b").y).toBe(100);230  await s.focus(); await page.keyboard.press("ControlOrMeta+z"); expect((await docOfCanvas(page)).nodes.find((n) => n.id === "b").y).toBe(160); await page.keyboard.press("ControlOrMeta+Shift+z"); expect((await docOfCanvas(page)).nodes.find((n) => n.id === "b").y).toBe(100);231  // duplicate, delete, copy / paste232  await page.keyboard.press("ControlOrMeta+d"); expect((await docOfCanvas(page)).nodes).toHaveLength(6); expect(await selection(page)).toHaveLength(2); await page.keyboard.press("Delete"); expect((await docOfCanvas(page)).nodes).toHaveLength(4);233  await card(page, "a").click(); await page.keyboard.press("ControlOrMeta+c"); await page.keyboard.press("ControlOrMeta+v"); d = await docOfCanvas(page); expect(d.nodes).toHaveLength(5); const pasted = d.nodes[4]; expect(pasted).toMatchObject({ kind: "text", text: "Alpha", x: 124, y: 124 }); expect(pasted.id).not.toBe("a");234  await page.keyboard.press("Backspace"); expect((await docOfCanvas(page)).nodes).toHaveLength(4);235  // stacking, arrow-key nudge, drag with the mouse, resize by a handle236  await card(page, "a").click(); await bar.getByRole("button", { name: "Bring to front" }).click(); d = await docOfCanvas(page); expect(d.nodes.find((n) => n.id === "a").z).toBeGreaterThan(Math.max(...d.nodes.filter((n) => n.id !== "a").map((n) => n.z)));237  await s.focus(); await page.keyboard.press("Shift+ArrowRight"); expect((await docOfCanvas(page)).nodes.find((n) => n.id === "a").x).toBe(110);238  const zoom = await local<number>(page, "(s) => s.viewport.zoom"); const g = await center(page, "c"); await page.mouse.move(g.x, g.y); await page.mouse.down(); await page.mouse.move(g.x + 60 * zoom, g.y + 90 * zoom, { steps: 8 }); await page.keyboard.down("Alt"); await page.mouse.move(g.x + 60 * zoom, g.y + 91 * zoom); await page.mouse.up(); await page.keyboard.up("Alt");239  d = await docOfCanvas(page); expect(Math.abs(d.nodes.find((n) => n.id === "c").x - 720)).toBeLessThan(3); expect(Math.abs(d.nodes.find((n) => n.id === "c").y - 171)).toBeLessThan(3);240  const h = (await page.locator(".cv-handle[data-handle='se']").boundingBox())!; await page.mouse.move(h.x + h.width / 2, h.y + h.height / 2); await page.mouse.down(); await page.mouse.move(h.x + h.width / 2 + 80 * zoom, h.y + h.height / 2 + 40 * zoom, { steps: 5 }); await page.mouse.up();241  d = await docOfCanvas(page); expect(Math.abs(d.nodes.find((n) => n.id === "c").w - 280)).toBeLessThan(3); expect(Math.abs(d.nodes.find((n) => n.id === "c").h - 140)).toBeLessThan(3);242  // the unknown card can be moved like any other243  await card(page, "alien").click(); await page.keyboard.press("ArrowDown");244  // context menu: on empty space and on a card245  await s.click({ button: "right", position: { x: 60, y: box.height - 200 } }); await page.getByRole("menuitem", { name: "Add shape here" }).click(); expect((await docOfCanvas(page)).nodes.filter((n) => n.kind === "shape")).toHaveLength(1);246  await card(page, "b").click({ button: "right" }); await page.getByRole("menuitem", { name: "Delete" }).click(); expect((await docOfCanvas(page)).nodes.some((n) => n.id === "b")).toBe(false);247  // connection tools: select the connection, switch to right angles and to a plain line248  await page.locator(".cv-edge[data-edge='e1'] .cv-edge-hit").click({ force: true }); const ebar = page.locator("[data-testid='canvas-edge-bar']"); await ebar.getByRole("button", { name: "Right angles" }).click(); await ebar.getByRole("button", { name: "Plain line" }).click(); await expect(page.locator(".cv-edge[data-edge='e1'] .cv-edge-head")).toHaveCount(0);249  // minimap and zoom controls250  const before = await local<{ x: number; y: number; zoom: number }>(page, "(s) => s.viewport"); const mm = (await page.locator("[data-testid='canvas-minimap']").boundingBox())!; await page.mouse.click(mm.x + 12, mm.y + 12);251  const after = await local<{ x: number; y: number }>(page, "(s) => s.viewport"); expect(after.x !== before.x || after.y !== before.y).toBe(true);252  await page.getByRole("button", { name: "Zoom in", exact: true }).click(); await page.waitForTimeout(300); expect(await local<number>(page, "(s) => s.viewport.zoom")).toBeGreaterThan(before.zoom); await page.getByRole("button", { name: "Zoom to fit" }).click(); await page.waitForTimeout(400);253  await saved(page); const end = await waitVersion(page, id, v0);254  // everything this client does not know came through the whole session untouched255  const alienOut = end.data.nodes.find((n) => n.id === "alien"); expect(alienOut).toEqual({ ...alien, y: 301 }); expect(end.data.theme).toEqual({ grid: true }); expect(end.data.nodes.find((n) => n.id === "c").future).toEqual([1, 2]);256  expect(end.data.edges[0]).toEqual({ id: "e1", from: "a", to: "c", weight: 7, kind: "line", route: "orthogonal" });257});258259test("entry points and exports: command center, canvasLink block, listing with its icon, page and file drops, PNG / SVG / Markdown", async ({ page }) => {260  test.setTimeout(120_000);261  await sql`delete from pages where title = 'New canvas'`; // leftovers of other tests would be offered as pages of that name262  await page.goto("/");263  // command center → New canvas264  await page.keyboard.press("ControlOrMeta+k"); await page.locator("[cmdk-input]").fill("New canvas"); await page.getByRole("option", { name: /New canvas/ }).first().click();265  await page.waitForURL(/\/p\/[0-9a-f-]{36}/); await ready(page).waitFor(); const id = page.url().split("/p/")[1].slice(0, 36); made.push(id);266  await page.getByLabel("Canvas title").fill(`E2E-canvas entry ${uid()}`); await page.waitForTimeout(1200);267  const title = await page.getByLabel("Canvas title").inputValue(); await expect(page.locator("nav[aria-label='Workspace']").first().getByText(title)).toBeVisible({ timeout: 10_000 });268  await expect(page.locator("nav[aria-label='Workspace']").first().locator(`a[href='/p/${id}'] [data-testid='canvas-icon']`).first()).toBeVisible();269  // drop a page dragged from the sidebar, a link and an image file from the desktop270  const other = await apiCall(page, "POST", "/api/pages", { title: `E2E-canvas dropped ${uid()}`, content: docOf(para("dropped page body")) }); made.push(other.body.id);271  const drop = (kind: "uri" | "file", value: string, y = 300) => page.evaluate(async ([k, v, top]) => {272    const el = document.querySelector("[data-testid='canvas-surface']")!; const r = el.getBoundingClientRect(); const dt = new DataTransfer();273    if (k === "uri") { dt.setData("text/uri-list", v); dt.setData("text/plain", v); } else { const bin = atob(v); const bytes = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); dt.items.add(new File([bytes], "dropped.png", { type: "image/png" })); }274    for (const type of ["dragover", "drop"]) el.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true, dataTransfer: dt, clientX: r.left + (k === "uri" ? 300 : 700), clientY: r.top + (top as number) }));275  }, [kind, value, y] as const);276  await drop("uri", `${ORIGIN}/p/${other.body.id}`); await expect(page.locator("[data-testid='canvas-page-card'] .cv-title")).toContainText(other.body.title);277  await drop("uri", "https://example.org/dropped-link", 520); await expect(page.locator(".cv-node[data-kind='link']")).toContainText("example.org");278  await drop("file", PNG.toString("base64")); await expect(page.locator(".cv-node[data-kind='image'] img")).toBeVisible({ timeout: 15_000 });279  await page.locator("[data-testid='canvas-toolbar']").getByRole("button", { name: "Add text", exact: true }).click(); await typeInCard(page, "Exported text"); await saved(page);280  const s1 = await waitFor(async () => { const s = await server(page, id); return s.data.nodes.length === 4 ? s : null; }); expect(s1.data.nodes.map((n) => n.kind).sort()).toEqual(["image", "link", "page", "text"]);281  // exports282  const exportAs = async (item: string) => { const [dl] = await Promise.all([page.waitForEvent("download"), (async () => { await page.locator("[data-testid='canvas-toolbar']").getByRole("button", { name: "Export" }).click(); await page.getByRole("menuitem", { name: item }).click(); })()]); const p = await dl.path(); return { name: dl.suggestedFilename(), data: fs.readFileSync(p!) }; };283  const svg = await exportAs("Export as SVG"); expect(svg.name).toMatch(/\.svg$/); const svgText = svg.data.toString("utf8"); expect(svgText.startsWith("<svg")).toBe(true); expect(svgText).toContain("Exported text"); expect(svgText).toContain(other.body.title.slice(0, 12)); expect(svgText).toContain("data:image/png;base64,"); expect((svgText.match(/<g data-id=/g) ?? []).length).toBe(4);284  const md = await exportAs("Export as Markdown outline"); const mdText = md.data.toString("utf8"); expect(mdText).toContain(`# ${title}`); expect(mdText).toContain("## Cards"); expect(mdText).toContain(`- **Page** — [${other.body.title}](/p/${other.body.id})`); expect(mdText).toContain("- **Text** — Exported text"); expect(mdText).toContain("## Connections");285  const png = await exportAs("Export as PNG"); expect(png.name).toMatch(/\.png$/); expect(png.data.subarray(0, 8).toString("hex")).toBe("89504e470d0a1a0a"); expect(png.data.length).toBeGreaterThan(2000);286  // double-click on a page card opens the note287  await page.locator("[data-testid='canvas-page-card']").dblclick(); await page.waitForURL(new RegExp(`/p/${other.body.id}`)); await expect(page.locator(".note-editor .ProseMirror").first()).toContainText("dropped page body");288  // a canvasLink block in a document opens the canvas289  const host = await apiCall(page, "POST", "/api/pages", { title: `E2E-canvas host ${uid()}`, content: docOf(para("before"), { type: "canvasLink", attrs: { pageId: id } }) }); made.push(host.body.id);290  await page.goto(`/p/${host.body.id}`); await page.locator(".canvas-card").click(); await page.waitForURL(new RegExp(`/p/${id}`)); await ready(page).waitFor(); await expect(page.locator(".cv-node")).toHaveCount(4);291  // a trashed canvas opens read-only292  await apiCall(page, "DELETE", `/api/pages/${id}`); await page.reload(); await ready(page).waitFor(); await expect(page.getByText("This canvas is in the trash (read-only).")).toBeVisible(); await expect(page.locator("[data-testid='canvas-toolbar']").getByRole("button", { name: "Add text", exact: true })).toHaveCount(0);293});294295test("performance probe: 1 000 cards — only what is visible is rendered; pan, zoom and drag frame times", async ({ page }) => {296  test.setTimeout(180_000);297  await page.goto("/");298  const N = 1000, COLS = 40; const kinds = ["text", "text", "shape", "link", "math", "table"];299  const nodes = Array.from({ length: N }, (_, i) => { const k = kinds[i % kinds.length]; return { id: `n${i}`, kind: k, x: (i % COLS) * 300, y: Math.floor(i / COLS) * 180, w: 240, h: 120, z: i, ...(k === "text" ? { text: `## Card ${i}\nSome **Markdown** text for card number ${i}.` } : k === "shape" ? { shape: ["rect", "ellipse", "diamond"][i % 3], text: `S${i}`, color: "#2f5ddb" } : k === "link" ? { url: `https://example.org/${i}`, text: `Link ${i}` } : k === "math" ? { latex: `x_{${i}}^2 + y^2` } : { text: `| A | B |\n| --- | --- |\n| ${i} | ${i * 2} |` }) }; });300  const edges = Array.from({ length: N - 1 }, (_, i) => ({ id: `e${i}`, from: `n${i}`, to: `n${i + 1}`, ...(i % 7 === 0 ? { label: `l${i}` } : {}) }));301  const id = await newCanvas(page, "perf 1000", { nodes, edges });302  await page.evaluate((k) => localStorage.setItem(k, JSON.stringify({ x: 2000, y: 1500, zoom: 1 })), `note-canvas-vp-${id}`);303  const t0 = Date.now(); await open(page, id); await expect(page.locator(".cv-node").first()).toBeVisible(); const openMs = Date.now() - t0;304  const rendered = () => page.evaluate(() => ({ nodes: document.querySelectorAll(".cv-node").length, lod: document.querySelectorAll(".cv-node[data-lod]").length, edges: document.querySelectorAll(".cv-edge").length }));305  const atOne = await rendered(); expect(atOne.nodes).toBeGreaterThan(8); expect(atOne.nodes, "culling: far fewer than 1 000 cards in the DOM at 100 %").toBeLessThan(160);306  /** Drives `frames` animation frames, calling `step(i)` before each, and returns the frame intervals. */307  const probe = (kind: "pan" | "zoom" | "sweep" | "drag", frames: number) => page.evaluate(async ([k, n]) => {308    const el = document.querySelector("[data-testid='canvas-surface']") as HTMLElement; const r = el.getBoundingClientRect(); const store = (window as any).__canvas.store; const M = (window as any).__canvas.model;309    const cx = r.left + r.width / 2, cy = r.top + r.height / 2; const dts: number[] = []; let dragId: string | null = null; let base = store.doc;310    if (k === "drag") { const vis = M.visibleRect(store.viewport, store.size); dragId = store.doc.nodes.find((x: any) => x.x > vis.x + 100 && x.y > vis.y + 100 && x.x < vis.x + vis.w - 300)?.id ?? store.doc.nodes[0].id; store.select([dragId]); base = store.doc; }311    const next = () => new Promise<number>((res) => requestAnimationFrame(res));312    let last = await next();313    for (let i = 0; i < n; i++) {314      if (k === "pan") el.dispatchEvent(new WheelEvent("wheel", { bubbles: true, cancelable: true, clientX: cx, clientY: cy, deltaX: 18 * Math.cos(i / 15), deltaY: 14.5 * Math.sin(i / 11) + 9.5 }));315      else if (k === "zoom" || k === "sweep") el.dispatchEvent(new WheelEvent("wheel", { bubbles: true, cancelable: true, clientX: cx, clientY: cy, deltaY: (i < n / 2 ? 1 : -1) * (k === "zoom" ? 1 : 4), ctrlKey: true }));316      else store.apply(() => M.moveNodes(base, [dragId], i * 3, Math.sin(i / 8) * 60), "transient");317      const t = await next(); dts.push(t - last); last = t;318    }319    if (k === "drag") store.cancel();320    const sorted = [...dts].sort((a, b) => a - b); const avg = dts.reduce((s, x) => s + x, 0) / dts.length;321    return { frames: n, avg: +avg.toFixed(2), p50: +sorted[Math.floor(n * 0.5)].toFixed(2), p95: +sorted[Math.floor(n * 0.95)].toFixed(2), max: +sorted[n - 1].toFixed(2), over33: dts.filter((x) => x > 33.4).length, fps: +(1000 / avg).toFixed(1) };322  }, [kind, frames] as const);323  const results: Record<string, unknown> = { cards: N, connections: N - 1, openMs, renderedAt100: atOne };324  results.panAt100 = await probe("pan", 240); results.zoomAround100 = await probe("zoom", 180); results.dragAt100 = await probe("drag", 180);325  // the hard case: pinch from 100 % out to where most cards turn into placeholders (≈ 3 %/frame) and back, hundreds of cards entering the view326  results.zoomSweep100to3 = await probe("sweep", 180);327  results.renderedAfterPan = await rendered();328  // everything on screen at once: zoom to fit (cards become placeholders)329  await surface(page).focus(); await page.keyboard.press("Shift+Digit1"); await page.waitForTimeout(900);330  const atFit = await rendered(); results.renderedAtFit = atFit; results.zoomAtFit = await local<number>(page, "(s) => +s.viewport.zoom.toFixed(3)");331  expect(atFit.nodes).toBe(N); expect(atFit.lod, "at this scale the cards are light placeholders").toBe(N);332  results.panAtFit = await probe("pan", 180); results.zoomAtFit2 = await probe("zoom", 120); results.dragAtFit = await probe("drag", 120);333  fs.mkdirSync(SHOTS, { recursive: true }); await page.screenshot({ path: path.join(SHOTS, "desktop-light-1000-cards.png") });334  // "Tidy up" on 1 000 connected cards (force-directed, 60 steps) — measured in the page335  results.tidyMs = await page.evaluate(() => { const c = (window as any).__canvas; const t = performance.now(); c.model.tidyLayout(c.store.doc, { seed: 1 }); return Math.round(performance.now() - t); });336  fs.writeFileSync(path.join(SHOTS, "..", "perf-1000.json"), JSON.stringify(results, null, 1)); console.log("CANVAS PERF " + JSON.stringify(results));337  for (const k of ["panAt100", "zoomAround100", "dragAt100", "panAtFit"]) { const r = results[k] as { p95: number; avg: number }; expect(r.avg, `${k} average frame time`).toBeLessThan(20); expect(r.p95, `${k} p95 frame time`).toBeLessThan(34); }338  expect((await server(page, id)).version, "the probe changed nothing").toBe(2);339});340
Files main
Menu

Theme