-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,16 +74,18 @@ describe("findMatchingRule", () => { | |
}); | ||
|
||
it("matches a group rule", async () => { | ||
const rule = getRule({ groupId: "group1" }); | ||
|
||
prisma.group.findMany.mockResolvedValue([ | ||
getGroup({ | ||
id: "group1", | ||
items: [ | ||
getGroupItem({ type: GroupItemType.FROM, value: "[email protected]" }), | ||
], | ||
rule, | ||
}), | ||
]); | ||
|
||
const rule = getRule({ groupId: "group1" }); | ||
const rules = [rule]; | ||
const message = getMessage({ | ||
headers: getHeaders({ from: "[email protected]" }), | ||
|
@@ -135,23 +137,29 @@ describe("findMatchingRule", () => { | |
}); | ||
|
||
it("matches a rule with multiple conditions AND (category and group)", async () => { | ||
const rule = getRule({ | ||
conditionalOperator: LogicalOperator.AND, | ||
categoryFilters: [getCategory({ id: "category1" })], | ||
groupId: "group1", | ||
}); | ||
|
||
prisma.group.findMany.mockResolvedValue([ | ||
getGroup({ | ||
id: "group1", | ||
items: [ | ||
getGroupItem({ type: GroupItemType.FROM, value: "[email protected]" }), | ||
getGroupItem({ | ||
groupId: "group1", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", | ||
}), | ||
], | ||
rule, | ||
}), | ||
]); | ||
prisma.newsletter.findUnique.mockResolvedValue( | ||
getNewsletter({ categoryId: "category1" }), | ||
); | ||
|
||
const rule = getRule({ | ||
conditionalOperator: LogicalOperator.AND, | ||
categoryFilters: [getCategory({ id: "category1" })], | ||
groupId: "group1", | ||
}); | ||
const rules = [rule]; | ||
const message = getMessage({ | ||
headers: getHeaders({ from: "[email protected]" }), | ||
|
@@ -269,6 +277,130 @@ describe("findMatchingRule", () => { | |
expect(result.rule?.id).toBe(rule.id); | ||
expect(result.reason).toBe('Matched category: "category"'); | ||
}); | ||
|
||
it("should not match when item matches but is in wrong group", async () => { | ||
const rule = getRule({ | ||
groupId: "correctGroup", // Rule specifically looks for correctGroup | ||
}); | ||
|
||
// Set up two groups - one referenced by the rule, one not | ||
prisma.group.findMany.mockResolvedValue([ | ||
getGroup({ | ||
id: "wrongGroup", | ||
items: [ | ||
getGroupItem({ | ||
groupId: "wrongGroup", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", | ||
}), | ||
], | ||
}), | ||
getGroup({ | ||
id: "correctGroup", | ||
items: [ | ||
getGroupItem({ | ||
groupId: "correctGroup", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", | ||
}), | ||
], | ||
rule, | ||
}), | ||
]); | ||
|
||
const rules = [rule]; | ||
const message = getMessage({ | ||
headers: getHeaders({ from: "[email protected]" }), // This matches item in wrongGroup | ||
}); | ||
const user = getUser(); | ||
|
||
const result = await findMatchingRule(rules, message, user); | ||
|
||
expect(result.rule).toBeUndefined(); | ||
expect(result.reason).toBeUndefined(); | ||
}); | ||
|
||
it("should match only when item is in the correct group", async () => { | ||
const rule = getRule({ groupId: "correctGroup" }); | ||
|
||
// Set up two groups with similar items | ||
prisma.group.findMany.mockResolvedValue([ | ||
getGroup({ | ||
id: "correctGroup", | ||
items: [ | ||
getGroupItem({ | ||
groupId: "correctGroup", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", | ||
}), | ||
], | ||
rule, | ||
}), | ||
getGroup({ | ||
id: "otherGroup", | ||
items: [ | ||
getGroupItem({ | ||
groupId: "otherGroup", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", // Same value, different group | ||
}), | ||
], | ||
}), | ||
]); | ||
|
||
const rules = [rule]; | ||
const message = getMessage({ | ||
headers: getHeaders({ from: "[email protected]" }), | ||
}); | ||
const user = getUser(); | ||
|
||
const result = await findMatchingRule(rules, message, user); | ||
|
||
expect(result.rule?.id).toBe(rule.id); | ||
expect(result.reason).toContain("[email protected]"); | ||
}); | ||
|
||
it("should handle multiple rules with different group conditions correctly", async () => { | ||
const rule1 = getRule({ id: "rule1", groupId: "group1" }); | ||
const rule2 = getRule({ id: "rule2", groupId: "group2" }); | ||
|
||
prisma.group.findMany.mockResolvedValue([ | ||
getGroup({ | ||
id: "group1", | ||
items: [ | ||
getGroupItem({ | ||
groupId: "group1", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", | ||
}), | ||
], | ||
rule: rule1, | ||
}), | ||
getGroup({ | ||
id: "group2", | ||
items: [ | ||
getGroupItem({ | ||
groupId: "group2", | ||
type: GroupItemType.FROM, | ||
value: "[email protected]", | ||
}), | ||
], | ||
rule: rule2, | ||
}), | ||
]); | ||
|
||
const rules = [rule1, rule2]; | ||
const message = getMessage({ | ||
headers: getHeaders({ from: "[email protected]" }), | ||
}); | ||
const user = getUser(); | ||
|
||
const result = await findMatchingRule(rules, message, user); | ||
|
||
// Should match the first rule only | ||
expect(result.rule?.id).toBe("rule1"); | ||
expect(result.reason).toContain("[email protected]"); | ||
}); | ||
}); | ||
|
||
function getRule( | ||
|
@@ -329,8 +461,10 @@ function getCategory(overrides: Partial<Category> = {}): Category { | |
} | ||
|
||
function getGroup( | ||
overrides: Partial<Prisma.GroupGetPayload<{ include: { items: true } }>> = {}, | ||
): Prisma.GroupGetPayload<{ include: { items: true } }> { | ||
overrides: Partial< | ||
Prisma.GroupGetPayload<{ include: { items: true; rule: true } }> | ||
> = {}, | ||
): Prisma.GroupGetPayload<{ include: { items: true; rule: true } }> { | ||
return { | ||
id: "group1", | ||
name: "group", | ||
|
@@ -339,6 +473,7 @@ function getGroup( | |
userId: "userId", | ||
prompt: null, | ||
items: [], | ||
rule: null, | ||
...overrides, | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ff0b1f8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
inbox-zero – ./
inbox-zero-inbox-zero.vercel.app
inbox-zero.vercel.app
inbox-zero-git-main-inbox-zero.vercel.app
www.getinboxzero.com
getinboxzero.com