import { describe, expect, it } from "vitest"; import { finalizeAssistantLivingChatTurn } from "../src/services/assistantLivingChatTurnFinalizeRuntimeAdapter"; describe("assistant living chat turn finalize runtime adapter", () => { it("builds assistant chat item and emits expected living chat log envelope", () => { const commitCalls: Array> = []; const output = finalizeAssistantLivingChatTurn({ sessionId: "asst-chat-1", userMessage: "что ты умеешь?", assistantReply: "Могу помочь с анализом данных 1С.", replyType: "factual_with_explanation", debug: { trace_id: "chat-trace-1", detected_mode: "chat" } as any, modeDecision: { mode: "chat", reason: "assistant_capability_query_detected" }, appendItem: () => {}, getSession: () => null, persistSession: () => {}, cloneConversation: () => [], logEvent: () => {}, messageIdFactory: () => "msg-chat-1", nowIso: () => "2026-04-10T13:00:00.000Z", commitFn: ((input: Record) => { commitCalls.push(input); return { currentSession: null, conversation: [] }; }) as any }); expect(output.assistantItem.message_id).toBe("msg-chat-1"); expect(output.assistantItem.created_at).toBe("2026-04-10T13:00:00.000Z"); expect(output.assistantItem.trace_id).toBe("chat-trace-1"); expect(commitCalls).toHaveLength(1); expect(commitCalls[0]?.["eventType"]).toBe("assistant_message_chat"); const logDetails = commitCalls[0]?.["logDetails"] as Record; expect(logDetails?.["session_id"]).toBe("asst-chat-1"); expect(logDetails?.["living_router_mode"]).toBe("chat"); expect(logDetails?.["living_router_reason"]).toBe("assistant_capability_query_detected"); expect(logDetails?.["assistant_reply"]).toBe("Могу помочь с анализом данных 1С."); expect(output.response.reply_type).toBe("factual_with_explanation"); }); it("uses default commit runtime and returns persisted conversation", () => { let appendCalls = 0; let persistCalls = 0; let logCalls = 0; let storedSession: any = null; const output = finalizeAssistantLivingChatTurn({ sessionId: "asst-chat-2", userMessage: "какие есть данные?", assistantReply: "Доступны данные по нескольким организациям.", replyType: "factual_with_explanation", debug: { trace_id: "chat-trace-2" } as any, appendItem: (_sessionId, item) => { appendCalls += 1; storedSession = { session_id: "asst-chat-2", updated_at: "2026-04-10T13:00:00.000Z", items: [item], investigation_state: null }; }, getSession: () => storedSession, persistSession: () => { persistCalls += 1; }, cloneConversation: (items) => items.map((item) => ({ ...item })), logEvent: () => { logCalls += 1; }, messageIdFactory: () => "msg-chat-2", nowIso: () => "2026-04-10T13:05:00.000Z" }); expect(appendCalls).toBe(1); expect(persistCalls).toBe(1); expect(logCalls).toBe(1); expect(output.response.ok).toBe(true); expect(output.response.session_id).toBe("asst-chat-2"); expect(output.response.reply_type).toBe("factual_with_explanation"); expect(output.response.conversation).toHaveLength(1); expect(output.response.conversation_item.message_id).toBe("msg-chat-2"); }); });