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("inventory selected-object follow-up", () => { it("inherits dated stock window for selected-object provenance and then auto-broadens history", async () => { executeAddressMcpQueryMock .mockResolvedValueOnce({ fetched_rows: 1, matched_rows: 1, raw_rows: [ { Period: "2021-03-09T00:00:00Z", Registrator: "Поступление товаров и услуг 00000000001 от 09.03.2021 0:00:00", AccountDt: "41.01", AccountKt: "60.01", Amount: 442075, SubcontoDt1: "Рабочая станция универсального специалиста с угловым элементом.", SubcontoDt3: "Основной склад", SubcontoKt1: "АСТРА", SubcontoKt2: "А_03/2021 от 01.03.2021г.", Organization: "ООО \\Альтернатива Плюс\\" } ], rows: [], error: null }) .mockResolvedValueOnce({ fetched_rows: 2, matched_rows: 2, raw_rows: [ { Period: "2020-02-11T00:00:00Z", Registrator: "Поступление товаров и услуг 00000000077 от 11.02.2020 0:00:00", AccountDt: "41.01", AccountKt: "60.01", Amount: 165.83, SubcontoDt1: "Кромка с клеем 33 альмандин 137 м", SubcontoDt3: "Основной склад", SubcontoKt1: "Торговый дом \\Союз МСК\\", SubcontoKt2: "Договор поставки № 12 от 01.02.2020", Organization: "ООО \\Альтернатива Плюс\\" }, { Period: "2020-02-11T00:00:00Z", Registrator: "Поступление товаров и услуг 00000000077 от 11.02.2020 0:00:00", AccountDt: "41.01", AccountKt: "60.01", Amount: 165.83, SubcontoDt1: "Кромка с клеем 33 дуб ниагара 137 м", SubcontoDt3: "Основной склад", SubcontoKt1: "Торговый дом \\Союз МСК\\", SubcontoKt2: "Договор поставки № 12 от 01.02.2020", Organization: "ООО \\Альтернатива Плюс\\" } ], rows: [], error: null }); const service = new AddressQueryService(); const result = await service.tryHandle( 'По выбранному объекту "Кромка с клеем 33 альмандин 137 м | склад: Основной склад | количество: 1,000 | стоимость: 165,83 ₽ | организация: ООО \\\\Альтернатива Плюс\\\\ | дата строки: 2021-03-31T23:59:59Z": От какого поставщика куплен товар', { followupContext: { previous_intent: "inventory_on_hand_as_of_date", previous_filters: { as_of_date: "2021-03-31", period_from: "2021-03-01", period_to: "2021-03-31", warehouse: "Основной склад", organization: "ООО \\Альтернатива Плюс\\" }, previous_anchor_type: "unknown", previous_anchor_value: null } } ); expect(result?.handled).toBe(true); expect(result?.response_type).toBe("FACTUAL_SUMMARY"); expect(result?.debug.detected_intent).toBe("inventory_purchase_provenance_for_item"); expect(result?.debug.extracted_filters?.item).toBe("Кромка с клеем 33 альмандин 137 м"); expect(result?.debug.extracted_filters?.as_of_date).toBe("2021-03-31"); expect(result?.debug.extracted_filters?.period_from).toBe("2021-03-01"); expect(result?.debug.extracted_filters?.period_to).toBe("2021-03-31"); expect(result?.debug.capability_id).toBe("inventory_inventory_purchase_provenance_for_item"); expect(result?.debug.capability_route_mode).toBe("exact"); expect(result?.debug.reasons).toContain("period_window_auto_broadened_to_available_data"); expect(result?.debug.limitations).toContain("period_window_auto_broadened_to_available_data"); const replyLines = String(result?.reply_text ?? "").split("\n"); expect(replyLines[0]).toContain("Товар Кромка с клеем 33 альмандин 137 м"); expect(replyLines[0]).toContain("Торговый дом \\Союз МСК\\"); expect(replyLines[1]).toContain("По окну 2021-03-01..2021-03-31 строк не найдено"); expect(executeAddressMcpQueryMock).toHaveBeenCalledTimes(2); }); it("handles selected-object supplier slang 'кто это поставил нам' as provenance follow-up", async () => { executeAddressMcpQueryMock.mockResolvedValueOnce({ fetched_rows: 1, matched_rows: 1, raw_rows: [ { Period: "2019-02-11T00:00:00Z", Registrator: "Поступление товаров и услуг 00000000077 от 11.02.2019 0:00:00", AccountDt: "41.01", AccountKt: "60.01", Amount: 3724.17, SubcontoDt1: "Столешница 600*3050*26 дуб ниагара", SubcontoDt3: "Основной склад", SubcontoKt1: "Торговый дом \\Союз МСК\\", SubcontoKt2: "Договор поставки № 12 от 01.02.2019", Organization: "ООО \\Альтернатива Плюс\\" } ], rows: [], error: null }); const service = new AddressQueryService(); const result = await service.tryHandle('По выбранному объекту "Столешница 600*3050*26 дуб ниагара": кто это поставил нам', { followupContext: { previous_intent: "inventory_on_hand_as_of_date", previous_filters: { as_of_date: "2019-03-31", period_from: "2019-03-01", period_to: "2019-03-31", warehouse: "Основной склад", organization: "ООО \\Альтернатива Плюс\\" }, previous_anchor_type: "unknown", previous_anchor_value: null } }); expect(result?.handled).toBe(true); expect(result?.response_type).toBe("FACTUAL_SUMMARY"); expect(result?.debug.detected_intent).toBe("inventory_purchase_provenance_for_item"); expect(result?.debug.extracted_filters?.item).toBe("Столешница 600*3050*26 дуб ниагара"); expect(result?.debug.extracted_filters?.as_of_date).toBe("2019-03-31"); expect(String(result?.reply_text ?? "")).toContain("Торговый дом \\Союз МСК\\"); }); it("handles selected-object purchase-doc slang 'по каким документам это купили' as exact purchase-doc follow-up", async () => { executeAddressMcpQueryMock.mockResolvedValueOnce({ fetched_rows: 1, matched_rows: 1, raw_rows: [ { Period: "2019-02-11T00:00:00Z", Registrator: "Поступление товаров и услуг 00000000077 от 11.02.2019 0:00:00", AccountDt: "41.01", AccountKt: "60.01", Amount: 3724.17, SubcontoDt1: "Столешница 600*3050*26 дуб ниагара", SubcontoDt3: "Основной склад", SubcontoKt1: "Торговый дом \\Союз МСК\\", SubcontoKt2: "Договор поставки № 12 от 01.02.2019", Organization: "ООО \\Альтернатива Плюс\\" } ], rows: [], error: null }); const service = new AddressQueryService(); const result = await service.tryHandle('По выбранному объекту "Столешница 600*3050*26 дуб ниагара": по каким документам это купили', { followupContext: { previous_intent: "inventory_purchase_provenance_for_item", previous_filters: { as_of_date: "2019-03-31", period_from: "2019-03-01", period_to: "2019-03-31", item: "Столешница 600*3050*26 дуб ниагара", warehouse: "Основной склад" }, previous_anchor_type: "unknown", previous_anchor_value: null } }); expect(result?.handled).toBe(true); expect(result?.response_type).toBe("FACTUAL_LIST"); expect(result?.debug.detected_intent).toBe("inventory_purchase_documents_for_item"); expect(result?.debug.selected_recipe).toBe("address_inventory_purchase_documents_for_item_v1"); expect(result?.debug.extracted_filters?.item).toBe("Столешница 600*3050*26 дуб ниагара"); expect(result?.debug.extracted_filters?.as_of_date).toBe("2019-03-31"); expect(String(result?.reply_text ?? "")).toContain("Поступление товаров и услуг 00000000077"); }); });