SPB Git

Repository Public

ai-ask.test.ts 22.9 KB 217 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/ai-ask.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 * Ask My Knowledge (SPEC §4.10): scope (all | page | project | selection), block-anchored and verified citations, the "no support →45 * say so" rules, and the new inline actions (coherence, questions, links, synthesis, structure). Hermetic: the real provider keys are46 * removed from this process, the answering model is a local fake OpenAI-compatible server, retrieval is full-text only.47 */48import "../helpers/testdb";49import { test, before, after } from "node:test";50import assert from "node:assert/strict";51import { and, eq, inArray, sql as dsql } from "drizzle-orm";52import { db, schema, sql } from "../../src/db/index";53import { startFakeLLM, registerFakeProvider, type FakeLLM } from "../helpers/fake-llm";54import { createPage } from "../../src/lib/pages/service";55import { indexSource, removeSource } from "../../src/lib/search/index";56import { docBlocks, type PMNode } from "../../src/lib/editor/text";57import { chat, validateCitations, resolveScope, ACTION_IDS, planAction, CHAT_SCOPES, type ChatEvent, type ChatRequest } from "../../src/lib/ai/chat";58import { blocksOfChunk, bestBlock, citingSentences, overlapScore, tokens } from "../../src/lib/ai/citations";59import { ACTION_SYSTEMS, LINKS_SYSTEM } from "../../src/lib/ai/prompts";60import { linkCandidates, validateLinkPicks, pickLinks } from "../../src/lib/ai/links";61import { route } from "../../src/lib/ai/router";6263delete process.env.OPENAI_API_KEY; delete process.env.ANTHROPIC_API_KEY; // nothing in this file may reach a real provider64const P = `fakeask_${Date.now().toString(36)}`; const MODEL = `${P}:echo`; const TAG = `ask${Date.now().toString(36)}`;65let fake: FakeLLM; let cleanup: () => Promise<void>; let ws: string; const made: string[] = []; const convs: string[] = [];66const para = (text: string): PMNode => ({ type: "paragraph", content: [{ type: "text", text }] });67const T = { rate: `Le taux variable ${TAG} suit le taux directeur de la Banque du Canada.`, penalty: `La pénalité de remboursement anticipé ${TAG} équivaut à trois mois d'intérêts.`, amort: `L'amortissement maximal ${TAG} est de vingt-cinq ans pour un prêt assuré.`, other: `La pénalité de remboursement ${TAG} est calculée selon le différentiel de taux.` };68let root: string, pageA: string, pageB: string; let blocksA: ReturnType<typeof docBlocks>; let blocksB: ReturnType<typeof docBlocks>;69const mk = async (title: string, content: PMNode[], parentId?: string) => { const p = await createPage(ws, { title, parentId: parentId ?? null, section: "projects", content: { type: "doc", content } }); made.push(p.id); await indexSource(ws, "page", p.id, p.title, { doc: p.content }, p.id).catch(() => {}); return p; };70const run = async (r: Partial<ChatRequest> & { message: string }) => { const out: ChatEvent[] = []; for await (const e of chat(ws, { model: MODEL, ...r })) { out.push(e); if (e.type === "meta" && e.conversationId) convs.push(e.conversationId); } return out; };71const ev = <K extends ChatEvent["type"]>(list: ChatEvent[], type: K) => list.find((e) => e.type === type) as Extract<ChatEvent, { type: K }> | undefined;72/** The fake model reads the numbered sources it was sent and cites the one whose title is given. */73const numberOf = (body: any, title: string) => Number((body.messages.at(-1).content as string).match(new RegExp(`\\[(\\d+)\\] "${title}"`))?.[1] ?? 0);7475before(async () => {76  const [w] = await db.select().from(schema.workspaces).limit(1); assert.ok(w, "workspace missing"); ws = w.id;77  fake = await startFakeLLM(); cleanup = (await registerFakeProvider(fake, P, [{ id: "echo", capabilities: { tools: true, structured_output: true }, inputPrice: 1, outputPrice: 2, contextWindow: 200_000 }])).cleanup;78  const r = await mk(`Projet ${TAG}`, [para(`Notes de cadrage ${TAG} du projet hypothécaire.`)]); root = r.id;79  const a = await mk(`Hypothèques ${TAG}`, [{ type: "heading", attrs: { level: 2 }, content: [{ type: "text", text: "Conditions" }] }, para(T.rate), para(T.penalty), para(T.amort)], root); pageA = a.id; blocksA = docBlocks(a.content as PMNode);80  const b = await mk(`Autre banque ${TAG}`, [para(T.other)]); pageB = b.id; blocksB = docBlocks(b.content as PMNode);81});82after(async () => {83  if (convs.length) await db.delete(schema.aiConversations).where(inArray(schema.aiConversations.id, [...new Set(convs)]));84  for (const id of made) await removeSource("page", id);85  if (made.length) { await db.delete(schema.pages).where(inArray(schema.pages.id, made)); await db.delete(schema.syncTombstones).where(inArray(schema.syncTombstones.id, made)); }86  await db.delete(schema.aiUsage).where(eq(schema.aiUsage.provider, P));87  await cleanup(); await fake.close(); await sql.end();88});8990// ---------- pure helpers91test("anchoring helpers: a chunk is mapped back to its blocks and the best block for a sentence is chosen", () => {92  const blocks = [{ id: "h", type: "heading", heading: "Conditions", text: "Conditions" }, { id: "b1", type: "paragraph", heading: "Conditions", text: T.rate }, { id: "b2", type: "paragraph", heading: "Conditions", text: T.penalty }, { id: "b3", type: "paragraph", heading: "Conditions", text: T.amort }, { id: "x", type: "paragraph", heading: null, text: "Sans rapport." }];93  const chunk = [T.rate, T.penalty, T.amort].join("\n");94  assert.deepEqual(blocksOfChunk(blocks, chunk).map((b) => b.id), ["b1", "b2", "b3"], "headings and foreign blocks are not part of the passage");95  assert.equal(bestBlock(blocksOfChunk(blocks, chunk), "Quelle est la pénalité de remboursement anticipé ?")?.id, "b2");96  assert.equal(bestBlock(blocksOfChunk(blocks, chunk), "amortissement maximal prêt assuré")?.id, "b3");97  assert.equal(bestBlock(blocksOfChunk(blocks, chunk), "zzz")?.id, "b1", "no lexical match → first block of the passage");98  assert.deepEqual(blocksOfChunk(blocks, "texte disparu de la page"), [], "a passage that is no longer in the page has no block");99  const long = "Phrase un. ".repeat(80); assert.deepEqual(blocksOfChunk([{ id: "L", type: "paragraph", heading: null, text: long }], long.slice(0, 200)).map((b) => b.id), ["L"], "a piece of a long block maps to that block");100  assert.equal(citingSentences("Le taux suit la Banque [1]. La pénalité vaut trois mois [2][3]. Rien ici.", 2), "La pénalité vaut trois mois.");101  assert.equal(citingSentences("- point A [1]\n- point B [2]", 1), "- point A"); assert.equal(citingSentences("Aucune citation.", 1), "");102  assert.ok(overlapScore("pénalité remboursement", T.penalty) > overlapScore("pénalité remboursement", T.rate)); assert.deepEqual(tokens("L'amortissement est de 25 ans"), ["amortissement", "25", "ans"]);103});104test("citation markers outside the retrieved set are removed and reported; code is left alone", () => {105  const v = validateCitations("Vrai [1]. Inventé [7]. Groupe [1, 2, 9]. `a[3]` reste.", new Set([1, 2]));106  assert.equal(v.text, "Vrai [1]. Inventé. Groupe [1][2]. `a[3]` reste."); assert.deepEqual(v.cited, [1, 2]); assert.deepEqual(v.invalid, [7, 9]);107});108109// ---------- scope110test("scope: project = root ancestor + descendants, page = that page, unknown or missing page is a coded error", async () => {111  assert.deepEqual(CHAT_SCOPES, ["all", "page", "project", "selection"]);112  assert.deepEqual(await resolveScope(ws, {}), { scope: "all", pageIds: null });113  assert.deepEqual(await resolveScope(ws, { scope: "page", pageId: pageA }), { scope: "page", pageIds: [pageA] });114  const proj = await resolveScope(ws, { scope: "project", pageId: pageA }); assert.deepEqual([...proj.pageIds!].sort(), [root, pageA].sort(), "from a child page: the whole project");115  assert.deepEqual([...(await resolveScope(ws, { scope: "project", projectId: root })).pageIds!].sort(), [root, pageA].sort());116  assert.deepEqual((await resolveScope(ws, { scope: "project", pageId: pageB })).pageIds, [pageB], "a top-level page is its own project");117  await assert.rejects(resolveScope(ws, { scope: "page" }), (e: any) => e.code === "page_required");118  await assert.rejects(resolveScope(ws, { scope: "project", projectId: "00000000-0000-4000-8000-000000000000" }), (e: any) => e.code === "not_found");119});120test("ask (scope all): the answer cites verified blocks — the anchor is the paragraph that supports the sentence, not the first block of the chunk; an invented marker is dropped", async () => {121  fake.reply = (body) => `La pénalité équivaut à trois mois d'intérêts [${numberOf(body, `Hypothèques ${TAG}`)}]. Ailleurs elle suit le différentiel de taux [${numberOf(body, `Autre banque ${TAG}`)}]. Affirmation inventée [9].`;122  const out = await run({ message: `pénalité remboursement ${TAG}` });123  const meta = ev(out, "meta")!; const done = ev(out, "done")!; assert.ok(done, JSON.stringify(out.at(-1)));124  assert.equal(meta.scope, "all"); assert.equal(meta.scopePages, null); assert.equal(meta.model, MODEL); assert.equal(meta.provider, P); assert.equal(meta.reason, "user selection"); assert.equal(meta.evidence, "ok");125  assert.equal(done.sources.length, 2); assert.deepEqual(done.invalidCitations, [9]); assert.ok(!done.content.includes("[9]")); assert.equal(done.evidence, "ok"); assert.equal(done.uncited, false);126  const a = done.sources.find((s) => s.pageId === pageA)!; const b = done.sources.find((s) => s.pageId === pageB)!;127  const penaltyBlock = blocksA.find((x) => x.text === T.penalty)!.id; const firstBlock = blocksA.find((x) => x.text === T.rate)!.id;128  assert.equal(a.blockId, penaltyBlock, "anchored to the paragraph about the penalty"); assert.notEqual(a.blockId, firstBlock); assert.equal(a.anchor, "block"); assert.equal(a.quote, T.penalty);129  assert.ok(a.blockIds!.includes(firstBlock!) && a.blockIds!.includes(penaltyBlock!), "all blocks of the passage are listed");130  assert.equal(b.blockId, blocksB[0].id); assert.equal(b.quote, T.other);131  // every cited block exists in the page as it is now132  for (const s of done.sources) { const [p] = await db.select({ content: schema.pages.content }).from(schema.pages).where(eq(schema.pages.id, s.pageId!)); assert.ok(docBlocks(p.content as PMNode).some((x) => x.id === s.blockId), `block ${s.blockId} exists`); }133  const [stored] = await db.select().from(schema.aiMessages).where(eq(schema.aiMessages.id, done.messageId)); assert.equal((stored.sources as any[]).length, 2); assert.equal((stored.sources as any[])[0].blockId != null, true);134});135test("ask (scope page / project): evidence is restricted to the scope", async () => {136  fake.reply = (body) => `Réponse [1]. ${(body.messages.at(-1).content as string).includes(`Autre banque ${TAG}`) ? "AUTRE" : "SEULE"}`;137  const page = await run({ message: `pénalité remboursement ${TAG}`, scope: "page", pageId: pageB });138  assert.equal(ev(page, "meta")!.scope, "page"); assert.equal(ev(page, "meta")!.scopePages, 1); assert.deepEqual(ev(page, "meta")!.sources.map((s) => s.pageId), [pageB]);139  const proj = await run({ message: `pénalité remboursement ${TAG}`, scope: "project", pageId: pageA });140  assert.equal(ev(proj, "meta")!.scopePages, 2); assert.deepEqual(ev(proj, "meta")!.sources.map((s) => s.pageId), [pageA], "the other bank's page is outside the project");141  assert.match(ev(proj, "done")!.content, /SEULE$/, "the excerpt of the page outside the project was not sent");142});143test("ask (scope selection): only the selected text is sent, no search, and the citation points to the block it was selected in", async () => {144  const blockId = blocksA.find((x) => x.text === T.amort)!.id!; fake.requests.length = 0; fake.reply = () => "Vingt-cinq ans pour un prêt assuré [1].";145  const out = await run({ message: "Quelle est la durée maximale ?", scope: "selection", selection: T.amort, selectionBlockId: blockId, pageId: pageA });146  const meta = ev(out, "meta")!; const done = ev(out, "done")!; assert.ok(done, JSON.stringify(out.at(-1)));147  assert.equal(meta.scope, "selection"); assert.equal(meta.sent.embeddedBy, null); assert.equal(meta.sources.length, 1); assert.equal(meta.sources[0].sourceType, "selection");148  assert.equal(done.sources[0].blockId, blockId); assert.equal(done.sources[0].anchor, "selection"); assert.equal(done.sources[0].pageId, pageA);149  const sent = fake.requests.at(-1)!.body.messages.at(-1).content as string; assert.ok(sent.includes(T.amort)); assert.ok(!sent.includes(T.penalty), "nothing but the selection is sent");150  // a block id that is not in the page is not trusted151  const bad = await run({ message: "Durée ?", scope: "selection", selection: T.amort, selectionBlockId: "not-a-block", pageId: pageA }); assert.equal(ev(bad, "done")!.sources[0].blockId, null);152});153test("ask: an unresolvable scope is refused before anything is stored or sent", async () => {154  const before = (await db.select({ n: dsql<number>`count(*)::int` }).from(schema.aiConversations))[0].n; fake.requests.length = 0;155  const cases: [Partial<ChatRequest>, string][] = [[{ scope: "page" }, "page_required"], [{ scope: "project" }, "page_required"], [{ scope: "selection" }, "selection_required"], [{ scope: "selection", selection: "   " }, "selection_required"], [{ scope: "page", pageId: pageA, mode: "general" }, "bad_scope"], [{ scope: "selection", selection: "x", mode: "draft" }, "bad_scope"], [{ scope: "project", projectId: "00000000-0000-4000-8000-000000000000" }, "not_found"]];156  for (const [r, code] of cases) { const out = await run({ message: "question", ...r }); assert.equal(out.length, 1, code); assert.equal(out[0].type, "error"); assert.equal((out[0] as any).code, code); assert.equal((out[0] as any).retryable, false); }157  assert.equal((await db.select({ n: dsql<number>`count(*)::int` }).from(schema.aiConversations))[0].n, before); assert.equal(fake.requests.length, 0);158});159160// ---------- no support → say so161test("ask: with no supporting block no provider is called and the answer says so", async () => {162  fake.requests.length = 0; const out = await run({ message: `zzqx${TAG} introuvable`, scope: "project", pageId: pageA });163  const done = ev(out, "done")!; assert.equal(done.evidence, "none"); assert.match(done.content, /^\*\*No supporting note found\.\*\*/); assert.deepEqual(done.sources, []); assert.equal(done.costUsd, 0);164  assert.equal(ev(out, "meta")!.model, null); assert.equal(fake.requests.length, 0, "the answering model was not called");165});166test("ask: an answer that cites nothing is withheld (not shown as grounded); the model's INSUFFICIENT is honoured", async () => {167  fake.reply = () => "La pénalité est probablement de six mois selon la pratique courante.";168  const a = ev(await run({ message: `pénalité remboursement ${TAG}` }), "done")!;169  assert.equal(a.uncited, true); assert.equal(a.evidence, "insufficient"); assert.deepEqual(a.sources, []); assert.match(a.content, /^\*\*No supporting note found\.\*\*/); assert.ok(!a.content.includes("six mois"), "the unverifiable text is not shown");170  fake.reply = () => "[[INSUFFICIENT]]\nLes extraits ne donnent pas le montant exact [1].";171  const b = ev(await run({ message: `pénalité remboursement ${TAG}` }), "done")!; assert.equal(b.evidence, "insufficient"); assert.deepEqual(b.sources, []); assert.ok(!b.content.includes("[1]")); assert.ok(!b.content.includes("INSUFFICIENT"));172  fake.reply = () => "Réponse qui ne cite que du faux [5][6].";173  const c = ev(await run({ message: `pénalité remboursement ${TAG}` }), "done")!; assert.equal(c.evidence, "insufficient"); assert.deepEqual(c.invalidCitations, [5, 6]); assert.deepEqual(c.sources, []);174});175test("ask: a passage that is no longer in the page (stale index) is dropped — its citation could not be verified", async () => {176  // the page changes but its index is not refreshed: the old passage is still found by the search177  await db.update(schema.pages).set({ content: { type: "doc", content: [{ type: "paragraph", attrs: { id: "new-block" }, content: [{ type: "text", text: "Contenu entièrement réécrit." }] }] } }).where(eq(schema.pages.id, pageB));178  fake.requests.length = 0; const out = await run({ message: `pénalité remboursement ${TAG}`, scope: "page", pageId: pageB });179  assert.equal(ev(out, "meta")!.staleDropped, 1); assert.deepEqual(ev(out, "meta")!.sources, []); assert.equal(ev(out, "done")!.evidence, "none"); assert.equal(fake.requests.length, 0);180  const all = await run({ message: `pénalité remboursement ${TAG}` }); assert.deepEqual(ev(all, "meta")!.sources.map((s) => s.pageId), [pageA], "the live passage is still usable");181});182test("ask: a provider failure stores no answer and reports a retryable error", async () => {183  const [m] = await db.insert(schema.aiModels).values({ id: `${P}:err-500`, provider: P, category: "text", enabled: true, available: true, capabilities: {} }).returning(); assert.ok(m);184  const { invalidateCatalog } = await import("../../src/lib/ai/catalog"); invalidateCatalog();185  const out = await run({ message: `pénalité remboursement ${TAG}`, model: `${P}:err-500` });186  const e = out.at(-1) as Extract<ChatEvent, { type: "error" }>; assert.equal(e.type, "error"); assert.equal(e.code, "provider_error"); assert.equal(e.retryable, true); assert.match(e.message, /temporarily unavailable/);187  const msgs = await db.select().from(schema.aiMessages).where(eq(schema.aiMessages.conversationId, e.conversationId!)); assert.deepEqual(msgs.map((x) => x.role), ["user"], "no assistant message stored");188  const [u] = await db.select().from(schema.aiUsage).where(and(eq(schema.aiUsage.provider, P), eq(schema.aiUsage.ok, false))).limit(1); assert.ok(u, "the failed call is in the usage log");189});190191// ---------- inline actions192test("actions: the five new actions exist next to every historical one, each with its prompt and a route plan", async () => {193  const historical = ["rewrite", "fix", "translate", "summarize", "expand", "explain", "to_list", "to_tasks", "table", "shorten", "formal", "simplify", "translate_fr", "translate_en", "explain_math", "continue", "outline", "study_notes", "action_items", "contradictions", "custom"];194  for (const a of historical) assert.ok((ACTION_IDS as readonly string[]).includes(a), `historical action ${a} kept`);195  for (const a of ["coherence", "questions", "links", "synthesis", "structure"]) assert.ok((ACTION_IDS as readonly string[]).includes(a), a);196  assert.equal(ACTION_IDS.length, 26);197  for (const a of ["coherence", "questions", "synthesis", "structure"]) { assert.ok(ACTION_SYSTEMS[a]?.length > 80, a); const { plan, dec } = await planAction(a as any, 500, MODEL); assert.equal(plan.error, null); assert.equal(dec?.model.id, MODEL); assert.equal(plan.expensive, null, `${a} is not an expensive class`); }198  assert.equal(ACTION_SYSTEMS.links, undefined, "links has its own JSON contract"); assert.match(LINKS_SYSTEM, /Never invent a page/);199  assert.equal((await planAction("links", 500, MODEL)).plan.task, "extract");200});201test("links: candidates come from the workspace (never the page itself nor a page already linked), picks are validated, nothing is written", async () => {202  const text = `Il faut vérifier la pénalité de remboursement ${TAG} avant de signer.`;203  const cands = await linkCandidates(ws, `pénalité remboursement ${TAG}`, pageB); assert.ok(cands.some((c) => c.pageId === pageA)); assert.ok(!cands.some((c) => c.pageId === pageB), "the page itself is not a candidate");204  const linking = await mk(`Lien existant ${TAG}`, [{ type: "paragraph", content: [{ type: "text", text: "voir", marks: [{ type: "link", attrs: { href: `/p/${pageA}` } }] }] }]);205  assert.ok(!(await linkCandidates(ws, `pénalité remboursement ${TAG}`, linking.id)).some((c) => c.pageId === pageA), "already linked from the page");206  const picks = validateLinkPicks({ links: [{ index: 1, anchorText: "pénalité de remboursement", reason: "Même sujet." }, { index: 1, anchorText: "x", reason: "doublon" }, { index: 99, anchorText: "", reason: "hors liste" }, { index: 0, anchorText: "", reason: "zéro" }, { index: 2, anchorText: "texte absent", reason: "ancre inventée" }] }, [cands[0], { ...cands[0], pageId: "other", title: "Autre" }], text);207  assert.equal(picks.length, 2); assert.equal(picks[0].anchorText, "pénalité de remboursement"); assert.equal(picks[1].anchorText, null, "an anchor that is not in the text is blanked"); assert.deepEqual(validateLinkPicks("nope", cands, text), []); assert.deepEqual(validateLinkPicks({ links: "x" }, cands, text), []);208  const edges = (await db.select({ n: dsql<number>`count(*)::int` }).from(schema.knowledgeEdges))[0].n; const links = (await db.select({ n: dsql<number>`count(*)::int` }).from(schema.links))[0].n;209  fake.reply = (body) => { const i = Number((body.messages.at(-1).content as string).match(new RegExp(`\\[(\\d+)\\] "Hypothèques ${TAG}"`))?.[1]); return JSON.stringify({ links: [{ index: i, anchorText: "pénalité de remboursement", reason: "La page détaille cette pénalité." }, { index: 42, anchorText: "", reason: "inventé" }] }); };210  const dec = await route({ task: "extract", schema: true, preferred: MODEL });211  const r = await pickLinks(dec, text, await linkCandidates(ws, text.replace("Il faut vérifier la ", "").replace(" avant de signer.", ""), null));212  assert.equal(r.links.length, 1); assert.equal(r.links[0].pageId, pageA); assert.equal(r.links[0].anchorText, "pénalité de remboursement"); assert.equal(r.links[0].title, `Hypothèques ${TAG}`);213  assert.equal(fake.requests.at(-1)!.body.response_format.type, "json_schema");214  fake.reply = () => "ceci n'est pas du JSON"; const bad = await pickLinks(dec, text, cands); assert.equal(bad.parsed, null); assert.deepEqual(bad.links, []);215  assert.equal((await db.select({ n: dsql<number>`count(*)::int` }).from(schema.knowledgeEdges))[0].n, edges, "no edge written"); assert.equal((await db.select({ n: dsql<number>`count(*)::int` }).from(schema.links))[0].n, links, "no link written");216});217
Files main
Menu

Theme