import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm, readFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { Workspace, atomicJSON } from "../dist/workspace/index.js"; import { Catalog } from "../dist/catalog/index.js"; import { investigate } from "../dist/core/engine.js"; import { writeReport } from "../dist/reports/html.js"; import { renderBiography, renderReportInfo } from "../dist/reports/article.js"; import { startServer } from "../dist/server/index.js"; async function setup(t) { const root = await mkdtemp(join(tmpdir(), "phomber-catalog-")); t.after(() => rm(root, { recursive: true, force: true })); const w = new Workspace(root); await w.init(); await atomicJSON(join(root, "library.json"), { schema_version: "1.0", articles: [], }); return w; } async function report(w, name, finished) { const v = await investigate( { schema_version: "1.0", subject: { name }, research: { offline: true } }, w, ); v.research_timing = { started_at: "2026-01-01T00:00:00Z", finished_at: finished, }; v.updated_at = finished; await writeReport(v, w); return v; } test("names alone stay separate; explicit moves, version sorting and decisions persist", async (t) => { const w = await setup(t), old = await report(w, "Same Person", "2026-01-02T00:00:00Z"), newer = await report(w, "Same Person", "2026-02-02T00:00:00Z"), other = await report(w, "Another Person", "2026-03-02T00:00:00Z"), catalog = new Catalog(w); let profiles = await catalog.read(); assert.equal(profiles.length, 3); assert.equal(profiles[0].articles[0].id, other.id); const olderProfile = profiles.find(p => p.articles.some(a => a.id === old.id)); await catalog.move("report:" + newer.id, olderProfile.id); profiles = await catalog.read(); const group = profiles.find((p) => p.title === "Same Person"); assert.equal(group.articles.length, 2); assert.equal(group.articles[0].id, newer.id); assert.equal(group.articles[0].version, 2); const original = await readFile( join(w.reportPath(newer.id), "investigation.json"), "utf8", ); const split = await catalog.move("report:" + newer.id); profiles = await new Catalog(w).read(); assert.equal(profiles.length, 3); assert.equal(profiles.find((p) => p.id === split).articles.length, 1); assert.equal( await readFile(join(w.reportPath(newer.id), "investigation.json"), "utf8"), original, ); await catalog.rename(split, "Same name - another person"); assert.equal( (await new Catalog(w).read()).find((p) => p.id === split).title, "Same name - another person", ); const merged = await catalog.move("report:" + newer.id, group.id); assert.equal(merged, group.id); assert.equal( (await catalog.read()).find((p) => p.id === group.id).articles.length, 2, ); await assert.rejects(() => catalog.move("report:../bad")); await assert.rejects(() => catalog.move("report:" + old.id, "profile-nonexistent"), ); }); test("biography is prose-first with numbered references, dynamic infobox, dates and external links", async (t) => { const w = await setup(t), v = await report(w, "Example Person", "2026-02-02T00:00:00Z"); v.sources = [ { id: "s1", adapter: "public-profile", url: "https://example.org/person", retrieved_at: "2026-02-01T12:00:00Z", status: "ok", }, ]; v.findings.push({ id: "f1", category: "career", field: "professional_title", value: "Software researcher", status: "lead", source_ids: ["s1"], identity: "candidate", confidence: { score: null, explanation: "Candidate" }, }); v.findings.push({ id: "f2", category: "online", field: "website", value: "https://example.org/person", status: "lead", source_ids: ["s1"], identity: "candidate", confidence: { score: null, explanation: "Candidate" }, }); v.evidence = [ { id: "e1", finding_id: "f1", source_id: "s1", excerpt: "Profile says software researcher.", retrieved_at: "2026-02-01T12:00:00Z", }, ]; v.ai = { model: "fixture", status: "complete", limitations: [], sections: [ { title: "Overview", paragraphs: [ { text: "This candidate profile describes a software researcher.", evidence_ids: ["e1"], }, ], }, { title: "Career", paragraphs: [ { text: "The professional profile reports research work.", evidence_ids: ["e1"], }, ], }, ], }; const html = renderBiography(v); assert.ok(html.includes("Profile information")); assert.ok(html.includes("Software researcher")); assert.ok(html.includes("possible match; source-reported")); assert.ok( html.indexOf("The professional profile reports") < html.indexOf("