From e7847c877e1993370fcf3b4e755932625d59ad7a Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:23:15 +0200 Subject: [PATCH] ai + category non match test --- .../utils/ai/choose-rule/match-rules.test.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/apps/web/utils/ai/choose-rule/match-rules.test.ts b/apps/web/utils/ai/choose-rule/match-rules.test.ts index 6a506237..7aa61943 100644 --- a/apps/web/utils/ai/choose-rule/match-rules.test.ts +++ b/apps/web/utils/ai/choose-rule/match-rules.test.ts @@ -195,6 +195,37 @@ describe("findMatchingRule", () => { expect(aiChooseRule).toHaveBeenCalledOnce(); }); + it("doesn't match when AI condition fails (category matches but AI doesn't)", async () => { + prisma.newsletter.findUnique.mockResolvedValue( + getNewsletter({ categoryId: "newsletterCategory" }), + ); + + const rule = getRule({ + conditionalOperator: LogicalOperator.AND, + instructions: "Match if the email is an AI newsletter", + categoryFilters: [getCategory({ id: "newsletterCategory" })], + }); + + (aiChooseRule as ReturnType).mockImplementationOnce(() => { + return { + reason: "Not an AI newsletter", + rule: undefined, + }; + }); + + const rules = [rule]; + const message = getMessage({ + headers: getHeaders({ from: "marketing@newsletter.com" }), + }); + const user = getUser(); + + const result = await findMatchingRule(rules, message, user); + + expect(result.rule).toBeUndefined(); + expect(result.reason).toBeDefined(); + expect(aiChooseRule).toHaveBeenCalledOnce(); + }); + it("doesn't match with only one of category or group", async () => { prisma.newsletter.findUnique.mockResolvedValue( getNewsletter({ categoryId: "category1" }),