Files
confidence-engine/tests/ui/v060h-report-identity.test.jsx
T
robbond 0cc9d087c3 fix(confidence-engine): permit empty plausibleInterpretations for honest zero state
- Zod validator: plausibleInterpretations accepts empty string (zero interpretations) while remaining required and string-typed
- Prompt instruction: SECTION B now explicitly requests "" instead of absence prose when no plausible inputs exist
- Report generation/update paths derive hasPlausibleInterpretations from actual synthesized data rather than hard-coding true
- Tests: updated validation contract, added hasPlausibleInterpretations derivation assertions, updated seam tests to use derived values
2026-10-02 06:48:24 +01:00

219 lines
11 KiB
React

import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";
/* ── Three-part report: unresolved question derivation (seam-level) ──────────── */
function deriveUnresolvedQuestions(situationGraph, resolvedNodeIds) {
const resolvedIds = new Set(resolvedNodeIds ?? []);
return (situationGraph?.nodes ?? [])
.filter((n) => n.kind === "unknown" && n.status !== "resolved" && !resolvedIds.has(n.id))
.map((n) => n.label || n.description);
}
describe("Three-part report — unresolved question derivation", () => {
it("Case A: unresolved questions exist and are returned in canonical order", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
{ id: "u2", kind: "unknown", status: "provisional", label: "Who is the primary customer?" },
] }, []);
expect(result).toHaveLength(2);
expect(result[0]).toBe("What is the revenue model?");
expect(result[1]).toBe("Who is the primary customer?");
});
it("Case A: unresolved text does NOT appear in supported-understanding content", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
] }, []);
expect(result).toHaveLength(1);
expect(result).not.toContain("");
});
it("Case B: resolved/clarified question is excluded from unresolved", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
{ id: "u2", kind: "unknown", status: "resolved", label: "Resolved question" },
] }, []);
expect(result).toHaveLength(1);
expect(result[0]).toBe("What is the revenue model?");
});
it("Case B: resolved node ID excluded via resolvedNodeIds", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "Unresolved question" },
{ id: "u2", kind: "unknown", status: "unclear", label: "Also unresolved" },
] }, ["u1"]);
expect(result).toHaveLength(1);
expect(result[0]).toBe("Also unresolved");
});
it("Case C: zero unresolved questions yields empty array", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "n1", kind: "observation", status: "known" },
{ id: "u1", kind: "unknown", status: "resolved" },
] }, []);
expect(result).toHaveLength(0);
});
it("Case C: no nodes yields empty array (no crash)", () => {
const result = deriveUnresolvedQuestions(null, []);
expect(result).toEqual([]);
});
it("Epistemic separation: assumption-kind nodes are excluded", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "Open question" },
{ id: "a1", kind: "assumption", status: "plausible", label: "Assumption" },
] }, []);
expect(result).toHaveLength(1);
expect(result[0]).toBe("Open question");
});
it("Epistemic separation: no readiness/completion language in derived output", () => {
const result = deriveUnresolvedQuestions({ nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
] }, []);
expect(result[0]).not.toMatch(/ready|complete|enough|sufficient|must resolve|critical|priority|blocker/);
});
});
/* ── v0.60h-a — seam-level mocks (no @testing-library/react) ─────────── */
let mockLastLoadedId = undefined;
let mockLastSavedSnapshot = null;
let mockLoadResult = null;
vi.mock("@/lib/storage/investigation-storage", () => ({
listInvestigations: () => [],
loadInvestigation: (id) => {
mockLastLoadedId = id;
return Promise.resolve(mockLoadResult);
},
saveInvestigation: (snapshot) => {
mockLastSavedSnapshot = snapshot;
return Promise.resolve(snapshot);
},
clearInvestigation: () => {},
}));
function makeSnapshot(overrides = {}) {
const situationGraph = {
evidence: [
{ id: "e1", claim: "Complaints rose.", type: "finding" },
{ id: "e2", claim: "Production increased.", type: "finding" },
],
reconstruction: {
plausibleInterpretations: ["The denominator may have been narrowed."],
},
};
return {
scenario: "Complaints increased by 35% while production increased by 40%.",
situationGraph,
selectedQuestion: null,
summary: "Production quality declined.",
updatedAt: new Date().toISOString(),
schemaVersion: 1,
investigationReport: null,
investigationRevision: 0,
findings: [],
...overrides,
};
}
async function cleanup() {
localStorage.removeItem("confidence-engine-investigation");
}
describe("Report page identity and lifecycle — v0.60h-a (seam-level)", () => {
beforeEach(() => {
mockLastLoadedId = undefined; mockLastSavedSnapshot = null; mockLoadResult = null;
localStorage.removeItem("confidence-engine-investigation");
global.fetch = vi.fn();
});
afterEach(async () => { await cleanup(); });
it("A — loadInvestigation invoked with route ID and overview POST issued", async () => {
const snap = makeSnapshot({ id: "inv-report-a", investigationReport: null, investigationRevision: 4, findings: [] });
mockLoadResult = snap;
localStorage.setItem("confidence-engine-investigation", JSON.stringify(snap));
const loaded = await (await import("@/lib/storage/investigation-storage")).loadInvestigation("inv-report-a");
expect(mockLastLoadedId).toBe("inv-report-a");
global.fetch.mockResolvedValue({ ok: true, json: () => Promise.resolve({ success: true, understanding: "Generated.", plausibleInterpretations: null }) });
const resp = await fetch("/api/cases/overview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ situationGraph: loaded?.situationGraph, findings: loaded?.findings || [] }) });
expect(resp.ok).toBe(true);
await (await import("@/lib/storage/investigation-storage")).saveInvestigation({ ...loaded, investigationReport: { understanding: "Generated.", plausibleInterpretations: null, hasPlausibleInterpretations: (null ?? "").trim().length > 0, generatedFromRevision: 4 } });
expect(mockLastSavedSnapshot.id).toBe("inv-report-a");
expect(mockLastSavedSnapshot.investigationReport.generatedFromRevision).toBe(4);
});
it("B — no overview call when report exists and revisions match", async () => {
const snap = makeSnapshot({ id: "inv-report-a", investigationReport: { understanding: "Persisted summary.", hasPlausibleInterpretations: false, generatedFromRevision: 4 }, investigationRevision: 4 });
mockLoadResult = snap;
localStorage.setItem("confidence-engine-investigation", JSON.stringify(snap));
const loaded = await (await import("@/lib/storage/investigation-storage")).loadInvestigation("inv-report-a");
expect(mockLastLoadedId).toBe("inv-report-a");
expect(loaded?.investigationReport?.understanding).toBe("Persisted summary.");
const calls = global.fetch.mock.calls.filter((c) => c[0] === "/api/cases/overview");
expect(calls).toHaveLength(0);
});
it("C — first generation posts exactly once and saves correctly", async () => {
const snap = makeSnapshot({ id: "inv-report-a", investigationReport: null, situationGraph: { evidence: [], reconstruction: {} }, investigationRevision: 4, findings: [] });
mockLoadResult = snap;
localStorage.setItem("confidence-engine-investigation", JSON.stringify(snap));
global.fetch.mockResolvedValue({ ok: true, json: () => Promise.resolve({ success: true, understanding: "Generated.", plausibleInterpretations: null }) });
const loaded = await (await import("@/lib/storage/investigation-storage")).loadInvestigation("inv-report-a");
expect(mockLastLoadedId).toBe("inv-report-a");
const resp = await fetch("/api/cases/overview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ situationGraph: loaded?.situationGraph, findings: [] }) });
expect(resp.ok).toBe(true);
const calls = global.fetch.mock.calls.filter((c) => c[0] === "/api/cases/overview");
expect(calls).toHaveLength(1);
});
it("D — manual update reloads same id and saves back", async () => {
const snap = makeSnapshot({ id: "inv-report-a", investigationReport: { understanding: "R1.", hasPlausibleInterpretations: false, generatedFromRevision: 4 }, investigationRevision: 5, findings: [{ id: "f-1", proposition: "P1" }] });
mockLoadResult = snap;
localStorage.setItem("confidence-engine-investigation", JSON.stringify(snap));
global.fetch.mockResolvedValue({ ok: true, json: () => Promise.resolve({ success: true, understanding: "R2.", plausibleInterpretations: null }) });
const loaded = await (await import("@/lib/storage/investigation-storage")).loadInvestigation("inv-report-a");
expect(mockLastLoadedId).toBe("inv-report-a");
const resp = await fetch("/api/cases/overview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ situationGraph: loaded?.situationGraph, findings: loaded?.findings }) });
expect(resp.ok).toBe(true);
await (await import("@/lib/storage/investigation-storage")).saveInvestigation({ ...loaded, investigationReport: { understanding: "R2.", plausibleInterpretations: null, hasPlausibleInterpretations: (null ?? "").trim().length > 0, generatedFromRevision: 5 } });
expect(mockLastSavedSnapshot.id).toBe("inv-report-a");
expect(mockLastSavedSnapshot.investigationReport.generatedFromRevision).toBe(5);
});
it("E — no partial save on generation failure", async () => {
const snap = makeSnapshot({ id: "inv-report-a", investigationReport: null, situationGraph: { evidence: [], reconstruction: {} }, investigationRevision: 1, findings: [] });
mockLoadResult = snap;
localStorage.setItem("confidence-engine-investigation", JSON.stringify(snap));
global.fetch.mockResolvedValue({ ok: false });
const resp = await fetch("/api/cases/overview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ situationGraph: snap.situationGraph, findings: [] }) });
expect(resp.ok).toBe(false);
expect(mockLastSavedSnapshot).toBeNull();
});
it("F — no save on update failure", async () => {
const snap = makeSnapshot({ id: "inv-report-a", investigationReport: { understanding: "R1.", hasPlausibleInterpretations: false, generatedFromRevision: 4 }, investigationRevision: 5, findings: [{ id: "f-1", proposition: "P1" }] });
mockLoadResult = snap;
localStorage.setItem("confidence-engine-investigation", JSON.stringify(snap));
global.fetch.mockResolvedValue({ ok: false });
const loaded = await (await import("@/lib/storage/investigation-storage")).loadInvestigation("inv-report-a");
await fetch("/api/cases/overview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ situationGraph: loaded?.situationGraph, findings: loaded?.findings }) });
expect(mockLastSavedSnapshot).toBeNull();
});
});