Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/e2e/next-integration.spec.ts9 * Description : SP Note Next Generation — cross-feature scenario on the10 * merged build (integration hardening). One note holding11 * every new block is followed through the features built by12 * separate workstreams: editor (every block rendered,13 * opening it writes nothing), journal / timeline, knowledge14 * graph (blockRef, canvasLink and citation edges), knowledge15 * health, daily brief, Ask with scope "This page" (answer16 * faked at the HTTP level: no model is called) citing one of17 * its blocks, offline edit → one sync, a canvas page offline18 * (honest "needs a connection", never the text editor), its19 * canvas, Markdown export → re-import. Desktop Chromium with20 * the service worker, scratch database only; everything21 * created carries the prefix `E2E-integ` and is removed.22 * Module : tests23 * Repository : https://git.spboucher.ai/note-spboucher-ai24 * Project Website : https://note.spboucher.ai25 * Created : 2026-11-1926 * Last Modified : 2026-11-1927 *28 * Author : Simon-Pierre Boucher29 * Position : Professor of Finance30 * Institution : Université du Québec en Outaouais (UQO)31 * Organization : SP Boucher32 * Website : https://spboucher.ai33 * Git : https://git.spboucher.ai34 *35 * Preferred Contact: contact@spboucher.ai36 * Development : dev@spboucher.ai37 * Support : support@spboucher.ai38 * Security/Admin : admin@spboucher.ai39 *40 * Copyright (c) 202641 * Simon-Pierre Boucher42 * All rights reserved where applicable. This file is governed by the43 * applicable project license, copyright notices and relevant law.44 * Existing third-party rights and notices remain unaffected.45 *46 * License : None specified (no license file in this repository)47 *48 * Do not embed credentials, private keys, access tokens,49 * certificates or confidential personal information.50 *51 * =============================================================================52 * END OF SP BOUCHER SOURCE HEADER53 * =============================================================================54 */5556import { test, expect, type Page } from "@playwright/test";57import fs from "node:fs";58import { apiCall, docOf, para, PDF, sql, ORIGIN } from "./helpers";5960const stamp = Date.now().toString(36); const PFX = "E2E-integ"; const T = (s: string) => `${PFX} ${s} ${stamp}`;61const PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", "base64");62const made: string[] = []; const refs: string[] = []; const TASK_ID = crypto.randomUUID();63const DUE = new Date(Date.now() + 36 * 3600_000).toISOString().slice(0, 10); // tomorrow or the day after: in the brief as due today / due soon64const server = async (page: Page, id: string) => (await apiCall(page, "GET", `/api/pages/${id}`)).body;65const text = (n: any): string => (!n ? "" : typeof n.text === "string" ? n.text : (n.content ?? []).map(text).join(" "));66const types = (n: any, out: Record<string, number> = {}): Record<string, number> => { if (n?.type && n.type !== "text") out[n.type] = (out[n.type] ?? 0) + 1; for (const c of n?.content ?? []) types(c, out); return out; };67const waitFor = async <V,>(fn: () => Promise<V | null | undefined | false>, ms = 30_000): Promise<V> => { 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, 300)); } };68const store = (page: Page, name: string) => page.evaluate((n) => new Promise<any[]>((resolve, reject) => { const r = indexedDB.open("note-local"); r.onerror = () => reject(r.error); r.onsuccess = () => { const d = r.result; if (!d.objectStoreNames.contains(n)) { d.close(); return resolve([]); } const g = d.transaction(n, "readonly").objectStore(n).getAll(); g.onsuccess = () => { d.close(); resolve(g.result.map((x: any) => ({ ...x, file: undefined }))); }; g.onerror = () => { d.close(); reject(g.error); }; }; }), name);69async function readyForOffline(page: Page, ids: string[]) {70 await page.goto("/"); const controlled = () => page.evaluate(() => Boolean(navigator.serviceWorker.controller));71 try { await waitFor(controlled, 15_000); } catch { await page.reload(); await waitFor(controlled, 30_000); }72 await waitFor(() => page.evaluate(async () => Boolean(await caches.match("/offline"))));73 await page.evaluate(() => new Promise<void>((resolve) => { const ch = new MessageChannel(); ch.port1.onmessage = () => resolve(); navigator.serviceWorker.controller!.postMessage({ type: "REFRESH_SHELL" }, [ch.port2]); setTimeout(resolve, 15_000); }));74 await page.reload(); await waitFor(async () => { const have = new Set((await store(page, "pages")).map((p) => p.id)); return ids.every((i) => have.has(i)); });75}76const upload = async (page: Page, name: string, mimeType: string, buffer: Buffer, pageId: string) => { const r = await page.request.fetch("/api/attachments", { method: "POST", headers: { origin: ORIGIN }, multipart: { file: { name, mimeType, buffer }, pageId } }); expect(r.status(), await r.text()).toBeLessThan(300); return (await r.json()).id as string; };7778test.afterAll(async () => {79 await sql`delete from tasks where id = ${TASK_ID} or title like ${PFX + "%"}`;80 const ids = (await sql`select id from pages where title like ${PFX + "%"} or id = any(${made}::uuid[])`).map((r) => r.id as string);81 if (ids.length) { await sql`delete from knowledge_edges where from_id = any(${ids}::uuid[]) or to_id = any(${ids}::uuid[])`; await sql`delete from attachments where page_id = any(${ids}::uuid[])`; await sql`delete from knowledge_events where page_id = any(${ids}::uuid[])`; await sql`delete from pages where id = any(${ids}::uuid[])`; }82 if (refs.length) { await sql`delete from knowledge_edges where to_id = any(${refs}::uuid[])`; await sql`delete from "references" where id = any(${refs}::uuid[])`; }83});8485test("one note with every new block lives in the timeline, the graph, health, the brief, Ask, offline, its canvas, and survives Markdown export → import", async ({ page, context, browser }) => {86 test.setTimeout(300_000); await page.goto("/");87 // ---------- the note and what it points to88 const target = await apiCall(page, "POST", "/api/pages", { title: T("target"), content: docOf(para(`Quoted passage ${stamp} about conditional variance.`)) }); made.push(target.body.id);89 const targetBlock = (await server(page, target.body.id)).content.content[0].attrs.id as string; expect(targetBlock).toBeTruthy();90 const canvas = await apiCall(page, "POST", "/api/pages", { title: T("canvas"), type: "canvas" }); expect(canvas.status).toBe(201); made.push(canvas.body.id);91 const ref = await apiCall(page, "POST", "/api/research/references", { title: `${PFX} Generalized autoregressive conditional heteroskedasticity ${stamp}`, authors: ["Bollerslev, Tim"], year: 1986 }); expect(ref.status).toBe(201); refs.push(ref.body.id);92 const shell = await apiCall(page, "POST", "/api/pages", { title: T("every block") }); expect(shell.status).toBe(201); made.push(shell.body.id); const N = shell.body.id as string;93 const png = await upload(page, "preview.png", "image/png", PNG, N); const pdf = await upload(page, "article.pdf", "application/pdf", PDF, N);94 const fixture = JSON.parse(fs.readFileSync("tests/fixtures/next-nodes/all-nodes.json", "utf8"));95 const rewrite = (n: any): any => {96 const a = n.attrs ? { ...n.attrs } : undefined;97 if (a) {98 if (n.type === "blockRef") { a.pageId = target.body.id; a.blockId = targetBlock; } if (n.type === "canvasLink") a.pageId = canvas.body.id; if (n.type === "citation") a.refId = ref.body.id;99 if (n.type === "pdf") a.attachmentId = pdf; if (n.type === "audio") a.attachmentId = png; if (n.type === "mermaid") a.preview = png; if (n.type === "drawing") { a.ink = png; a.preview = png; }100 if (n.type === "queryView") a.filter = { status: "open" }; delete a.id; // ids are given by the server101 }102 return { ...n, ...(a ? { attrs: a } : {}), ...(n.content ? { content: n.content.map(rewrite) } : {}) };103 };104 const body = rewrite(fixture).content as any[];105 const content = docOf(para(`Integration zeta ${stamp}: the opening paragraph of the note.`), ...body,106 { type: "callout", attrs: { kind: "decision", icon: "⚖️" }, content: [para("We keep the GARCH(1,1) specification.")] }, { type: "callout", attrs: { kind: "question", icon: "❓" }, content: [para("Is the persistence stable?")] }, { type: "callout", attrs: { kind: "hypothesis", icon: "🧪" }, content: [para("Volatility clusters.")] },107 { type: "taskList", content: [{ type: "taskItem", attrs: { checked: false, due: DUE, priority: "high", taskId: TASK_ID }, content: [para(`${PFX} task ${stamp}`)] }] }, para("Closing paragraph."));108 const saved = await apiCall(page, "PATCH", `/api/pages/${N}`, { content, baseVersion: shell.body.version }); expect(saved.status, JSON.stringify(saved.body).slice(0, 300)).toBe(200);109 const stored = await server(page, N); const kinds = types(stored.content); const NEW = ["mermaid", "drawing", "chart", "queryView", "citation", "bibliography", "pdf", "blockRef", "runCode", "audio", "canvasLink"];110 for (const k of NEW) expect(kinds[k], `stored ${k}`).toBe(1); expect(kinds.callout).toBe(3); expect(kinds.taskItem).toBe(1);111 const zetaBlock = stored.content.content[0].attrs.id as string;112113 // ---------- editor: every block rendered, not locked, and VIEWING writes nothing114 await page.goto(`/p/${N}`); const ed = page.locator("#main-scroll .note-editor .ProseMirror").first(); await ed.waitFor();115 await expect(page.getByTestId("editor-lock")).toHaveCount(0);116 for (const k of NEW.filter((k) => k !== "citation")) await expect(page.locator(`.nb[data-node='${k}']`).first(), `rendered ${k}`).toBeVisible({ timeout: 20_000 });117 await expect(page.locator(".nb[data-node='unknownBlock']")).toHaveCount(0);118 await page.waitForTimeout(4000); const viewed = await server(page, N);119 const changed = (viewed.content.content as any[]).map((n, i) => (JSON.stringify(n) === JSON.stringify(stored.content.content[i]) ? null : `${n.type}: ${JSON.stringify(stored.content.content[i]).slice(0, 400)} → ${JSON.stringify(n).slice(0, 400)}`)).filter(Boolean);120 expect(changed, "opening the note changed no block").toEqual([]); expect(viewed.version, "opening the note saved nothing").toBe(stored.version);121122 // ---------- journal / timeline, graph, health, brief123 const ev = (await apiCall(page, "GET", `/api/events?pageId=${N}`)).body.events.map((e: any) => e.kind) as string[]; expect(ev).toContain("page.created"); expect(ev).toContain("page.edited");124 await page.goto("/timeline"); await expect(page.getByTestId("timeline")).toContainText(T("every block"), { timeout: 20_000 });125 const edges = await sql`select to_id, kind, target_kind from knowledge_edges where from_id = ${N}`; const edge = (to: string) => edges.find((e) => e.to_id === to);126 expect(edge(target.body.id)?.kind, "blockRef → link edge").toBe("link"); expect(edge(canvas.body.id)?.kind, "canvasLink → link edge").toBe("link"); expect(edge(ref.body.id)?.kind, "citation → cites edge").toBe("cites"); expect(edge(ref.body.id)?.target_kind).toBe("reference");127 const local = (await apiCall(page, "GET", `/api/graph?view=local&pageId=${N}`)).body; const nodeIds = local.nodes.map((n: any) => n.id); expect(local.center).toBe(N); expect(nodeIds).toContain(target.body.id); expect(nodeIds).toContain(canvas.body.id);128 const tm = (await apiCall(page, "GET", `/api/tasks/${TASK_ID}`)).body; expect(tm.pageId, "the taskItem became a task of the page").toBe(N); expect(tm.dueAt, "a bare date of a taskItem is stored at 12:00 UTC").toBe(`${DUE}T12:00:00.000Z`);129 await page.goto(`/graph?pageId=${N}`); await page.getByTestId("graph-canvas").waitFor({ timeout: 30_000 });130 const health = (await apiCall(page, "GET", "/api/health/knowledge")).body; expect(health.orphans.ids, "a note with links is not an orphan").not.toContain(N); expect(health.score.pages).toBeGreaterThanOrEqual(3);131 const brief = (await apiCall(page, "GET", "/api/brief")).body; expect(brief.recentNotes.map((n: any) => n.id)).toContain(N); expect(JSON.stringify(brief.tasks)).toContain(TASK_ID);132 await page.goto("/"); await expect(page.getByTestId("brief")).toContainText(T("every block"));133134 // ---------- Ask, scope "This page": the answer (faked at the HTTP level) cites a block of the note; the chip jumps to it135 const noSw = await browser.newContext({ baseURL: ORIGIN, storageState: "test-results/.auth/state.json", viewport: { width: 1280, height: 800 }, serviceWorkers: "block" }); const ap = await noSw.newPage();136 const sent: any[] = []; const sse = (events: unknown[]) => events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join("") + "data: [DONE]\n\n";137 const src = { n: 1, chunkId: "c1", pageId: N, title: T("every block"), icon: null, heading: null, blockId: zetaBlock, excerpt: "Integration zeta: the opening paragraph of the note.", score: 0.02, semantic: 0.7, sourceType: "page", blockIds: [zetaBlock], anchor: "block", quote: "the opening paragraph of the note" };138 await ap.route("**/api/ai/chat", (route) => { const b = route.request().postDataJSON(); sent.push(b); return route.fulfill({ contentType: "text/event-stream", body: sse([{ type: "meta", conversationId: "00000000-0000-4000-8000-000000000002", clientId: b.clientId, mode: "notes", model: "fake-model", provider: "fake", reason: "test", task: "chat", sources: [src], weak: [], evidence: "ok", sent: { pageChars: 0, excerpts: 1, references: 0, images: 0, history: 0, embeddedBy: null, rankedBy: null }, budgetNote: null, replayed: false, scope: b.scope ?? "all", scopePages: 1, staleDropped: 0 }, { type: "text", delta: "It opens the note [1]." }, { type: "done", messageId: "m1", content: "It opens the note [1].", usage: null, costUsd: 0, latencyMs: 10, ttftMs: 5, sources: [src], evidence: "ok", invalidCitations: [], uncited: false, replayed: false }]) }); });139 await ap.goto(`/p/${N}`); await ap.locator(".note-editor .ProseMirror").first().waitFor(); await ap.getByRole("button", { name: "Ask about this page" }).first().click();140 const panel = ap.getByRole("complementary", { name: "AI assistant" }); await panel.getByTestId("ask-scope").getByRole("radio", { name: "This page" }).click();141 await panel.getByLabel("Message").fill("What does the first paragraph say?"); await panel.getByRole("button", { name: "Send" }).click();142 const chip = panel.getByTestId("cite-chip").first(); await expect(chip).toBeVisible({ timeout: 30_000 }); expect(sent.at(-1)).toMatchObject({ scope: "page", pageId: N }); await expect(chip).toHaveAttribute("data-block", zetaBlock);143 await ap.evaluate(() => { document.getElementById("main-scroll")!.scrollTop = 99999; }); await chip.click(); await expect(ap.locator(`#block-${zetaBlock}`)).toBeInViewport();144 expect((await server(page, N)).version, "asking wrote nothing into the note").toBe(stored.version); await noSw.close();145146 // ---------- offline: the note is edited, a canvas and a database page say they need a connection, then exactly one sync147 const dbPage = await apiCall(page, "POST", "/api/pages", { title: T("database"), type: "database", section: "databases" }); made.push(dbPage.body.id);148 await readyForOffline(page, [N, canvas.body.id, dbPage.body.id]); const before = await server(page, N); const editsBefore = (await apiCall(page, "GET", `/api/events?pageId=${N}&kinds=page.edited`)).body.events.length;149 const canvasBefore = (await apiCall(page, "GET", `/api/canvases/${canvas.body.id}`)).body;150 await context.setOffline(true);151 for (const [id, word] of [[canvas.body.id, "Canvases"], [dbPage.body.id, "Databases"]] as const) {152 await page.goto(`/p/${id}`); await expect(page.getByTestId("offline-app")).toBeVisible(); const op = page.locator(`[data-testid=offline-page][data-page-id="${id}"]`); await expect(op).toBeVisible();153 await expect(op).toContainText(`${word} are not kept on this device`); await expect(op.locator(".ProseMirror")).toHaveCount(0); // never the text editor on a canvas / database page154 }155 await page.goto(`/p/${N}`); await expect(page.getByTestId("offline-app")).toBeVisible(); const off = page.locator("[data-testid=offline-page] .ProseMirror").first(); await expect(off).toContainText(`Integration zeta ${stamp}`);156 await off.locator("> p").first().click(); await page.keyboard.press("End"); await page.keyboard.type(" Edited offline.");157 await waitFor(async () => (await store(page, "drafts")).some((d) => d.pageId === N && text(d.patch.content).includes("Edited offline.")));158 await context.setOffline(false); await expect(page.getByTestId("offline-app")).toBeHidden({ timeout: 60_000 });159 const after = await waitFor(async () => { const s = await server(page, N); return text(s.content).includes("Edited offline.") ? s : null; }, 60_000);160 await page.waitForTimeout(3000); const settled = await server(page, N);161 expect(settled.version, "synced once: one new version").toBe(before.version + 1); expect(after.version).toBe(settled.version);162 expect((await apiCall(page, "GET", `/api/events?pageId=${N}&kinds=page.edited`)).body.events.length - editsBefore, "at most one journal entry for the offline edit (edits are coalesced)").toBeLessThanOrEqual(1);163 expect(types(settled.content), "every block survived the offline editor").toEqual(types(before.content));164 const canvasAfter = (await apiCall(page, "GET", `/api/canvases/${canvas.body.id}`)).body; expect(canvasAfter.version, "the canvas was not touched by being opened offline").toBe(canvasBefore.version); expect((await server(page, canvas.body.id)).type).toBe("canvas");165166 // ---------- its canvas opens from the canvasLink block167 await page.goto(`/p/${N}`); await ed.waitFor(); await page.locator(".nb[data-node='canvasLink']").first().getByRole("link").first().click().catch(async () => { await page.locator(".nb[data-node='canvasLink']").first().dblclick(); });168 await page.waitForURL(new RegExp(`/p/${canvas.body.id}`), { timeout: 20_000 }); await expect(page.locator("[data-testid='canvas-surface']")).toBeVisible({ timeout: 20_000 });169170 // ---------- Markdown: the documented forms, then re-import171 const exp = await page.request.get(`/api/export?format=md&pageId=${N}`); expect(exp.status()).toBe(200); const md = await exp.text();172 for (const form of ["```mermaid", `[[${target.body.id}#${targetBlock}]]`, `[Canvas](/p/${canvas.body.id})`, "```javascript", "```output", "[PDF: article.pdf]", "[Audio: Réunion de laboratoire]", "| Trimestre | Ventes | Marge |", "[Vue: Tâches ouvertes]", "[@", `Integration zeta ${stamp}`, "Edited offline."]) expect(md, `Markdown form ${form}`).toContain(form);173 const imp = await page.request.fetch("/api/import", { method: "POST", headers: { origin: ORIGIN }, multipart: { file: { name: `${PFX} reimport ${stamp}.md`, mimeType: "text/markdown", buffer: Buffer.from(md, "utf8") }, sync: "1" } });174 expect(imp.status(), await imp.text()).toBeLessThan(300); const impBody = await imp.json(); expect(impBody.pageId).toBeTruthy(); made.push(impBody.pageId);175 if (impBody.jobId) await apiCall(page, "DELETE", `/api/import?jobId=${impBody.jobId}`); await sql`delete from settings where key like 'import.%' and value->>'filename' like ${PFX + "%"}`; // the job record of this import176 const back = await server(page, impBody.pageId); const bt = types(back.content); const backText = text(back.content);177 // what the documented Markdown forms carry back: structural blocks as blocks, the rest as readable text (never lost)178 for (const k of ["mermaid", "runCode", "blockRef", "canvasLink", "table"]) expect(bt[k] ?? 0, `re-imported as a ${k} block (${JSON.stringify(bt)})`).toBeGreaterThanOrEqual(1);179 for (const s of [`Integration zeta ${stamp}`, "Edited offline.", "Ventes par trimestre", "Closing paragraph.", "We keep the GARCH(1,1) specification.", "Bonjour à tous."]) expect(backText, `text kept: ${s}`).toContain(s);180 expect(JSON.stringify(back.content)).toContain(targetBlock);181});182