import { describe, expect, it } from "vitest"; import { attachAssistantStateTransition, resolveAssistantStateTransitionRuntime } from "../src/services/assistantStateTransitionRuntimeAdapter"; describe("assistant state transition runtime adapter", () => { it("applies root entry as a root-frame create and selected-object clear", () => { const transition = resolveAssistantStateTransitionRuntime({ addressDebug: { capability_id: "confirmed_inventory_on_hand_as_of_date", detected_intent: "inventory_on_hand_as_of_date", rows_matched: 4, route_expectation_status: "matched" }, groundingStatus: "grounded", replyType: "factual" }); expect(transition).toEqual( expect.objectContaining({ schema_version: "assistant_state_transition_runtime_v1", state_owner: "assistantStateTransitionRuntimeAdapter", transition_id: "T1", transition_title: "Root Query Entry", application_status: "applied", declared_carryover_depth: "none", truth_gate_carryover_depth: "root_only", effective_carryover_depth: "none", expected_answer_mode: "confirmed" }) ); expect(transition.state_actions).toEqual( expect.objectContaining({ root_frame_state: "create", selected_object_frame_state: "clear", coverage_gate_state: "create", answer_context_state: "create" }) ); expect(transition.coverage_gate_state.truth_mode).toBe("confirmed"); }); it("keeps selected-object short follow-ups object-scoped", () => { const transition = resolveAssistantStateTransitionRuntime({ addressDebug: { capability_id: "inventory_inventory_purchase_provenance_for_item", detected_intent: "inventory_purchase_provenance_for_item", rows_matched: 1, route_expectation_status: "matched" }, addressRuntimeMeta: { dialogContinuationContract: { decision: "continue_previous", target_intent: "inventory_purchase_provenance_for_item" } }, groundingStatus: "grounded", replyType: "factual" }); expect(transition.transition_id).toBe("T4"); expect(transition.application_status).toBe("applied"); expect(transition.effective_carryover_depth).toBe("object_only"); expect(transition.state_actions.selected_object_frame_state).toBe("reuse"); expect(transition.state_actions.root_frame_state).toBe("preserve"); expect(transition.forbidden_carryover).toEqual( expect.arrayContaining(["generic_chat_fallback", "data_scope_selection_fallback", "object_focus_reset"]) ); }); it("turns missing-anchor cases into clarification state transitions", () => { const transition = resolveAssistantStateTransitionRuntime({ addressDebug: { capability_id: "inventory_inventory_purchase_provenance_for_item", missing_required_filters: ["item"], limited_reason_category: "missing_anchor" }, replyType: "partial_coverage" }); expect(transition.transition_id).toBe("T7"); expect(transition.application_status).toBe("clarification_required"); expect(transition.effective_carryover_depth).toBe("full"); expect(transition.state_actions.clarification_state).toBe("update"); expect(transition.coverage_gate_state.truth_mode).toBe("clarification_required"); }); it("blocks route expectation failures and forbids implicit follow-up reuse", () => { const transition = resolveAssistantStateTransitionRuntime({ addressDebug: { capability_id: "confirmed_inventory_on_hand_as_of_date", route_expectation_status: "mismatch" }, groundingStatus: "route_mismatch_blocked", replyType: "partial_coverage" }); expect(transition.transition_id).toBe("T10"); expect(transition.application_status).toBe("blocked"); expect(transition.effective_carryover_depth).toBe("none"); expect(transition.state_actions.coverage_gate_state).toBe("block"); expect(transition.forbidden_carryover).toEqual( expect.arrayContaining(["blocked_as_confirmed_factual_answer", "implicit_followup_reuse"]) ); }); it("attaches compact debug fields and preserves the nested transition contract", () => { const debug = attachAssistantStateTransition( { assistant_runtime_contract_v1: { schema_version: "assistant_runtime_contracts_v1", transition_contract_id: "T8", transition_contract_title: "Meta Follow-Up Over Answer Object", transition_contract_reason: ["capability_meta_followup_tool_gate_reason"], capability_contract_id: "confirmed_inventory_on_hand_as_of_date", capability_contract_reason: ["intent_matched_capability_contract"], truth_gate_contract_status: "partial_supported", carryover_eligibility: "meta_only" }, assistant_truth_answer_policy_v1: { schema_version: "assistant_truth_answer_policy_runtime_v1", policy_owner: "assistantTruthAnswerPolicyRuntimeAdapter", truth_gate: { schema_version: "assistant_truth_answer_policy_runtime_v1", policy_owner: "assistantTruthAnswerPolicyRuntimeAdapter", coverage_status: "partial", evidence_grade: "weak", grounding_status: "partial", truth_mode: "limited", carryover_eligibility: "meta_only", reason_codes: ["truth_gate_partial_supported"], source_truth_gate_status: "partial_supported", blocked_or_limited_explanation: "evidence_or_coverage_is_partial" }, answer_shape: { schema_version: "assistant_truth_answer_policy_runtime_v1", policy_owner: "assistantTruthAnswerPolicyRuntimeAdapter", answer_shape: "limited_with_reason", reply_type: "deep_analysis", capability_contract_id: "confirmed_inventory_on_hand_as_of_date", transition_contract_id: "T8", may_state_confirmed_facts: true, must_include_limitation: true, may_power_followup: true, required_sections: ["direct_answer", "evidence_window", "limitations"], downgrade_only: true } } }, { replyType: "deep_analysis" } ); expect(debug.state_transition_id).toBe("T8"); expect(debug.state_transition_status).toBe("applied"); expect(debug.effective_carryover_depth).toBe("meta_only"); expect(debug.assistant_state_transition_v1.state_actions.meta_frame_state).toBe("create"); expect(debug.state_transition_contract.state_actions.answer_context_state).toBe("reuse"); }); });