NODEDC_1C/llm_normalizer/backend/tests/assistantUserTurnBootstrapR...

73 lines
2.4 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { runAssistantUserTurnBootstrapRuntime } from "../src/services/assistantUserTurnBootstrapRuntimeAdapter";
describe("assistant user turn bootstrap runtime adapter", () => {
it("normalizes user message, appends user turn and persists session", () => {
const session = {
session_id: "asst-1",
items: []
} as any;
const appendItem = vi.fn();
const getSession = vi.fn(() => session);
const persistSession = vi.fn();
const output = runAssistantUserTurnBootstrapRuntime({
payload: {
session_id: "asst-1",
user_message: " source "
},
ensureSession: () => session,
appendItem,
getSession,
persistSession,
compactWhitespace: (value: unknown) => String(value ?? "").replace(/\s+/g, " ").trim(),
repairAddressMojibake: () => " fixed user text ",
resolveRuntimeAnalysisContext: () => ({ as_of_date: "2020-07-31" }),
messageIdFactory: () => "msg-fixed",
nowIso: () => "2026-04-10T00:00:00.000Z"
});
expect(output.session).toBe(session);
expect(output.sessionId).toBe("asst-1");
expect(output.userMessageRaw).toBe("source");
expect(output.userMessage).toBe("fixed user text");
expect(output.runtimeAnalysisContext).toEqual({ as_of_date: "2020-07-31" });
expect(appendItem).toHaveBeenCalledWith(
"asst-1",
expect.objectContaining({
message_id: "msg-fixed",
role: "user",
text: "fixed user text",
created_at: "2026-04-10T00:00:00.000Z"
})
);
expect(getSession).toHaveBeenCalledWith("asst-1");
expect(persistSession).toHaveBeenCalledWith(session);
});
it("falls back to raw message when repaired text is empty", () => {
const session = {
session_id: "asst-2",
items: []
} as any;
const output = runAssistantUserTurnBootstrapRuntime({
payload: {
session_id: "asst-2",
message: " raw fallback "
},
ensureSession: () => session,
appendItem: () => undefined,
getSession: () => null,
persistSession: () => undefined,
compactWhitespace: () => "",
repairAddressMojibake: () => "",
resolveRuntimeAnalysisContext: () => ({ as_of_date: null })
});
expect(output.userMessageRaw).toBe("raw fallback");
expect(output.userMessage).toBe("raw fallback");
expect(output.runtimeAnalysisContext).toEqual({ as_of_date: null });
});
});