fix(confidence-engine): persist done-for-now episode closure
This commit is contained in:
@@ -1,11 +1,45 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { makeGraph, makeNode } from "@/lib/graph/schema.js";
|
||||
|
||||
const mockUpdateCase = vi.fn();
|
||||
const mockReconsiderCompletedEpisode = vi.fn();
|
||||
const mockApplyValidatedProposal = vi.fn();
|
||||
const mockPrepareCompletedEpisode = vi.fn();
|
||||
|
||||
vi.mock("@/lib/graph/orchestrator.js", () => ({
|
||||
updateCase: (...args) => mockUpdateCase(...args),
|
||||
reconsiderCompletedEpisode: (...args) => mockReconsiderCompletedEpisode(...args),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/graph/apply-proposal.js", () => ({
|
||||
applyValidatedProposal: (...args) => mockApplyValidatedProposal(...args),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/graph/episode-preparation.js", () => ({
|
||||
prepareCompletedEpisode: (...args) => mockPrepareCompletedEpisode(...args),
|
||||
}));
|
||||
|
||||
function makeEpisodeGraph() {
|
||||
return makeGraph({
|
||||
centralStatement: "Episode scenario",
|
||||
currentSummary: "Episode graph",
|
||||
nodes: [makeNode({ id: "target", label: "Target question", kind: "unknown", status: "unknown" })],
|
||||
edges: [],
|
||||
activeUnknownNodeId: "target",
|
||||
resolvedNodeIds: ["already-resolved"],
|
||||
});
|
||||
}
|
||||
|
||||
function mockSuccessfulEpisodeApplication(graph, overrides = {}) {
|
||||
mockPrepareCompletedEpisode.mockReturnValue({ turns: [{ question: "Q?", answer: "A." }] });
|
||||
mockReconsiderCompletedEpisode.mockResolvedValue({ success: true, proposal: {} });
|
||||
mockApplyValidatedProposal.mockResolvedValue({
|
||||
success: true,
|
||||
updatedSituationGraph: graph,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
function makeSuccessResult() {
|
||||
return {
|
||||
success: true,
|
||||
@@ -76,6 +110,67 @@ describe("app/api/cases/update route", () => {
|
||||
expect(mockUpdateCase.mock.calls[0][0]).not.toHaveProperty("modelName");
|
||||
});
|
||||
|
||||
it("authoritatively resolves the selected target after a successful no-op episode", async () => {
|
||||
const graph = makeEpisodeGraph();
|
||||
mockSuccessfulEpisodeApplication(graph);
|
||||
const { POST } = await import("@/app/api/cases/update/route.js");
|
||||
|
||||
const response = await POST(new Request("http://localhost/api/cases/update", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
episodeMode: true,
|
||||
situationGraph: graph,
|
||||
targetNodeId: "target",
|
||||
contributions: [{ targetNodeId: "target" }],
|
||||
}),
|
||||
}));
|
||||
|
||||
const body = await response.json();
|
||||
expect(response.status).toBe(200);
|
||||
expect(body.updatedSituationGraph.resolvedNodeIds).toEqual(
|
||||
expect.arrayContaining(["already-resolved", "target"]),
|
||||
);
|
||||
expect(body.updatedSituationGraph.nodes).toEqual(graph.nodes);
|
||||
expect(body.updatedSituationGraph.edges).toEqual(graph.edges);
|
||||
});
|
||||
|
||||
it("preserves meaningful episode mutations while authoritatively resolving the target", async () => {
|
||||
const graph = makeEpisodeGraph();
|
||||
const mutatedGraph = {
|
||||
...graph,
|
||||
nodes: [...graph.nodes, makeNode({ id: "meaningful", label: "Meaningful change", kind: "observation", status: "known" })],
|
||||
};
|
||||
mockSuccessfulEpisodeApplication(mutatedGraph);
|
||||
const { POST } = await import("@/app/api/cases/update/route.js");
|
||||
|
||||
const response = await POST(new Request("http://localhost/api/cases/update", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ episodeMode: true, situationGraph: graph, targetNodeId: "target", contributions: [{ targetNodeId: "target" }] }),
|
||||
}));
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.updatedSituationGraph.nodes).toEqual(mutatedGraph.nodes);
|
||||
expect(body.updatedSituationGraph.resolvedNodeIds).toContain("target");
|
||||
});
|
||||
|
||||
it("does not return an authoritative closure when completed-episode reconsideration fails", async () => {
|
||||
const graph = makeEpisodeGraph();
|
||||
mockPrepareCompletedEpisode.mockReturnValue({ turns: [{ question: "Q?", answer: "A." }] });
|
||||
mockReconsiderCompletedEpisode.mockResolvedValue({ success: false, stage: "proposal_compatibility", error: "invalid" });
|
||||
const { POST } = await import("@/app/api/cases/update/route.js");
|
||||
|
||||
const response = await POST(new Request("http://localhost/api/cases/update", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ episodeMode: true, situationGraph: graph, targetNodeId: "target", contributions: [{ targetNodeId: "target" }] }),
|
||||
}));
|
||||
|
||||
expect(response.status).toBe(422);
|
||||
await expect(response.json()).resolves.not.toHaveProperty("updatedSituationGraph");
|
||||
});
|
||||
|
||||
it("invalid JSON returns 400", async () => {
|
||||
const { POST } = await import("@/app/api/cases/update/route.js");
|
||||
const request = {
|
||||
|
||||
Reference in New Issue
Block a user