NODEDC_1C/llm_normalizer/backend/tests/assistantDeepTurnAnalysisAt...

136 lines
6.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { buildAssistantDeepTurnAnalysisRuntimeInput } from "../src/services/assistantDeepTurnAnalysisAttemptInputBuilder";
function buildInput(overrides: Record<string, unknown> = {}) {
return {
userMessage: "why debt not closed",
normalizedPayload: { fragments: [] },
routeSummary: { mode: "deterministic_v2", decisions: [] },
runtimeAnalysisContext: {
active: true,
as_of_date: "2020-07-31",
period_from: null,
period_to: null,
source: "analysis_context"
},
followupUsage: { applied: true },
investigationState: { session_id: "asst-1" },
featureAnswerPolicyV11: true,
featureProblemCentricAnswerV1: true,
featureLifecycleAnswerV1: true,
resolveBusinessScopeAlignment: vi.fn(() => ({ route_summary_resolved: { mode: "deterministic_v2", decisions: [] } })),
inferP0DomainFromMessage: vi.fn(() => "settlements_60_62"),
resolveBusinessScopeFromLiveContext: vi.fn(({ current }) => current),
extractRequirements: vi.fn(() => ({ requirements: [{ id: "R1" }], byFragment: new Map([["F1", ["R1"]]]) })),
toExecutionPlan: vi.fn(() => [{ fragment_id: "F1", requirement_ids: ["R1"], route: "store_canonical", should_execute: true }]),
enforceRbpLiveRoutePlan: vi.fn(({ executionPlan }) => ({ executionPlan, audit: { rbp: true } })),
enforceFaLiveRoutePlan: vi.fn(({ executionPlan }) => ({ executionPlan, audit: { fa: true } })),
executeRouteRuntime: vi.fn(async () => ({})),
mapNoRouteReason: vi.fn(() => "skipped"),
buildSkippedResult: vi.fn(() => ({ status: "skipped" })),
evaluateCoverage: vi.fn(() => ({ requirements: [{ id: "R1" }], coverage: {} })),
checkGrounding: vi.fn(() => ({ status: "grounded", reasons: [] })),
collectRbpLiveRouteAudit: vi.fn(() => ({ routed: 1 })),
collectFaLiveRouteAudit: vi.fn(() => ({ routed: 1 })),
hasExplicitPeriodAnchor: vi.fn(() => false),
runDeepTurnContextRuntimeSafe: vi.fn(() => ({ context: true })),
runDeepTurnPlanRuntimeSafe: vi.fn(() => ({ plan: true })),
runDeepTurnRetrievalRuntimeSafe: vi.fn(async () => ({ retrieval: true })),
runDeepTurnGuardRuntimeSafe: vi.fn(() => ({ guard: true })),
runDeepTurnGroundingRuntimeSafe: vi.fn(() => ({ grounding: true })),
runDeepTurnCompositionRuntimeSafe: vi.fn(() => ({ composition: true })),
...overrides
} as any;
}
describe("assistant deep turn analysis attempt input builder", () => {
it("builds analysis runtime callbacks and delegates stage payload mapping", async () => {
const input = buildInput();
const runtimeInput = buildAssistantDeepTurnAnalysisRuntimeInput(input);
runtimeInput.runContextRuntime();
runtimeInput.runExecutionPlanRuntime({
resolvedRouteSummary: { mode: "deterministic_v2", decisions: [] } as any,
claimAnchorAudit: { claim_type: "prove_settlement_closure_state" } as any,
temporalGuard: { primary_period_window: {} },
domainPolarityGuardInitial: { polarity: "supplier_payable" }
});
await runtimeInput.runRetrievalRuntime({
executionPlan: [{ fragment_id: "F1", route: "store_canonical" }],
liveTemporalHint: { as_of_date: "2020-07-31", period_from: null, period_to: null, source: "analysis_context" }
} as any);
runtimeInput.runGuardRuntime({
retrievalResults: [],
domainPolarityGuardInitial: { polarity: "supplier_payable" },
claimAnchorAudit: { claim_type: "prove_settlement_closure_state" },
temporalGuard: { primary_period_window: {} },
focusDomainForGuards: "settlements_60_62",
companyAnchors: { accounts: ["60.01"] },
userMessage: "runtime-user-message"
} as any);
runtimeInput.runGroundingRuntime({
claimType: "prove_settlement_closure_state",
retrievalResults: [],
rbpPlanAudit: { rbp: true },
faPlanAudit: { fa: true },
routeSummary: { mode: "deterministic_v2", decisions: [] },
requirementExtraction: { requirements: [{ id: "R1" }], byFragment: new Map([["F1", ["R1"]]]) },
temporalGuard: { primary_period_window: {} },
polarityAudit: { polarity: "supplier_payable" },
evidenceAudit: { admissible_evidence_count: 1 },
claimAnchorAudit: { claim_type: "prove_settlement_closure_state" },
targetedEvidenceHitRate: 1,
businessScopeResolved: ["company_specific_accounting"]
} as any);
runtimeInput.runCompositionRuntime({
resolvedRouteSummary: { mode: "deterministic_v2", decisions: [] },
retrievalResults: [],
requirements: [{ id: "R1" }],
coverageReport: { requirements_total: 1, requirements_covered: 1 },
groundingCheck: { status: "grounded", reasons: [] },
companyAnchors: { accounts: ["60.01"] }
} as any);
expect(input.runDeepTurnContextRuntimeSafe).toHaveBeenCalledWith(
expect.objectContaining({
userMessage: "why debt not closed",
runtimeAnalysisContext: expect.objectContaining({ as_of_date: "2020-07-31" }),
resolveCompanyAnchors: expect.any(Function)
})
);
expect(input.runDeepTurnPlanRuntimeSafe).toHaveBeenCalledWith(
expect.objectContaining({
claimType: "prove_settlement_closure_state",
userMessage: "why debt not closed"
})
);
expect(input.runDeepTurnRetrievalRuntimeSafe).toHaveBeenCalledWith(
expect.objectContaining({
executeRouteRuntime: input.executeRouteRuntime,
mapNoRouteReason: input.mapNoRouteReason
})
);
expect(input.runDeepTurnGuardRuntimeSafe).toHaveBeenCalledWith(
expect.objectContaining({
userMessage: "runtime-user-message",
focusDomainForGuards: "settlements_60_62"
})
);
expect(input.runDeepTurnGroundingRuntimeSafe).toHaveBeenCalledWith(
expect.objectContaining({
normalizedPayload: { fragments: [] },
userMessage: "why debt not closed",
targetedEvidenceHitRate: 1
})
);
expect(input.runDeepTurnCompositionRuntimeSafe).toHaveBeenCalledWith(
expect.objectContaining({
userMessage: "why debt not closed",
featureAnswerPolicyV11: true,
featureProblemCentricAnswerV1: true,
featureLifecycleAnswerV1: true
})
);
});
});