@remit/mailbox-service 0.0.99 → 0.0.101
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/body-sync.ts +16 -113
- package/src/classification-backfill.test.ts +543 -0
- package/src/classification-backfill.ts +345 -0
- package/src/classify-message.test.ts +146 -0
- package/src/classify-message.ts +113 -0
- package/src/heuristics/authenticationResults.test.ts +189 -0
- package/src/heuristics/authenticationResults.ts +212 -0
- package/src/heuristics/classifyByHeaders.test.ts +313 -2
- package/src/heuristics/classifyByHeaders.ts +95 -27
- package/src/index.ts +16 -1
- package/src/message-category.ts +18 -0
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the `backfillClassifications` pass (issue #1197) — the resumable,
|
|
3
|
+
* checkpointed full-corpus pass that classifies rows still carrying the
|
|
4
|
+
* `NotExamined` sentinel on `Message.classificationState`, from their stored
|
|
5
|
+
* bodies, and marks them `Examined`.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors `list-id-backfill.test.ts`: per-account pages, checkpoint after
|
|
8
|
+
* every page, resume by account-id order, and per-message failure
|
|
9
|
+
* containment via `.then(fulfilled, rejected)`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import assert from "node:assert/strict";
|
|
13
|
+
import { describe, it } from "node:test";
|
|
14
|
+
import type {
|
|
15
|
+
AccountConfigItem,
|
|
16
|
+
AddressItem,
|
|
17
|
+
IAccountConfigRepository,
|
|
18
|
+
IAddressRepository,
|
|
19
|
+
IMessageRepository,
|
|
20
|
+
IThreadMessageRepository,
|
|
21
|
+
MessageItem,
|
|
22
|
+
ResultList,
|
|
23
|
+
ThreadMessageItem,
|
|
24
|
+
UpdateMessageInput,
|
|
25
|
+
UpdateThreadMessageInput,
|
|
26
|
+
} from "@remit/data-ports";
|
|
27
|
+
import { NotFoundError } from "@remit/data-ports/errors";
|
|
28
|
+
import type { StorageService } from "@remit/storage-service";
|
|
29
|
+
import { backfillClassifications } from "./classification-backfill.js";
|
|
30
|
+
|
|
31
|
+
const PLAIN_EML = Buffer.from(
|
|
32
|
+
[
|
|
33
|
+
"From: Alice <alice@example.com>",
|
|
34
|
+
"To: me@example.com",
|
|
35
|
+
"Subject: Hi",
|
|
36
|
+
"Content-Type: text/plain",
|
|
37
|
+
"",
|
|
38
|
+
"hi",
|
|
39
|
+
].join("\r\n"),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const LIST_EML = Buffer.from(
|
|
43
|
+
[
|
|
44
|
+
"From: Alice <alice@example.com>",
|
|
45
|
+
"To: me@example.com",
|
|
46
|
+
"Subject: Newsletter",
|
|
47
|
+
"List-Id: <news.example.com>",
|
|
48
|
+
"List-Unsubscribe: <https://example.com/unsub>",
|
|
49
|
+
"Content-Type: text/plain",
|
|
50
|
+
"",
|
|
51
|
+
"this week in news",
|
|
52
|
+
].join("\r\n"),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const row = (overrides: Partial<ThreadMessageItem>): ThreadMessageItem =>
|
|
56
|
+
({
|
|
57
|
+
threadMessageId: "tm-1",
|
|
58
|
+
accountConfigId: "acc-1",
|
|
59
|
+
threadId: "thread-1",
|
|
60
|
+
messageId: "m-1",
|
|
61
|
+
mailboxId: "mb-1",
|
|
62
|
+
uid: 1,
|
|
63
|
+
referenceOrder: 0,
|
|
64
|
+
internalDate: 1,
|
|
65
|
+
sentDate: 1,
|
|
66
|
+
isRead: false,
|
|
67
|
+
hasAttachment: false,
|
|
68
|
+
star: "none",
|
|
69
|
+
hasStars: false,
|
|
70
|
+
isDeleted: false,
|
|
71
|
+
category: "uncategorized",
|
|
72
|
+
createdAt: 1,
|
|
73
|
+
updatedAt: 1,
|
|
74
|
+
...overrides,
|
|
75
|
+
}) as unknown as ThreadMessageItem;
|
|
76
|
+
|
|
77
|
+
const message = (overrides: Partial<MessageItem>): MessageItem =>
|
|
78
|
+
({
|
|
79
|
+
messageId: "m-1",
|
|
80
|
+
mailboxId: "mb-1",
|
|
81
|
+
uid: 1,
|
|
82
|
+
status: "active",
|
|
83
|
+
syncStatus: "synced",
|
|
84
|
+
category: "uncategorized",
|
|
85
|
+
classificationState: "NotExamined",
|
|
86
|
+
hasListUnsubscribe: false,
|
|
87
|
+
movedByRemit: false,
|
|
88
|
+
bodyStorageKey: "s3://m-1",
|
|
89
|
+
authenticityVerdict: "NotEvaluated",
|
|
90
|
+
createdAt: 1,
|
|
91
|
+
updatedAt: 1,
|
|
92
|
+
...overrides,
|
|
93
|
+
}) as unknown as MessageItem;
|
|
94
|
+
|
|
95
|
+
const asAccount = (accountConfigId: string): AccountConfigItem =>
|
|
96
|
+
({ accountConfigId }) as unknown as AccountConfigItem;
|
|
97
|
+
|
|
98
|
+
interface Harness {
|
|
99
|
+
accountConfigService: Pick<IAccountConfigRepository, "listAll">;
|
|
100
|
+
addressService: Pick<IAddressRepository, "getAddress">;
|
|
101
|
+
threadMessageService: Pick<
|
|
102
|
+
IThreadMessageRepository,
|
|
103
|
+
"listByAccount" | "findAllByMessageId" | "update"
|
|
104
|
+
>;
|
|
105
|
+
messageService: Pick<IMessageRepository, "get" | "update">;
|
|
106
|
+
storageService: Pick<StorageService, "retrieve">;
|
|
107
|
+
messageUpdates: Array<{ messageId: string; input: UpdateMessageInput }>;
|
|
108
|
+
threadUpdates: Array<{
|
|
109
|
+
accountConfigId: string;
|
|
110
|
+
threadMessageId: string;
|
|
111
|
+
input: UpdateThreadMessageInput;
|
|
112
|
+
}>;
|
|
113
|
+
retrieved: string[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const buildHarness = (options: {
|
|
117
|
+
accounts?: AccountConfigItem[];
|
|
118
|
+
rows: ThreadMessageItem[];
|
|
119
|
+
messages: MessageItem[];
|
|
120
|
+
retrieve?: (key: string) => Promise<Buffer>;
|
|
121
|
+
pageSize?: number;
|
|
122
|
+
addressFlags?: AddressItem["flags"];
|
|
123
|
+
}): Harness => {
|
|
124
|
+
const accounts = options.accounts ?? [
|
|
125
|
+
{ accountConfigId: "acc-1" } as unknown as AccountConfigItem,
|
|
126
|
+
];
|
|
127
|
+
const messagesById = new Map(options.messages.map((m) => [m.messageId, m]));
|
|
128
|
+
const messageUpdates: Harness["messageUpdates"] = [];
|
|
129
|
+
const threadUpdates: Harness["threadUpdates"] = [];
|
|
130
|
+
const retrieved: string[] = [];
|
|
131
|
+
const pageSize = options.pageSize ?? 200;
|
|
132
|
+
|
|
133
|
+
const accountConfigService: Pick<IAccountConfigRepository, "listAll"> = {
|
|
134
|
+
listAll: async () => accounts,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const addressService = {
|
|
138
|
+
getAddress: (async () => {
|
|
139
|
+
if (options.addressFlags === undefined)
|
|
140
|
+
throw new NotFoundError("no Address row for this sender");
|
|
141
|
+
return { flags: options.addressFlags } as AddressItem;
|
|
142
|
+
}) as unknown as IAddressRepository["getAddress"],
|
|
143
|
+
} as Pick<IAddressRepository, "getAddress">;
|
|
144
|
+
|
|
145
|
+
const threadMessageService: Pick<
|
|
146
|
+
IThreadMessageRepository,
|
|
147
|
+
"listByAccount" | "findAllByMessageId" | "update"
|
|
148
|
+
> = {
|
|
149
|
+
listByAccount: async (
|
|
150
|
+
accountConfigId: string,
|
|
151
|
+
opts?: { limit?: number; continuationToken?: string },
|
|
152
|
+
): Promise<ResultList<ThreadMessageItem>> => {
|
|
153
|
+
const scoped = options.rows.filter(
|
|
154
|
+
(r) => r.accountConfigId === accountConfigId,
|
|
155
|
+
);
|
|
156
|
+
const start = opts?.continuationToken
|
|
157
|
+
? Number(opts.continuationToken)
|
|
158
|
+
: 0;
|
|
159
|
+
const limit = opts?.limit ?? pageSize;
|
|
160
|
+
const page = scoped.slice(start, start + limit);
|
|
161
|
+
const nextStart = start + page.length;
|
|
162
|
+
return {
|
|
163
|
+
items: page,
|
|
164
|
+
continuationToken:
|
|
165
|
+
nextStart < scoped.length ? String(nextStart) : undefined,
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
findAllByMessageId: async (_accountConfigId, messageId) =>
|
|
169
|
+
options.rows.filter((r) => r.messageId === messageId),
|
|
170
|
+
update: async (accountConfigId, threadMessageId, input) => {
|
|
171
|
+
threadUpdates.push({ accountConfigId, threadMessageId, input });
|
|
172
|
+
const existing = options.rows.find(
|
|
173
|
+
(r) => r.threadMessageId === threadMessageId,
|
|
174
|
+
);
|
|
175
|
+
return { ...existing, ...input } as ThreadMessageItem;
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const messageService: Pick<IMessageRepository, "get" | "update"> = {
|
|
180
|
+
get: (async (messageIds: string | string[]) => {
|
|
181
|
+
if (Array.isArray(messageIds)) {
|
|
182
|
+
return messageIds
|
|
183
|
+
.map((id) => messagesById.get(id))
|
|
184
|
+
.filter((m): m is MessageItem => m !== undefined);
|
|
185
|
+
}
|
|
186
|
+
const found = messagesById.get(messageIds);
|
|
187
|
+
if (!found) throw new Error(`no fixture for ${messageIds}`);
|
|
188
|
+
return found;
|
|
189
|
+
}) as IMessageRepository["get"],
|
|
190
|
+
update: async (messageId, input) => {
|
|
191
|
+
messageUpdates.push({ messageId, input });
|
|
192
|
+
const existing = messagesById.get(messageId);
|
|
193
|
+
return { ...existing, ...input } as MessageItem;
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const storageService: Pick<StorageService, "retrieve"> = {
|
|
198
|
+
retrieve: async (key: string) => {
|
|
199
|
+
retrieved.push(key);
|
|
200
|
+
return options.retrieve ? options.retrieve(key) : PLAIN_EML;
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
accountConfigService,
|
|
206
|
+
addressService,
|
|
207
|
+
threadMessageService,
|
|
208
|
+
messageService,
|
|
209
|
+
storageService,
|
|
210
|
+
messageUpdates,
|
|
211
|
+
threadUpdates,
|
|
212
|
+
retrieved,
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
describe("backfillClassifications", () => {
|
|
217
|
+
it("classifies a NotExamined candidate from its stored body and marks it Examined", async () => {
|
|
218
|
+
const harness = buildHarness({
|
|
219
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
220
|
+
messages: [message({ messageId: "m-1" })],
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const result = await backfillClassifications(harness);
|
|
224
|
+
|
|
225
|
+
assert.equal(result.backfilled, 1);
|
|
226
|
+
assert.equal(result.failed, 0);
|
|
227
|
+
assert.equal(result.alreadyExamined, 0);
|
|
228
|
+
assert.deepEqual(harness.retrieved, ["s3://m-1"]);
|
|
229
|
+
assert.equal(harness.messageUpdates.length, 1);
|
|
230
|
+
assert.equal(harness.messageUpdates[0].messageId, "m-1");
|
|
231
|
+
// The plain fixture carries no bulk markers, so the header rule table
|
|
232
|
+
// answers `personal`.
|
|
233
|
+
assert.equal(harness.messageUpdates[0].input.category, "personal");
|
|
234
|
+
assert.equal(
|
|
235
|
+
harness.messageUpdates[0].input.classificationState,
|
|
236
|
+
"Examined",
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("denormalizes the classifier's answer onto the thread row", async () => {
|
|
241
|
+
const harness = buildHarness({
|
|
242
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
243
|
+
messages: [message({ messageId: "m-1" })],
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
await backfillClassifications(harness);
|
|
247
|
+
|
|
248
|
+
// The thread row is written BEFORE the Message update, so a failure
|
|
249
|
+
// between the two leaves both undone for the rerun (issue #320).
|
|
250
|
+
assert.equal(harness.threadUpdates.length, 1);
|
|
251
|
+
assert.equal(harness.threadUpdates[0].threadMessageId, "tm-1");
|
|
252
|
+
assert.equal(harness.threadUpdates[0].input.category, "personal");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("writes the derived bulk fields and the row's List-Id, from the same bytes", async () => {
|
|
256
|
+
const harness = buildHarness({
|
|
257
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
258
|
+
messages: [message({ messageId: "m-1", bodyStorageKey: "s3://list" })],
|
|
259
|
+
retrieve: async () => LIST_EML,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const result = await backfillClassifications(harness);
|
|
263
|
+
|
|
264
|
+
assert.equal(result.backfilled, 1);
|
|
265
|
+
assert.equal(harness.messageUpdates[0].input.category, "newsletter");
|
|
266
|
+
assert.equal(harness.messageUpdates[0].input.hasListUnsubscribe, true);
|
|
267
|
+
assert.equal(harness.threadUpdates[0].input.listId, "news.example.com");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("applies the sender's Address.flags.category override over the header category", async () => {
|
|
271
|
+
const harness = buildHarness({
|
|
272
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
273
|
+
messages: [message({ messageId: "m-1" })],
|
|
274
|
+
addressFlags: {
|
|
275
|
+
category: { value: "marketing", setAt: 1 },
|
|
276
|
+
} as AddressItem["flags"],
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const result = await backfillClassifications(harness);
|
|
280
|
+
|
|
281
|
+
assert.equal(result.backfilled, 1);
|
|
282
|
+
assert.equal(harness.messageUpdates[0].input.category, "marketing");
|
|
283
|
+
assert.equal(harness.threadUpdates[0].input.category, "marketing");
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("marks an already-categorized candidate Examined without re-deriving its category", async () => {
|
|
287
|
+
const harness = buildHarness({
|
|
288
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
289
|
+
messages: [
|
|
290
|
+
message({
|
|
291
|
+
messageId: "m-1",
|
|
292
|
+
category: "newsletter",
|
|
293
|
+
classificationState: "NotExamined",
|
|
294
|
+
}),
|
|
295
|
+
],
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
const result = await backfillClassifications(harness);
|
|
299
|
+
|
|
300
|
+
// Those rows were classified by a pass that predates the field; the
|
|
301
|
+
// write-once category is carried forward, never recomputed (#355).
|
|
302
|
+
assert.equal(result.alreadyCategorized, 1);
|
|
303
|
+
assert.equal(result.backfilled, 0);
|
|
304
|
+
assert.deepEqual(harness.retrieved, []);
|
|
305
|
+
assert.equal(harness.messageUpdates.length, 1);
|
|
306
|
+
assert.deepEqual(harness.messageUpdates[0].input, {
|
|
307
|
+
classificationState: "Examined",
|
|
308
|
+
});
|
|
309
|
+
assert.deepEqual(harness.threadUpdates, []);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("skips a row already Examined, without reading storage", async () => {
|
|
313
|
+
const harness = buildHarness({
|
|
314
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
315
|
+
messages: [
|
|
316
|
+
message({ messageId: "m-1", classificationState: "Examined" }),
|
|
317
|
+
],
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
const result = await backfillClassifications(harness);
|
|
321
|
+
|
|
322
|
+
assert.equal(result.alreadyExamined, 1);
|
|
323
|
+
assert.equal(result.backfilled, 0);
|
|
324
|
+
assert.deepEqual(harness.retrieved, []);
|
|
325
|
+
assert.deepEqual(harness.messageUpdates, []);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("skips a candidate whose body was never synced, without reading storage", async () => {
|
|
329
|
+
const harness = buildHarness({
|
|
330
|
+
rows: [row({ threadMessageId: "tm-1", messageId: "m-1" })],
|
|
331
|
+
messages: [message({ messageId: "m-1", bodyStorageKey: undefined })],
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
const result = await backfillClassifications(harness);
|
|
335
|
+
|
|
336
|
+
assert.equal(result.skippedNoBody, 1);
|
|
337
|
+
assert.deepEqual(harness.retrieved, []);
|
|
338
|
+
assert.deepEqual(harness.messageUpdates, []);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("examines a message once, however many thread rows index it", async () => {
|
|
342
|
+
const harness = buildHarness({
|
|
343
|
+
rows: [
|
|
344
|
+
row({ threadMessageId: "tm-1", messageId: "m-1" }),
|
|
345
|
+
row({
|
|
346
|
+
threadMessageId: "tm-2",
|
|
347
|
+
messageId: "m-1",
|
|
348
|
+
threadId: "thread-2",
|
|
349
|
+
}),
|
|
350
|
+
],
|
|
351
|
+
messages: [message({ messageId: "m-1" })],
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
const result = await backfillClassifications(harness);
|
|
355
|
+
|
|
356
|
+
assert.equal(result.backfilled, 1);
|
|
357
|
+
assert.equal(result.alreadyExamined, 1);
|
|
358
|
+
assert.deepEqual(harness.retrieved, ["s3://m-1"]);
|
|
359
|
+
assert.equal(harness.messageUpdates.length, 1);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("contains a classify failure to the one message and keeps going", async () => {
|
|
363
|
+
const harness = buildHarness({
|
|
364
|
+
rows: [
|
|
365
|
+
row({ threadMessageId: "tm-bad", messageId: "m-bad" }),
|
|
366
|
+
row({ threadMessageId: "tm-good", messageId: "m-good" }),
|
|
367
|
+
],
|
|
368
|
+
messages: [
|
|
369
|
+
message({ messageId: "m-bad", bodyStorageKey: "s3://m-bad" }),
|
|
370
|
+
message({ messageId: "m-good", bodyStorageKey: "s3://m-good" }),
|
|
371
|
+
],
|
|
372
|
+
retrieve: async (key) => {
|
|
373
|
+
if (key === "s3://m-bad") throw new Error("AccessDenied");
|
|
374
|
+
return PLAIN_EML;
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
const result = await backfillClassifications(harness);
|
|
379
|
+
|
|
380
|
+
assert.equal(result.failed, 1);
|
|
381
|
+
assert.deepEqual(result.failedThreadMessageIds, ["tm-bad"]);
|
|
382
|
+
assert.equal(result.backfilled, 1);
|
|
383
|
+
assert.equal(harness.messageUpdates.length, 1);
|
|
384
|
+
assert.equal(harness.messageUpdates[0].messageId, "m-good");
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("reports progress as pages are processed", async () => {
|
|
388
|
+
const rows = Array.from({ length: 3 }, (_, i) =>
|
|
389
|
+
row({ threadMessageId: `tm-${i}`, messageId: `m-${i}` }),
|
|
390
|
+
);
|
|
391
|
+
const messages = rows.map((r) =>
|
|
392
|
+
message({
|
|
393
|
+
messageId: r.messageId,
|
|
394
|
+
bodyStorageKey: `s3://${r.messageId}`,
|
|
395
|
+
}),
|
|
396
|
+
);
|
|
397
|
+
const harness = buildHarness({ rows, messages, pageSize: 2 });
|
|
398
|
+
const progress: Array<{ accountConfigId: string; scanned: number }> = [];
|
|
399
|
+
|
|
400
|
+
const result = await backfillClassifications(harness, {
|
|
401
|
+
batchSize: 2,
|
|
402
|
+
onProgress: (p) => progress.push({ ...p }),
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
assert.equal(result.backfilled, 3);
|
|
406
|
+
assert.equal(progress.length, 2);
|
|
407
|
+
assert.equal(progress[0].scanned, 2);
|
|
408
|
+
assert.equal(progress[1].scanned, 3);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("scans every account returned by listAll, in account-id order", async () => {
|
|
412
|
+
const accountIds = ["acc-1", "acc-2", "acc-3"];
|
|
413
|
+
const rows = accountIds.map((accountConfigId) =>
|
|
414
|
+
row({
|
|
415
|
+
threadMessageId: `tm-${accountConfigId}`,
|
|
416
|
+
messageId: `m-${accountConfigId}`,
|
|
417
|
+
accountConfigId,
|
|
418
|
+
}),
|
|
419
|
+
);
|
|
420
|
+
const harness = buildHarness({
|
|
421
|
+
accounts: ["acc-3", "acc-1", "acc-2"].map(asAccount),
|
|
422
|
+
rows,
|
|
423
|
+
messages: rows.map((r) =>
|
|
424
|
+
message({
|
|
425
|
+
messageId: r.messageId,
|
|
426
|
+
bodyStorageKey: `s3://${r.messageId}`,
|
|
427
|
+
}),
|
|
428
|
+
),
|
|
429
|
+
});
|
|
430
|
+
const progress: Array<{ accountConfigId: string; scanned: number }> = [];
|
|
431
|
+
|
|
432
|
+
const result = await backfillClassifications(harness, {
|
|
433
|
+
onProgress: (p) => progress.push({ ...p }),
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
assert.equal(result.backfilled, 3);
|
|
437
|
+
assert.deepEqual(
|
|
438
|
+
progress.map((p) => p.accountConfigId),
|
|
439
|
+
accountIds,
|
|
440
|
+
);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("checkpoints after each page and clears it on completion", async () => {
|
|
444
|
+
const rows = Array.from({ length: 3 }, (_, i) =>
|
|
445
|
+
row({ threadMessageId: `tm-${i}`, messageId: `m-${i}` }),
|
|
446
|
+
);
|
|
447
|
+
const messages = rows.map((r) =>
|
|
448
|
+
message({
|
|
449
|
+
messageId: r.messageId,
|
|
450
|
+
bodyStorageKey: `s3://${r.messageId}`,
|
|
451
|
+
}),
|
|
452
|
+
);
|
|
453
|
+
const harness = buildHarness({ rows, messages, pageSize: 2 });
|
|
454
|
+
|
|
455
|
+
const saved: Array<{
|
|
456
|
+
accountConfigId: string;
|
|
457
|
+
continuationToken?: string;
|
|
458
|
+
}> = [];
|
|
459
|
+
let cleared = false;
|
|
460
|
+
|
|
461
|
+
await backfillClassifications(harness, {
|
|
462
|
+
batchSize: 2,
|
|
463
|
+
checkpointStore: {
|
|
464
|
+
load: async () => undefined,
|
|
465
|
+
save: async (checkpoint) => {
|
|
466
|
+
saved.push(checkpoint);
|
|
467
|
+
},
|
|
468
|
+
clear: async () => {
|
|
469
|
+
cleared = true;
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
assert.equal(saved.length, 2);
|
|
475
|
+
assert.equal(saved[0].accountConfigId, "acc-1");
|
|
476
|
+
assert.equal(saved[0].continuationToken, "2");
|
|
477
|
+
assert.equal(saved[1].continuationToken, undefined);
|
|
478
|
+
assert.equal(cleared, true);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it("resumes from a saved checkpoint instead of rescanning from the start", async () => {
|
|
482
|
+
const rows = Array.from({ length: 3 }, (_, i) =>
|
|
483
|
+
row({ threadMessageId: `tm-${i}`, messageId: `m-${i}` }),
|
|
484
|
+
);
|
|
485
|
+
const messages = rows.map((r) =>
|
|
486
|
+
message({
|
|
487
|
+
messageId: r.messageId,
|
|
488
|
+
bodyStorageKey: `s3://${r.messageId}`,
|
|
489
|
+
}),
|
|
490
|
+
);
|
|
491
|
+
const harness = buildHarness({ rows, messages, pageSize: 2 });
|
|
492
|
+
|
|
493
|
+
const result = await backfillClassifications(harness, {
|
|
494
|
+
batchSize: 2,
|
|
495
|
+
checkpointStore: {
|
|
496
|
+
load: async () => ({
|
|
497
|
+
accountConfigId: "acc-1",
|
|
498
|
+
continuationToken: "2",
|
|
499
|
+
}),
|
|
500
|
+
save: async () => {},
|
|
501
|
+
clear: async () => {},
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
assert.equal(result.scanned, 1);
|
|
506
|
+
assert.equal(result.backfilled, 1);
|
|
507
|
+
assert.deepEqual(
|
|
508
|
+
harness.messageUpdates.map((u) => u.messageId),
|
|
509
|
+
["m-2"],
|
|
510
|
+
);
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it("restarts the pass when the checkpointed account no longer exists", async () => {
|
|
514
|
+
const rows = Array.from({ length: 3 }, (_, i) =>
|
|
515
|
+
row({ threadMessageId: `tm-${i}`, messageId: `m-${i}` }),
|
|
516
|
+
);
|
|
517
|
+
const messages = rows.map((r) =>
|
|
518
|
+
message({
|
|
519
|
+
messageId: r.messageId,
|
|
520
|
+
bodyStorageKey: `s3://${r.messageId}`,
|
|
521
|
+
}),
|
|
522
|
+
);
|
|
523
|
+
const harness = buildHarness({ rows, messages, pageSize: 2 });
|
|
524
|
+
|
|
525
|
+
const result = await backfillClassifications(harness, {
|
|
526
|
+
batchSize: 2,
|
|
527
|
+
checkpointStore: {
|
|
528
|
+
load: async () => ({
|
|
529
|
+
accountConfigId: "acc-removed",
|
|
530
|
+
continuationToken: "2",
|
|
531
|
+
}),
|
|
532
|
+
save: async () => {},
|
|
533
|
+
clear: async () => {},
|
|
534
|
+
},
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
assert.equal(result.scanned, 3);
|
|
538
|
+
assert.deepEqual(
|
|
539
|
+
harness.messageUpdates.map((u) => u.messageId),
|
|
540
|
+
["m-0", "m-1", "m-2"],
|
|
541
|
+
);
|
|
542
|
+
});
|
|
543
|
+
});
|