SPB Git

Repository Public

next-canvas-phones.spec.ts 12.0 KB 126 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/e2e/next-canvas-phones.spec.ts9 * Description      : Infinite Canvas on phones (360×740, 375×667, 430×932,10 *                    touch) — a dedicated touch interface: dock with large11 *                    targets, bottom sheets to add and edit cards, outline /12 *                    list mode (open, edit text, reorder, locate), connect by13 *                    tapping, touch pan and pinch; the app chrome never14 *                    overflows horizontally.15 * Module           : tests16 * Repository       : https://git.spboucher.ai/note-spboucher-ai17 * Project Website  : https://note.spboucher.ai18 * Created          : 2026-11-1919 * Last Modified    : 2026-11-1920 *21 * Author           : Simon-Pierre Boucher22 * Position         : Professor of Finance23 * Institution      : Université du Québec en Outaouais (UQO)24 * Organization     : SP Boucher25 * Website          : https://spboucher.ai26 * Git              : https://git.spboucher.ai27 *28 * Preferred Contact: contact@spboucher.ai29 * Development      : dev@spboucher.ai30 * Support          : support@spboucher.ai31 * Security/Admin   : admin@spboucher.ai32 *33 * Copyright (c) 202634 * Simon-Pierre Boucher35 * All rights reserved where applicable. This file is governed by the36 * applicable project license, copyright notices and relevant law.37 * Existing third-party rights and notices remain unaffected.38 *39 * License          : None specified (no license file in this repository)40 *41 * Do not embed credentials, private keys, access tokens,42 * certificates or confidential personal information.43 *44 * =============================================================================45 * END OF SP BOUCHER SOURCE HEADER46 * =============================================================================47 */4849import { test, expect, type Page } from "@playwright/test";50import fs from "node:fs";51import os from "node:os";52import path from "node:path";53import { apiCall, expectNoHorizontalOverflow, sql } from "./helpers";5455const SHOTS = process.env.CANVAS_SHOTS ?? path.join(os.homedir(), "dev/note-next-c-data/shots");56const made: string[] = []; const uid = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 6);57const text = (id: string, x: number, y: number, t: string, extra: Record<string, unknown> = {}) => ({ id, kind: "text", x, y, w: 200, h: 100, z: 0, text: t, ...extra });58async function newCanvas(page: Page, title: string, data: { nodes: unknown[]; edges: unknown[] }) {59  const r = await apiCall(page, "POST", "/api/pages", { title: `E2E-canvasp ${title} ${uid()}`, type: "canvas" }); expect(r.status).toBe(201); made.push(r.body.id);60  const put = await apiCall(page, "PUT", `/api/canvases/${r.body.id}`, { data: { format: "spn-canvas-1", viewport: { x: 0, y: 0, zoom: 1 }, ...data }, baseVersion: 1 }); expect(put.status, JSON.stringify(put.body)).toBe(200);61  return r.body.id as string;62}63const server = async (page: Page, id: string) => (await apiCall(page, "GET", `/api/canvases/${id}`)).body as { data: { nodes: any[]; edges: any[] }; version: number };64const saved = (page: Page) => expect(page.locator("[data-testid='canvas-status']")).toHaveAttribute("data-status", "saved", { timeout: 20_000 });65const 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)); } };66const local = <T,>(page: Page, fn: string): Promise<T> => page.evaluate(`(() => { const c = window.__canvas; return (${fn})(c.store); })()`) as Promise<T>;67test.afterAll(async () => { if (made.length) await sql`delete from pages where id = any(${made}::uuid[])`; });6869test("phone canvas: touch interface, outline mode, sheets, connect by tapping, no horizontal overflow", async ({ page }, info) => {70  test.setTimeout(120_000); const w = page.viewportSize()!.width;71  await page.goto("/");72  const id = await newCanvas(page, "phone", { nodes: [text("a", 0, 0, "First card"), text("b", 260, 40, "Second card", { color: "#008b4e" }), { id: "s", kind: "shape", shape: "diamond", x: 60, y: 220, w: 160, h: 120, z: 2, text: "Decide" }], edges: [{ id: "e1", from: "a", to: "b", label: "then" }] });73  await page.goto(`/p/${id}`); await page.locator("[data-testid='canvas-page'][data-state='ready']").waitFor(); const v0 = (await server(page, id)).version;74  const surface = page.locator("[data-testid='canvas-surface']"); await surface.waitFor(); await expect(page.locator(".cv-node")).toHaveCount(3);75  // a touch layout, not the desktop one: dock and view switch, no toolbar / minimap / zoom bar76  const dock = page.locator("[data-testid='canvas-dock']"); await expect(dock).toBeVisible(); await expect(page.getByRole("tab", { name: "List" })).toBeVisible();77  await expect(page.locator("[data-testid='canvas-toolbar']")).toHaveCount(0); await expect(page.locator("[data-testid='canvas-minimap']")).toHaveCount(0);78  await expectNoHorizontalOverflow(page);79  for (const b of await dock.getByRole("button").all()) { const box = (await b.boundingBox())!; expect(box.height, "dock targets are at least 44 px tall").toBeGreaterThanOrEqual(44); expect(box.width).toBeGreaterThanOrEqual(44); }80  // the whole canvas was fitted into the narrow view on first open81  const fit = await local<{ zoom: number }>(page, "(s) => s.viewport"); expect(fit.zoom).toBeLessThan(1);82  // tap selects; the dock shows the actions of the selection; handles are finger-sized83  const tap = async (sel: string) => { const b = (await page.locator(sel).boundingBox())!; await page.touchscreen.tap(b.x + b.width / 2, b.y + b.height / 2); };84  await tap(".cv-node[data-id='a']"); expect(await local<string[]>(page, "(s) => s.get().selection")).toEqual(["a"]);85  await expect(dock.getByRole("button", { name: "Edit" })).toBeVisible(); await expect(dock.getByRole("button", { name: "Connect" })).toBeVisible();86  const handle = (await page.locator(".cv-handle[data-handle='se']").boundingBox())!; expect(handle.width).toBeGreaterThanOrEqual(15);87  fs.mkdirSync(SHOTS, { recursive: true }); await page.screenshot({ path: path.join(SHOTS, `phone-${w}-light-canvas.png`) });88  // connect by tapping: Connect, then the target card89  await dock.getByRole("button", { name: "Connect" }).click(); await expect(page.locator("[data-testid='canvas-connect-hint']")).toBeVisible(); await tap(".cv-node[data-id='s']");90  await expect(page.locator(".cv-edge")).toHaveCount(2); await expect(page.locator("[data-testid='canvas-connect-hint']")).toHaveCount(0);91  // one finger on empty space pans; two fingers pinch (CDP touch events)92  const box = (await surface.boundingBox())!; const cdp = await page.context().newCDPSession(page); const before = await local<{ x: number; y: number; zoom: number }>(page, "(s) => s.viewport");93  const touch = (type: string, pts: { x: number; y: number; id: number }[]) => cdp.send("Input.dispatchTouchEvent", { type: type as any, touchPoints: pts.map((p) => ({ x: p.x, y: p.y, id: p.id })) });94  const ex = box.x + box.width - 30, ey = box.y + box.height - 120; // an empty corner95  await touch("touchStart", [{ x: ex, y: ey, id: 1 }]); for (let i = 1; i <= 5; i++) await touch("touchMove", [{ x: ex - i * 12, y: ey - i * 8, id: 1 }]); await touch("touchEnd", []);96  const panned = await local<{ x: number; y: number; zoom: number }>(page, "(s) => s.viewport"); expect(panned.x).toBeGreaterThan(before.x + 20); expect(panned.zoom).toBe(before.zoom);97  const cx = box.x + box.width / 2, cy = box.y + box.height / 2;98  await touch("touchStart", [{ x: cx - 40, y: cy, id: 1 }, { x: cx + 40, y: cy, id: 2 }]); for (let i = 1; i <= 6; i++) await touch("touchMove", [{ x: cx - 40 - i * 10, y: cy, id: 1 }, { x: cx + 40 + i * 10, y: cy, id: 2 }]); await touch("touchEnd", []);99  const pinched = await local<{ zoom: number }>(page, "(s) => s.viewport"); expect(pinched.zoom).toBeGreaterThan(panned.zoom * 1.5);100  await dock.getByRole("button", { name: "Fit" }).click(); await page.waitForTimeout(400);101  // dragging a card with one finger moves it102  const a0 = (await local<any>(page, "(s) => s.doc.nodes.find((n) => n.id === 'b')")); const bb = (await page.locator(".cv-node[data-id='b']").boundingBox())!; const bx = bb.x + bb.width / 2, by = bb.y + bb.height / 2;103  await touch("touchStart", [{ x: bx, y: by, id: 1 }]); for (let i = 1; i <= 6; i++) await touch("touchMove", [{ x: bx, y: by + i * 10, id: 1 }]); await touch("touchEnd", []);104  const a1 = (await local<any>(page, "(s) => s.doc.nodes.find((n) => n.id === 'b')")); expect(a1.y).toBeGreaterThan(a0.y + 20);105  // add from the bottom sheet (large buttons), edit in a sheet with large fields106  await dock.getByRole("button", { name: "Add" }).click(); const add = page.locator("[data-testid='canvas-add-sheet']"); await expect(add).toBeVisible(); await expectNoHorizontalOverflow(page);107  for (const b of await add.locator(".cv-add-grid button").all()) expect((await b.boundingBox())!.height).toBeGreaterThanOrEqual(44);108  await page.screenshot({ path: path.join(SHOTS, `phone-${w}-light-add-sheet.png`) });109  await add.getByRole("button", { name: "Text" }).click(); const form = page.locator("[data-testid='canvas-card-form']"); await expect(form).toBeVisible();110  await form.getByLabel("Card text").fill("Added on the phone"); await expectNoHorizontalOverflow(page); await form.getByRole("button", { name: "Done" }).click(); await expect(page.locator(".cv-node")).toHaveCount(4);111  // outline / list mode: every card, its kind and connections; open + edit text; reorder; locate112  await page.getByRole("tab", { name: "List" }).click(); const rows = page.locator("[data-testid='canvas-outline-row']"); await expect(rows).toHaveCount(4); await expectNoHorizontalOverflow(page);113  await expect(rows.nth(0)).toContainText("First card"); await expect(rows.nth(0)).toContainText("→ 2"); await expect(rows.nth(1)).toContainText("← 1"); await expect(rows.nth(2)).toContainText("Decide");114  await page.screenshot({ path: path.join(SHOTS, `phone-${w}-light-outline.png`) });115  if (info.project.name.endsWith("375")) { await page.evaluate(() => (window as any).__noteUI().setTheme("dark")); await page.waitForTimeout(250); await page.screenshot({ path: path.join(SHOTS, `phone-${w}-dark-outline.png`) }); await page.getByRole("tab", { name: "Canvas" }).click(); await page.waitForTimeout(250); await page.screenshot({ path: path.join(SHOTS, `phone-${w}-dark-canvas.png`) }); await page.getByRole("tab", { name: "List" }).click(); await page.evaluate(() => (window as any).__noteUI().setTheme("light")); }116  await rows.nth(1).locator(".cv-outline-main").click(); await expect(form).toBeVisible(); await form.getByLabel("Card text").fill("Second card, edited from the list"); await form.getByRole("button", { name: "Done" }).click();117  await expect(rows.nth(1)).toContainText("Second card, edited from the list");118  await rows.nth(0).getByRole("button", { name: /Move “First card” down/ }).click(); await expect(rows.nth(1)).toContainText("First card"); await expect(rows.nth(0)).toContainText("edited from the list");119  await rows.nth(2).getByRole("button", { name: /Show “Decide” on the canvas/ }).click(); await expect(surface).toBeVisible(); expect(await local<string[]>(page, "(s) => s.get().selection")).toEqual(["s"]);120  await expectNoHorizontalOverflow(page);121  // everything was saved through the normal path122  await saved(page); const s = await waitFor(async () => { const x = await server(page, id); return x.version > v0 && x.data.nodes.length === 4 && x.data.nodes[0].id === "b" ? x : null; });123  expect(s.data.nodes.map((n) => n.id).slice(0, 3)).toEqual(["b", "a", "s"]); expect(s.data.nodes[0].text).toBe("Second card, edited from the list"); expect(s.data.nodes[3].text).toBe("Added on the phone");124  expect(s.data.edges.map((e) => `${e.from}>${e.to}`).sort()).toEqual(["a>b", "a>s"]);125});126
Files main
Menu

Theme