Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/unit/next-graph.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 * Knowledge graph 2.0 (SPEC §3.3, §4.3): typed relations (user ones accepted, model ones never auto-accepted), graph views, concepts,45 * deterministic intelligence (PageRank, orphans, clusters, recurring topics, duplicates) and the `graph-insights` job run with an46 * injected fake model — no provider is called. Runs against a *_dev database.47 */48import { test, before, after } from "node:test";49import assert from "node:assert/strict";50import { randomUUID } from "node:crypto";51import { and, eq } from "drizzle-orm";52import { db, schema } from "../../src/db/index";53import { workspace, mk, doc, made, cleanup, code, kinds, eventsOf, para } from "../helpers/next";54import { createRelation, updateRelation, deleteRelation, listRelations, getRelation, REL_TYPES } from "../../src/lib/graph/relations";55import { decideSuggestion } from "../../src/lib/graph/service";56import { buildGraphView, GRAPH_VIEWS } from "../../src/lib/graph/views";57import { buildGraph } from "../../src/lib/graph/service";58import { pureGraphMetrics, computeIntelligence } from "../../src/lib/graph/intelligence";59import { createConcept, updateConcept, getConcept, listConcepts, deleteConcept } from "../../src/lib/graph/concepts";60import { runGraphInsights, refreshDeterministicInsights, listInsights, decideInsight, INSIGHT_SCHEMA, type InsightModel } from "../../src/lib/graph/insights";61import { createReference } from "../../src/lib/research/references";62import { setPageTags } from "../../src/lib/inbox/service";63import { getPage, updatePage } from "../../src/lib/pages/service";64import { QUEUES, QUEUE_POLICY } from "../../src/lib/jobs/queues";65import { handleGraphInsights } from "../../src/worker/handlers/graph-insights";66import type { PMNode } from "../../src/lib/editor/text";6768let ws: string; const PFX = "nx-graph ·";69before(async () => { ws = await workspace(); }); after(async () => { made.tags.push("nx-graph-topic"); await cleanup(ws, PFX); });70const long = (s: string) => doc(`${s} ` + "mot ".repeat(25));7172test("pure metrics: PageRank, degrees, components and communities on fixed graphs", () => {73 const ring = pureGraphMetrics(["a", "b", "c", "d"], [["a", "b"], ["b", "c"], ["c", "d"], ["d", "a"]]);74 for (const id of ["a", "b", "c", "d"]) assert.ok(Math.abs(ring.pagerank.get(id)! - 0.25) < 1e-9, "a symmetric ring ranks everyone equally");75 const star = pureGraphMetrics(["hub", "x", "y", "z", "lonely"], [["x", "hub"], ["y", "hub"], ["z", "hub"], ["x", "hub"], ["hub", "hub"]]);76 assert.equal(star.degree.get("hub"), 3, "duplicates and self-loops ignored"); assert.equal(star.in.get("hub"), 3); assert.equal(star.out.get("x"), 1); assert.equal(star.degree.get("lonely"), 0);77 const sum = [...star.pagerank.values()].reduce((a, b) => a + b, 0); assert.ok(Math.abs(sum - 1) < 1e-9, "scores sum to 1"); assert.ok(star.pagerank.get("hub")! > star.pagerank.get("x")! * 2);78 assert.ok(Math.abs(star.pagerank.get("x")! - star.pagerank.get("lonely")!) < 1e-9, "a node nobody links to has the base score");79 assert.deepEqual(star.components, [["hub", "x", "y", "z"], ["lonely"]]); assert.deepEqual(star.communities, [["hub", "x", "y", "z"]], "a singleton is not a community");80 // two cliques of 8 joined by one bridge: one component, two communities81 const A = Array.from({ length: 8 }, (_, i) => `a${i}`), B = Array.from({ length: 8 }, (_, i) => `b${i}`); const edges: [string, string][] = [];82 for (const g of [A, B]) for (let i = 0; i < g.length; i++) for (let j = i + 1; j < g.length; j++) edges.push([g[i], g[j]]); edges.push(["a0", "b0"]);83 const two = pureGraphMetrics([...A, ...B], edges); assert.equal(two.components.length, 1); assert.deepEqual(two.communities, [A, B]);84 assert.deepEqual(pureGraphMetrics([...B, ...A].reverse(), [...edges].reverse()).communities, [A, B], "deterministic whatever the input order");85 assert.deepEqual(pureGraphMetrics([], []).components, []);86});87test("a user relation is typed, accepted at once, journaled, unique per (from, to, type) and idempotent on its id", async () => {88 const a = await mk(ws, `${PFX} A`, doc("a")); const b = await mk(ws, `${PFX} B`, doc("b")); const id = randomUUID();89 const r = await createRelation(ws, { id, fromId: a.id, toId: b.id, relType: "supports", note: "parce que", sourceBlockId: "blkA", validFrom: new Date("2026-01-01T00:00:00Z") });90 assert.equal(r.existing, false); assert.deepEqual([r.relation.id, r.relation.kind, r.relation.relType, r.relation.origin, r.relation.status, r.relation.sourceKind, r.relation.targetKind], [id, "relation", "supports", "user", "accepted", "page", "page"]);91 assert.ok(r.relation.decidedAt); assert.equal(r.relation.fromTitle, `${PFX} A`); assert.equal(r.relation.toTitle, `${PFX} B`); assert.equal(r.relation.sourceBlockId, "blkA");92 assert.equal((await createRelation(ws, { id, fromId: a.id, toId: b.id, relType: "contradicts" })).existing, true, "same id: the stored relation, untouched");93 assert.equal((await getRelation(ws, id)).relType, "supports");94 assert.equal(await code(createRelation(ws, { fromId: a.id, toId: b.id, relType: "supports" })), "relation_exists");95 const other = await createRelation(ws, { fromId: a.id, toId: b.id, relType: "extends" }); assert.equal(other.existing, false, "another type between the same two pages is another relation");96 assert.equal(await code(createRelation(ws, { fromId: a.id, toId: a.id, relType: "related" })), "bad_node"); assert.equal(await code(createRelation(ws, { fromId: a.id, toId: randomUUID(), relType: "related" })), "bad_node");97 assert.equal(await code(createRelation(ws, { fromId: a.id, toId: b.id, relType: "follows", validFrom: new Date("2026-02-01"), validTo: new Date("2026-01-01") })), "bad_request");98 assert.equal(await code(updateRelation(ws, other.relation.id, { relType: "supports" })), "relation_exists");99 const edited = await updateRelation(ws, id, { note: null, validTo: new Date("2026-12-31T00:00:00Z"), relType: "part_of" }); assert.equal(edited.note, null); assert.equal(edited.relType, "part_of"); assert.ok(edited.rev > r.relation.rev);100 assert.deepEqual((await listRelations(ws, { nodeId: a.id })).map((x) => x.relType).sort(), ["extends", "part_of"]); assert.equal((await listRelations(ws, { nodeId: a.id, relType: "extends" })).length, 1);101 await deleteRelation(ws, id); assert.equal(await code(getRelation(ws, id)), "not_found"); assert.equal(await deleteRelation(ws, id), false, "idempotent: nothing left to delete");102 assert.deepEqual((await kinds(a.id)).filter((k) => k.startsWith("relation.")), ["relation.created", "relation.created", "relation.removed"]);103 assert.equal(REL_TYPES.length, 12);104});105test("nothing created by a model is accepted without a user decision", async () => {106 const a = await mk(ws, `${PFX} M1`, doc("a")); const b = await mk(ws, `${PFX} M2`, doc("b"));107 const s = await createRelation(ws, { fromId: a.id, toId: b.id, relType: "contradicts" }, { origin: "ai", model: "fake-1", confidence: 0.7, rationale: "x dit le contraire de y" });108 assert.deepEqual([s.relation.origin, s.relation.status, s.relation.decidedAt, s.relation.model], ["ai", "suggested", null, "fake-1"]); assert.equal((await kinds(a.id)).filter((k) => k.startsWith("relation.")).length, 0, "a suggestion is not an event");109 assert.equal((await createRelation(ws, { fromId: a.id, toId: b.id, relType: "contradicts" }, { origin: "ai", model: "fake-2" })).existing, true, "a model never re-proposes a known pair");110 const g = await buildGraphView(ws, "local", { pageId: a.id, suggestions: false }); assert.equal(g.edges.some((e) => e.id === s.relation.id), false, "hidden when suggestions are off");111 const acc = await updateRelation(ws, s.relation.id, { status: "accepted" }); assert.equal(acc.status, "accepted"); assert.ok(acc.decidedAt); assert.equal(acc.origin, "ai", "the origin is kept: provenance is never lost");112 const back = await updateRelation(ws, s.relation.id, { status: "suggested" }); assert.equal(back.decidedAt, null); const rej = await updateRelation(ws, s.relation.id, { status: "rejected" }); assert.ok(rej.decidedAt);113 assert.deepEqual((await kinds(a.id)).filter((k) => k.startsWith("relation.")), ["relation.accepted", "relation.rejected"]);114 assert.equal((await createRelation(ws, { fromId: a.id, toId: b.id, relType: "contradicts" }, { origin: "ai" })).relation.status, "rejected", "a rejected suggestion stays rejected");115});116test("edges derived from a document are read-only; only a link suggestion can be decided as one", async () => {117 const t = await mk(ws, `${PFX} target`, doc("t")); const p = await mk(ws, `${PFX} source`, doc({ type: "paragraph", content: [{ type: "mention", attrs: { id: t.id, label: "t" } }] }));118 const K = schema.knowledgeEdges; const [link] = await db.select().from(K).where(and(eq(K.fromId, p.id), eq(K.kind, "link"))); assert.ok(link);119 assert.equal(await code(updateRelation(ws, link.id, { status: "rejected" })), "derived_edge"); assert.equal(await code(deleteRelation(ws, link.id)), "derived_edge");120 assert.equal(await code(decideSuggestion(ws, link.id, "rejected")), "status-404", "D-12: a link mirror edge is not a suggestion");121 assert.equal((await db.select().from(K).where(eq(K.id, link.id)))[0].status, "accepted");122 const [sug] = await db.insert(K).values({ workspaceId: ws, fromId: t.id, toId: p.id, kind: "ai_suggested", origin: "ai", status: "suggested", model: "m", confidence: 0.8, rationale: `${PFX} why` }).returning();123 assert.equal(await code(updateRelation(ws, sug.id, { note: "x" })), "derived_edge");124 assert.equal((await updateRelation(ws, sug.id, { status: "accepted" })).status, "accepted"); assert.equal((await db.select().from(schema.links).where(and(eq(schema.links.fromPageId, t.id), eq(schema.links.kind, "ai")))).length, 1, "2.x behaviour kept: the accepted suggestion is a backlink");125 assert.ok((await kinds(t.id)).includes("relation.accepted")); assert.equal(await code(deleteRelation(ws, sug.id)), "derived_edge");126});127test("graph views: global, local, project, timeline, concepts, documents, references", async () => {128 assert.deepEqual([...GRAPH_VIEWS], ["global", "local", "project", "timeline", "concepts", "documents", "references"]);129 const root = await mk(ws, `${PFX} V project`, undefined, { section: "projects" }); const kid = await mk(ws, `${PFX} V kid`, doc("k"), { parentId: root.id, section: "projects" });130 const ext = await mk(ws, `${PFX} V external`, doc("e")); const far = await mk(ws, `${PFX} V far`, doc("f")); const docPage = await mk(ws, `${PFX} V document`, doc("d"), { section: "documents" }); const cv = await mk(ws, `${PFX} V canvas`, undefined, { type: "canvas" });131 const ref = await createReference(ws, { title: `${PFX} V ref`, year: 2021 }); made.references.push(ref.id);132 const { concept } = await createConcept(ws, { name: `${PFX} V concept`, pageIds: [kid.id, ext.id] }); made.concepts.push(concept.id);133 const { relation } = await createRelation(ws, { fromId: kid.id, toId: ext.id, relType: "supports" }); await createRelation(ws, { fromId: ext.id, toId: far.id, relType: "precedes" });134 await createRelation(ws, { fromId: kid.id, toId: ref.id, targetKind: "reference", relType: "cites" }); await createRelation(ws, { fromId: concept.id, sourceKind: "concept", toId: ref.id, targetKind: "reference", relType: "about" });135 await updatePage(ws, docPage.id, { content: doc({ type: "paragraph", content: [{ type: "citation", attrs: { refId: ref.id, label: "R" } }] }, { type: "canvasLink", attrs: { pageId: cv.id } }) });136 const ids = (g: { nodes: { id: string }[] }) => new Set(g.nodes.map((n) => n.id));137 const global = await buildGraphView(ws, "global"); for (const id of [root.id, kid.id, ext.id, far.id, docPage.id, cv.id, `ref:${ref.id}`, `concept:${concept.id}`]) assert.ok(ids(global).has(id), `global has ${id}`);138 assert.equal(global.nodes.find((n) => n.id === cv.id)!.kind, "canvas"); assert.equal(global.nodes.find((n) => n.id === `ref:${ref.id}`)!.entityId, ref.id);139 const typed = global.edges.find((e) => e.id === relation.id)!; assert.deepEqual([typed.kind, typed.relType, typed.origin, typed.status, typed.source, typed.target], ["typed", "supports", "user", "accepted", kid.id, ext.id]);140 assert.ok(global.edges.some((e) => e.kind === "cites" && e.source === docPage.id && e.target === `ref:${ref.id}` && e.sourceBlockId), "a citation is an edge anchored to its block");141 assert.ok(global.edges.some((e) => e.kind === "link" && e.source === docPage.id && e.target === cv.id), "canvasLink is a link edge");142 assert.ok(global.edges.some((e) => e.kind === "about" && e.source === kid.id && e.target === `concept:${concept.id}`)); assert.ok(global.edges.some((e) => e.source === `concept:${concept.id}` && e.target === `ref:${ref.id}`));143 const node = global.nodes.find((n) => n.id === kid.id)!; assert.equal(node.degree, node.in + node.out); assert.ok(node.degree >= 3); assert.ok(global.stats.references >= 1 && global.stats.concepts >= 1);144 const local1 = await buildGraphView(ws, "local", { pageId: kid.id, depth: 1 }); assert.equal(local1.center, kid.id); assert.ok(ids(local1).has(ext.id)); assert.equal(ids(local1).has(far.id), false);145 const local2 = await buildGraphView(ws, "local", { pageId: kid.id, depth: 2 }); assert.ok(ids(local2).has(far.id), "depth 2 reaches one hop further");146 assert.equal(await code(buildGraphView(ws, "local", {})), "bad_request"); assert.equal(await code(buildGraphView(ws, "local", { pageId: randomUUID() })), "not_found");147 const project = await buildGraphView(ws, "project", { pageId: root.id }); assert.ok(ids(project).has(root.id) && ids(project).has(kid.id)); assert.equal(project.nodes.find((n) => n.id === ext.id)?.external, true, "a neighbour outside the project is kept, flagged external"); assert.equal(ids(project).has(far.id), false); assert.equal(project.nodes.find((n) => n.id === kid.id)?.external, undefined);148 const tl = await buildGraphView(ws, "timeline", { from: new Date(Date.now() - 3600_000), to: new Date(Date.now() + 60_000) }); assert.ok(ids(tl).has(kid.id)); assert.ok(tl.range && tl.buckets && tl.buckets.length >= 1); assert.ok(tl.buckets!.reduce((a, b) => a + b.created, 0) >= 6); assert.ok(tl.edges.some((e) => e.id === relation.id));149 const old = await buildGraphView(ws, "timeline", { from: new Date("2020-01-01"), to: new Date("2020-02-01") }); assert.equal(old.nodes.length, 0); assert.equal(await code(buildGraphView(ws, "timeline", { from: new Date("2026-02-01"), to: new Date("2026-01-01") })), "bad_request");150 const cg = await buildGraphView(ws, "concepts"); assert.ok(ids(cg).has(`concept:${concept.id}`) && ids(cg).has(kid.id) && ids(cg).has(ext.id)); assert.equal(ids(cg).has(far.id), false); assert.ok(cg.edges.every((e) => e.kind === "about" || e.source.startsWith("concept:") || e.target.startsWith("concept:")));151 const dg = await buildGraphView(ws, "documents"); assert.ok(ids(dg).has(docPage.id) && ids(dg).has(`ref:${ref.id}`)); assert.equal(ids(dg).has(kid.id), false); assert.ok(dg.nodes.filter((n) => n.kind === "page").every((n) => n.section === "documents"));152 const rg = await buildGraphView(ws, "references"); assert.ok(ids(rg).has(`ref:${ref.id}`) && ids(rg).has(kid.id) && ids(rg).has(docPage.id)); assert.ok(rg.edges.every((e) => e.source.startsWith("ref:") || e.target.startsWith("ref:")));153 const hidden = await buildGraphView(ws, "references", { hideOrphans: true }); assert.ok(hidden.nodes.every((n) => n.degree > 0));154 const legacy = await buildGraph(ws, {}); assert.deepEqual(Object.keys(legacy), ["nodes", "edges", "center", "stats"], "the 2.x answer of /api/graph keeps its shape"); assert.deepEqual(Object.keys(legacy.stats), ["pages", "edges", "suggested"]);155});156test("concepts: create, link pages, rename, decide, delete", async () => {157 const a = await mk(ws, `${PFX} C1`, doc("a")); const b = await mk(ws, `${PFX} C2`, doc("b")); const id = randomUUID(); made.concepts.push(id);158 const c = await createConcept(ws, { id, name: ` ${PFX} volatilité `, description: "d", pageIds: [a.id] }); assert.equal(c.concept.name, `${PFX} volatilité`); assert.deepEqual([c.concept.origin, c.concept.status], ["user", "accepted"]);159 assert.equal((await createConcept(ws, { id, name: "autre" })).existing, true); assert.equal(await code(createConcept(ws, { name: `${PFX} VOLATILITÉ` })), "concept_exists"); assert.equal(await code(createConcept(ws, { name: " " })), "bad_request");160 assert.equal(await code(createConcept(ws, { name: `${PFX} bad pages`, pageIds: [randomUUID()] })), "bad_page");161 let g = await getConcept(ws, id); assert.equal(g.pages, 1); assert.deepEqual(g.pageLinks.map((l) => [l.pageId, l.origin, l.status]), [[a.id, "user", "accepted"]]);162 g = await updateConcept(ws, id, { addPages: [b.id], description: null }); assert.equal(g.pages, 2); assert.equal(g.description, null); assert.ok(g.rev > c.concept.rev, "linking a page moves the concept in the feed");163 g = await updateConcept(ws, id, { removePages: [a.id], name: `${PFX} variance` }); assert.equal(g.pages, 1); assert.equal(g.name, `${PFX} variance`);164 assert.ok((await listConcepts(ws, { pageId: b.id })).some((x) => x.id === id)); assert.equal((await listConcepts(ws, { pageId: a.id })).some((x) => x.id === id), false);165 const ai = await createConcept(ws, { name: `${PFX} suggéré` }, { origin: "ai", model: "fake" }); made.concepts.push(ai.concept.id); assert.equal(ai.concept.status, "suggested"); assert.equal((await listConcepts(ws)).some((x) => x.id === ai.concept.id), false, "a suggested concept is not listed with the accepted ones"); assert.ok((await listConcepts(ws, { status: "suggested" })).some((x) => x.id === ai.concept.id));166 assert.equal((await updateConcept(ws, ai.concept.id, { status: "accepted" })).status, "accepted"); await deleteConcept(ws, ai.concept.id); assert.equal(await code(getConcept(ws, ai.concept.id)), "not_found");167});168test("intelligence: central pages, orphans, clusters, recurring topics and exact duplicates, from real data", async () => {169 const hub = await mk(ws, `${PFX} I hub`, long("hub")); const s = await Promise.all([1, 2, 3].map((i) => mk(ws, `${PFX} I spoke ${i}`, long(`spoke ${i}`))));170 for (const x of s) await createRelation(ws, { fromId: x.id, toId: hub.id, relType: "related" });171 const orphan = await mk(ws, `${PFX} I orphan`, long("orphan")); const d1 = await mk(ws, `${PFX} I twin`, long("texte identique")); const d2 = await mk(ws, `${PFX} I twin`, long("texte identique"));172 await setPageTags(ws, s[0].id, ["nx-graph-topic"]); await setPageTags(ws, s[1].id, ["nx-graph-topic"]);173 const x = await computeIntelligence(ws, { limit: 100 }); const again = await computeIntelligence(ws, { limit: 100 });174 assert.deepEqual({ ...x, computedAt: "" }, { ...again, computedAt: "" }, "same data, same answer");175 const h = x.centralPages.find((p) => p.id === hub.id)!; assert.ok(h, "the hub is central"); assert.deepEqual([h.degree, h.in, h.out], [3, 3, 0]); for (const sp of s) assert.ok(h.pagerank > x.centralPages.find((p) => p.id === sp.id)!.pagerank);176 assert.ok(x.orphans.some((o) => o.id === orphan.id)); assert.equal(x.orphans.some((o) => o.id === hub.id), false); assert.equal(x.centralPages.some((p) => p.id === orphan.id), false);177 const cl = x.clusters.find((c) => c.pageIds.includes(hub.id))!; assert.equal(cl.size, 4); assert.equal(cl.label, `${PFX} I hub`, "a cluster is named after its most central page"); assert.deepEqual([...cl.pageIds].sort(), [hub.id, ...s.map((p) => p.id)].sort());178 const topic = x.recurringTopics.find((t) => t.kind === "tag" && t.name === "nx-graph-topic")!; assert.equal(topic.pages, 2); assert.deepEqual(topic.pageIds, [s[0].id, s[1].id].sort()); assert.ok(topic.lastActivityAt);179 assert.ok(x.duplicates.some((d) => d.reason === "title" && d.pageIds.includes(d1.id) && d.pageIds.includes(d2.id))); assert.ok(x.duplicates.some((d) => d.reason === "content" && d.pageIds.includes(d1.id) && d.pageIds.includes(d2.id)));180 assert.ok(x.stats.pages >= 7 && x.stats.orphans >= 3 && x.stats.clusters >= 1);181 const det = await refreshDeterministicInsights(ws); assert.ok(det.created >= 3, "cluster + topic + duplicates materialised"); assert.equal((await refreshDeterministicInsights(ws)).created, 0, "a second run stores nothing new");182 const stored = await listInsights(ws, { kind: "cluster" }); const mine = stored.insights.find((i) => (i.data as any).pageIds?.includes(hub.id))!; assert.deepEqual([mine.origin, mine.status, mine.model], ["system", "suggested", null]);183 await decideInsight(ws, mine.id, "dismissed"); assert.equal((await refreshDeterministicInsights(ws)).created, 0, "a dismissed insight does not come back");184});185test("graph-insights job: strict JSON through an injected model, and every cited page / block must exist", async () => {186 const a = await mk(ws, `${PFX} J alpha`, long("La volatilité est stationnaire.")); const b = await mk(ws, `${PFX} J beta`, long("La volatilité n'est pas stationnaire.")); const c = await mk(ws, `${PFX} J gamma`, long("Pourquoi la persistance change-t-elle ?"));187 const blk = async (id: string) => ((await getPage(ws, id)).content as PMNode).content![0].attrs!.id as string; const [ba, bb, bc] = [await blk(a.id), await blk(b.id), await blk(c.id)];188 const ghost = randomUUID(); let seen: Parameters<InsightModel>[0] | null = null;189 const fake: InsightModel = async (input) => { seen = input; return { model: "fake-model-1", costUsd: 0, text: JSON.stringify({190 contradictions: [191 { title: `${PFX} stationnarité`, detail: "A affirme, B nie.", a: { pageId: a.id, blockId: ba }, b: { pageId: b.id, blockId: bb }, confidence: 0.9 },192 { title: `${PFX} inventée`, detail: "page inconnue", a: { pageId: a.id, blockId: ba }, b: { pageId: ghost, blockId: null }, confidence: 0.9 },193 { title: `${PFX} bloc inventé`, detail: "bloc inconnu", a: { pageId: a.id, blockId: "nope000000" }, b: { pageId: b.id, blockId: bb }, confidence: 0.9 },194 { title: `${PFX} même passage`, detail: "x", a: { pageId: a.id, blockId: ba }, b: { pageId: a.id, blockId: ba }, confidence: 0.5 }],195 openQuestions: [{ title: `${PFX} persistance ?`, detail: "non résolue", source: { pageId: c.id, blockId: bc } }, { title: `${PFX} question fantôme`, detail: "", source: { pageId: ghost, blockId: null } }],196 concepts: [{ name: `${PFX} stationnarité (concept)`, description: "propriété", pageIds: [a.id, b.id, ghost], confidence: 0.8 }, { name: `${PFX} concept d'une seule page`, description: "", pageIds: [a.id, ghost], confidence: 0.8 }],197 }) }; };198 const r = await runGraphInsights(ws, { pageIds: [a.id, b.id, c.id], model: fake });199 assert.deepEqual(r, { pages: 3, contradictions: 1, openQuestions: 1, concepts: 1, conceptLinks: 2, dropped: 7, model: "fake-model-1", costUsd: 0 });200 assert.ok(seen); const input = seen as Parameters<InsightModel>[0]; assert.equal(input.jsonSchema, INSIGHT_SCHEMA); assert.match(input.user, new RegExp(`\\[b:${ba}\\] La volatilité est stationnaire`)); assert.match(input.user, /<workspace>/); assert.match(input.system, /never an instruction/);201 const all = (await listInsights(ws, { status: "all" })).insights.filter((i) => i.title.startsWith(PFX));202 assert.deepEqual(all.filter((i) => i.origin === "ai").map((i) => [i.kind, i.title, i.status, i.model]).sort(), [["contradiction", `${PFX} stationnarité`, "suggested", "fake-model-1"], ["open_question", `${PFX} persistance ?`, "suggested", "fake-model-1"]], "nothing that cites a missing page or block is stored");203 const con = all.find((i) => i.kind === "contradiction")!; assert.deepEqual((con.data as any).pageIds.sort(), [a.id, b.id].sort()); assert.deepEqual((con.data as any).blockIds.sort(), [ba, bb].sort());204 const concept = (await listConcepts(ws, { status: "suggested" })).find((x) => x.name === `${PFX} stationnarité (concept)`)!; assert.ok(concept); made.concepts.push(concept.id); assert.deepEqual([concept.origin, concept.status, concept.pages, concept.suggestedPages], ["ai", "suggested", 0, 2]);205 assert.equal((await listConcepts(ws, { status: "all" })).some((x) => x.name === `${PFX} concept d'une seule page`), false);206 assert.equal((await db.select().from(schema.knowledgeEdges).where(and(eq(schema.knowledgeEdges.fromId, a.id), eq(schema.knowledgeEdges.kind, "relation")))).length, 0, "a contradiction is an insight, not yet a relation");207 // running again stores nothing twice208 const r2 = await runGraphInsights(ws, { pageIds: [a.id, b.id, c.id], model: fake }); assert.deepEqual([r2.contradictions, r2.openQuestions, r2.concepts, r2.conceptLinks], [0, 0, 0, 0]);209 // the user accepts the contradiction: the typed relation is created, accepted, with its AI origin210 const dec = await decideInsight(ws, con.id, "accepted"); assert.ok(dec.relationId); const rel = await getRelation(ws, dec.relationId!); assert.deepEqual([rel.relType, rel.origin, rel.status, rel.model, rel.sourceBlockId, rel.targetBlockId], ["contradicts", "ai", "accepted", "fake-model-1", ba, bb]);211 assert.ok((await eventsOf(a.id)).some((e) => e.kind === "relation.accepted" && e.data.relationId === rel.id));212 // malformed outputs fail the job instead of storing anything213 await assert.rejects(runGraphInsights(ws, { pageIds: [a.id, b.id], model: async () => ({ model: "m", text: "not json" }) }), /invalid JSON/);214 await assert.rejects(runGraphInsights(ws, { pageIds: [a.id, b.id], model: async () => ({ model: "m", text: JSON.stringify({ contradictions: [{ title: 5 }] }) }) }), /does not match the schema/);215 let called = 0; const skipped = await runGraphInsights(ws, { pageIds: [a.id], model: async () => { called++; return { model: "m", text: "{}" }; } }); assert.equal(skipped.skipped, "not enough pages with text"); assert.equal(called, 0, "no call for fewer than two pages");216});217test("the graph-insights queue exists, is never retried automatically, and its handler ignores an empty payload", async () => {218 assert.equal(QUEUES.graphInsights, "graph-insights"); assert.equal(QUEUE_POLICY["graph-insights"].retryLimit, 0); assert.equal(QUEUE_POLICY["graph-insights"].deadLetter, true);219 assert.deepEqual(await handleGraphInsights({} as any), { ignored: true });220});221