import { describe, expect, it } from "vitest"; import { readAddressDebugTemporalScope, resolveAddressDebugCarryoverFilters, resolveAddressDebugContextFacts, resolveAddressDebugAnchorContext, resolveAssistantOrganizationAuthority } from "../src/services/assistantContinuityPolicy"; describe("assistantContinuityPolicy organization authority", () => { it("prefers explicit assistant organization authority over older grounded continuity and merges known organizations once", () => { const authority = resolveAssistantOrganizationAuthority({ sessionItems: [ { role: "assistant", debug: { execution_lane: "address_query", answer_grounding_check: { status: "grounded" }, extracted_filters: { organization: "Org Grounded", as_of_date: "2020-03-31" }, detected_intent: "receivables_confirmed_as_of_date" } }, { role: "assistant", debug: { execution_lane: "living_chat", living_chat_selected_organization: "Org Selected", assistant_active_organization: "Org Selected", assistant_known_organizations: ["Org Selected", "Org Known"], living_chat_data_scope_probe_organizations: ["Org Probe"] } } ], sessionKnownOrganizations: ["Org Session"], lastOrganizationClarificationDebug: { organization_candidates: ["Org Candidate", "Org Selected"] } }); expect(authority.continuitySnapshot.activeOrganization).toBe("Org Grounded"); expect(authority.continuityActiveOrganization).toBe("Org Selected"); expect(authority.selectedOrganization).toBe("Org Selected"); expect(authority.activeOrganization).toBe("Org Selected"); expect(authority.knownOrganizations).toEqual([ "Org Session", "Org Selected", "Org Known", "Org Probe", "Org Grounded" ]); expect(authority.organizationClarificationCandidates).toEqual([ "Org Candidate", "Org Selected", "Org Session", "Org Known", "Org Probe", "Org Grounded" ]); expect(authority.organizationClarificationSelectionFromScope).toBe("Org Selected"); }); it("reads item, organization and scoped date from root-frame fallback when direct filters are missing", () => { const facts = resolveAddressDebugContextFacts({ anchor_type: "item", anchor_value_resolved: "Рабочая станция", address_root_frame_context: { organization: 'ООО "Альтернатива Плюс"', as_of_date: "2020-03-31" } }); expect(facts).toEqual({ item: "Рабочая станция", organization: 'ООО "Альтернатива Плюс"', scopedDate: "31.03.2020" }); }); it("resolves carryover temporal scope and inferred anchor from debug filters when explicit anchor fields are absent", () => { const debug = { extracted_filters: { item: "Рабочая станция", period_from: "2020-03-01" }, address_root_frame_context: { as_of_date: "2020-03-31", period_to: "2020-03-31" } }; expect(readAddressDebugTemporalScope(debug)).toEqual({ asOfDate: "2020-03-31", periodFrom: "2020-03-01", periodTo: "2020-03-31" }); expect(resolveAddressDebugAnchorContext(debug)).toEqual({ anchorType: "item", anchorValue: "Рабочая станция" }); }); it("prefers inventory root-frame filters over stale drilldown date scope in carryover filters", () => { const filters = resolveAddressDebugCarryoverFilters({ detected_intent: "inventory_purchase_documents_for_item", extracted_filters: { item: "Workstation", organization: "Org Alt", as_of_date: "2021-04-15" }, address_root_frame_context: { root_intent: "inventory_on_hand_as_of_date", root_filters: { organization: "Org Alt", warehouse: "Main Warehouse", as_of_date: "2021-03-31", period_from: "2021-03-01", period_to: "2021-03-31" }, root_anchor_type: "organization", root_anchor_value: "Org Alt", current_frame_kind: "inventory_drilldown" } }); expect(filters).toEqual({ item: "Workstation", organization: "Org Alt", warehouse: "Main Warehouse", as_of_date: "2021-03-31", period_from: "2021-03-01", period_to: "2021-03-31" }); }); });