113 lines
3.5 KiB
TypeScript
113 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { finalizeAssistantDeepTurn } from "../src/services/assistantDeepTurnFinalizeRuntimeAdapter";
|
|
|
|
describe("assistant deep turn finalize runtime adapter", () => {
|
|
it("commits assistant turn and builds response with committed conversation", () => {
|
|
const callOrder: string[] = [];
|
|
const conversation = [
|
|
{
|
|
message_id: "msg-1",
|
|
session_id: "asst-1",
|
|
role: "assistant",
|
|
text: "ok",
|
|
reply_type: "factual",
|
|
created_at: "2026-04-10T10:00:00.000Z",
|
|
trace_id: "trace-1",
|
|
debug: null
|
|
}
|
|
] as any;
|
|
|
|
const output = finalizeAssistantDeepTurn({
|
|
sessionId: "asst-1",
|
|
assistantReply: "safe-reply",
|
|
replyType: "factual",
|
|
assistantItem: conversation[0],
|
|
debug: { d: 1 },
|
|
deepAnalysisLogDetails: { stage: "deep" },
|
|
appendItem: () => {},
|
|
getSession: () => null,
|
|
persistSession: () => {},
|
|
cloneConversation: () => [],
|
|
logEvent: () => {},
|
|
commitFn: ((input: Record<string, unknown>) => {
|
|
callOrder.push("commit");
|
|
expect(input.eventType).toBe("assistant_message");
|
|
expect(input.sessionId).toBe("asst-1");
|
|
expect(input.logDetails).toEqual({ stage: "deep" });
|
|
return {
|
|
currentSession: null,
|
|
conversation
|
|
};
|
|
}) as any,
|
|
buildResponseFn: ((input: Record<string, unknown>) => {
|
|
callOrder.push("response");
|
|
expect(input.conversation).toBe(conversation);
|
|
return {
|
|
ok: true,
|
|
session_id: "asst-1",
|
|
assistant_reply: "safe-reply",
|
|
reply_type: "factual",
|
|
conversation_item: conversation[0],
|
|
debug: { d: 1 },
|
|
conversation
|
|
};
|
|
}) as any
|
|
});
|
|
|
|
expect(callOrder).toEqual(["commit", "response"]);
|
|
expect(output.commitResult.conversation).toBe(conversation);
|
|
expect(output.response.assistant_reply).toBe("safe-reply");
|
|
expect(output.response.conversation).toBe(conversation as any);
|
|
});
|
|
|
|
it("uses default commit/response functions when custom hooks are not provided", () => {
|
|
const assistantItem = {
|
|
message_id: "msg-1",
|
|
session_id: "asst-1",
|
|
role: "assistant",
|
|
text: "ok",
|
|
reply_type: "factual",
|
|
created_at: "2026-04-10T10:00:00.000Z",
|
|
trace_id: "trace-1",
|
|
debug: null
|
|
} as any;
|
|
const storedSession = {
|
|
session_id: "asst-1",
|
|
updated_at: "2026-04-10T10:00:00.000Z",
|
|
items: [assistantItem],
|
|
investigation_state: null
|
|
};
|
|
let appendCalled = 0;
|
|
let persistCalled = 0;
|
|
let logCalled = 0;
|
|
|
|
const output = finalizeAssistantDeepTurn({
|
|
sessionId: "asst-1",
|
|
assistantReply: "safe-reply",
|
|
replyType: "factual",
|
|
assistantItem,
|
|
debug: { debug: true },
|
|
deepAnalysisLogDetails: { info: "x" },
|
|
appendItem: () => {
|
|
appendCalled += 1;
|
|
},
|
|
getSession: () => storedSession as any,
|
|
persistSession: () => {
|
|
persistCalled += 1;
|
|
},
|
|
cloneConversation: (items) => items.map((item) => ({ ...item })),
|
|
logEvent: () => {
|
|
logCalled += 1;
|
|
}
|
|
});
|
|
|
|
expect(appendCalled).toBe(1);
|
|
expect(persistCalled).toBe(1);
|
|
expect(logCalled).toBe(1);
|
|
expect(output.response.ok).toBe(true);
|
|
expect(output.response.session_id).toBe("asst-1");
|
|
expect(output.response.reply_type).toBe("factual");
|
|
expect(output.response.conversation.length).toBe(1);
|
|
});
|
|
});
|