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" }),