NODEDC_1C/llm_normalizer/backend/tests/assistantDeepTurnNormalizat...

146 lines
4.5 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { buildAssistantDeepTurnNormalizationRuntime } from "../src/services/assistantDeepTurnNormalizationRuntimeAdapter";
describe("assistant deep turn normalization runtime adapter", () => {
it("uses followup state binding when feature flags are enabled and state exists", async () => {
const followupBinding = {
normalizedQuestion: "normalized question",
mergedContext: {
period_hint: "2020-07",
business_context: "ctx-from-followup"
},
usage: {
applied: true
}
};
const buildFollowupStateBinding = vi.fn(() => followupBinding);
const normalize = vi.fn(async (request) => ({
trace_id: "trace-1",
ok: true,
normalized: { schema_version: "normalized_query_v2_0_2" } as any,
route_hint_summary: null,
raw_model_output: {},
validation: { passed: true, errors: [] },
usage: { input_tokens: 10, output_tokens: 20, total_tokens: 30 },
latency_ms: 7,
prompt_version: String(request.promptVersion ?? ""),
schema_version: "normalized_query_v2_0_2",
request_count_for_case: 1
}));
const runtime = await buildAssistantDeepTurnNormalizationRuntime({
userMessage: "raw question",
payload: {
llmProvider: "openai",
apiKey: "k",
model: "m",
baseUrl: "https://api.example.com",
temperature: 0.2,
maxOutputTokens: 333,
promptVersion: "normalizer_v2_0_2",
systemPrompt: "sys",
developerPrompt: "dev",
domainPrompt: "dom",
fewShotExamples: "few",
context: {
period_hint: "2020-06"
},
useMock: true
},
featureInvestigationStateV1: true,
featureStateFollowupBindingV1: true,
sessionInvestigationState: {
active_domain: "settlements_60_62"
},
buildFollowupStateBinding,
normalize
});
expect(buildFollowupStateBinding).toHaveBeenCalledTimes(1);
expect(normalize).toHaveBeenCalledTimes(1);
expect(normalize).toHaveBeenCalledWith({
llmProvider: "openai",
apiKey: "k",
model: "m",
baseUrl: "https://api.example.com",
temperature: 0.2,
maxOutputTokens: 333,
promptVersion: "normalizer_v2_0_2",
systemPrompt: "sys",
developerPrompt: "dev",
domainPrompt: "dom",
fewShotExamples: "few",
userQuestion: "normalized question",
context: {
period_hint: "2020-07",
business_context: "ctx-from-followup"
},
useMock: true
});
expect(runtime.followupBinding).toBe(followupBinding);
expect(runtime.normalizePayload.userQuestion).toBe("normalized question");
});
it("falls back to raw user message when followup binding is disabled", async () => {
const buildFollowupStateBinding = vi.fn();
const normalize = vi.fn(async () => ({
trace_id: "trace-2",
ok: true,
normalized: { schema_version: "normalized_query_v2_0_2" } as any,
route_hint_summary: null,
raw_model_output: {},
validation: { passed: true, errors: [] },
usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 },
latency_ms: 1,
prompt_version: "address_query_runtime_v1",
schema_version: "normalized_query_v2_0_2",
request_count_for_case: 1
}));
const runtime = await buildAssistantDeepTurnNormalizationRuntime({
userMessage: "raw fallback",
payload: {
llmProvider: "openai",
context: {
business_context: "payload-context"
},
useMock: undefined
},
featureInvestigationStateV1: false,
featureStateFollowupBindingV1: true,
sessionInvestigationState: {
some: "state"
},
buildFollowupStateBinding,
normalize
});
expect(buildFollowupStateBinding).not.toHaveBeenCalled();
expect(normalize).toHaveBeenCalledWith({
llmProvider: "openai",
apiKey: undefined,
model: undefined,
baseUrl: undefined,
temperature: undefined,
maxOutputTokens: undefined,
promptVersion: "address_query_runtime_v1",
systemPrompt: undefined,
developerPrompt: undefined,
domainPrompt: undefined,
fewShotExamples: undefined,
userQuestion: "raw fallback",
context: {
business_context: "payload-context"
},
useMock: false
});
expect(runtime.followupBinding).toEqual({
normalizedQuestion: "raw fallback",
mergedContext: {
business_context: "payload-context"
},
usage: null
});
});
});