NODEDC_1C/llm_normalizer/backend/tests/assistantDeepTurnPackagingR...

185 lines
6.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { runAssistantDeepTurnPackagingRuntime } from "../src/services/assistantDeepTurnPackagingRuntimeAdapter";
function buildBaseInput() {
return {
featureInvestigationStateV1: true,
sessionId: "asst-1",
questionId: "msg-user-1",
userMessage: "проверь кейс",
normalized: {
trace_id: "trace-1",
prompt_version: "p1",
schema_version: "s1",
normalized: { schema_version: "normalized_query_v2_0_2" } as any
},
normalizedQuestion: "проверь кейс",
routeSummary: null,
executionPlan: [],
requirementExtractionRequirements: [],
coverageEvaluationRequirements: [],
coverageReport: {
requirements_total: 0,
requirements_covered: 0,
requirements_uncovered: [],
requirements_partially_covered: [],
clarification_needed_for: [],
out_of_scope_requirements: []
},
groundingCheck: {
status: "no_grounded_answer",
route_subject_match: false,
missing_requirements: [],
reasons: [],
why_included_summary: [],
selection_reason_summary: []
},
retrievalCalls: [],
retrievalResultsRaw: [],
retrievalResults: [],
questionTypeClass: "factual_lookup",
companyAnchors: {},
runtimeAnalysisContext: {
active: false,
as_of_date: null,
period_from: null,
period_to: null,
source: null,
snapshot_mode: "auto" as const
},
businessScopeResolution: {},
temporalGuard: {},
polarityAudit: {},
claimAnchorAudit: {},
targetedEvidenceAudit: {},
evidenceAdmissibilityGateAudit: {},
rbpLiveRouteAudit: null,
faLiveRouteAudit: null,
groundedAnswerEligibilityGuard: {},
followupStateUsage: null,
followupApplied: false,
composition: {
assistant_reply: "raw-reply",
reply_type: "factual" as const,
fallback_type: "none"
},
featureContractsV11: true,
featureAnswerPolicyV11: true,
previousInvestigationState: null,
addressRuntimeMetaForDeep: null,
extractDroppedIntentSegments: () => [],
buildDebugRoutes: () => [],
extractExecutionState: () => null,
sanitizeReply: (value: string) => value,
persistInvestigationState: () => {},
messageIdFactory: () => "msg-fixed"
};
}
describe("assistant deep turn packaging runtime adapter", () => {
it("executes pre-packaging, snapshot, persist, input-build and assembly in stable order", () => {
const callOrder: string[] = [];
let persistedByCallback = 0;
const output = runAssistantDeepTurnPackagingRuntime({
...buildBaseInput(),
persistInvestigationState: () => {
persistedByCallback += 1;
},
buildPrePackagingContextFn: (() => {
callOrder.push("pre");
return {
droppedIntentSegments: ["F2_dropped"],
analysisContextForContract: null,
routesForDebug: [{ fragment_id: "F1" }],
resolvedExecutionState: [{ fragment_id: "F1", execution_readiness: "ready" }],
safeAssistantReplyBase: "safe-base"
};
}) as any,
buildInvestigationStateSnapshotFn: (() => {
callOrder.push("snapshot");
return { schema_version: "investigation_state_v1" };
}) as any,
persistInvestigationStateSnapshotFn: ((input: Record<string, unknown>) => {
callOrder.push("persist");
(input.persist as Function)(input.sessionId, input.snapshot);
return true;
}) as any,
buildDeepTurnPackagingInputFn: ((input: Record<string, unknown>) => {
callOrder.push("input");
expect(input.messageId).toBe("msg-fixed");
expect(input.droppedIntentSegments).toEqual(["F2_dropped"]);
expect(input.safeAssistantReplyBase).toBe("safe-base");
return input;
}) as any,
assembleDeepTurnPackagingFn: (() => {
callOrder.push("assemble");
return {
deepAnswerArtifacts: {
safeAssistantReply: "assistant-safe"
},
debug: { ok: true },
assistantItem: {
message_id: "msg-fixed",
session_id: "asst-1",
role: "assistant",
text: "assistant-safe",
reply_type: "factual",
created_at: "2026-04-10T10:00:00.000Z",
trace_id: "trace-1",
debug: null
},
deepAnalysisLogDetails: { stage: "deep_analysis" }
};
}) as any
});
expect(callOrder).toEqual(["pre", "snapshot", "persist", "input", "assemble"]);
expect(persistedByCallback).toBe(1);
expect(output.messageId).toBe("msg-fixed");
expect(output.safeAssistantReply).toBe("assistant-safe");
expect(output.debug).toEqual({ ok: true });
expect(output.deepAnalysisLogDetails).toEqual({ stage: "deep_analysis" });
});
it("does not persist investigation snapshot when feature is disabled", () => {
let persistedByCallback = 0;
const output = runAssistantDeepTurnPackagingRuntime({
...buildBaseInput(),
featureInvestigationStateV1: false,
persistInvestigationState: () => {
persistedByCallback += 1;
},
buildPrePackagingContextFn: (() => ({
droppedIntentSegments: [],
analysisContextForContract: null,
routesForDebug: [],
resolvedExecutionState: null,
safeAssistantReplyBase: "safe-base"
})) as any,
buildDeepTurnPackagingInputFn: ((input: Record<string, unknown>) => input) as any,
assembleDeepTurnPackagingFn: (() => ({
deepAnswerArtifacts: {
safeAssistantReply: "assistant-safe"
},
debug: {},
assistantItem: {
message_id: "msg-fixed",
session_id: "asst-1",
role: "assistant",
text: "assistant-safe",
reply_type: "factual",
created_at: "2026-04-10T10:00:00.000Z",
trace_id: "trace-1",
debug: null
},
deepAnalysisLogDetails: {}
})) as any
});
expect(output.investigationStateSnapshot).toBeNull();
expect(persistedByCallback).toBe(0);
});
});