-
Notifications
You must be signed in to change notification settings - Fork 591
/
Copy pathai-categorize-senders.test.ts
212 lines (185 loc) · 6.55 KB
/
ai-categorize-senders.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { describe, it, expect, vi } from "vitest";
import {
aiCategorizeSenders,
REQUEST_MORE_INFORMATION_CATEGORY,
} from "@/utils/ai/categorize-sender/ai-categorize-senders";
import { defaultCategory } from "@/utils/categories";
import { aiCategorizeSender } from "@/utils/ai/categorize-sender/ai-categorize-single-sender";
// pnpm test-ai ai-categorize-senders
const isAiTest = process.env.RUN_AI_TESTS === "true";
vi.mock("server-only", () => ({}));
const testUser = {
email: "[email protected]",
aiProvider: null,
aiModel: null,
aiApiKey: null,
};
const testSenders = [
{
emailAddress: "[email protected]",
emails: [
{ subject: "Latest updates and news from our company", snippet: "" },
],
expectedCategory: "Newsletter",
},
{
emailAddress: "[email protected]",
emails: [{ subject: "Your ticket #1234 has been updated", snippet: "" }],
expectedCategory: "Support",
},
{
emailAddress: "[email protected]",
emails: [],
expectedCategory: "Unknown",
},
{
emailAddress: "[email protected]",
emails: [
{ subject: "Special offer: 20% off our enterprise plan", snippet: "" },
],
expectedCategory: "Marketing",
},
{
emailAddress: "[email protected]",
emails: [{ subject: "John Smith mentioned you in a comment", snippet: "" }],
expectedCategory: "Social",
},
];
describe.runIf(isAiTest)("AI Sender Categorization", () => {
describe("Bulk Categorization", () => {
it("should categorize senders with snippets using AI", async () => {
const result = await aiCategorizeSenders({
user: testUser,
senders: testSenders,
categories: getEnabledCategories(),
});
expect(result).toHaveLength(testSenders.length);
// Test newsletter categorization with snippet
const newsletterResult = result.find(
(r) => r.sender === "[email protected]",
);
expect(newsletterResult?.category).toBe("Newsletter");
// Test support categorization with ticket snippet
const supportResult = result.find(
(r) => r.sender === "[email protected]",
);
expect(supportResult?.category).toBe("Support");
// Test sales categorization with offer snippet
const salesResult = result.find((r) => r.sender === "[email protected]");
expect(salesResult?.category).toBe("Marketing");
}, 15_000);
it("should handle empty senders list", async () => {
const result = await aiCategorizeSenders({
user: testUser,
senders: [],
categories: [],
});
expect(result).toEqual([]);
});
it("should categorize senders for all valid SenderCategory values", async () => {
const senders = getEnabledCategories()
.filter((category) => category.name !== "Unknown")
.map((category) => `${category.name}@example.com`);
const result = await aiCategorizeSenders({
user: testUser,
senders: senders.map((sender) => ({
emailAddress: sender,
emails: [],
})),
categories: getEnabledCategories(),
});
expect(result).toHaveLength(senders.length);
for (const sender of senders) {
const category = sender.split("@")[0];
const senderResult = result.find((r) => r.sender === sender);
expect(senderResult).toBeDefined();
expect(senderResult?.category).toBe(category);
}
}, 15_000);
});
describe("Single Sender Categorization", () => {
it("should categorize individual senders with snippets", async () => {
for (const { emailAddress, emails, expectedCategory } of testSenders) {
const result = await aiCategorizeSender({
user: testUser,
sender: emailAddress,
previousEmails: emails,
categories: getEnabledCategories(),
});
if (expectedCategory === "Unknown") {
expect([REQUEST_MORE_INFORMATION_CATEGORY, "Unknown"]).toContain(
result?.category,
);
} else {
expect(result?.category).toBe(expectedCategory);
}
}
}, 30_000);
it("should handle unknown sender appropriately", async () => {
const unknownSender = testSenders.find(
(s) => s.expectedCategory === "Unknown",
);
if (!unknownSender) throw new Error("No unknown sender in test data");
const result = await aiCategorizeSender({
user: testUser,
sender: unknownSender.emailAddress,
previousEmails: [],
categories: getEnabledCategories(),
});
expect([REQUEST_MORE_INFORMATION_CATEGORY, "Unknown"]).toContain(
result?.category,
);
}, 15_000);
});
describe("Comparison Tests", () => {
it("should produce consistent results between bulk and single categorization", async () => {
// Run bulk categorization
const bulkResults = await aiCategorizeSenders({
user: testUser,
senders: testSenders,
categories: getEnabledCategories(),
});
// Run individual categorizations and pair with senders
const singleResults = await Promise.all(
testSenders.map(async ({ emailAddress, emails }) => {
const result = await aiCategorizeSender({
user: testUser,
sender: emailAddress,
previousEmails: emails,
categories: getEnabledCategories(),
});
return {
sender: emailAddress,
category: result?.category,
};
}),
);
// Compare results for each sender
for (const { emailAddress, expectedCategory } of testSenders) {
const bulkResult = bulkResults.find((r) => r.sender === emailAddress);
const singleResult = singleResults.find(
(r) => r.sender === emailAddress,
);
// Both should either have a category or both be undefined
if (bulkResult?.category || singleResult?.category) {
expect(bulkResult?.category).toBeDefined();
expect(singleResult?.category).toBeDefined();
expect(bulkResult?.category).toBe(singleResult?.category);
// If not Unknown, check against expected category
if (expectedCategory !== "Unknown") {
expect(bulkResult?.category).toBe(expectedCategory);
expect(singleResult?.category).toBe(expectedCategory);
}
}
}
}, 30_000);
});
});
const getEnabledCategories = () => {
return Object.entries(defaultCategory)
.filter(([_, value]) => value.enabled)
.map(([_, value]) => ({
name: value.name,
description: value.description,
}));
};