SPB Git

Repository Public

shots-k.mjs 15.6 KB 125 lines javascript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/e2e/next/shots-k.mjs9 * 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// file: tests/e2e/next/shots-k.mjs44// description: screenshots (light + dark, desktop + phone) of the knowledge screens — Graph 2.0, Thought Constellation, Timeline,45//   Time Machine, Knowledge Health, Daily Brief, Semantic Zoom, Ask My Knowledge — on a small demo workspace created through the API46//   (pages, links, typed relations, a concept, tasks, a capture). The two rows a model would normally produce (one suggested47//   relation, one contradiction insight) are inserted as fixtures and carry the model name "fixture".48// author: Simon-Pierre Boucher — contact@spboucher.ai49// usage: set -a; . ./.env; set +a; E2E_BASE_URL=http://localhost:8487 node tests/e2e/next/shots-k.mjs <outDir> <sessionTokenFile> [--only name,name]50import { chromium } from "@playwright/test"; import fs from "node:fs"; import postgres from "postgres";51const base = process.env.E2E_BASE_URL ?? "http://localhost:8487"; const out = process.argv[2] ?? "test-results/shots-k"; const token = fs.readFileSync(process.argv[3], "utf8").trim(); fs.mkdirSync(out, { recursive: true });52const only = process.argv.includes("--only") ? process.argv[process.argv.indexOf("--only") + 1].split(",") : null;53if (!/(_dev|_test|_scratch)$/.test((process.env.DATABASE_URL ?? "").split("/").pop() ?? "")) throw new Error("scratch database only");54const sql = postgres(process.env.DATABASE_URL, { max: 1, onnotice: () => {} });55const T = (text, marks) => ({ type: "text", text, ...(marks ? { marks } : {}) }); const P = (...c) => ({ type: "paragraph", content: c.map((x) => (typeof x === "string" ? T(x) : x)) }); const H = (level, text) => ({ type: "heading", attrs: { level }, content: [T(text)] });56const M = (id, label) => ({ type: "mention", attrs: { id, label } });57const auth = { extraHTTPHeaders: { cookie: `__Host-note_session=${token}` } }; // a __Host- cookie cannot be planted on http://localhost through CDP: the header is sent instead58const b = await chromium.launch();59const seedCtx = await b.newContext({ baseURL: base, ...auth }); const rq = seedCtx.request;60const call = async (method, url, data) => { const r = await rq.fetch(url, { method, data, headers: { origin: base, ...(data ? { "content-type": "application/json" } : {}) } }); const j = await r.json().catch(() => null); if (!r.ok()) throw new Error(`${method} ${url} → ${r.status()} ${JSON.stringify(j)}`); return j; };61const tree = await call("GET", "/api/tree"); let demo = Object.fromEntries(tree.filter((n) => n.title.startsWith("Demo · ")).map((n) => [n.title.slice(7), n.id]));62if (!demo["Mortgage renewal"]) {63  const mk = async (title, content, extra = {}) => (await call("POST", "/api/pages", { title: `Demo · ${title}`, content: { type: "doc", content }, ...extra })).id;64  const project = await mk("Mortgage renewal", [H(1, "Goal"), P("Renew the mortgage before March with the lowest total cost over five years."), H(2, "Open points"), P("Fixed or variable rate? The bank offer expires in 90 days.")], { section: "projects", icon: "🏦" });65  const offer = await mk("Bank offer", [H(1, "Conditions"), P("The five-year fixed rate offered is 4.29 %, guaranteed for 90 days."), P("The prepayment penalty equals three months of interest."), H(2, "Amortisation"), P("Twenty-two years remain on the amortisation schedule."), H(2, "Decision"), { type: "callout", attrs: { kind: "decision", icon: "⚖️" }, content: [P("Ask the broker for a competing quote before signing.")] }], { parentId: project, section: "projects", icon: "📄" });66  const broker = await mk("Broker quote", [H(1, "Quote"), P("The broker found a five-year fixed rate of 4.04 % with another lender."), P("The prepayment penalty is the greater of three months of interest and the interest rate differential."), P("See ", M(offer, "Demo · Bank offer"), " for the bank's conditions.")], { parentId: project, section: "projects", icon: "🤝" });67  const rates = await mk("Rate outlook", [H(1, "Outlook"), P("Most forecasts expect the policy rate to fall by 50 basis points within a year."), P("A variable rate would then become cheaper than the fixed offers in ", M(offer, "Demo · Bank offer"), " and ", M(broker, "Demo · Broker quote"), "."), H(2, "Risk"), P("If inflation returns, the variable rate rises and the monthly payment with it.")], { section: "notes", icon: "📈" });68  const budget = await mk("Household budget", [H(1, "Monthly"), P("The mortgage payment must stay under 2 100 $ a month."), P("Related: ", M(project, "Demo · Mortgage renewal"), ".")], { section: "notes", icon: "🧮" });69  const reading = await mk("Reading notes — fixed vs variable", [H(1, "Summary"), P("Historically the variable rate has cost less than the fixed rate about two times out of three."), P("The study does not cover periods of rapidly rising rates.")], { section: "documents", icon: "📚" });70  const lonely = await mk("Garden ideas", [P("Plant tomatoes along the south wall; nothing links here yet.")], { section: "notes", icon: "🌱" });71  await mk("Reading notes — fixed vs variable", [P("Second copy of the reading notes, to be merged.")], { section: "notes" });72  await call("POST", "/api/pages", { title: "", content: { type: "doc", content: [P("An untitled thought about renewal dates.")] } });73  await call("POST", "/api/graph/relations", { fromId: reading, toId: rates, relType: "supports", note: "The historical study backs the outlook." });74  await call("POST", "/api/graph/relations", { fromId: broker, toId: offer, relType: "contradicts", note: "Different penalty formula." });75  await call("POST", "/api/graph/relations", { fromId: budget, toId: project, relType: "part_of" });76  await call("POST", "/api/graph/relations", { fromId: rates, toId: project, relType: "answers", validFrom: "2026-10-01T00:00:00.000Z" });77  await call("POST", "/api/concepts", { name: "Demo · Prepayment penalty", description: "What it costs to break the mortgage early.", pageIds: [offer, broker] });78  const day = 86_400_000; const due = (d) => new Date(Date.now() + d * day).toISOString();79  await call("POST", "/api/tasks", { title: "Demo · Call the broker back", pageId: broker, dueAt: due(-2), priority: "high" }); await call("POST", "/api/tasks", { title: "Demo · Send pay stubs to the bank", pageId: offer, dueAt: due(0) });80  await call("POST", "/api/tasks", { title: "Demo · Compare the two penalties", pageId: project, dueAt: due(3) }); await call("POST", "/api/tasks", { title: "Demo · Read the lender's fine print", pageId: null });81  await call("POST", "/api/capture", { kind: "note", text: "Demo · Ask about portability of the mortgage" }).catch((e) => console.log("capture:", e.message));82await rq.fetch(`/api/pages/${offer}/tags`, { method: "PUT", data: { tags: ["finance", "mortgage"] }, headers: { origin: base, "content-type": "application/json" } }); await rq.fetch(`/api/pages/${broker}/tags`, { method: "PUT", data: { tags: ["finance", "mortgage"] }, headers: { origin: base, "content-type": "application/json" } }); await rq.fetch(`/api/pages/${rates}/tags`, { method: "PUT", data: { tags: ["finance"] }, headers: { origin: base, "content-type": "application/json" } });83  // a second saved state for the time machine: a history entry dated two days ago, then an edit today84  const cur = await call("GET", `/api/pages/${offer}`);85  await call("PATCH", `/api/pages/${offer}`, { content: cur.content, title: cur.title, baseVersion: cur.version }).catch(() => {});86  await sql`update page_versions set created_at = now() - interval '2 days', title = 'Demo · Bank offer (draft)', content = ${sql.json({ type: "doc", content: [H(1, "Conditions"), P("The five-year fixed rate offered is 4.49 %."), P("Penalty to be confirmed.")] })} where page_id = ${offer} and version = (select min(version) from page_versions where page_id = ${offer} and version > 0)`;87  await sql`update pages set created_at = now() - interval '5 days' where id = ${offer}`; await sql`update knowledge_events set at = now() - interval '5 days' where page_id = ${offer} and kind = 'page.created'`; void cur;88  // fixtures standing for model output (never produced by this script): one suggested relation, one contradiction insight89  const [ws] = await sql`select workspace_id from pages where id = ${offer}`;90  const blocks = (await call("GET", `/api/pages/${offer}/zoom?level=2`)).sections; const bblocks = (await call("GET", `/api/pages/${broker}/zoom?level=2`)).sections;91  await sql`insert into knowledge_edges (workspace_id, from_id, to_id, kind, rel_type, origin, status, confidence, rationale, model) values (${ws.workspace_id}, ${budget}, ${rates}, 'relation', 'extends', 'ai', 'suggested', 0.74, 'The budget ceiling depends on the rate scenario described in the outlook.', 'fixture') on conflict do nothing`;92  await sql`insert into knowledge_insights (workspace_id, kind, status, title, detail, data, origin, model, fingerprint) values (${ws.workspace_id}, 'contradiction', 'suggested', 'Demo · Two different prepayment penalties', 'One note says the penalty is three months of interest; the other says it is the greater of three months and the rate differential.', ${sql.json({ pageIds: [offer, broker], blockIds: [blocks[0]?.blockId, bblocks[0]?.blockId].filter(Boolean), a: { pageId: offer, blockId: blocks[0]?.blockId ?? null }, b: { pageId: broker, blockId: bblocks[0]?.blockId ?? null }, confidence: 0.8 })}, 'ai', 'fixture', ${"demo-" + offer}) on conflict do nothing`;93  await sql`insert into knowledge_insights (workspace_id, kind, status, title, detail, data, origin, model, fingerprint) values (${ws.workspace_id}, 'open_question', 'suggested', 'Demo · Is the mortgage portable to another property?', 'The notes mention a move as a possibility but never say whether the loan can follow.', ${sql.json({ pageIds: [project], blockIds: [], source: { pageId: project, blockId: null } })}, 'ai', 'fixture', ${"demo-q-" + project}) on conflict do nothing`;94  demo = { "Mortgage renewal": project, "Bank offer": offer, "Broker quote": broker, "Rate outlook": rates, "Household budget": budget, "Garden ideas": lonely };95  console.log("demo workspace created");96}97await seedCtx.close(); await sql.end();98const offer = demo["Bank offer"], project = demo["Mortgage renewal"];99const SHOTS = [100  ["brief", "/", async (p) => { await p.getByTestId("brief-tasks").waitFor(); }],101  ["graph-global", "/graph", async (p) => { await p.getByTestId("graph-canvas").waitFor(); await p.waitForTimeout(2500); }],102  ["graph-local-inspector", `/graph?view=local&pageId=${offer}&panel=suggested`, async (p) => { await p.getByTestId("graph-canvas").waitFor(); await p.waitForTimeout(2500); }],103  ["graph-insights", "/graph?view=concepts&panel=insights", async (p) => { await p.getByTestId("insights-panel").waitFor(); await p.waitForTimeout(2500); }],104  ["constellation", "/constellation", async (p) => { await p.locator("[data-testid=constellation],[data-testid=constellation-empty]").first().waitFor(); await p.waitForTimeout(2200); const c = p.getByTestId("constellation-canvas"); if (await c.count()) { const bb = await c.boundingBox(); for (let i = 0; i < 4; i++) { await p.mouse.move(bb.x + bb.width / 2, bb.y + bb.height / 2); await p.mouse.wheel(0, -260); } await p.waitForTimeout(500); await c.focus(); await p.keyboard.press("ArrowRight"); await p.waitForTimeout(600); } }],105  ["timeline", "/timeline", async (p) => { await p.getByTestId("event-list").waitFor(); }],106  ["timemachine", `/timemachine?pageId=${offer}&at=${encodeURIComponent(new Date(Date.now() - 86_400_000).toISOString())}`, async (p) => { await p.getByTestId("tm-honesty").waitFor(); await p.getByTestId("tm-diff-tab").click(); await p.getByTestId("tm-diff").waitFor(); }],107  ["health", "/health?show=brokenLinks", async (p) => { await p.getByTestId("health-score").waitFor(); await p.getByTestId("health-orphans").click(); await p.getByTestId("health-detail").waitFor(); }],108  ["zoom-outline", `/p/${offer}`, async (p) => { await p.getByTestId("zoom-2").click(); await p.getByTestId("zoom-outline").waitFor(); }],109  ["zoom-synopsis", `/p/${offer}`, async (p) => { await p.getByTestId("zoom-1").click(); await p.getByTestId("zoom-synopsis").waitFor(); await p.waitForTimeout(600); }],110  ["zoom-connections", `/p/${offer}`, async (p) => { await p.getByTestId("zoom-4").click(); await p.getByTestId("zoom-connections").waitFor(); }],111  ["ask", "/ai", async (p) => { await p.getByTestId("ask-page").waitFor(); await p.getByTestId("starter-questions").waitFor(); }],112].filter((s) => !only || only.includes(s[0]));113const problems = [];114for (const theme of ["light", "dark"]) for (const [dev, w, h] of [["desktop", 1280, 800], ["phone", 375, 667]]) {115  const ctx = await b.newContext({ baseURL: base, viewport: { width: w, height: h }, hasTouch: dev === "phone", isMobile: dev === "phone", deviceScaleFactor: 2, colorScheme: theme, locale: "en-CA", timezoneId: "America/Toronto", ...auth });116  await ctx.addInitScript((t) => { try { localStorage.setItem("note-theme", t); const u = JSON.parse(localStorage.getItem("note-ui") ?? '{"state":{},"version":0}'); u.state.theme = t; localStorage.setItem("note-ui", JSON.stringify(u)); } catch {} }, theme);117  const p = await ctx.newPage(); p.on("pageerror", (e) => problems.push(`${dev}-${theme}: pageerror at ${p.url()} ${e.message.slice(0, 120)}`)); p.on("console", (m) => { if (m.type() === "error" && !/favicon|Failed to load resource/.test(m.text())) problems.push(`${dev}-${theme}: console ${m.text().slice(0, 200)}`); });118  for (const [name, url, ready] of SHOTS) {119    try { await p.goto(url); await ready(p); await p.waitForTimeout(700); const over = await p.evaluate(() => { const d = document.documentElement; const m = document.getElementById("main-scroll"); return d.scrollWidth > d.clientWidth + 1 || (m ? m.scrollWidth > m.clientWidth + 1 : false); }); if (over) problems.push(`${dev}-${theme}-${name}: horizontal overflow`); await p.screenshot({ path: `${out}/${name}-${dev}-${theme}.png` }); }120    catch (e) { problems.push(`${dev}-${theme}-${name}: ${String(e.message).split("\n")[0].slice(0, 200)}`); await p.screenshot({ path: `${out}/${name}-${dev}-${theme}-FAILED.png` }).catch(() => {}); }121  }122  await ctx.close();123}124await b.close(); console.log(problems.length ? `PROBLEMS:\n${[...new Set(problems)].join("\n")}` : "no problem detected"); console.log(`shots in ${out}`);125
Files main
Menu

Theme