import request from "supertest"; import { describe, expect, it } from "vitest"; import { createApp } from "../src/server"; import { INVESTIGATION_MAX_EVIDENCE_REFS, INVESTIGATION_MAX_UNCERTAINTIES } from "../src/types/stage1Contracts"; describe("investigation_state flow scaffolding", () => { it("keeps bounded investigation_state across follow-up turns", async () => { const app = createApp(); const sessionId = `asst-wave1-${Date.now()}`; const first = await request(app).post("/api/assistant/message").send({ session_id: sessionId, useMock: true, promptVersion: "normalizer_v2_0_2", user_message: "Prover schet 97 i riskovye zony za 2020-06." }); expect(first.status).toBe(200); expect(first.body.debug?.investigation_state_snapshot?.schema_version).toBe("investigation_state_v1"); expect(first.body.debug?.investigation_state_snapshot?.question_scope_id).toBeTruthy(); expect(first.body.debug?.investigation_state_snapshot?.scope_origin).toBe("explicit_from_message"); expect(first.body.debug?.answer_structure_v11?.schema_version).toBe("answer_structure_v1_1"); expect(first.body.debug?.followup_state_usage).toBeUndefined(); const evidenceResult = (first.body.debug?.retrieval_results ?? []).find( (item: { evidence?: unknown[] }) => Array.isArray(item.evidence) && item.evidence.length > 0 ) as { evidence?: Array<{ pointer?: { source?: { entity?: string } } }> } | undefined; if (evidenceResult?.evidence?.length) { expect(typeof evidenceResult.evidence[0].pointer?.source?.entity).toBe("string"); } const second = await request(app).post("/api/assistant/message").send({ session_id: sessionId, useMock: true, promptVersion: "normalizer_v2_0_2", user_message: "Dobav proverku po postavshchikam i utochni nezakrytye trebovaniya." }); expect(second.status).toBe(200); expect(second.body.debug?.investigation_state_snapshot?.turn_index).toBe(2); expect(second.body.debug?.followup_state_usage?.applied).toBe(true); expect(second.body.debug?.investigation_state_snapshot?.question_scope_id).toBeTruthy(); expect(second.body.debug?.investigation_state_snapshot?.followup_context?.question_scope_id).toBeTruthy(); const sessionResponse = await request(app).get(`/api/assistant/session/${sessionId}`); expect(sessionResponse.status).toBe(200); const investigationState = sessionResponse.body.session?.investigation_state; expect(investigationState).toBeTruthy(); expect(investigationState.turn_index).toBe(2); expect(Array.isArray(investigationState.evidence_refs)).toBe(true); expect(Array.isArray(investigationState.open_uncertainties)).toBe(true); expect(investigationState.evidence_refs.length).toBeLessThanOrEqual(INVESTIGATION_MAX_EVIDENCE_REFS); expect(investigationState.open_uncertainties.length).toBeLessThanOrEqual(INVESTIGATION_MAX_UNCERTAINTIES); expect(typeof investigationState.question_id).toBe("string"); }); });