import { describe, expect, it, vi } from "vitest"; import { runAssistantTurnAttemptRuntime } from "../src/services/assistantTurnAttemptRuntimeAdapter"; function buildUserTurn(overrides: Record = {}) { return { session: { session_id: "asst-1", updated_at: "2026-04-11T00:00:00.000Z", items: [{ role: "user", text: "msg" }], investigation_state: { focus: "settlements_60_62" } }, sessionId: "asst-1", userMessageRaw: "where tail", userMessage: "where tail", runtimeAnalysisContext: { as_of_date: "2020-07-31" }, userItem: { message_id: "msg-q1", session_id: "asst-1", role: "user", text: "where tail", reply_type: null, created_at: "2026-04-11T00:00:00.000Z", trace_id: null, debug: null }, ...overrides } as any; } describe("assistant turn attempt runtime adapter", () => { it("returns address response and skips deep runtime when address lane handled the turn", async () => { const runUserTurnBootstrapRuntime = vi.fn(() => buildUserTurn()); const resolveSessionOrganizationScopeContext = vi.fn(() => ({ knownOrganizations: ["Org A"], selectedOrganization: "Org A", activeOrganization: "Org A" })); const runAddressAttemptRuntime = vi.fn(async () => ({ handled: true, response: { lane: "address" }, addressRuntimeMetaForDeep: { source: "address_runtime" } })); const runDeepTurnAttemptRuntime = vi.fn(async () => ({ response: { lane: "deep" } })); const runtime = await runAssistantTurnAttemptRuntime({ payload: { user_message: "where tail" }, runUserTurnBootstrapRuntime, resolveSessionOrganizationScopeContext, runAddressAttemptRuntime, runDeepTurnAttemptRuntime }); expect(runtime.response).toEqual({ lane: "address" }); expect(runtime.source).toBe("address"); expect(runtime.addressRuntimeMetaForDeep).toEqual({ source: "address_runtime" }); expect(runAddressAttemptRuntime).toHaveBeenCalledWith( expect.objectContaining({ sessionId: "asst-1", userMessage: "where tail", runtimeAnalysisContext: { as_of_date: "2020-07-31" } }) ); expect(runDeepTurnAttemptRuntime).not.toHaveBeenCalled(); }); it("falls through to deep runtime when address lane does not return final response", async () => { const runUserTurnBootstrapRuntime = vi.fn(() => buildUserTurn()); const resolveSessionOrganizationScopeContext = vi.fn(() => ({ knownOrganizations: [], selectedOrganization: null, activeOrganization: null })); const runAddressAttemptRuntime = vi.fn(async () => ({ handled: false, response: null, addressRuntimeMetaForDeep: { attempted: true } })); const runDeepTurnAttemptRuntime = vi.fn(async () => ({ response: { lane: "deep", ok: true } })); const runtime = await runAssistantTurnAttemptRuntime({ payload: { user_message: "where tail" }, runUserTurnBootstrapRuntime, resolveSessionOrganizationScopeContext, runAddressAttemptRuntime, runDeepTurnAttemptRuntime }); expect(runtime.response).toEqual({ lane: "deep", ok: true }); expect(runtime.source).toBe("deep"); expect(runDeepTurnAttemptRuntime).toHaveBeenCalledWith( expect.objectContaining({ sessionId: "asst-1", questionId: "msg-q1", userMessage: "where tail", addressRuntimeMetaForDeep: { attempted: true }, sessionInvestigationState: { focus: "settlements_60_62" } }) ); }); });