import { afterEach, describe, expect, it, vi } from "vitest"; const PROBLEM_UNITS_FLAG = "FEATURE_ASSISTANT_PROBLEM_UNITS_V1"; const GRAPH_RUNTIME_FLAG = "FEATURE_ASSISTANT_GRAPH_RUNTIME_V1"; const ORIGINAL_PROBLEM_UNITS_FLAG = process.env[PROBLEM_UNITS_FLAG]; const ORIGINAL_GRAPH_RUNTIME_FLAG = process.env[GRAPH_RUNTIME_FLAG]; function restoreFlags(): void { if (ORIGINAL_PROBLEM_UNITS_FLAG === undefined) { delete process.env[PROBLEM_UNITS_FLAG]; } else { process.env[PROBLEM_UNITS_FLAG] = ORIGINAL_PROBLEM_UNITS_FLAG; } if (ORIGINAL_GRAPH_RUNTIME_FLAG === undefined) { delete process.env[GRAPH_RUNTIME_FLAG]; } else { process.env[GRAPH_RUNTIME_FLAG] = ORIGINAL_GRAPH_RUNTIME_FLAG; } } async function normalizeWithFlags(input: { problemUnits: "0" | "1"; graphRuntime: "0" | "1"; }) { process.env[PROBLEM_UNITS_FLAG] = input.problemUnits; process.env[GRAPH_RUNTIME_FLAG] = input.graphRuntime; vi.resetModules(); const { normalizeRetrievalResult } = await import("../src/services/retrievalResultNormalizer"); return normalizeRetrievalResult("F1", ["R1"], "hybrid_store_plus_live", { status: "ok", result_type: "list", items: [ { source_entity: "Document", source_id: "DOC-1", risk_score: 5 } ], summary: { broad_query_detected: false }, evidence: [ { evidence_id: "ev-1", claim_ref: "requirement:R1", source_type: "retrieval_item", pointer: { fragment_id: "F1", route: "hybrid_store_plus_live", source: { namespace: "snapshot_2020", entity: "Document", id: "DOC-1", period: "2020-06" }, locator: { field_path: "risk_score", item_index: 0 } }, failed_expected_edge: "payment_to_settlement", anomaly_patterns: ["broken_lifecycle", "missing_link"], confidence: "medium" } ], why_included: ["synthetic-test"], selection_reason: ["synthetic-test"], risk_factors: ["broken_lifecycle"], business_interpretation: ["synthetic-test"], confidence: "medium", limitations: [], errors: [] }); } describe.sequential("retrieval dual payload compatibility for graph runtime", () => { afterEach(() => { restoreFlags(); vi.resetModules(); }); it("keeps stage2 payload when graph runtime flag is OFF", async () => { const result = await normalizeWithFlags({ problemUnits: "1", graphRuntime: "0" }); expect(Array.isArray(result.problem_units)).toBe(true); expect(result.problem_units?.length).toBeGreaterThan(0); expect(result.accounting_graph).toBeUndefined(); expect(result.summary.graph_runtime_enabled).toBeUndefined(); expect(result.problem_units?.some((item) => item.graph_binding)).toBe(false); }); it("adds graph runtime payload when graph runtime flag is ON", async () => { const result = await normalizeWithFlags({ problemUnits: "1", graphRuntime: "1" }); expect(Array.isArray(result.problem_units)).toBe(true); expect(result.problem_units?.length).toBeGreaterThan(0); expect(result.accounting_graph?.schema_version).toBe("accounting_graph_v0_1"); expect((result.accounting_graph?.nodes.length ?? 0) > 0).toBe(true); expect((result.accounting_graph?.edges.length ?? 0) > 0).toBe(true); expect(result.summary.graph_runtime_enabled).toBe(true); expect(typeof result.summary.graph_nodes_count).toBe("number"); expect(typeof result.summary.graph_edges_count).toBe("number"); expect(result.problem_unit_summary?.graph_summary).toBeDefined(); expect(result.problem_units?.some((item) => Boolean(item.graph_binding?.graph_node_id))).toBe(true); }); });