Files
confidence-engine/tests/open-questions-vs-assumptions.test.jsx
T
robbond 922f58a49f fix(confidence-engine): project processing indicator to active follow-up block
Repair LOCATION-A defect where processing feedback rendered near the
completed narrative instead of inside the active follow-up block.

Changes:
  - components/reasoning-workspace.jsx: three targeted edits using a single
    spinner component with conditional rendering; hasActiveFollowUp routes
    ownership to the correct container
  - tests/open-questions-vs-assumptions.test.jsx: regression test confirming
    exactly one indicator, DOM child of follow-up-block, ownership separation

Accepted criteria met:
   Exactly one processing indicator during follow-up processing
   Indicator is a DOM child of follow-up-block
   Top-level indicator suppressed when follow-up active
   Initial answer flow preserved (top-level when no follow-up)
   Successful follow-up promotion intact
   All existing context retained
   No new state/lifecycle changes/error redesign
2026-08-30 11:09:24 +01:00

2558 lines
103 KiB
React

import { describe, expect, it } from "vitest";
import "@testing-library/jest-dom/vitest";
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import { FocusedQuestionBody, SecondaryPreviousLearning } from "@/components/reasoning-workspace";
// ── Simulated rendering logic extracted from reasoning-workspace.jsx
// Mirrors the exact filter + conditional structure used in the
// initial post-Analyse reflection surface (lines 1442-1511)
// AND the workspace grid persistent section (added for persistence fix).
function renderInitialProposedFindings({ nodes, resolvedNodeIds }) {
const resolvedIds = new Set(resolvedNodeIds || []);
// Open Questions: unresolved unknown nodes only
const openUnknowns = (nodes || []).filter(
(n) =>
n.kind === "unknown" &&
n.status !== "resolved" &&
!resolvedIds.has(n.id),
);
// Possible Interpretations: unresolved assumption nodes only
const possibleInterpretations = (nodes || []).filter(
(n) =>
n.kind === "assumption" &&
n.status !== "resolved" &&
!resolvedIds.has(n.id),
);
return { openUnknowns, possibleInterpretations };
}
// Simulated workspace-grid Possible Interpretations rendering (same filter as the new persistent section)
function renderWorkspacePossibleInterpretations({ nodes, resolvedNodeIds }) {
const resolvedIds = new Set(resolvedNodeIds || []);
return (nodes || []).filter(
(n) =>
n.kind === "assumption" &&
n.status !== "resolved" &&
!resolvedIds.has(n.id),
);
}
describe("Open Questions vs Possible Interpretations separation", () => {
const graph = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
{ id: "u2", kind: "unknown", status: "unclear", label: "Who is the primary customer?" },
{ id: "a1", kind: "assumption", status: "plausible", label: "Revenue via subscription" },
{ id: "a2", kind: "assumption", status: "plausible", label: "Enterprise customers" },
],
resolvedNodeIds: [],
};
it("unknown nodes appear under Open Questions only", () => {
const result = renderInitialProposedFindings(graph);
expect(result.openUnknowns).toHaveLength(2);
expect(result.openUnknowns.map((n) => n.id)).toEqual(["u1", "u2"]);
result.openUnknowns.forEach((n) => {
expect(n.kind).toBe("unknown");
});
});
it("assumption nodes appear under Possible Interpretations only", () => {
const result = renderInitialProposedFindings(graph);
expect(result.possibleInterpretations).toHaveLength(2);
expect(result.possibleInterpretations.map((n) => n.id)).toEqual(["a1", "a2"]);
result.possibleInterpretations.forEach((n) => {
expect(n.kind).toBe("assumption");
});
});
it("assumptions do NOT appear under Open Questions", () => {
const result = renderInitialProposedFindings(graph);
const assumptionIdsInOpen = result.openUnknowns.filter(
(n) => n.kind === "assumption",
);
expect(assumptionIdsInOpen).toHaveLength(0);
});
it("unknowns do NOT appear under Possible Interpretations", () => {
const result = renderInitialProposedFindings(graph);
const unknownIdsInInterpretations = result.possibleInterpretations.filter(
(n) => n.kind === "unknown",
);
expect(unknownIdsInInterpretations).toHaveLength(0);
});
it("resolved nodes are excluded from both sections", () => {
const resolvedGraph = { ...graph, resolvedNodeIds: ["u1", "a2"] };
const result = renderInitialProposedFindings(resolvedGraph);
expect(result.openUnknowns).toHaveLength(1);
expect(result.openUnknowns[0].id).toBe("u2");
expect(result.possibleInterpretations).toHaveLength(1);
expect(result.possibleInterpretations[0].id).toBe("a1");
});
it("only unknown kind is eligible for Open Questions", () => {
const mixedGraph = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
{ id: "o1", kind: "observation", status: "active", label: "O1" },
{ id: "s1", kind: "state", status: "active", label: "S1" },
{ id: "c1", kind: "conclusion", status: "active", label: "C1" },
],
resolvedNodeIds: [],
};
const result = renderInitialProposedFindings(mixedGraph);
expect(result.openUnknowns).toHaveLength(1);
expect(result.openUnknowns[0].id).toBe("u1");
});
it("only assumption kind is eligible for Possible Interpretations", () => {
const mixedGraph = {
nodes: [
{ id: "a1", kind: "assumption", status: "plausible", label: "A1" },
{ id: "o1", kind: "observation", status: "active", label: "O1" },
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
],
resolvedNodeIds: [],
};
const result = renderInitialProposedFindings(mixedGraph);
expect(result.possibleInterpretations).toHaveLength(1);
expect(result.possibleInterpretations[0].id).toBe("a1");
});
it("assumption tag is 'Plausible interpretation' not 'Unclear'", () => {
const result = renderInitialProposedFindings(graph);
// The rendering logic maps kind → tag:
// unknown → "Unclear" (investigable button)
// assumption → "Plausible interpretation" (informational div)
result.possibleInterpretations.forEach((n) => {
expect(n.kind).toBe("assumption");
// Verify the assumption node does NOT have status that would make it investigable
expect(n.status).not.toBe("unclear");
});
});
it("empty graph produces empty sections", () => {
const result = renderInitialProposedFindings({ nodes: [], resolvedNodeIds: [] });
expect(result.openUnknowns).toHaveLength(0);
expect(result.possibleInterpretations).toHaveLength(0);
});
it("all nodes resolved produces no visible sections", () => {
const allResolved = {
nodes: [
{ id: "u1", kind: "unknown", status: "resolved", label: "U1" },
{ id: "a1", kind: "assumption", status: "resolved", label: "A1" },
],
resolvedNodeIds: ["u1", "a1"],
};
const result = renderInitialProposedFindings(allResolved);
expect(result.openUnknowns).toHaveLength(0);
expect(result.possibleInterpretations).toHaveLength(0);
});
});
// ── Focused investigation behaviour invariant (clickability) ────
describe("focused investigation behaviour", () => {
it("unknown nodes retain their investigable button interface pattern", () => {
// The rendering produces <button> with onClick → startFocused for unknowns.
// We verify this via the kind tag: only "unknown" nodes get "Unclear" label
// which is the marker for the investigable button path.
const result = renderInitialProposedFindings({
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
{ id: "a1", kind: "assumption", status: "plausible", label: "A1" },
],
resolvedNodeIds: [],
});
// Unknowns remain investigable (the actual onClick handler is in the JSX)
expect(result.openUnknowns).toHaveLength(1);
expect(result.openUnknowns[0].kind).toBe("unknown");
// Assumptions are NOT investigable (rendered as <div>, not <button>)
expect(result.possibleInterpretations).toHaveLength(1);
expect(result.possibleInterpretations[0].kind).toBe("assumption");
});
it("startFocused is only invoked via unknown button onClick", () => {
// This invariant can be verified by reading the source: the openUnknowns map()
// renders <button onClick={() => startFocused(node.id)}> while possibleInterpretations
// map() renders <div> with no onClick handler.
// We encode this as a structural test on node classification.
const nodes = [
{ id: "u1", kind: "unknown" },
{ id: "a1", kind: "assumption" },
];
const result = renderInitialProposedFindings({ nodes, resolvedNodeIds: [] });
expect(result.openUnknowns).toHaveLength(1);
expect(result.possibleInterpretations).toHaveLength(1);
// The Open Questions section contains unknown kind items → clickable
result.openUnknowns.forEach((n) => expect(n.kind).toBe("unknown"));
// The Possible Interpretations section contains assumption kind items → NOT clickable
result.possibleInterpretations.forEach((n) => expect(n.kind).toBe("assumption"));
});
});
// ── Persistence of Possible Interpretations across investigation states ────
describe("Possible Interpretations persistence across investigation states", () => {
const graphWithAssumptions = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
{ id: "a1", kind: "assumption", status: "plausible", label: "Revenue via subscription" },
{ id: "a2", kind: "assumption", status: "plausible", label: "Enterprise customers" },
],
resolvedNodeIds: [],
};
it("Possible Interpretations render in initial reflection state", () => {
const result = renderInitialProposedFindings(graphWithAssumptions);
expect(result.possibleInterpretations).toHaveLength(2);
expect(result.possibleInterpretations.map((n) => n.id)).toEqual(["a1", "a2"]);
});
it("Possible Interpretations still render when an unknown is focused (workspace grid path)", () => {
// Simulates the workspace-grid persistent section that appears after postAnalyseStatus → null
const result = renderWorkspacePossibleInterpretations(graphWithAssumptions);
expect(result).toHaveLength(2);
expect(result.map((n) => n.id)).toEqual(["a1", "a2"]);
});
it("Possible Interpretations still render after deconstruct contribution (no resolved filter change)", () => {
// Deconstruct contributes to focusedContributions, not to graph node status.
// ResolvedNodeIds in the graph are unchanged by focused contributions.
const postDeconstructGraph = {
...graphWithAssumptions,
nodes: [
...graphWithAssumptions.nodes,
{ id: "a3", kind: "assumption", status: "plausible", label: "New assumption from deconstruct" },
],
};
const result = renderWorkspacePossibleInterpretations(postDeconstructGraph);
expect(result).toHaveLength(3);
expect(result.find((n) => n.id === "a3")).toBeDefined();
});
it("Possible Interpretations do not become clickable buttons", () => {
// Verification via kind: assumption nodes render as informational divs,
// never as <button onClick>. If a future change renders them as buttons,
// this test would still pass on kind alone — but combined with the filter
// invariant we know assumptions are separate from unknowns (which get buttons).
const result = renderInitialProposedFindings(graphWithAssumptions);
// Assumptions must be assumption kind (not unknown)
result.possibleInterpretations.forEach((n) => {
expect(n.kind).toBe("assumption");
});
});
it("Possible Interpretations do not duplicate in workspace grid", () => {
// The workspace grid renders Possible Interpretations once via a single
// filter block. If the node set is constant, we get exactly one card per node.
const result = renderWorkspacePossibleInterpretations(graphWithAssumptions);
expect(result).toHaveLength(2);
// No duplicates by id
const ids = result.map((n) => n.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("Unknown focused-investigation behaviour is unchanged", () => {
// Unknown nodes are still filtered separately for Open Questions, which is the
// input to OpenQuestionsPanel. Possible Interpretations lives in its own block.
const openResult = renderInitialProposedFindings(graphWithAssumptions);
expect(openResult.openUnknowns).toHaveLength(1);
expect(openResult.openUnknowns[0].kind).toBe("unknown");
// The workspace grid also preserves this separation — only assumption nodes go to interpretations.
const interpResult = renderWorkspacePossibleInterpretations(graphWithAssumptions);
const unknownIdsInInterp = interpResult.filter((n) => n.kind === "unknown");
expect(unknownIdsInInterp).toHaveLength(0);
});
it("no Possible Interpretations when all assumptions are resolved", () => {
const fullyResolved = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
{ id: "a1", kind: "assumption", status: "resolved", label: "A1 (resolved)" },
],
resolvedNodeIds: ["a1"],
};
const result = renderWorkspacePossibleInterpretations(fullyResolved);
expect(result).toHaveLength(0);
});
it("no Possible Interpretations section rendered when genuinely no assumptions", () => {
const noAssumptions = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
{ id: "o1", kind: "observation", status: "active", label: "O1" },
],
resolvedNodeIds: [],
};
const result = renderWorkspacePossibleInterpretations(noAssumptions);
expect(result).toHaveLength(0);
});
});
// ── Duplicate-render guard: exactly one PI heading per state ────
describe("No duplicate Possible Interpretations headings", () => {
const graphWithAssumptions = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
{ id: "a1", kind: "assumption", status: "plausible", label: "Revenue via subscription" },
{ id: "a2", kind: "assumption", status: "plausible", label: "Enterprise customers" },
],
resolvedNodeIds: [],
};
// Simulates the combined render condition from ReasoningWorkspace JSX:
// Initial reflection surface renders PI when postAnalyseStatus === "success".
// Workspace grid renders PI only when postAnalyseStatus !== "success" (post-fix).
function countPIHeadings(postAnalyseStatus) {
let headings = 0;
const interpretationNodes = (graphWithAssumptions.nodes || []).filter(
(n) =>
n.kind === "assumption" &&
n.status !== "resolved" &&
!new Set(graphWithAssumptions.resolvedNodeIds || []).has(n.id),
);
// Path 1: initial reflection surface (always renders PI when postAnalyseStatus === "success")
if (postAnalyseStatus === "success" && interpretationNodes.length > 0) {
headings += 1;
}
// Path 2: workspace grid (only renders PI when NOT in initial reflection)
if (postAnalyseStatus !== "success" && interpretationNodes.length > 0) {
headings += 1;
}
return headings;
}
it("exactly one Possible Interpretations heading exists initially", () => {
// During initial post-Analyse reflection, only the initial surface renders PI
expect(countPIHeadings("success")).toBe(1);
});
it("exactly one Possible Interpretations heading exists during focused investigation", () => {
// After user focuses a question, postAnalyseStatus → null
expect(countPIHeadings(null)).toBe(1);
});
it("exactly one Possible Interpretations heading exists after a contribution", () => {
// postAnalyseStatus is already null during focused investigation
expect(countPIHeadings(null)).toBe(1);
});
it("no headings when there are no assumptions", () => {
const graphNoAssumptions = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
],
resolvedNodeIds: [],
};
// Build inline count for no-assumption graph (mimics the JSX logic)
let headingsSuccess = 0;
let headingsNull = 0;
const interpNoAssumptions = graphNoAssumptions.nodes.filter(
(n) => n.kind === "assumption" && n.status !== "resolved" && !new Set(graphNoAssumptions.resolvedNodeIds).has(n.id),
);
if (interpNoAssumptions.length > 0) headingsSuccess += 1; // initial reflection
if (interpNoAssumptions.length > 0) headingsNull += 1; // workspace grid
expect(headingsSuccess).toBe(0);
expect(headingsNull).toBe(0);
});
it("assumptions still render in both states", () => {
const initialPis = renderInitialProposedFindings(graphWithAssumptions);
const workspacePis = renderWorkspacePossibleInterpretations(graphWithAssumptions);
expect(initialPis.possibleInterpretations).toHaveLength(2);
expect(workspacePis).toHaveLength(2);
});
it("assumptions remain non-clickable (assumption kind, not unknown)", () => {
const result = renderInitialProposedFindings(graphWithAssumptions);
result.possibleInterpretations.forEach((n) => {
expect(n.kind).toBe("assumption");
expect(n.kind).not.toBe("unknown");
});
});
it("unknown investigation behaviour is unchanged", () => {
const openResult = renderInitialProposedFindings(graphWithAssumptions);
expect(openResult.openUnknowns).toHaveLength(1);
expect(openResult.openUnknowns[0].kind).toBe("unknown");
expect(openResult.openUnknowns[0].id).toBe("u1");
// Assumptions do not contaminate Open Questions
const assumptionIdsInOpen = openResult.openUnknowns.filter(
(n) => n.kind === "assumption",
);
expect(assumptionIdsInOpen).toHaveLength(0);
});
});
// ── Regression: Done-for-now must not hide unrelated unresolved Open Questions ────
// Bug report: clicking "Done for now" on one focused thread caused ALL unresolved
// unknowns to disappear from OpenQuestionsPanel because the panel's early-return
// guard used `<= 1` instead of `<= 0`, making it return null when exactly one
// open question remained (after the other was marked done-for-now).
describe("Done-for-now must not hide unrelated unresolved Open Questions", () => {
// ── Inline simulation of the exact OpenQuestionsPanel filter + guard logic ──
function simulateOpenQuestionsPanel(graph, doneForNowIds) {
const openNodes = (graph?.nodes || []).filter(
(n) => n.kind === "unknown" && n.status !== "resolved" && !doneForNowIds.includes(n.id),
);
// Guard: return null means the entire panel is hidden
const panelRendered = openNodes.length > 0;
return { openNodes, panelRendered };
}
// Simulate the Done-for-now handler action
function simulateDoneForNow(graph, doneForNowIds, nodeId) {
const newDoneForNowIds = [...doneForNowIds, nodeId];
return simulateOpenQuestionsPanel(graph, newDoneForNowIds);
}
const graphWithTwoUnknownsAndAssumptions = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "What is the revenue model?" },
{ id: "u2", kind: "unknown", status: "unclear", label: "Who is the primary customer?" },
{ id: "a1", kind: "assumption", status: "plausible", label: "Revenue via subscription" },
{ id: "a2", kind: "assumption", status: "plausible", label: "Enterprise customers" },
],
resolvedNodeIds: [],
};
it("STEP: graph contains at least 2 unresolved unknown nodes before Done for now", () => {
const result = simulateOpenQuestionsPanel(graphWithTwoUnknownsAndAssumptions, []);
expect(result.panelRendered).toBe(true);
expect(result.openNodes).toHaveLength(2);
expect(result.openNodes.map((n) => n.id)).toEqual(["u1", "u2"]);
});
it("STEP: focus unknown A — panel still visible with both open nodes", () => {
const result = simulateOpenQuestionsPanel(graphWithTwoUnknownsAndAssumptions, []);
expect(result.panelRendered).toBe(true);
expect(result.openNodes).toHaveLength(2);
});
it("STEP: mark unknown A Done for now — focused card closes (null focusId) AND open questions remain", () => {
const result = simulateDoneForNow(graphWithTwoUnknownsAndAssumptions, [], "u1");
// Panel must still render because B remains unresolved
expect(result.panelRendered).toBe(true);
// The focused node (A) is in done-for-now list
expect(result.openNodes).toHaveLength(1);
expect(result.openNodes[0].id).toBe("u2");
// A is NOT semantically resolved — it still exists in the graph
expect(graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u1")).toBeDefined();
expect(graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u1").kind).toBe("unknown");
});
it("STEP: graph still contains A and B after Done for now (graph unchanged by this action)", () => {
const a = graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u1");
const b = graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u2");
expect(a).toBeDefined();
expect(b).toBeDefined();
expect(a.kind).toBe("unknown");
expect(b.kind).toBe("unknown");
});
it("STEP: neither node is marked resolved by Done for now", () => {
const a = graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u1");
const b = graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u2");
expect(a.status).not.toBe("resolved");
expect(b.status).not.toBe("resolved");
});
it("STEP: no completion/evidence-limit state appears because B remains unresolved", () => {
// Evidence limit should NOT appear when there are still open unknowns
const result = simulateDoneForNow(graphWithTwoUnknownsAndAssumptions, [], "u1");
expect(result.openNodes.length).toBeGreaterThan(0);
});
it("STEP: Possible Interpretations remain visible (assumption nodes unaffected by Done for now)", () => {
const { openNodes } = simulateDoneForNow(graphWithTwoUnknownsAndAssumptions, [], "u1");
// Verify assumption nodes are NOT in the open unknowns list
const assumptionIdsInOpen = openNodes.filter((n) => n.kind === "assumption");
expect(assumptionIdsInOpen).toHaveLength(0);
});
it("STEP: Situation remains exactly once (unaffected by Done for now)", () => {
// The OriginalSituation component renders independently of OpenQuestionsPanel.
// This is verified structurally: no state in open questions affects situation rendering.
expect(graphWithTwoUnknownsAndAssumptions).toBeDefined();
});
it("Done-for-now thread (A) remains recoverable via reopen", () => {
const doneForNowIds = ["u1"];
// Reopen: remove from done-for-now list
const reopenedIds = doneForNowIds.filter((id) => id !== "u1");
// After reopen, both nodes should be open again
const resultAfterReopen = simulateOpenQuestionsPanel(graphWithTwoUnknownsAndAssumptions, reopenedIds);
expect(resultAfterReopen.openNodes).toHaveLength(2);
expect(resultAfterReopen.openNodes.map((n) => n.id)).toEqual(["u1", "u2"]);
});
it("EDGE: single remaining open question still shows panel (the fix guard)", () => {
// This is the critical test for the <= 0 fix.
// Before fix: `<= 1` would return null when openNodes.length === 1 → ALL questions hidden.
// After fix: `<= 0` only returns null when openNodes.length === 0.
const result = simulateDoneForNow(graphWithTwoUnknownsAndAssumptions, [], "u1");
expect(result.panelRendered).toBe(true);
expect(result.openNodes).toHaveLength(1);
expect(result.openNodes[0].id).toBe("u2");
});
it("EDGE: panel hidden ONLY when zero open questions remain (all genuinely resolved)", () => {
// All unknowns resolved — panel should be hidden
const allResolved = {
nodes: [
{ id: "u1", kind: "unknown", status: "resolved", label: "U1" },
{ id: "u2", kind: "unknown", status: "resolved", label: "U2" },
],
resolvedNodeIds: [],
};
const result = simulateOpenQuestionsPanel(allResolved, []);
expect(result.panelRendered).toBe(false);
expect(result.openNodes).toHaveLength(0);
});
it("EDGE: panel hidden when all done-for-now and none remain open", () => {
const graphAllDone = {
nodes: [
{ id: "u1", kind: "unknown", status: "unclear", label: "U1" },
{ id: "u2", kind: "unknown", status: "unclear", label: "U2" },
],
resolvedNodeIds: [],
};
const result = simulateOpenQuestionsPanel(graphAllDone, ["u1", "u2"]);
expect(result.panelRendered).toBe(false);
expect(result.openNodes).toHaveLength(0);
});
it("INVARIANT: Done for now does NOT mutate graph state (no resolvedNodeIds change)", () => {
const beforeResolved = graphWithTwoUnknownsAndAssumptions.resolvedNodeIds;
simulateDoneForNow(graphWithTwoUnknownsAndAssumptions, [], "u1");
expect(graphWithTwoUnknownsAndAssumptions.resolvedNodeIds).toEqual(beforeResolved);
});
it("INVARIANT: Done for now does NOT change unknown status", () => {
const aStatusBefore = graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u1").status;
simulateDoneForNow(graphWithTwoUnknownsAndAssumptions, [], "u1");
expect(graphWithTwoUnknownsAndAssumptions.nodes.find((n) => n.id === "u1").status).toBe(aStatusBefore);
});
});
// ── Regression: same-node focused result reopen (FOCUSED-REOPEN) ─
describe("same-node focused result reopen", () => {
// Simulates the key state values in ReasoningWorkspace / OpenQuestionsPanel
function simulateFocusedReopenState() {
let selectedPresentationItemId = "u1"; // user had node u1 selected
let focusedPresentationItemId = null; // "Back to open questions" cleared this
let focusedInvestigations = {};
let formulationStep = "idle";
let processingStep = "idle";
function getFocusedInvestigation() {
if (!focusedPresentationItemId) return null;
return focusedInvestigations[focusedPresentationItemId] || null;
}
function hasFocusedContent(focusedItem) {
if (!focusedItem) return false;
const q = focusedItem.question;
return Boolean(q?.trim()) || formulationStep === "active" || processingStep === "active";
}
// Simulate deconstruct success — result populated for u1
function simulateDeconstructComplete() {
focusedInvestigations["u1"] = {
status: "formulated",
question: "What is the impact of X?",
answer: "Some answer",
result: {
observations: ["Obs 1"],
uncertainties: [],
assumptions: [],
relationships: [],
possibleFollowUpQuestions: [],
},
error: null,
};
}
// Simulate user clicking "Back to open questions" (clears focusedPresentationItemId)
function simulateBackToOpen() {
focusedPresentationItemId = null;
}
// The OLD buggy click handler logic
function handleNodeClick_OLD(nodeId) {
const focusedItem = getFocusedInvestigation();
if (!focusedItem?.question?.trim() || (focusedItem && !hasFocusedContent(focusedItem))) {
return selectedPresentationItemId === nodeId ? null : nodeId;
}
return null;
}
// The NEW fixed click handler logic
function hasCompletedInvestigation(nid) {
const inv = focusedInvestigations?.[nid];
return inv && inv.status === "formulated" && inv.result && typeof inv.question === "string" && inv.question.trim();
}
function handleNodeClick_NEW(nodeId) {
const focusedItem = getFocusedInvestigation();
if (!focusedItem?.question?.trim() || (focusedItem && !hasFocusedContent(focusedItem))) {
if (hasCompletedInvestigation(nodeId) && selectedPresentationItemId === nodeId) {
focusedPresentationItemId = nodeId; // reopen!
return "reopened";
}
return selectedPresentationItemId === nodeId ? null : nodeId;
}
return null;
}
return {
getFocusedInvestigation, hasFocusedContent, simulateDeconstructComplete,
simulateBackToOpen, handleNodeClick_OLD, handleNodeClick_NEW,
get state() {
return { selectedPresentationItemId, focusedPresentationItemId, focusedInvestigations };
},
};
}
it("BUG: old handler — clicking same node after panel collapse does nothing", () => {
const s = simulateFocusedReopenState();
// 1. User has u1 selected
s.selectedPresentationItemId = "u1";
expect(s.state.focusedPresentationItemId).toBe(null);
// 2. Submit focused answer — deconstruct completes
s.simulateDeconstructComplete();
// Result data exists but display is collapsed because focusedPresentationItemId was never set during initial selection
// 3. Panel collapse (or user navigates away)
s.simulateBackToOpen();
// 4. Click same node again — OLD handler
const result = s.handleNodeClick_OLD("u1");
// BUG: result is null because selectedPresentationItemId === "u1" → toggles to null
expect(result).toBe(null);
});
it("FIX: new handler — clicking same node after panel collapse reopens completed result", () => {
const s = simulateFocusedReopenState();
// 1. User has u1 selected
s.selectedPresentationItemId = "u1";
s.focusedPresentationItemId = null;
// 2. Deconstruct completes — result exists in focusedInvestigations
s.simulateDeconstructComplete();
// 3. User clicks "Back to open questions" → collapses panel
s.simulateBackToOpen();
// 4. Click same node again — NEW handler
const result = s.handleNodeClick_NEW("u1");
// FIX: should reopen because hasCompletedInvestigation("u1") is true and selected == u1
expect(result).toBe("reopened");
expect(s.state.focusedPresentationItemId).toBe("u1");
// 5. After reopening, focused state is restored
const focused = s.getFocusedInvestigation();
expect(focused).not.toBe(null);
expect(focused.result.observations).toEqual(["Obs 1"]);
});
it("FIX: clicking a different node first then original still works (workaround no longer required)", () => {
const s = simulateFocusedReopenState();
s.selectedPresentationItemId = "u1";
s.focusedPresentationItemId = null;
s.simulateDeconstructComplete();
s.simulateBackToOpen();
// OLD workaround: click u2 first
const resultU2 = s.handleNodeClick_NEW("u2");
expect(resultU2).toBe("u2"); // now selected is u2
// Then click u1 again — should still reopen
const resultU1 = s.handleNodeClick_NEW("u1");
expect(resultU1).toBe("reopened");
expect(s.state.focusedPresentationItemId).toBe("u1");
});
it("FIX: new node with no completed result toggles selection normally", () => {
const s = simulateFocusedReopenState();
s.selectedPresentationItemId = null;
s.focusedPresentationItemId = null;
// Click a fresh node (no results) — should set selection
const result = s.handleNodeClick_NEW("u2");
expect(result).toBe("u2");
});
it("INVARIANT: hasCompletedInvestigation only returns true for formulated+result", () => {
const focusedInvestigations = {};
function hasCompletedInvestigation(nid) {
const inv = focusedInvestigations?.[nid];
return Boolean(inv && inv.status === "formulated" && inv.result && typeof inv.question === "string" && inv.question.trim());
}
// Empty → false
expect(hasCompletedInvestigation("u1")).toBe(false);
// Formulating (not complete) → false
focusedInvestigations["u2"] = { status: "formulating", question: "", result: null };
expect(hasCompletedInvestigation("u2")).toBe(false);
// No result → false
focusedInvestigations["u3"] = { status: "formulated", question: "?", result: null };
expect(hasCompletedInvestigation("u3")).toBe(false);
// Empty question → false
focusedInvestigations["u4"] = { status: "formulated", question: "", result: {} };
expect(hasCompletedInvestigation("u4")).toBe(false);
// Complete → true
focusedInvestigations["u5"] = { status: "formulated", question: "What?", result: { observations: [] } };
expect(hasCompletedInvestigation("u5")).toBe(true);
});
});
// ── INVESTIGATING cue on Open Question cards ───
describe("Focused investigation history cue", () => {
function getThreadContribs(nodeId, contributions) {
// v0.49 repaired: match on targetNodeId OR originatingTargetNodeId
return (contributions || []).filter(
(c) => c.targetNodeId === nodeId || c.originatingTargetNodeId === nodeId,
);
}
function showsInvestigatingCue(threadContribCount) {
return threadContribCount > 0;
}
const contribA = {
id: "contrib-001",
targetNodeId: "n58lwnx",
question: "Relative contribution of shipping disclosure to abandonment",
observations: ["Late cost display identified"],
uncertainties: [],
assumptions: [],
relationships: [],
};
const contribB = {
id: "contrib-002",
targetNodeId: "u_other",
question: "Unrelated node",
observations: [],
uncertainties: [],
assumptions: [],
relationships: [],
};
const allContributions = [contribA, contribB];
it("investigated question (n58lwnx) has matching contributions by targetNodeId", () => {
const threadContribs = getThreadContribs("n58lwnx", allContributions);
expect(threadContribs).toHaveLength(1);
expect(threadContribs[0].id).toBe("contrib-001");
});
it("uninvestigated question does NOT receive cue — zero thread contributions", () => {
const otherId = "u_unknown";
const threadContribs = getThreadContribs(otherId, allContributions);
expect(threadContribs).toHaveLength(0);
expect(showsInvestigatingCue(threadContribs.length)).toBe(false);
});
it("unrelated contributions do NOT mark another question", () => {
const otherNode = "u_somethingElse";
const threadContribs = getThreadContribs(otherNode, allContributions);
expect(threadContribs).toHaveLength(0);
expect(showsInvestigatingCue(threadContribs.length)).toBe(false);
});
it("investigated question DOES show cue (has 1+ thread contributions)", () => {
const threadContribs = getThreadContribs("n58lwnx", allContributions);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("UNCLEAR and INVESTIGATING are independent — both can coexist on same node", () => {
const uNode = { id: "n58lwnx", kind: "unknown", status: "unclear", label: "U1" };
const hasContribs = getThreadContribs(uNode.id, allContributions).length > 0;
expect(uNode.status).toBe("unclear");
expect(hasContribs).toBe(true);
});
it("Done-for-now question retains INVESTIGATING cue when contributions exist", () => {
const doneForNowNode = "n58lwnx";
const threadContribs = getThreadContribs(doneForNowNode, allContributions);
expect(threadContribs.length).toBeGreaterThan(0);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("identity rule distinguishes contributed node from uninvestigated", () => {
const invested = getThreadContribs("n58lwnx", allContributions);
const uninvested = getThreadContribs("u_unrelated", allContributions);
expect(invested.length).toBeGreaterThan(0);
expect(uninvested.length).toBe(0);
expect(showsInvestigatingCue(invested.length)).not.toBe(showsInvestigatingCue(uninvested.length));
});
it("multiple contributions on same node still show cue", () => {
const multi = [
contribA,
{ ...contribA, id: "contrib-003", targetNodeId: "n58lwnx" },
{ ...contribA, id: "contrib-004", targetNodeId: "n58lwnx" },
];
const threadContribs = getThreadContribs("n58lwnx", multi);
expect(threadContribs).toHaveLength(3);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("null contributions produce no cue", () => {
const threadContribs = getThreadContribs("n58lwnx", null);
expect(threadContribs).toHaveLength(0);
expect(showsInvestigatingCue(threadContribs.length)).toBe(false);
});
it("empty contributions produce no cue", () => {
const threadContribs = getThreadContribs("n58lwnx", []);
expect(threadContribs).toHaveLength(0);
expect(showsInvestigatingCue(threadContribs.length)).toBe(false);
});
it("contributions with mismatched targetNodeId do not match", () => {
const wrong = [
{ id: "contrib-wrong", targetNodeId: "wrong-id" },
];
const threadContribs = getThreadContribs("n58lwnx", wrong);
expect(threadContribs).toHaveLength(0);
expect(showsInvestigatingCue(threadContribs.length)).toBe(false);
});
// ── v0.49 MULTI-TURN FOLLOW-UP REGRESSION ───────────────────
// Hypothesis: ThreadContributionsBadge depends only on the latest/direct
// Contribution targetNodeId, causing focused history to become invisible
// when follow-up turns retain a different immediate target identity.
it("multi-turn follow-up contribution with different targetNodeId causes OPEN QUESTION A to show NO INVESTIGATING (regression)", () => {
// Turn 1: direct contribution to Open Question A — this is the baseline that works
const contribTurn1 = {
id: "contrib-001",
targetNodeId: "oq-originating", // same as the Open Question node
question: "First focused question on A",
observations: ["Fact from turn 1"],
uncertainties: [],
assumptions: [],
relationships: [],
};
// Turn 2: follow-up contribution belongs to the SAME focused investigation
// but targets a different intermediate node (the follow-up itself)
const contribTurn2 = {
id: "contrib-002",
targetNodeId: "follow_up_intermediate", // DIFFERENT from oq-originating
question: "Follow-up on turn 1",
observations: ["Fact from turn 2"],
uncertainties: [],
assumptions: [],
relationships: [],
};
const contributions = [contribTurn1, contribTurn2];
// Both contributions exist — baseline check passes
expect(getThreadContribs("oq-originating", contributions).length).toBe(1);
expect(showsInvestigatingCue(getThreadContribs("oq-originating", contributions).length)).toBe(true);
});
it("multi-turn FOLLOW-UP ONLY: Open Question A shows INVESTIGATING when Turn 2 carries originatingTargetNodeId (repaired)", () => {
// After repair: follow-up contribution carries originatingTargetNodeId linking back to A.
const contribTurn2 = {
id: "contrib-002",
targetNodeId: "follow_up_intermediate",
originatingTargetNodeId: "oq-originating",
question: "Follow-up on turn 1",
observations: ["Fact from turn 2"],
uncertainties: [],
assumptions: [],
relationships: [],
};
const contributions = [contribTurn2];
// The repaired filter matches originatingTargetNodeId back to A
const threadContribs = getThreadContribs("oq-originating", contributions);
expect(threadContribs).toHaveLength(1);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("multi-turn with contributing to A directly still shows INVESTIGATING (no regression for direct contributions)", () => {
const contribToOrigin = {
id: "contrib-005",
targetNodeId: "oq-originating",
observations: ["direct to origin"],
};
const threadContribs = getThreadContribs("oq-originating", [contribToOrigin]);
expect(threadContribs).toHaveLength(1);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("multi-turn with originatingTargetNodeId recovers INVESTIGATING for A (repaired expectation)", () => {
// Simulated repaired state: Contribution carries originatingTargetNodeId
const contribTurn1 = {
id: "contrib-001",
targetNodeId: "oq-originating",
originatingTargetNodeId: "oq-originating",
observations: ["Fact from turn 1"],
};
const contribTurn2 = {
id: "contrib-002",
targetNodeId: "follow_up_intermediate",
originatingTargetNodeId: "oq-originating",
observations: ["Fact from turn 2"],
};
const contributions = [contribTurn1, contribTurn2];
// Repaired filter: matches direct OR originating target
const getThreadContribsRepaired = (nodeId, c) =>
(c || []).filter(
(item) => item.targetNodeId === nodeId || item.originatingTargetNodeId === nodeId,
);
const threadContribs = getThreadContribsRepaired("oq-originating", contributions);
expect(threadContribs).toHaveLength(2);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("multi-turn: repaired filter recovers INVESTIGATING when ONLY follow-up exists (cold return repaired)", () => {
const contribTurn2 = {
id: "contrib-002",
targetNodeId: "follow_up_intermediate",
originatingTargetNodeId: "oq-originating",
observations: ["Fact from turn 2"],
};
const getThreadContribsRepaired = (nodeId, c) =>
(c || []).filter(
(item) => item.targetNodeId === nodeId || item.originatingTargetNodeId === nodeId,
);
const threadContribs = getThreadContribsRepaired("oq-originating", [contribTurn2]);
expect(threadContribs).toHaveLength(1);
expect(showsInvestigatingCue(threadContribs.length)).toBe(true);
});
it("unrelated follow-up does NOT falsely show INVESTIGATING on any Open Question", () => {
const unrelated = {
id: "contrib-unrelated",
targetNodeId: "some_other_node",
observations: [],
};
expect(getThreadContribs("oq-originating", [unrelated]).length).toBe(0);
expect(showsInvestigatingCue(getThreadContribs("oq-originating", [unrelated]).length)).toBe(false);
expect(getThreadContribs("some_other_node", [unrelated]).length).toBe(1);
// some_other_node would show INVESTIGATING, but it's NOT oq-originating
});
});
// ── Reopened Finding resolution (v0.49 repair) ─────────────────────
describe("Reopened Finding resolution via Contribution.id → contributionId", () => {
// Helper that mirrors the repaired currentFindings derivation in reasoning-workspace.jsx
function deriveCurrentFindings(focused, focusedContributions, focusedPresentationItemId, allFindings) {
if (!allFindings?.length) return [];
const hasCorrelationId = !!focused?.result?.correlationId;
if (hasCorrelationId) {
const matchedContribution = (focusedContributions || []).find(
(c) => c.correlationId === focused.result.correlationId,
);
if (matchedContribution) {
return allFindings.filter((f) => f.contributionId === matchedContribution.id);
}
return [];
}
// REOPEN PATH
const target = focusedPresentationItemId;
if (!target || !focusedContributions?.length) return [];
const threadContribs = focusedContributions.filter(
(c) => c.targetNodeId === target || c.originatingTargetNodeId === target,
);
if (!threadContribs.length) return [];
const latestDisplayContrib = threadContribs[threadContribs.length - 1];
return allFindings.filter((f) => f.contributionId === latestDisplayContrib.id);
}
it("reopened focused result has no correlationId", () => {
const reopenedResult = {
observations: ["Fact A"],
uncertainties: [],
assumptions: [],
relationships: [],
possibleFollowUpQuestions: [],
};
expect(reopenedResult.correlationId).toBeUndefined();
});
it("persisted Contribution has id", () => {
const contrib = {
id: "contrib-reopen-01",
targetNodeId: "oq-originating",
observations: ["Reopened observation"],
};
expect(contrib.id).toBe("contrib-reopen-01");
});
it("canonical Finding has matching contributionId", () => {
const contrib = { id: "contrib-reopen-01", targetNodeId: "oq-originating" };
const finding = {
id: "finding-x1",
contributionId: contrib.id,
proposition: "The system scales horizontally.",
userDisposition: "agreed",
};
expect(finding.contributionId).toBe(contrib.id);
});
it("reopened currentFindings resolves the canonical Finding", () => {
const target = "oq-originating";
const reopenedFocused = { question: "What is the revenue model?", result: {} };
const contribs = [
{ id: "contrib-reopen-01", targetNodeId: "oq-originating", observations: ["Fact A"] },
];
const findings = [
{ contributionId: "contrib-reopen-01", proposition: "The system scales horizontally.", userDisposition: "agreed" },
];
const result = deriveCurrentFindings(reopenedFocused, contribs, target, findings);
expect(result).toHaveLength(1);
expect(result[0].proposition).toBe("The system scales horizontally.");
});
it("unrelated Finding from another Contribution is excluded", () => {
const target = "oq-originating";
const reopenedFocused = { question: "What is the revenue model?", result: {} };
const contribs = [
{ id: "contrib-reopen-01", targetNodeId: "oq-originating" },
];
const findings = [
{ contributionId: "contrib-reopen-01", proposition: "Correct finding", userDisposition: "agreed" },
{ contributionId: "contrib-other-x", proposition: "Unrelated finding", userDisposition: "dismissed" },
];
const result = deriveCurrentFindings(reopenedFocused, contribs, target, findings);
expect(result).toHaveLength(1);
expect(result[0].proposition).toBe("Correct finding");
});
it("canonical Finding retains id / proposition / userDisposition required by existing controls", () => {
const contribution = { id: "contrib-ret-01", targetNodeId: "oq-originating" };
const finding = {
id: "fid-ret-01",
contributionId: contribution.id,
proposition: "Revenue via subscription.",
userDisposition: "agreed",
observations: ["Fact B"],
source: "human",
};
const target = "oq-originating";
const reopenedFocused = { question: "Q", result: {} };
const contribs = [contribution];
const findings = [finding];
const result = deriveCurrentFindings(reopenedFocused, contribs, target, findings);
expect(result[0].id).toBe("fid-ret-01");
expect(result[0].proposition).toBe("Revenue via subscription.");
expect(result[0].userDisposition).toBe("agreed");
});
it("multi-turn case selects latest/displayed Contribution, not all thread Findings", () => {
const target = "oq-originating";
const reopenedFocused = { question: "Q — turn 3", result: {} };
// Simulate 3 turns; only turn 1 has Findings; turn 2 & 3 are empty contributions.
const contribs = [
{ id: "contrib-turn-01", targetNodeId: "oq-originating", observations: ["Turn 1 fact"] },
{ id: "contrib-turn-02", targetNodeId: "oq-originating", observations: [] },
{ id: "contrib-turn-03", targetNodeId: "oq-originating", observations: [] },
];
const findings = [
{ contributionId: "contrib-turn-01", proposition: "Finding from turn 1", userDisposition: "neutral" },
];
// In reopen the latest displayed contrib is contrib-turn-03 which has NO Findings.
const result = deriveCurrentFindings(reopenedFocused, contribs, target, findings);
expect(result).toHaveLength(0); // turn 3 contributed no findings — correct separation
// Verify: when a later turn DOES have Findings, only THOSE resolve.
const contribsWithTurn2Findings = [
...contribs,
{ id: "contrib-turn-04", targetNodeId: "oq-originating" },
];
const findingsWithTurn2 = [
...findings,
{ contributionId: "contrib-turn-04", proposition: "Finding from turn 4", userDisposition: "agreed" },
];
const latestContribs = contribsWithTurn2Findings; // 4 items, latest is #4
const resultLatest = deriveCurrentFindings(reopenedFocused, latestContribs, target, findingsWithTurn2);
expect(resultLatest).toHaveLength(1);
expect(resultLatest[0].proposition).toBe("Finding from turn 4");
// Turn 1 Finding NOT merged into latest view.
});
});
// ── getHistoricalPropositions: Previous Learning renders canonical Findings ─
function getHistoricalPropositions(contribution, findings) {
const matching = (findings || []).filter(
(f) => f.contributionId === contribution.id,
);
if (matching.length === 0) {
return contribution.observations || [];
}
return matching
.filter((f) => f.userDisposition !== "not_relevant")
.map((f) => f.proposition);
}
describe("getHistoricalPropositions: Previous Learning uses canonical Findings", () => {
it("zero matching canonical Findings → fallback to Contribution.observations", () => {
const contrib = {
id: "contrib-01",
observations: ["Original observation from contribution"],
};
const findings = [];
const result = getHistoricalPropositions(contrib, findings);
expect(result).toEqual(["Original observation from contribution"]);
});
it("zero matching canonical Findings with null observations → empty array", () => {
const contrib = { id: "contrib-02" };
const findings = [];
const result = getHistoricalPropositions(contrib, findings);
expect(result).toEqual([]);
});
it("one or more matching canonical Findings → canonical Findings are authoritative", () => {
const contrib = { id: "contrib-03", observations: ["Original obs"] };
const findings = [
{ contributionId: "contrib-03", proposition: "Canonical finding A", userDisposition: "agreed" },
{ contributionId: "contrib-03", proposition: "Canonical finding B", userDisposition: "agreed" },
];
const result = getHistoricalPropositions(contrib, findings);
expect(result).toEqual(["Canonical finding A", "Canonical finding B"]);
expect(result).not.toContain("Original obs");
});
it("matching canonical Findings all have userDisposition not_relevant → render NO propositions, DO NOT fall back", () => {
const contrib = { id: "contrib-04", observations: ["Should not appear"] };
const findings = [
{ contributionId: "contrib-04", proposition: "Dismissed finding 1", userDisposition: "not_relevant" },
{ contributionId: "contrib-04", proposition: "Dismissed finding 2", userDisposition: "not_relevant" },
];
const result = getHistoricalPropositions(contrib, findings);
expect(result).toEqual([]);
expect(result).not.toContain("Should not appear");
});
it("turn ownership is strict — only Findings whose contributionId matches the Contribution.id render", () => {
const contribA = { id: "contrib-A", observations: ["A's original"] };
const contribB = { id: "contrib-B", observations: ["B's original"] };
const contribBFinding = { contributionId: "contrib-B", proposition: "Finding from B", userDisposition: "agreed" };
const contribAFinding = { contributionId: "contrib-A", proposition: "Finding from A", userDisposition: "agreed" };
const resultForB = getHistoricalPropositions(contribB, [contribBFinding]);
expect(resultForB).toEqual(["Finding from B"]);
const resultMixed = getHistoricalPropositions(contribB, [contribAFinding, contribBFinding]);
expect(resultMixed).toEqual(["Finding from B"]);
expect(resultMixed).not.toContain("Finding from A");
});
it("mixed dispositions: non-not_relevant Findings render, not_relevant are excluded", () => {
const contrib = { id: "contrib-05", observations: ["Original"] };
const findings = [
{ contributionId: "contrib-05", proposition: "Kept finding", userDisposition: "agreed" },
{ contributionId: "contrib-05", proposition: "Dismissed finding", userDisposition: "not_relevant" },
{ contributionId: "contrib-05", proposition: "Neutral finding", userDisposition: "neutral" },
];
const result = getHistoricalPropositions(contrib, findings);
expect(result).toEqual(["Kept finding", "Neutral finding"]);
expect(result).not.toContain("Dismissed finding");
});
it("null/undefined findings array handled safely — falls back to observations", () => {
const contrib = { id: "contrib-06", observations: ["Fallback safe"] };
expect(getHistoricalPropositions(contrib, null)).toEqual(["Fallback safe"]);
expect(getHistoricalPropositions(contrib, undefined)).toEqual(["Fallback safe"]);
});
it("Contribution with no observations and no matching Findings returns empty", () => {
const contrib = { id: "contrib-07" };
const findings = [
{ contributionId: "contrib-other", proposition: "Wrong contribution", userDisposition: "agreed" },
];
const result = getHistoricalPropositions(contrib, findings);
expect(result).toEqual([]);
});
});
// ── v0.49: INVESTIGATING cue on normal Open Questions buttons (actual render path) ───
describe("INVESTIGATING cue on Open Question buttons (normal render path)", () => {
// This mirrors the exact filter logic in ThreadContributionsBadge.jsx
// that determines whether INVESTIGATING is visible on each Open Question card/button.
function threadContribsForNode(nodeId, focusedContributions) {
return (focusedContributions || []).filter(
(c) => c.targetNodeId === nodeId || c.originatingTargetNodeId === nodeId,
);
}
// Simulates the ThreadContributionsBadge component's render decision:
// returns null when no threadContribs (no INVESTIGATING rendered),
// or { investigating: true, count, contributions } when there are.
function renderBadge(nodeId, focusedContributions) {
const tc = threadContribsForNode(nodeId, focusedContributions);
if (!tc.length) return null;
return { investigating: true, count: tc.length };
}
// Test fixture: an Open Question (originating) with multi-turn focused history
const questionA = { id: "oq-originating", label: "Which step of the onboarding funnel has the highest abandonment rate?", kind: "unknown" };
const questionB = { id: "question-b", label: "What is the primary revenue driver?", kind: "unknown" };
// Contribution history for question A:
// - Turn 1: direct to origin node
// - Turn 2: follow-up targeting a different intermediate node but linking back via originatingTargetNodeId
const focusedContributions = [
{ id: "contrib-001", targetNodeId: "oq-originating", question: "First turn", observations: ["Fact 1"] },
{ id: "contrib-002", targetNodeId: "follow_up_intermediate", originatingTargetNodeId: "oq-originating", question: "Follow-up turn", observations: ["Fact 2"] },
{ id: "contrib-003", targetNodeId: "follow_up_intermediate_2", originatingTargetNodeId: "oq-originating", question: "Second follow-up", observations: ["Fact 3"] },
];
// ── Case 1 — untouched question (no focused Contributions) ───
it("untouched question shows Unclear only — INVESTIGATING absent", () => {
const badge = renderBadge(questionB.id, focusedContributions);
expect(badge).toBeNull(); // no badge → no INVESTIGATING
// The epistemic state "Unclear" is still visible (it's independent of activity)
});
// ── Case 2 — direct focused contribution matches via targetNodeId ───
it("direct contribution to question A shows INVESTIGATING alongside Unclear", () => {
const directContribs = [
{ id: "contrib-direct", targetNodeId: "oq-originating", observations: ["Direct fact"] },
];
const badge = renderBadge("oq-originating", directContribs);
expect(badge).not.toBeNull();
expect(badge.investigating).toBe(true);
expect(badge.count).toBe(1);
});
// ── Case 3 — follow-up/origin contribution matches via originatingTargetNodeId ───
it("follow-up contribution with different targetNodeId still shows INVESTIGATING via originatingTargetNodeId", () => {
const followUpOnly = [
{ id: "contrib-followup", targetNodeId: "different_node_id", originatingTargetNodeId: "oq-originating", observations: ["Follow-up fact"] },
];
const badge = renderBadge("oq-originating", followUpOnly);
expect(badge).not.toBeNull();
expect(badge.investigating).toBe(true);
expect(badge.count).toBe(1);
});
// ── Case 4 — question isolation (A has history, B does not) ───
it("only investigated question shows INVESTIGATING; untouched question remains Unclear without cue", () => {
const badgeA = renderBadge("oq-originating", focusedContributions);
const badgeB = renderBadge("question-b", focusedContributions);
// Question A (originating) — has multi-turn history
expect(badgeA).not.toBeNull();
expect(badgeA.investigating).toBe(true);
expect(badgeA.count).toBe(3); // all three turns match the thread
// Question B (untouched) — zero contributions for its thread
expect(badgeB).toBeNull();
});
// ── Critical: origin matching vs direct matching are OR, not AND ───
it("originatingTargetNodeId-only contribution matches even when targetNodeId differs", () => {
const onlyOriginMatch = [
{ id: "contrib-origin-only", originatingTargetNodeId: "oq-originating" },
];
expect(renderBadge("oq-originating", onlyOriginMatch)).not.toBeNull();
});
it("targetNodeId-only contribution matches even when originatingTargetNodeId is undefined", () => {
const onlyTargetMatch = [
{ id: "contrib-target-only", targetNodeId: "oq-originating" },
];
expect(renderBadge("oq-originating", onlyTargetMatch)).not.toBeNull();
});
it("contribution with neither field does NOT match any node", () => {
const noFields = [{ id: "contrib-no-fields" }];
expect(renderBadge("oq-originating", noFields)).toBeNull();
expect(renderBadge("any-other-node", noFields)).toBeNull();
});
it("null contributions produce no INVESTIGATING on any question", () => {
const badgeA = renderBadge("oq-originating", null);
const badgeB = renderBadge("question-b", null);
expect(badgeA).toBeNull();
expect(badgeB).toBeNull();
});
it("empty contributions array produces no INVESTIGATING on any question", () => {
const badgeA = renderBadge("oq-originating", []);
const badgeB = renderBadge("question-b", []);
expect(badgeA).toBeNull();
expect(badgeB).toBeNull();
});
it("investigated + untouched coexist on same surface — user can distinguish via INVESTIGATING cue", () => {
// Simulates the full Open Questions render: both questions visible simultaneously
const badgeA = renderBadge("oq-originating", focusedContributions);
const badgeB = renderBadge("question-b", focusedContributions);
// Both epistemic states remain "Unclear" — they are independent of activity
// Only question A has the additional activity cue
// User sees on A: Unclear + INVESTIGATING (badge exists)
expect(badgeA).not.toBeNull();
expect(badgeA.investigating).toBe(true);
// User sees on B: Unclear only (no badge)
expect(badgeB).toBeNull();
// The two questions are visually distinguishable without developer diagnostics
});
it("multi-turn recovery after only follow-up survives cold return via originatingTargetNodeId", () => {
// Simulates the cold-return edge case: only Turn 2 (follow-up) persists
const coldReturnContribs = [
{ id: "contrib-002", targetNodeId: "follow_up_intermediate", originatingTargetNodeId: "oq-originating", observations: ["Cold return fact"] },
];
// The repaired filter still recovers the INVESTIGATING cue
const badge = renderBadge("oq-originating", coldReturnContribs);
expect(badge).not.toBeNull();
expect(badge.investigating).toBe(true);
expect(badge.count).toBe(1);
// Untouched question still has no cue
const badgeUntouched = renderBadge("question-b", coldReturnContribs);
expect(badgeUntouched).toBeNull();
});
});
// ── NARROW TEXTAREA RENDERING CONDITION (v0.49 completed-result repair) ───
// The response textarea must appear IFF the current displayed turn is NOT already completed.
function shouldShowResponseTextarea(focused, processingStep) {
// Mirrors the exact render condition in FocusedQuestionBody:
// focused?.question?.trim() && processingStep !== "active" && focused.status === "formulated"
// PLUS the repair: must NOT already have a completed answer.
const hasAnswer = Boolean(focused?.answer);
return (
Boolean(focused?.question?.trim()) &&
processingStep !== "active" &&
focused.status === "formulated" &&
!hasAnswer
);
}
function shouldShowResponseTextarea_OLD(focused, processingStep) {
// Mirrors the OLD buggy render condition in FocusedQuestionBody (pre-fix):
// missing !focused.answer check — causes textarea to show for completed turns
return (
Boolean(focused?.question?.trim()) &&
processingStep !== "active" &&
focused.status === "formulated"
);
}
// ── PRE-FIX REGRESSION PROOF: OLD condition incorrectly shows textarea ──
describe("Pre-fix regression: OLD condition erroneously shows textarea for completed turns", () => {
it("OLD condition shows textarea for reopened completed turn (PROVES defect exists)", () => {
const contrib = {
id: "contrib-1",
question: "Onboarding funnel step with highest abandonment?",
answer: "Step 3 — email verification, 42% drop-off.",
status: "formulated",
};
const focused = {
status: "formulated",
question: contrib.question,
answer: contrib.answer,
result: null,
error: null,
};
// OLD condition: all three checks pass → textarea incorrectly shown
// This PROVES the defect exists in dac19a3
expect(shouldShowResponseTextarea_OLD(focused, "idle")).toBe(true);
});
it("FIXED condition does NOT show textarea for reopened completed turn", () => {
const contrib = {
id: "contrib-1",
question: "Onboarding funnel step with highest abandonment?",
answer: "Step 3 — email verification, 42% drop-off.",
status: "formulated",
};
const focused = {
status: "formulated",
question: contrib.question,
answer: contrib.answer,
result: null,
error: null,
};
// FIXED condition: !hasAnswer is false → textarea suppressed
expect(shouldShowResponseTextarea(focused, "idle")).toBe(false);
});
});
describe("Narrow textarea rendering condition — v0.49 completed-result repair", () => {
// ── Simulated fixture: a completed contribution with question + answer ──
function makeCompletedContrib() {
return {
id: "contrib-completed-1",
targetNodeId: "oq-test",
originatingTargetNodeId: "oq-test",
question: "What specific step of the onboarding funnel has the highest abandonment rate?",
answer: "Step 3 — email verification. Data shows 42% drop-off at this gate.",
sequence: 1,
observations: ["Step 3 is the critical drop point"],
possibleFollowUpQuestions: [
"What drives the Step 3 abandonment?",
"Can we reduce friction at Step 3?",
],
};
}
function makeContributions(n) {
return Array.from({ length: n }, (_, i) => ({
id: `contrib-${i}`,
targetNodeId: "oq-test",
originatingTargetNodeId: "oq-test",
question: `Question ${i + 1}`,
answer: `Answer ${i + 1} — completed with data.`,
sequence: i + 1,
observations: [`Observation ${i + 1}`],
possibleFollowUpQuestions: [`Follow-up from turn ${i + 1}`],
}));
}
// ── CASE A — reopened completed result must NOT show textarea ──
describe("Case A — reopened completed result suppresses textarea", () => {
const contrib = makeCompletedContrib();
it("single completed turn: has question + answer, should NOT show textarea", () => {
// Simulated startFocused reopen path: reconstructs latest Contribution
const focused = {
status: "formulated",
question: contrib.question,
answer: contrib.answer, // non-null = completed turn
result: { observations: contrib.observations, possibleFollowUpQuestions: contrib.possibleFollowUpQuestions },
error: null,
};
const processingStep = "idle"; // not processing anything
expect(focused.question?.trim()).toBeTruthy();
expect(focused.status).toBe("formulated");
expect(focused.answer).toBeTruthy();
// The repaired condition: answer exists → no textarea
expect(shouldShowResponseTextarea(focused, processingStep)).toBe(false);
});
it("multiple completed turns (3): latest has answer, should NOT show textarea", () => {
const contributions = makeContributions(3);
const latest = contributions[2];
const focused = {
status: "formulated",
question: latest.question,
answer: latest.answer,
result: { observations: latest.observations, possibleFollowUpQuestions: latest.possibleFollowUpQuestions },
error: null,
};
expect(shouldShowResponseTextarea(focused, "idle")).toBe(false);
});
it("contributions exist but answer is null (data integrity edge): shows textarea", () => {
// Edge case: what if an answer somehow became null?
const contrib = makeCompletedContrib();
const focused = {
status: "formulated",
question: contrib.question,
answer: null, // should not happen normally but let's handle it
result: null,
error: null,
};
// If answer is genuinely missing, textarea appears so user can provide one
expect(shouldShowResponseTextarea(focused, "idle")).toBe(true);
});
});
// ── CASE B — explicit follow-up selection shows textarea ──
describe("Case B — explicit follow-up selection creates unanswered state", () => {
it("after setFollowUpQuestion: answer=null → textarea appears", () => {
const contributions = makeContributions(1);
// Step 1: reopen completed thread (answer is non-null)
const beforeSelect = {
status: "formulated",
question: contributions[0].question,
answer: contributions[0].answer,
result: null,
error: null,
};
expect(shouldShowResponseTextarea(beforeSelect, "idle")).toBe(false);
// Step 2: user selects a follow-up — simulates setFollowUpQuestion()
const focusedAfterSelect = {
...beforeSelect,
question: contributions[0].possibleFollowUpQuestions[0],
answer: null, // setFollowUpQuestion sets answer: null
};
// After explicit selection: textarea should appear
expect(shouldShowResponseTextarea(focusedAfterSelect, "idle")).toBe(true);
});
});
// ── CASE C — fresh investigation preserves textarea ──
describe("Case C — fresh investigation preserves textarea", () => {
it("fresh formulated question with no prior answer: shows textarea", () => {
const focused = {
status: "formulated",
question: "What do you think about this approach?",
answer: null, // no prior completion
result: null,
error: null,
};
expect(shouldShowResponseTextarea(focused, "idle")).toBe(true);
});
it("processing active: does NOT show textarea even with question", () => {
const focused = {
status: "formulated",
question: "Active analysis in progress",
answer: null,
result: null,
error: null,
};
expect(shouldShowResponseTextarea(focused, "active")).toBe(false);
});
});
// ── v0.50: IN-PLACE FOLLOW-UP CONTINUATION (defect: follow-up selection clears hasAnswer, causing "Question" header to reappear) ─
// Simulated FQB condition for determining whether to render completed-narrative framing
function shouldRenderCompletedFraming(focused) {
const hasAnswer = Boolean(focused?.answer);
// OLD (buggy): only checks answer
return hasAnswer && Boolean(focused?.question?.trim());
}
// FIXED condition: also treats a non-null result as "completed context exists"
function shouldRenderCompletedFraming_FIXED(focused) {
const hasAnswer = Boolean(focused?.answer);
const hasCompletedContext = Boolean(focused?.result);
return (hasAnswer || hasCompletedContext) && Boolean(focused?.question?.trim());
}
// ── Simulated reverse rendering for Previous Learning chronology ────────
function renderPriorContribs(chronological) {
// Mirrors PriorContributionsSummary / SecondaryPreviousLearning: slice(0, -1) preserves order
const prior = chronological.slice(0, -1);
return prior.map((c, idx) => ({
label: `Turn ${c.sequence || idx + 1}`,
sequence: c.sequence,
id: c.id,
}));
}
function renderPriorContribs_REVERSED(chronological) {
// Reversed at presentation boundary only
const prior = chronological.slice(0, -1);
return [...prior].reverse().map((c, idx) => ({
label: `Turn ${c.sequence || idx + 1}`,
sequence: c.sequence,
id: c.id,
}));
}
describe("v0.50 Case A — selected follow-up continues in place with completed context", () => {
it("OLD: selecting follow-up (answer=null, result exists) does NOT render completed framing → proves defect", () => {
const beforeSelect = {
question: "Turn 2 question",
answer: "Turn 2 answer", // non-null = hasAnswer = true
status: "formulated",
result: { observations: ["Finding A"], uncertainties: [], possibleFollowUpQuestions: ["Should we explore X?"] },
};
expect(shouldRenderCompletedFraming(beforeSelect)).toBe(true);
const afterSelect = {
...beforeSelect,
question: "Should we explore X?", // follow-up question
answer: null, // setFollowUpQuestion clears answer
status: "formulated",
result: beforeSelect.result, // result stays (causal context)
};
// BUG: after selecting a follow-up, completed framing is lost
expect(shouldRenderCompletedFraming(afterSelect)).toBe(false);
});
it("FIXED: after follow-up selection with result, completed framing IS preserved → proves repair", () => {
const afterSelect = {
question: "Should we explore X?",
answer: null,
status: "formulated",
result: { observations: ["Finding A"], uncertainties: [], possibleFollowUpQuestions: ["Another option?"] },
};
expect(shouldRenderCompletedFraming_FIXED(afterSelect)).toBe(true);
});
});
// ── Simulated reverse rendering for Previous Learning ──────────────
function renderPriorContribs(contributions) {
// PriorContributionsSummary and SecondaryPreviousLearning use: slice(0, -1)
const prior = contributions.slice(0, -1);
return prior.map((c, idx) => ({
label: `Turn ${c.sequence || idx + 1}`,
sequence: c.sequence,
id: c.id,
}));
}
function renderPriorContribs_REVERSED(contributions) {
const prior = contributions.slice(0, -1);
// Reverse at presentation boundary only
return [...prior].reverse().map((c, idx) => ({
label: `Turn ${c.sequence || idx + 1}`,
sequence: c.sequence,
id: c.id,
}));
}
describe("v0.50 Prior contribs display order — reversed at presentation boundary", () => {
const threeContributions = [
{ id: "contrib-turn-1", sequence: 1, targetNodeId: "node-A", observations: ["Turn 1 finding"] },
{ id: "contrib-turn-2", sequence: 2, targetNodeId: "node-A", observations: ["Turn 2 finding"] },
{ id: "contrib-turn-3", sequence: 3, targetNodeId: "node-A", observations: ["Turn 3 finding"] }, // latest = excluded as current result
];
it("chronological order: slice(0,-1) preserves input sequence [1, 2]", () => {
const ordered = renderPriorContribs(threeContributions);
expect(ordered.map((c) => c.sequence)).toEqual([1, 2]);
});
it("REVERSED display order: newest prior turn first [2, 1]", () => {
const reversed = renderPriorContribs_REVERSED(threeContributions);
expect(reversed.map((c) => c.sequence)).toEqual([2, 1]);
});
it("reversal puts Turn 2 (newest prior) at top of the list for the user", () => {
const reversed = renderPriorContribs_REVERSED(threeContributions);
expect(reversed[0].label).toBe("Turn 2");
expect(reversed[1].label).toBe("Turn 1");
});
it("data order is unchanged — only the presentation layer reverses", () => {
const ordered = renderPriorContribs(threeContributions);
const reversed = renderPriorContribs_REVERSED(threeContributions);
// The data order (ordered) is unaffected by reversing
expect(ordered.map((c) => c.sequence)).toEqual([1, 2]);
// Reversal produces the opposite visual order for the user
expect(reversed.map((c) => c.sequence)).toEqual([2, 1]);
// Both refer to the same underlying contributions (same IDs)
const orderedIds = new Set(ordered.map((c) => c.id));
const reversedIds = new Set(reversed.map((c) => c.id));
expect(orderedIds).toEqual(reversedIds);
});
it("two-turn thread: reversal still works", () => {
const twoContributions = [
{ id: "contrib-turn-1", sequence: 1, targetNodeId: "node-B" },
{ id: "contrib-turn-2", sequence: 2, targetNodeId: "node-B" }, // excluded as current
];
const reversed = renderPriorContribs_REVERSED(twoContributions);
expect(reversed).toHaveLength(1);
expect(reversed[0].sequence).toBe(1);
});
it("single prior turn: reversal is no-op (one element)", () => {
const single = [
{ id: "contrib-turn-1", sequence: 1, targetNodeId: "node-C" },
{ id: "contrib-turn-2", sequence: 2, targetNodeId: "node-C" }, // excluded as current
];
const reversed = renderPriorContribs_REVERSED(single);
expect(reversed).toHaveLength(1);
expect(reversed[0].sequence).toBe(1);
});
it("reversal with non-sequential sequences preserves correct chronological reverse", () => {
const irregular = [
{ id: "contrib-A", sequence: 5, targetNodeId: "node-D" },
{ id: "contrib-B", sequence: 10, targetNodeId: "node-D" },
{ id: "contrib-C", sequence: 15, targetNodeId: "node-D" }, // excluded as current
];
const reversed = renderPriorContribs_REVERSED(irregular);
expect(reversed.map((c) => c.sequence)).toEqual([10, 5]);
});
});
// ── v0.50 INVESTIGATING cue on normal Open Questions buttons (actual render path) ───
describe("v0.49 Case A — completed turn provenance narrative", () => {
it("completed turn renders distinct user response and derived findings sections", () => {
const focused = {
question: "Completed question",
answer: "Exact user wording",
status: "formulated",
result: {
observations: ["System-derived interpretation"],
uncertainties: ["Remaining uncertainty"],
possibleFollowUpQuestions: ["A follow-up?"],
},
error: null,
};
// Structural requirements for completed-result provenance presentation:
// 1. Completed/historical question context is visible (not bare active QUESTION)
expect(focused.question).toBe("Completed question");
// 2. User verbatim answer is available as a distinct field
expect(focused.answer).toBe("Exact user wording");
// 3. hasAnswer === true → indicates COMPLETED result, not active question
const hasAnswer = Boolean(focused?.answer);
expect(hasAnswer).toBe(true);
// 4. System-derived interpretation is separately available on result
expect(focused.result.observations[0]).toBe("System-derived interpretation");
// 5. User answer ≠ derived Finding (they are different fields)
expect(focused.answer).not.toBe(focused.result.observations[0]);
// 6. No response textarea should appear for completed result
expect(shouldShowResponseTextarea(focused, "idle")).toBe(false);
// Structural proof: user answer and derived finding are in distinct slots
const userAnswerSlot = focused.answer;
const derivedFindingSlot = focused.result.observations[0];
expect(userAnswerSlot).not.toBe(derivedFindingSlot);
});
it("active question (answer null) does NOT show completed-turn framing", () => {
const activeFocused = {
question: "Active follow-up question",
answer: null,
status: "formulated",
result: null,
error: null,
};
expect(Boolean(activeFocused?.answer)).toBe(false);
// An active question should render as active QUESTION, not completed result
expect(shouldShowResponseTextarea(activeFocused, "idle")).toBe(true);
});
});
describe("v0.49 Case B — verbatim preservation", () => {
it("informal user answer preserved exactly without cleanup", () => {
const informalAnswer = "yeah we could prob move some of his dev stuff over first";
const focused = {
question: "What about the development resources?",
answer: informalAnswer,
status: "formulated",
result: { observations: ["User suggested moving dev work early"], uncertainties: [], possibleFollowUpQuestions: [] },
error: null,
};
// Verbatim preservation: stored answer must appear exactly as typed
expect(focused.answer).toBe(informalAnswer);
// The rendering logic must preserve the exact string — not clean it up
const rendered = focused.answer; // simulates what FQB would render
expect(rendered).toBe("yeah we could prob move some of his dev stuff over first");
// Not polished or corrected
expect(rendered).not.toBe("Yeah, we could probably move some of his development work over first.");
});
});
describe("v0.49 Case C — active selected follow-up", () => {
it("answer null → active QUESTION with textarea, no completed framing", () => {
const answer = null;
const focused = {
question: "pick this question",
answer: answer,
status: "formulated",
result: null,
error: null,
};
// Structural requirements for active-question presentation:
expect(Boolean(focused?.answer)).toBe(false); // no completed framing
expect(shouldShowResponseTextarea(focused, "idle")).toBe(true); // textarea visible
// No derived findings when result is null (active question, not completed)
expect(focused.result).toBeNull();
// Answer field exists but is explicitly null — distinguishes from missing data
expect("answer" in focused).toBe(true);
});
});
describe("v0.49 Case D — derived sections preserved for completed turn", () => {
it("Findings, Still Unclear, Questions This Raises remain available on completed result", () => {
const findings = ["Finding A", "Finding B"];
const uncertainties = ["Unclear factor X"];
const followUps = ["Should we consider Y?"];
const focused = {
question: "Completed investigation question",
answer: "User provided a thorough response.",
status: "formulated",
result: {
observations: findings,
uncertainties: uncertainties,
possibleFollowUpQuestions: followUps,
},
error: null,
};
// Findings accessible
expect(focused.result.observations).toEqual(findings);
// Still Unclear accessible
expect(focused.result.uncertainties).toEqual(uncertainties);
// Questions This Raises accessible
expect(focused.result.possibleFollowUpQuestions).toEqual(followUps);
// None are the user's answer
expect(focused.result.observations.some((f) => f === focused.answer)).toBe(false);
});
});
});
// ── v0.49 RENDERED REGRESSION — in-place follow-up ownership defect ──
// Proves: after selecting a follow-up under QUESTIONS THIS RAISES,
// the completed narrative sources question/answer from the canonical
// completed Contribution (not from the mutated active focused state),
// and exactly one textarea renders (in-place under Q4, not at top).
function makeContrib(question, answer, seq) {
return {
id: `contrib-t${seq}`,
targetNodeId: "node-test",
originatingTargetNodeId: "node-test",
question,
answer,
sequence: seq,
observations: [`Finding for turn ${seq}`],
possibleFollowUpQuestions: [],
};
}
function renderFQB(props) {
const defaultProps = {
nodeId: "node-test",
isFocused: true,
hasContent: true,
processingStep: "idle",
formulationStep: "active",
deconstructMsg: "Processing…",
focusedAnswer: "",
setFocusedAnswer: () => {},
handleDeconstructSubmit: () => {},
retryFormulation: () => {},
setFollowUpQuestion: () => {},
focusedContributions: [],
currentFindings: [],
findings: [],
onUpdateFindingDisposition: () => {},
onUpdateFindingProposition: () => {},
...props,
};
return render(<FocusedQuestionBody {...defaultProps} />);
}
describe("v0.49 RENDERED — in-place follow-up context ownership", () => {
describe("before selection — completed result with answer present", () => {
it("renders Q3 under Previously Answered and A3 non-empty under Your Response, no textarea", () => {
const contrib = makeContrib(
"Which specific step of the onboarding funnel has the highest abandonment rate?",
"Step 3 — account verification / phone confirmation.",
1,
);
renderFQB({
focused: {
question: contrib.question,
answer: contrib.answer,
status: "formulated",
result: {
observations: [contrib.observations[0]],
uncertainties: ["Is this a UX problem or trust issue?"],
possibleFollowUpQuestions: [
"What drives the Step 3 abandonment rate?",
"Can reducing friction at Step 3 reduce overall abandonment?",
],
assumptions: [],
relationships: [],
},
error: null,
},
focusedContributions: [contrib],
processingStep: "idle",
});
// Completed narrative correct
expect(screen.getByText("Previously answered")).toBeInTheDocument();
expect(screen.getByText(contrib.question)).toBeInTheDocument();
expect(screen.getByText("Your response")).toBeInTheDocument();
expect(screen.getByText(contrib.answer)).toBeInTheDocument();
// No textarea visible for completed result
const textareas = screen.queryAllByTestId("response-textarea");
expect(textareas).toHaveLength(0);
});
});
describe("after selecting follow-up Q4 under QUESTIONS THIS RAISES", () => {
it("completed narrative still shows Q3/A3 (not mutated), in-place textarea renders under Q4, exactly one textarea total", async () => {
const prevQ = "Which specific step of the onboarding funnel has the highest abandonment rate?";
const prevA = "Step 3 — account verification / phone confirmation.";
const followUpQ = "What drives the Step 3 abandonment rate?";
const contrib = makeContrib(prevQ, prevA, 1);
// Simulate post-selection state: focused.question mutated to follow-up, answer cleared, result preserved
const focusedPostSelection = {
question: followUpQ, // MUTATED — this is the defect we're testing against
answer: null, // CLEARED — this is the defect
status: "active", // follow-up selection changes status to active
result: {
observations: [contrib.observations[0]],
uncertainties: ["Is this a UX problem or trust issue?"],
possibleFollowUpQuestions: [followUpQ, "Can reducing friction at Step 3 reduce overall abandonment?"],
assumptions: [],
relationships: [],
},
error: null,
};
renderFQB({
focused: focusedPostSelection,
focusedContributions: [contrib],
processingStep: "idle", // idle = hasCompletedContext is true
});
// === COMPLETED NARRATIVE — must source from contribution, not mutated focused ===
// Previously answered shows Q3 (original), NOT the follow-up question
expect(screen.getByText("Previously answered")).toBeInTheDocument();
const pqText = screen.getByText(prevQ);
expect(pqText).toBeInTheDocument();
// Q4 should NOT appear under "Previously answered" — it should only appear under "Questions This Raises"
const prevSection = pqText.closest("div");
expect(prevSection?.textContent?.includes(followUpQ)).toBe(false);
// Your response shows A3 and is non-empty
expect(screen.getByText("Your response")).toBeInTheDocument();
const arText = screen.getByText(prevA);
expect(arText).toBeInTheDocument();
expect(arText.textContent?.trim().length).toBeGreaterThan(0);
// === QUESTIONS THIS RAISES — Q4 selected with in-place textarea ===
expect(screen.getByText("Questions this raises")).toBeInTheDocument();
// Q4 text should be visible under Questions This Raises
expect(screen.getByText(followUpQ)).toBeInTheDocument();
// Exactly one textarea (in-place, not top-level)
const allTextareas = screen.getAllByRole("textbox");
expect(allTextareas).toHaveLength(1);
// The single textarea has the correct placeholder
const fuTextarea = screen.getByPlaceholderText("What do you know about this?");
expect(fuTextarea).toBeInTheDocument();
// Submit button visible under in-place follow-up
expect(screen.getByRole("button", { name: /submit/i })).toBeInTheDocument();
});
it("top-level QUESTION Q4 does NOT appear as a separate section after selection", () => {
const prevQ = "Which specific step of the onboarding funnel has the highest abandonment rate?";
const followUpQ = "What drives the Step 3 abandonment rate?";
const contrib = makeContrib(prevQ, "Step 3 answer.", 1);
renderFQB({
focused: {
question: followUpQ,
answer: null,
status: "formulated",
result: {
observations: ["Finding"],
uncertainties: [],
possibleFollowUpQuestions: [followUpQ],
assumptions: [],
relationships: [],
},
error: null,
},
focusedContributions: [contrib],
processingStep: "idle",
});
// There should be NO "Question" heading (top-level) — only "Previously answered"
const questionHeadings = screen.queryAllByText("Question");
expect(questionHeadings).toHaveLength(0);
});
});
describe("v0.49 Previous Learning duplication — PriorContributionsSummary must not render in focused workspace", () => {
it("when multiple prior turns exist, Previous Learning heading appears ONLY on right panel (SecondaryPreviousLearning), NOT embedded in left FocusedQuestionBody", () => {
const turn1Q = "What is the primary user motivation for signing up?";
const turn1A = "To access premium features faster than free users.";
const turn2Q = "Which feature combination drives the most retention?";
const turn2A = "Analytics dashboard + automated reports at $29/mo tier.";
const currentQ = "What drives the Step 3 abandonment rate?";
const contribs = [
makeContrib(turn1Q, turn1A, 1),
makeContrib(turn2Q, turn2A, 2),
];
renderFQB({
focused: {
question: currentQ,
answer: null,
status: "active",
result: {
observations: [turn2A],
uncertainties: ["Is this causal or correlational?"],
possibleFollowUpQuestions: [currentQ, "How does retention vary by segment?"],
assumptions: [],
relationships: [],
},
error: null,
},
focusedContributions: contribs,
processingStep: "idle",
});
// === COMPLETED NARRATIVE — Q3/A3 from latest contribution preserved ===
expect(screen.getByText("Previously answered")).toBeInTheDocument();
const pqText = screen.getByText(turn2Q);
expect(pqText).toBeInTheDocument();
expect(screen.getByText("Your response")).toBeInTheDocument();
// Use the paragraph under "Your response" to avoid ambiguity with PriorContributionsSummary rendering
const yourResponseSection = screen.getByText("Your response").parentElement;
const arText = yourResponseSection?.querySelector("p");
expect(arText).toHaveTextContent(turn2A);
// === QUESTIONS THIS RAISES — current Q with textarea ===
expect(screen.getByText("Questions this raises")).toBeInTheDocument();
expect(screen.getByText(currentQ)).toBeInTheDocument();
// Exactly one textarea (in-place follow-up)
const allTextareas = screen.getAllByRole("textbox");
expect(allTextareas).toHaveLength(1);
// === CRITICAL: Previous Learning heading must NOT appear in left pane ===
// PriorContributionsSummary renders <h4> with text "Previous learning" (case-insensitive match)
const prevLearningHeadings = screen.queryAllByText(/previous learning/i);
expect(prevLearningHeadings.length).toBeLessThanOrEqual(1);
// If a Previous Learning heading exists in the full DOM, it should be on the right panel only
// In FocusedQuestionBody alone there is no SecondaryPreviousLearning, so with NO PriorContributionsSummary
// there should be zero "Previous learning" headings in this isolated render.
expect(prevLearningHeadings.length).toBe(0);
});
it("after SecondaryPreviousLearning is mounted on the right, exactly one Previous Learning heading total", () => {
const turn1Q = "Turn 1 question";
const turn1A = "Turn 1 answer.";
const turn2Q = "Turn 2 question";
const turn2A = "Turn 2 answer.";
const currentQ = "Current focused question?";
// Two prior contributions (the most recent will be excluded by slice(0,-1), Turn 1 remains)
const contribs = [makeContrib(turn1Q, turn1A, 1), makeContrib(turn2Q, turn2A, 2)];
// Render FQB + SecondaryPreviousLearning as the workspace does
render(
<div style={{ display: "grid", gridTemplateColumns: "1fr 1fr" }}>
<FocusedQuestionBody
nodeId="node-test"
isFocused={true}
hasContent={true}
processingStep="idle"
formulationStep="active"
deconstructMsg=""
focusedAnswer=""
setFocusedAnswer={() => {}}
handleDeconstructSubmit={() => {}}
retryFormulation={() => {}}
setFollowUpQuestion={() => {}}
focused={
{
question: currentQ,
answer: null,
status: "active",
result: {
observations: ["finding"],
uncertainties: [],
possibleFollowUpQuestions: [currentQ],
assumptions: [],
relationships: [],
},
error: null,
}
}
focusedContributions={contribs}
/>
<SecondaryPreviousLearning nodeId="node-test" contributions={contribs} findings={[]} />
</div>,
);
// Exactly one Previous Learning heading across the two-column workspace
const prevLearningHeadings = screen.queryAllByText(/previous learning/i);
expect(prevLearningHeadings).toHaveLength(1);
});
// ── v0.49 REGRESSION: selected follow-up must not render twice ──
it("v0.49 regression: selected follow-up renders exactly once, no '(current question)' duplicate", () => {
const turn1Q = "Completed question?";
const turn1A = "Complete answer.";
// Q3 completed with raised follow-up Q4
const contribs = [
makeContrib(turn1Q, turn1A, 1),
];
// After setFollowUpQuestion: focused.question === "Q4", possibleFollowUpQuestions = ["Q4"], hasActiveFollowUp = true
render(
<div style={{ display: "grid", gridTemplateColumns: "1fr 1fr" }}>
<FocusedQuestionBody
nodeId="node-test"
isFocused={true}
hasContent={true}
processingStep="idle"
formulationStep="active"
deconstructMsg=""
focusedAnswer=""
setFocusedAnswer={() => {}}
handleDeconstructSubmit={() => {}}
retryFormulation={() => {}}
setFollowUpQuestion={() => {}}
focused={
{
question: "Q4",
answer: null,
status: "formulated",
result: {
observations: ["finding"],
uncertainties: [],
possibleFollowUpQuestions: ["Q4"],
assumptions: [],
relationships: [],
},
error: null,
}
}
focusedContributions={contribs}
/>
</div>,
);
// The selected follow-up text should appear exactly once (in the active block), not duplicated in candidate row.
const q4Text = screen.getAllByText("Q4");
expect(q4Text).toHaveLength(1);
// After selection, "(current question)" label must NOT be rendered — the active form communicates selection unambiguously.
const currentQuestionLabels = screen.queryAllByText(/\(current question\)/i);
expect(currentQuestionLabels).toHaveLength(0);
});
});
// ── v0.51 CASE A: processing preserves workspace context ─────────────
describe("processing state preserves completed context and active follow-up", () => {
it("completed narrative remains visible during processing (hasCompletedContext stays true via contributions fallback)", async () => {
const prevQ = "Which specific step of the onboarding funnel has the highest abandonment rate?";
const prevA = "Step 3 — account verification / phone confirmation.";
const followUpQ = "What drives the Step 3 abandonment rate?";
const contrib = makeContrib(prevQ, prevA, 1);
// Simulate post-submit processing: result still exists (hasCompletedContext stays true),
// but processingStep === "active" used to collapse context.
renderFQB({
focused: {
question: followUpQ,
answer: null,
status: "formulated",
result: {
observations: [contrib.observations[0]],
uncertainties: ["Is this causal?"],
possibleFollowUpQuestions: [followUpQ, "How does it compare to competitors?"],
assumptions: [],
relationships: [],
},
error: null,
},
focusedContributions: [contrib],
processingStep: "active", // ← THIS IS THE DEFECT: hasCompletedContext becomes false
deconstructMsg: "Working through your response…", // matches DECONSTRUCT_MESSAGES[0]
});
// Completed context must remain during processing
expect(screen.getByText("Previously answered")).toBeInTheDocument();
expect(screen.getByText(prevQ)).toBeInTheDocument();
expect(screen.getByText("Your response")).toBeInTheDocument();
expect(screen.getByText(prevA)).toBeInTheDocument();
// Active follow-up question remains visible under Questions this raises
expect(screen.getByText(followUpQ)).toBeInTheDocument();
expect(screen.getByText("Questions this raises")).toBeInTheDocument();
// Processing message present (spinner + text)
expect(screen.getByText(/Working through your response/i)).toBeInTheDocument();
});
it("active follow-up textarea present but disabled during processing, spinner shown", async () => {
const contrib = makeContrib("Q3", "A3", 1);
renderFQB({
focused: {
question: "Q4",
answer: null,
status: "formulated",
result: { observations: [], uncertainties: [], possibleFollowUpQuestions: ["Q4"] },
error: null,
},
focusedContributions: [contrib],
processingStep: "active",
deconstructMsg: "Working through your response…",
});
// In-place textarea visible (not hidden) but disabled during processing
const followUpTextarea = screen.queryAllByTestId("follow-up-textarea");
expect(followUpTextarea).toHaveLength(1);
expect(followUpTextarea[0].disabled).toBe(true);
// Submit button also disabled
const submitBtn = screen.getByRole("button", { name: /submit/i });
expect(submitBtn.disabled).toBe(true);
// Processing message visible
expect(screen.getByText(/Working through your response/i)).toBeInTheDocument();
});
it("previous contributions data available during processing for derived sections", async () => {
const contrib = makeContrib("Q3", "A3", 1);
renderFQB({
focused: {
question: "Q4",
answer: null,
status: "formulated",
result: { observations: [], uncertainties: [], possibleFollowUpQuestions: ["Q4"] },
error: null,
},
focusedContributions: [contrib],
processingStep: "active",
});
// Derived sections (from priorContribs fallback) remain visible — "What this tells us" etc.
expect(screen.getByText("What this tells us")).toBeInTheDocument();
});
it("no top-level QUESTION Q4 screen during processing", async () => {
const contrib = makeContrib("Q3", "A3", 1);
renderFQB({
focused: {
question: "Q4",
answer: null,
status: "formulated",
result: { observations: [], uncertainties: [], possibleFollowUpQuestions: ["Q4"] },
error: null,
},
focusedContributions: [contrib],
processingStep: "active",
});
// Should NOT show a top-level "Question" heading (the active block only)
const questionHeadings = screen.queryAllByText(/^Question$/);
expect(questionHeadings).toHaveLength(0);
});
it("processing indicator belongs to active follow-up block, not to completed narrative", async () => {
const prevQ = "Which specific step of the onboarding funnel has the highest abandonment rate?";
const prevA = "Step 3 — account verification / phone confirmation.";
const followUpQ = "What drives the Step 3 abandonment rate?";
const contrib = makeContrib(prevQ, prevA, 1);
renderFQB({
focused: {
question: followUpQ,
answer: null,
status: "formulated",
result: {
observations: [contrib.observations[0]],
uncertainties: ["Is this causal?"],
possibleFollowUpQuestions: [followUpQ, "How does it compare to competitors?"],
assumptions: [],
relationships: [],
},
error: null,
},
focusedContributions: [contrib],
processingStep: "active",
deconstructMsg: "Working through your response…",
});
// ── Derived completed context (from prior contributions) remains visible during processing ──
expect(screen.getByText("Previously answered")).toBeInTheDocument();
expect(screen.getByText(prevQ)).toBeInTheDocument();
expect(screen.getByText("What this tells us")).toBeInTheDocument();
// ── Exactly one processing indicator exists (no duplicates, no missing) ──
const allProcessingIndicators = screen.queryAllByTestId("processing-indicator");
expect(allProcessingIndicators).toHaveLength(1);
const processingIndicator = allProcessingIndicators[0];
// ── Processing indicator must be inside the active follow-up block (Q4) ──
const followUpBlock = screen.getByTestId("follow-up-block");
expect(followUpBlock.contains(processingIndicator)).toBe(true);
// ── Processing text visible inside follow-up block ──
expect(processingIndicator.textContent).toContain("Working through your response…");
// ── Follow-up textarea and submit are present inside the same block ──
const followUpTextarea = screen.getByTestId("follow-up-textarea");
expect(followUpTextarea.disabled).toBe(true);
const followUpSubmit = screen.getByRole("button", { name: /submit/i });
expect(followUpSubmit.closest("[data-testid='follow-up-block']")).toBeInTheDocument();
// ── Completed narrative that DOES render must not contain follow-up elements ──
const completedNarrative = screen.queryByTestId("completed-narrative");
if (completedNarrative) {
expect(completedNarrative.querySelector('[data-testid="follow-up-block"]')).not.toBeInTheDocument();
}
});
});
// ── v0.51 CASE B: error preserves workspace context ────────────────
describe("error state preserves completed context and active follow-up", () => {
it("completed narrative remains visible after deconstruction failure", async () => {
const prevQ = "Which specific step of the onboarding funnel has the highest abandonment rate?";
const prevA = "Step 3 — account verification / phone confirmation.";
const followUpQ = "What drives the Step 3 abandonment rate?";
// Prior contribution includes possibleFollowUpQuestions (real deconstruction always returns them)
const contrib = { ...makeContrib(prevQ, prevA, 1), possibleFollowUpQuestions: [followUpQ] };
// Simulate error state: result cleared to null by handleDeconstructSubmit catch block
renderFQB({
focused: {
question: followUpQ,
answer: null,
status: "formulated",
result: null, // ← NULLED BY ERROR HANDLER (but priorContribs still has the data)
error: "Deconstruction failed",
},
focusedContributions: [contrib],
processingStep: "idle",
});
// Completed context from contributions must survive the error
expect(screen.getByText("Previously answered")).toBeInTheDocument();
expect(screen.getByText(prevQ)).toBeInTheDocument();
expect(screen.getByText("Your response")).toBeInTheDocument();
expect(screen.getByText(prevA)).toBeInTheDocument();
// Active follow-up remains under Questions This Raises
expect(screen.getByText(followUpQ)).toBeInTheDocument();
expect(screen.getByText("Questions this raises")).toBeInTheDocument();
});
it("failed submitted response visible with YOUR RESPONSE label", async () => {
const prevQ = "Q3";
const failedAnswer = "My detailed answer that couldn't be processed.";
const contrib = makeContrib(prevQ, "Previous turn answer", 1);
renderFQB({
focused: {
question: "Q4",
answer: null, // no fresh answer to show yet
status: "formulated",
result: null,
error: "Deconstruction failed",
},
focusedAnswer: failedAnswer, // ← this is what the user typed before failure
focusedContributions: [contrib],
processingStep: "idle",
});
// Error message visible
expect(screen.getByText(/unable to process/i)).toBeInTheDocument();
// Retry button visible
expect(screen.getByRole("button", { name: /retry/i })).toBeInTheDocument();
});
it("derived sections remain during error (priorContribs fallback active)", async () => {
const contrib = makeContrib("Q3", "A3", 1);
renderFQB({
focused: {
question: "Q4",
answer: null,
status: "formulated",
result: null,
error: "Deconstruction failed",
},
focusedContributions: [contrib],
processingStep: "idle",
});
// Derived sections from priorContribs fallback remain visible during error
expect(screen.getByText("What this tells us")).toBeInTheDocument();
});
it("no top-level QUESTION Q4 screen during error", async () => {
const contrib = makeContrib("Q3", "A3", 1);
renderFQB({
focused: {
question: "Q4",
answer: null,
status: "formulated",
result: null,
error: "Deconstruction failed",
},
focusedContributions: [contrib],
processingStep: "idle",
});
// Should NOT show a top-level "Question" heading (follow-up stays in place)
const questionHeadings = screen.queryAllByText(/^Question$/);
expect(questionHeadings).toHaveLength(0);
});
});
// ── v0.51 CASE C: retry returns to processing path ────────────────
describe("retry returns to processing without duplicating follow-up", () => {
it("retry does not clear previous context or duplicate the follow-up question", async () => {
const prevQ = "Which specific step of the onboarding funnel has the highest abandonment rate?";
const prevA = "Step 3 — account verification / phone confirmation.";
const followUpQ = "What drives the Step 3 abandonment rate?";
// Prior contribution includes possibleFollowUpQuestions (real deconstruction always returns them)
const contrib = { ...makeContrib(prevQ, prevA, 1), possibleFollowUpQuestions: [followUpQ] };
// Simulate pre-retry state: error was just retried, processing re-activates
renderFQB({
focused: {
question: followUpQ,
answer: null,
status: "formulated",
result: null, // still null until retry completes
error: null, // cleared by retry before re-submitting
},
focusedContributions: [contrib],
processingStep: "active", // retry re-enters processing
deconstructMsg: "Working through your response…",
});
// Previous completed turn remains visible (from contributions)
expect(screen.getByText("Previously answered")).toBeInTheDocument();
expect(screen.getByText(prevQ)).toBeInTheDocument();
// Follow-up question appears exactly once in active block (not duplicated)
const q4Elements = screen.getAllByText(followUpQ);
expect(q4Elements).toHaveLength(1);
// Processing indicator shows again
expect(screen.getByText(/Working through your response/i)).toBeInTheDocument();
});
});
});