NODEDC_1C/llm_normalizer/backend/tests/assistantDeepTurnResponseRu...

164 lines
4.8 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { runAssistantDeepTurnResponseRuntime } from "../src/services/assistantDeepTurnResponseRuntimeAdapter";
function buildBaseInput(overrides: Record<string, unknown> = {}) {
return {
featureInvestigationStateV1: true,
featureContractsV11: true,
featureAnswerPolicyV11: true,
sessionId: "asst-1",
questionId: "msg-q1",
userMessage: "question",
normalized: {
trace_id: "trace-1",
prompt_version: "normalizer_v2_0_2",
schema_version: "normalized_query_v2_0_2",
normalized: { schema_version: "normalized_query_v2_0_2" }
},
normalizedQuestion: "normalized-question",
routeSummary: { mode: "deterministic_v2", decisions: [] as any[] },
executionPlan: [],
requirementExtractionRequirements: [],
coverageEvaluationRequirements: [],
coverageReport: {},
groundingCheck: { status: "no_grounded_answer", reasons: [] },
retrievalCalls: [],
retrievalResultsRaw: [],
retrievalResults: [],
questionTypeClass: "single_fact_lookup",
companyAnchors: {},
runtimeAnalysisContext: {},
businessScopeResolution: {},
temporalGuard: {},
polarityAudit: {},
claimAnchorAudit: {},
targetedEvidenceAudit: {},
evidenceAdmissibilityGateAudit: {},
rbpLiveRouteAudit: {},
faLiveRouteAudit: {},
groundedAnswerEligibilityGuard: {},
followupStateUsage: null,
followupApplied: false,
composition: {
reply_type: "factual",
assistant_reply: "assistant answer"
},
previousInvestigationState: null,
addressRuntimeMetaForDeep: null,
extractDroppedIntentSegments: () => [],
buildDebugRoutes: () => [],
extractExecutionState: () => [],
sanitizeReply: (value: string) => value,
persistInvestigationState: () => {},
messageIdFactory: () => "msg-a1",
appendItem: () => {},
getSession: () => ({ session_id: "asst-1", items: [] }),
persistSession: () => {},
cloneConversation: () => [],
logEvent: () => {},
...overrides
} as any;
}
describe("assistant deep turn response runtime adapter", () => {
it("wires packaging output into deep finalization", () => {
const runPackagingRuntime = vi.fn(() => ({
messageId: "msg-a1",
investigationStateSnapshot: null,
droppedIntentSegments: [],
analysisContextForContract: null,
routesForDebug: [],
resolvedExecutionState: [],
safeAssistantReplyBase: "base",
safeAssistantReply: "safe-reply",
debug: { trace_id: "trace-1" },
assistantItem: {
message_id: "msg-a1",
session_id: "asst-1",
role: "assistant",
text: "safe-reply",
reply_type: "factual",
created_at: "2026-04-10T00:00:00.000Z",
trace_id: "trace-1",
debug: null
},
deepAnalysisLogDetails: { info: "ok" }
}));
const responsePayload = {
ok: true,
session_id: "asst-1",
conversation: [],
debug: { trace_id: "trace-1" }
};
const runFinalizeDeepTurn = vi.fn(() => ({
response: responsePayload
}));
const runtime = runAssistantDeepTurnResponseRuntime(
buildBaseInput({
runPackagingRuntime,
runFinalizeDeepTurn
})
);
expect(runPackagingRuntime).toHaveBeenCalledTimes(1);
expect(runFinalizeDeepTurn).toHaveBeenCalledWith(
expect.objectContaining({
sessionId: "asst-1",
assistantReply: "safe-reply",
replyType: "factual"
})
);
expect(runtime.response).toEqual(responsePayload);
expect(runtime.debug).toEqual({ trace_id: "trace-1" });
});
it("passes feature flags and followup flags into packaging stage", () => {
const runPackagingRuntime = vi.fn(() => ({
messageId: "msg-a1",
investigationStateSnapshot: null,
droppedIntentSegments: [],
analysisContextForContract: null,
routesForDebug: [],
resolvedExecutionState: [],
safeAssistantReplyBase: "base",
safeAssistantReply: "safe-reply",
debug: {},
assistantItem: {
message_id: "msg-a1",
session_id: "asst-1",
role: "assistant",
text: "safe-reply",
reply_type: "factual",
created_at: "2026-04-10T00:00:00.000Z",
trace_id: "trace-1",
debug: null
},
deepAnalysisLogDetails: {}
}));
runAssistantDeepTurnResponseRuntime(
buildBaseInput({
featureInvestigationStateV1: false,
followupApplied: true,
runPackagingRuntime,
runFinalizeDeepTurn: () => ({
response: {
ok: true,
session_id: "asst-1",
conversation: [],
debug: null
}
})
})
);
expect(runPackagingRuntime).toHaveBeenCalledWith(
expect.objectContaining({
featureInvestigationStateV1: false,
followupApplied: true
})
);
});
});