import { afterEach, describe, expect, it, vi } from "vitest"; const { executeAddressMcpQueryMock } = vi.hoisted(() => ({ executeAddressMcpQueryMock: vi.fn() })); vi.mock("../src/services/addressMcpClient", async () => { const actual = await vi.importActual( "../src/services/addressMcpClient" ); return { ...actual, executeAddressMcpQuery: executeAddressMcpQueryMock }; }); import { AddressQueryService } from "../src/services/addressQueryService"; afterEach(() => { executeAddressMcpQueryMock.mockReset(); vi.restoreAllMocks(); }); describe("referential organization scope grounding", () => { it("grounds 'по этой компании' to the active organization and clears bogus counterparty anchor", async () => { executeAddressMcpQueryMock.mockResolvedValueOnce({ fetched_rows: 1, matched_rows: 1, raw_rows: [ { Period: "2026-04-19T23:59:59Z", Registrator: "Остатки товаров на складах", AccountDt: "41.01", AccountKt: "00.00", Amount: 9800, Quantity: 2, SubcontoDt1: "Рабочая станция универсального специалиста", Warehouse: "Основной склад", Organization: "РАЙМ" } ], rows: [], error: null }); const service = new AddressQueryService(); const result = await service.tryHandle("а по этой компании что сейчас на складе?", { activeOrganization: "РАЙМ", knownOrganizations: ["ООО Альтернатива Плюс", "РАЙМ"], followupContext: { previous_intent: "inventory_on_hand_as_of_date", previous_filters: { organization: "ООО Альтернатива Плюс", as_of_date: "2026-04-19" }, previous_anchor_type: "organization", previous_anchor_value: "ООО Альтернатива Плюс" } }); expect(result?.handled).toBe(true); expect(result?.reply_type).toBe("factual"); expect(result?.debug.detected_intent).toBe("inventory_on_hand_as_of_date"); expect(result?.debug.extracted_filters?.organization).toBe("РАЙМ"); expect(result?.debug.extracted_filters?.counterparty).toBeUndefined(); expect(result?.debug.reasons).toContain("organization_grounded_from_referential_scope"); expect(result?.debug.reasons).toContain("counterparty_cleared_from_referential_organization_scope"); expect(String(result?.reply_text ?? "")).toContain("Рабочая станция универсального специалиста"); }); });