import { beforeEach, describe, expect, it, vi } from "vitest"; import type { Invoice } from "../drizzle/schema"; import { getAutomationRulesByUser } from "./db"; import { applyAutomationRules } from "./automationEngine"; vi.mock("./db", () => ({ getAutomationRulesByUser: vi.fn(), })); const mockedGetAutomationRules = vi.mocked(getAutomationRulesByUser); const invoice = { id: 1, supplierName: "Microsoft Ireland Operations Ltd", isSubscription: 0, } as Invoice; describe("applyAutomationRules", () => { beforeEach(() => { mockedGetAutomationRules.mockReset(); }); it("applique Abonnement = Oui et trace le champ automatisé", async () => { mockedGetAutomationRules.mockResolvedValue([ { id: 1, userId: 1, name: "Microsoft est un abonnement", isActive: 1, priority: 1, conditions: JSON.stringify([{ field: "supplierName", operator: "contains", value: "microsoft" }]), conditionsLogic: "AND", actions: JSON.stringify({ isSubscription: 1 }), createdAt: new Date(), updatedAt: new Date(), }, ] as never); await expect(applyAutomationRules(1, invoice)).resolves.toMatchObject({ isSubscription: 1, autoFilledFields: JSON.stringify(["isSubscription"]), }); }); it("autorise Abonnement = Non sans le confondre avec une absence d’action", async () => { mockedGetAutomationRules.mockResolvedValue([ { id: 2, userId: 1, name: "Microsoft n’est pas un abonnement", isActive: 1, priority: 1, conditions: JSON.stringify([{ field: "supplierName", operator: "contains", value: "microsoft" }]), conditionsLogic: "AND", actions: JSON.stringify({ isSubscription: 0 }), createdAt: new Date(), updatedAt: new Date(), }, ] as never); await expect(applyAutomationRules(1, invoice)).resolves.toMatchObject({ isSubscription: 0, autoFilledFields: JSON.stringify(["isSubscription"]), }); }); });