@remit/mailbox-service 0.0.111 → 0.0.112
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 +21 -6
- package/src/body-part-mapper.ts +51 -1
- package/src/body-sync-queue.test.ts +39 -99
- package/src/body-sync-terminal.test.ts +115 -277
- package/src/body-sync-terminal.ts +10 -12
- package/src/body-sync.test.ts +925 -0
- package/src/body-sync.ts +156 -235
- package/src/classification-backfill.test.ts +197 -527
- package/src/classify-message.test.ts +11 -74
- package/src/external-move.test.ts +102 -209
- package/src/external-move.ts +13 -0
- package/src/filter-operations.test.ts +319 -0
- package/src/filter-operations.ts +377 -0
- package/src/filter-update.test.ts +313 -0
- package/src/filters/filter-config.test.ts +55 -197
- package/src/filters/filter-config.ts +4 -39
- package/src/filters/match.test.ts +1 -38
- package/src/filters/match.ts +3 -52
- package/src/filters/pipeline.test.ts +158 -289
- package/src/filters/pipeline.ts +56 -170
- package/src/flag-push-terminal.ts +9 -11
- package/src/flag-push.test.ts +76 -372
- package/src/flag-queue.test.ts +197 -0
- package/src/folder-operations.test.ts +273 -0
- package/src/folder-operations.ts +217 -0
- package/src/forwarded-message.test.ts +122 -0
- package/src/imapflow-connect-retry.test.ts +76 -113
- package/src/imapflow-connection.test.ts +34 -0
- package/src/imapflow-connection.ts +5 -4
- package/src/index.ts +59 -12
- package/src/list-id-backfill.test.ts +133 -499
- package/src/mailbox-cursor.test.ts +81 -189
- package/src/mailbox-delete-intent.test.ts +78 -205
- package/src/mailbox-intent.ts +5 -3
- package/src/mailbox-management.test.ts +107 -220
- package/src/mailbox-management.ts +1 -2
- package/src/mailbox-presence.test.ts +36 -34
- package/src/mailbox-queue.ts +2 -20
- package/src/mailbox-rename-intent.test.ts +156 -325
- package/src/mailbox-rename-subtree.test.ts +205 -582
- package/src/mailbox-sync.test.ts +182 -744
- package/src/message-move.test.ts +726 -0
- package/src/message-move.ts +227 -57
- package/src/message-read.ts +87 -0
- package/src/message-save-retry.test.ts +90 -0
- package/src/message-sync-flags.test.ts +10 -1
- package/src/message-sync.test.ts +762 -0
- package/src/message-sync.ts +409 -239
- package/src/mime-walker.ts +16 -15
- package/src/organize-job.ts +93 -0
- package/src/outbox-attachment.test.ts +97 -209
- package/src/outbox-failed-recovery.test.ts +198 -269
- package/src/outbox-queue.test.ts +24 -0
- package/src/outbox-queue.ts +25 -8
- package/src/ownership.ts +140 -0
- package/src/placement-move-terminal.ts +9 -12
- package/src/placement-move.ts +33 -6
- package/src/placement-settled.ts +28 -4
- package/src/placement.test.ts +404 -0
- package/src/quarantine.test.ts +90 -148
- package/src/quarantine.ts +3 -2
- package/src/refusal.ts +29 -0
- package/src/restore-source-placement.ts +0 -4
- package/src/spam-report.test.ts +238 -710
- package/src/sync-warning.test.ts +213 -0
- package/src/sync-warning.ts +56 -0
- package/src/test-helpers/fake-imap.ts +690 -0
- package/src/test-helpers/test-queue.ts +56 -0
- package/src/test-helpers/test-store.ts +299 -0
- package/src/thread-root.test.ts +29 -0
- package/src/thread-root.ts +51 -0
- package/src/types/mailparser-augment.d.ts +8 -0
- package/src/adversarial-mime.e2e.test.ts +0 -307
- package/src/body-sync-auto-read-writeonce.test.ts +0 -219
- package/src/body-sync-calendar-suggestion.test.ts +0 -433
- package/src/body-sync-category-denormalize.test.ts +0 -352
- package/src/body-sync-category-override.test.ts +0 -501
- package/src/body-sync-category-writeonce.test.ts +0 -234
- package/src/body-sync-filter.test.ts +0 -237
- package/src/body-sync-message-gone.test.ts +0 -139
- package/src/body-sync-placement-flags.test.ts +0 -347
- package/src/body-sync-placement-writeonce.test.ts +0 -337
- package/src/body-sync-quarantine.test.ts +0 -276
- package/src/body-sync-resync.test.ts +0 -605
- package/src/body-sync-unclassifiable-once.test.ts +0 -260
- package/src/body-sync-unsubscribed-auto-read.test.ts +0 -176
- package/src/filters/anchor-drift.ts +0 -55
- package/src/filters/message-embedder.test.ts +0 -39
- package/src/filters/message-embedder.ts +0 -33
- package/src/flag-push-terminal.test.ts +0 -209
- package/src/flag-queue-stars.test.ts +0 -171
- package/src/imapflow-connection.e2e.test.ts +0 -335
- package/src/imapflow-connection.integ.test.ts +0 -316
- package/src/message-delete-junk-reconcile.test.ts +0 -117
- package/src/message-delete-no-trash.test.ts +0 -245
- package/src/message-delete-unsettled-placement.test.ts +0 -396
- package/src/message-move-copy.test.ts +0 -562
- package/src/message-move-empty-trash.test.ts +0 -326
- package/src/message-move-same-mailbox.test.ts +0 -139
- package/src/message-move-unsettled-placement.test.ts +0 -378
- package/src/message-sync-address-spoof.test.ts +0 -293
- package/src/message-sync-category.test.ts +0 -142
- package/src/message-sync-changedsince.test.ts +0 -840
- package/src/message-sync-junk-harvest.test.ts +0 -244
- package/src/message-sync-quarantine.test.ts +0 -416
- package/src/message-sync-repoint.test.ts +0 -419
- package/src/outbox-queue-settled-row.test.ts +0 -140
- package/src/pass-through-unit-of-work.ts +0 -17
- package/src/placement-move-terminal.test.ts +0 -300
- package/src/placement-move-unsettled.test.ts +0 -275
- package/src/placement-transition-race.test.ts +0 -482
- package/src/stale-message-reconcile.test.ts +0 -82
- package/src/stale-message-reconcile.ts +0 -55
- package/src/test-helpers/isolated-mailbox.ts +0 -44
- package/src/test-helpers/mailfuzz-connection.ts +0 -27
- package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +0 -21
- package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +0 -31
- package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +0 -33
- package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +0 -42
- package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +0 -25
- package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +0 -15
- package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +0 -17
- package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +0 -18
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
3
|
+
import type {
|
|
4
|
+
AccountItem,
|
|
5
|
+
CreateMessageInput,
|
|
6
|
+
MailboxItem,
|
|
7
|
+
MessageItem,
|
|
8
|
+
} from "@remit/data-ports";
|
|
9
|
+
import { MessagePlacementUnsettledError } from "@remit/data-ports/errors";
|
|
10
|
+
import { deriveAddressId } from "@remit/data-ports/id";
|
|
11
|
+
import { MessageMoveService } from "./message-move.js";
|
|
12
|
+
import { PlacementMoveService } from "./placement-move.js";
|
|
13
|
+
import { resolveExhaustedPlacementMoveFailure } from "./placement-move-terminal.js";
|
|
14
|
+
import { eml, FakeImapServer } from "./test-helpers/fake-imap.js";
|
|
15
|
+
import {
|
|
16
|
+
arriveAt,
|
|
17
|
+
beforeNext,
|
|
18
|
+
captureQueue,
|
|
19
|
+
openStore,
|
|
20
|
+
type QueueCapture,
|
|
21
|
+
type QueuedEvent,
|
|
22
|
+
type Store,
|
|
23
|
+
seedAccount,
|
|
24
|
+
seedMailbox,
|
|
25
|
+
seedMessage,
|
|
26
|
+
silentLog,
|
|
27
|
+
} from "./test-helpers/test-store.js";
|
|
28
|
+
|
|
29
|
+
let store: Store;
|
|
30
|
+
let capture: QueueCapture;
|
|
31
|
+
let queue: QueuedEvent[];
|
|
32
|
+
let imap: FakeImapServer;
|
|
33
|
+
let account: AccountItem;
|
|
34
|
+
let inbox: MailboxItem;
|
|
35
|
+
let archive: MailboxItem;
|
|
36
|
+
let junk: MailboxItem;
|
|
37
|
+
let trash: MailboxItem;
|
|
38
|
+
|
|
39
|
+
beforeEach(async () => {
|
|
40
|
+
capture = captureQueue();
|
|
41
|
+
queue = capture.sent;
|
|
42
|
+
store = await openStore();
|
|
43
|
+
imap = new FakeImapServer();
|
|
44
|
+
account = await seedAccount(store);
|
|
45
|
+
inbox = await seedMailbox(store, account, "INBOX");
|
|
46
|
+
archive = await seedMailbox(store, account, "Archive");
|
|
47
|
+
junk = await seedMailbox(store, account, "Junk", { specialUse: "Junk" });
|
|
48
|
+
trash = await seedMailbox(store, account, "Trash", { specialUse: "Trash" });
|
|
49
|
+
for (const mailbox of await store.mailbox.listAllByAccount(
|
|
50
|
+
account.accountId,
|
|
51
|
+
)) {
|
|
52
|
+
imap.addFolder(mailbox.fullPath);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterEach(async () => {
|
|
57
|
+
capture.restore();
|
|
58
|
+
await store.close();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const filing = () =>
|
|
62
|
+
new PlacementMoveService({
|
|
63
|
+
messageService: store.message,
|
|
64
|
+
threadMessageService: store.threadMessage,
|
|
65
|
+
markerService: store.placementMove,
|
|
66
|
+
addressService: store.address,
|
|
67
|
+
mailboxSpecialUseService: store.mailboxSpecialUse,
|
|
68
|
+
mailboxService: store.mailbox,
|
|
69
|
+
sqsQueueUrl: "https://sqs.test/message-mgmt",
|
|
70
|
+
moveSettleTimeoutMs: 200,
|
|
71
|
+
moveSettlePollMs: 10,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const userMoves = () =>
|
|
75
|
+
new MessageMoveService({
|
|
76
|
+
messageService: store.message,
|
|
77
|
+
mailboxService: store.mailbox,
|
|
78
|
+
mailboxSpecialUseService: store.mailboxSpecialUse,
|
|
79
|
+
threadMessageService: store.threadMessage,
|
|
80
|
+
addressService: store.address,
|
|
81
|
+
sqsQueueUrl: "http://localhost:9324/000000000000/remit-messages.fifo",
|
|
82
|
+
moveSettleTimeoutMs: 200,
|
|
83
|
+
moveSettlePollMs: 10,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const rowIn = async (
|
|
87
|
+
mailbox: MailboxItem,
|
|
88
|
+
uid: number,
|
|
89
|
+
message: Partial<CreateMessageInput> = {},
|
|
90
|
+
) =>
|
|
91
|
+
(await seedMessage(store, account, mailbox, uid, { message })).message
|
|
92
|
+
.messageId;
|
|
93
|
+
|
|
94
|
+
const row = (messageId: string) => store.message.get(messageId);
|
|
95
|
+
|
|
96
|
+
const listing = async (messageId: string) =>
|
|
97
|
+
(
|
|
98
|
+
await store.threadMessage.findAllByMessageId(
|
|
99
|
+
account.accountConfigId,
|
|
100
|
+
messageId,
|
|
101
|
+
)
|
|
102
|
+
)[0];
|
|
103
|
+
|
|
104
|
+
const file = (messageId: string, destination: MailboxItem) =>
|
|
105
|
+
filing().moveMessage(
|
|
106
|
+
account.accountConfigId,
|
|
107
|
+
messageId,
|
|
108
|
+
destination.mailboxId,
|
|
109
|
+
account.accountId,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const confirmMove = async (messageId: string, uid: number) => {
|
|
113
|
+
const marker = await store.placementMove.find(messageId);
|
|
114
|
+
await store.message.updateUid(
|
|
115
|
+
messageId,
|
|
116
|
+
uid,
|
|
117
|
+
marker?.destinationMailboxId ?? "",
|
|
118
|
+
);
|
|
119
|
+
await store.placementMove.delete(messageId);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const settleElsewhere = (messageId: string) => () =>
|
|
123
|
+
store.message.updateUid(messageId, 907, archive.mailboxId);
|
|
124
|
+
|
|
125
|
+
describe("a classification filing", () => {
|
|
126
|
+
it("moves the row and its listing locally, leaves a queued marker and enqueues the push", async () => {
|
|
127
|
+
const messageId = await rowIn(inbox, 42);
|
|
128
|
+
|
|
129
|
+
await file(messageId, junk);
|
|
130
|
+
|
|
131
|
+
const moved = await row(messageId);
|
|
132
|
+
assert.equal(moved.mailboxId, junk.mailboxId);
|
|
133
|
+
assert.equal(moved.status, "moving");
|
|
134
|
+
assert.equal(moved.originalUid, 42);
|
|
135
|
+
assert.equal((await listing(messageId)).mailboxId, junk.mailboxId);
|
|
136
|
+
assert.equal((await store.placementMove.find(messageId))?.state, "queued");
|
|
137
|
+
assert.deepEqual(
|
|
138
|
+
queue.map((event) => event.body.type),
|
|
139
|
+
["PLACEMENT_MOVE_PUSH"],
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("withholds the sender of a message it files into Junk", async () => {
|
|
144
|
+
const messageId = await arriveAt(
|
|
145
|
+
store,
|
|
146
|
+
imap,
|
|
147
|
+
account,
|
|
148
|
+
inbox,
|
|
149
|
+
eml({ From: "Spam <spam@example.com>" }),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
await file(messageId, junk);
|
|
153
|
+
|
|
154
|
+
const sender = await store.address.getAddress(
|
|
155
|
+
account.accountConfigId,
|
|
156
|
+
deriveAddressId(account.accountConfigId, "spam@example.com"),
|
|
157
|
+
);
|
|
158
|
+
assert.equal(sender.flags.junkOnly?.value, true);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("does nothing for a message already in the destination", async () => {
|
|
162
|
+
const messageId = await rowIn(junk, 42);
|
|
163
|
+
|
|
164
|
+
await file(messageId, junk);
|
|
165
|
+
|
|
166
|
+
assert.equal(await store.placementMove.find(messageId), null);
|
|
167
|
+
assert.equal(queue.length, 0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("binds a second filing to the confirmed row once the first move settles", async () => {
|
|
171
|
+
const messageId = await rowIn(inbox, 42);
|
|
172
|
+
await file(messageId, archive);
|
|
173
|
+
|
|
174
|
+
setTimeout(() => void confirmMove(messageId, 907), 30);
|
|
175
|
+
await file(messageId, junk);
|
|
176
|
+
|
|
177
|
+
const marker = await store.placementMove.find(messageId);
|
|
178
|
+
assert.equal(marker?.sourceMailboxId, archive.mailboxId);
|
|
179
|
+
assert.equal((await row(messageId)).originalUid, 907);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("leaves the first move standing when it never settles", async () => {
|
|
183
|
+
const messageId = await rowIn(inbox, 42);
|
|
184
|
+
await file(messageId, archive);
|
|
185
|
+
|
|
186
|
+
await assert.rejects(file(messageId, junk), /has not settled/);
|
|
187
|
+
|
|
188
|
+
const marker = await store.placementMove.find(messageId);
|
|
189
|
+
assert.equal(marker?.sourceMailboxId, inbox.mailboxId);
|
|
190
|
+
assert.equal(marker?.destinationMailboxId, archive.mailboxId);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("drives a marker whose push never went out forward without moving again", async () => {
|
|
194
|
+
const messageId = await rowIn(inbox, 42);
|
|
195
|
+
await file(messageId, archive);
|
|
196
|
+
await store.placementMove.updateState(messageId, "pending");
|
|
197
|
+
|
|
198
|
+
await file(messageId, archive);
|
|
199
|
+
|
|
200
|
+
assert.equal((await store.placementMove.find(messageId))?.state, "queued");
|
|
201
|
+
assert.equal((await row(messageId)).originalUid, 42);
|
|
202
|
+
assert.equal(queue.length, 2);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("skips a message a delete has claimed without waiting", async () => {
|
|
206
|
+
const messageId = await rowIn(inbox, 42);
|
|
207
|
+
await userMoves().deleteMessages(
|
|
208
|
+
account.accountConfigId,
|
|
209
|
+
[messageId],
|
|
210
|
+
account.accountId,
|
|
211
|
+
{ permanent: true },
|
|
212
|
+
);
|
|
213
|
+
assert.equal((await row(messageId)).status, "deleting");
|
|
214
|
+
queue.length = 0;
|
|
215
|
+
|
|
216
|
+
const startedAt = Date.now();
|
|
217
|
+
await file(messageId, junk);
|
|
218
|
+
|
|
219
|
+
assert.ok(Date.now() - startedAt < 100);
|
|
220
|
+
assert.equal(await store.placementMove.find(messageId), null);
|
|
221
|
+
assert.equal(queue.length, 0);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("writes nothing, listing included, when the row settles elsewhere mid-decision", async () => {
|
|
225
|
+
const messageId = await rowIn(inbox, 42);
|
|
226
|
+
beforeNext(store.placementMove, "put", settleElsewhere(messageId));
|
|
227
|
+
|
|
228
|
+
await file(messageId, junk);
|
|
229
|
+
|
|
230
|
+
const after = await row(messageId);
|
|
231
|
+
assert.equal(after.mailboxId, archive.mailboxId);
|
|
232
|
+
assert.equal(after.uid, 907);
|
|
233
|
+
assert.equal((await listing(messageId)).mailboxId, inbox.mailboxId);
|
|
234
|
+
assert.equal(await store.placementMove.find(messageId), null);
|
|
235
|
+
assert.equal(queue.length, 0);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe("a user mutation writes only against the placement it read", () => {
|
|
240
|
+
it("refuses a move when the row settles elsewhere mid-decision", async () => {
|
|
241
|
+
const messageId = await rowIn(inbox, 42);
|
|
242
|
+
beforeNext(store.mailbox, "get", settleElsewhere(messageId));
|
|
243
|
+
|
|
244
|
+
await assert.rejects(
|
|
245
|
+
userMoves().moveMessage(
|
|
246
|
+
account.accountConfigId,
|
|
247
|
+
messageId,
|
|
248
|
+
junk.mailboxId,
|
|
249
|
+
account.accountId,
|
|
250
|
+
),
|
|
251
|
+
MessagePlacementUnsettledError,
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const after = await row(messageId);
|
|
255
|
+
assert.equal(after.mailboxId, archive.mailboxId);
|
|
256
|
+
assert.equal(after.status, "active");
|
|
257
|
+
assert.equal(queue.length, 0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
for (const source of ["INBOX", "Trash"] as const) {
|
|
261
|
+
it(`reports a delete out of ${source} it could not claim`, async () => {
|
|
262
|
+
const messageId = await rowIn(source === "Trash" ? trash : inbox, 42);
|
|
263
|
+
beforeNext(
|
|
264
|
+
store.mailboxSpecialUse,
|
|
265
|
+
"resolveTrashRole",
|
|
266
|
+
settleElsewhere(messageId),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
const { refusedMessageIds } = await userMoves().deleteMessages(
|
|
270
|
+
account.accountConfigId,
|
|
271
|
+
[messageId],
|
|
272
|
+
account.accountId,
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
assert.deepEqual(refusedMessageIds, [messageId]);
|
|
276
|
+
assert.equal((await row(messageId)).mailboxId, archive.mailboxId);
|
|
277
|
+
assert.equal(queue.length, 0);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
it("reports a row whose folder belongs to another account", async () => {
|
|
282
|
+
const other = await seedAccount(store);
|
|
283
|
+
const foreign = await seedMailbox(store, other, "INBOX");
|
|
284
|
+
const messageId = await rowIn(foreign, 42);
|
|
285
|
+
|
|
286
|
+
const { refusedMessageIds } = await userMoves().deleteMessages(
|
|
287
|
+
account.accountConfigId,
|
|
288
|
+
[messageId],
|
|
289
|
+
account.accountId,
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
assert.deepEqual(refusedMessageIds, [messageId]);
|
|
293
|
+
assert.equal(queue.length, 0);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
describe("a filing that exhausted its retries", () => {
|
|
298
|
+
const resolve = (
|
|
299
|
+
messageId: string,
|
|
300
|
+
uid: number,
|
|
301
|
+
source: MailboxItem,
|
|
302
|
+
placement: Pick<MessageItem, "status" | "mailboxId" | "uid">,
|
|
303
|
+
) =>
|
|
304
|
+
resolveExhaustedPlacementMoveFailure(
|
|
305
|
+
{
|
|
306
|
+
messageService: store.message,
|
|
307
|
+
threadMessageService: store.threadMessage,
|
|
308
|
+
markerService: store.placementMove,
|
|
309
|
+
log: silentLog,
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
placement,
|
|
313
|
+
accountId: account.accountId,
|
|
314
|
+
accountConfigId: account.accountConfigId,
|
|
315
|
+
messageId,
|
|
316
|
+
uid,
|
|
317
|
+
sourceMailboxId: source.mailboxId,
|
|
318
|
+
sourceMailboxPath: source.fullPath,
|
|
319
|
+
getConnection: async () => imap.connection(),
|
|
320
|
+
},
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
const filedFromInbox = async () => {
|
|
324
|
+
const uid = imap.deliver("INBOX", eml({}));
|
|
325
|
+
const messageId = await rowIn(inbox, uid);
|
|
326
|
+
await file(messageId, junk);
|
|
327
|
+
const read = await row(messageId);
|
|
328
|
+
return {
|
|
329
|
+
messageId,
|
|
330
|
+
uid,
|
|
331
|
+
placement: {
|
|
332
|
+
status: read.status,
|
|
333
|
+
mailboxId: read.mailboxId,
|
|
334
|
+
uid: read.uid,
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
it("removes the row of a message gone from the folder it left", async () => {
|
|
340
|
+
const { messageId, uid, placement } = await filedFromInbox();
|
|
341
|
+
imap.expunge("INBOX", uid);
|
|
342
|
+
|
|
343
|
+
const { outcome } = await resolve(messageId, uid, inbox, placement);
|
|
344
|
+
|
|
345
|
+
assert.equal(outcome, "reconciled");
|
|
346
|
+
await assert.rejects(row(messageId));
|
|
347
|
+
assert.equal(await store.placementMove.find(messageId), null);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("puts a message still in its source back there instead of leaving it moving", async () => {
|
|
351
|
+
const { messageId, uid, placement } = await filedFromInbox();
|
|
352
|
+
|
|
353
|
+
const { outcome } = await resolve(messageId, uid, inbox, placement);
|
|
354
|
+
|
|
355
|
+
assert.equal(outcome, "broken");
|
|
356
|
+
const restored = await row(messageId);
|
|
357
|
+
assert.equal(restored.mailboxId, inbox.mailboxId);
|
|
358
|
+
assert.equal(restored.uid, uid);
|
|
359
|
+
assert.equal(restored.status, "active");
|
|
360
|
+
assert.equal(restored.abandonedMutation, "none");
|
|
361
|
+
assert.equal((await listing(messageId)).mailboxId, inbox.mailboxId);
|
|
362
|
+
assert.equal(await store.placementMove.find(messageId), null);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("does not read a dropped FETCH row as the message being gone", async () => {
|
|
366
|
+
const { messageId, uid, placement } = await filedFromInbox();
|
|
367
|
+
const connection = imap.connection();
|
|
368
|
+
connection.fetchMessages = async () => [];
|
|
369
|
+
|
|
370
|
+
const { outcome } = await resolveExhaustedPlacementMoveFailure(
|
|
371
|
+
{
|
|
372
|
+
messageService: store.message,
|
|
373
|
+
threadMessageService: store.threadMessage,
|
|
374
|
+
markerService: store.placementMove,
|
|
375
|
+
log: silentLog,
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
placement,
|
|
379
|
+
accountId: account.accountId,
|
|
380
|
+
accountConfigId: account.accountConfigId,
|
|
381
|
+
messageId,
|
|
382
|
+
uid,
|
|
383
|
+
sourceMailboxId: inbox.mailboxId,
|
|
384
|
+
sourceMailboxPath: "INBOX",
|
|
385
|
+
getConnection: async () => connection,
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
assert.equal(outcome, "broken");
|
|
390
|
+
assert.ok(await row(messageId));
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("leaves a row another lane already settled to that lane", async () => {
|
|
394
|
+
const { messageId, uid, placement } = await filedFromInbox();
|
|
395
|
+
await confirmMove(messageId, 5);
|
|
396
|
+
|
|
397
|
+
const { outcome } = await resolve(messageId, uid, inbox, placement);
|
|
398
|
+
|
|
399
|
+
assert.equal(outcome, "broken");
|
|
400
|
+
const after = await row(messageId);
|
|
401
|
+
assert.equal(after.mailboxId, junk.mailboxId);
|
|
402
|
+
assert.equal((await listing(messageId)).mailboxId, junk.mailboxId);
|
|
403
|
+
});
|
|
404
|
+
});
|
package/src/quarantine.test.ts
CHANGED
|
@@ -1,65 +1,39 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import { describe, it } from "node:test";
|
|
3
|
-
import type {
|
|
4
|
-
IMailboxSpecialUseRepository,
|
|
5
|
-
IQuarantineRepository,
|
|
6
|
-
MessageData,
|
|
7
|
-
MessageItem,
|
|
8
|
-
QuarantineItem,
|
|
9
|
-
QuarantineUpsertInput,
|
|
10
|
-
} from "@remit/data-ports";
|
|
2
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
3
|
+
import type { AccountItem, MailboxItem } from "@remit/data-ports";
|
|
11
4
|
import {
|
|
12
|
-
QuarantinedUids,
|
|
13
5
|
QuarantineService,
|
|
14
6
|
resolveMailboxRole,
|
|
15
7
|
shapeFromMessageData,
|
|
16
8
|
} from "./quarantine.js";
|
|
9
|
+
import { eml, FakeImapServer } from "./test-helpers/fake-imap.js";
|
|
10
|
+
import {
|
|
11
|
+
arriveAt,
|
|
12
|
+
openStore,
|
|
13
|
+
type Store,
|
|
14
|
+
seedAccount,
|
|
15
|
+
seedMailbox,
|
|
16
|
+
silentLog,
|
|
17
|
+
} from "./test-helpers/test-store.js";
|
|
18
|
+
|
|
19
|
+
let store: Store;
|
|
20
|
+
let account: AccountItem;
|
|
21
|
+
let spam: MailboxItem;
|
|
22
|
+
let service: QuarantineService;
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
store = await openStore();
|
|
26
|
+
account = await seedAccount(store);
|
|
27
|
+
spam = await seedMailbox(store, account, "Spam", { specialUse: "Junk" });
|
|
28
|
+
service = new QuarantineService(
|
|
29
|
+
store.quarantine,
|
|
30
|
+
store.mailboxSpecialUse,
|
|
31
|
+
"sha-abc",
|
|
32
|
+
silentLog,
|
|
33
|
+
);
|
|
34
|
+
});
|
|
17
35
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const buildService = (
|
|
21
|
-
specialUse: string[] = [],
|
|
22
|
-
): {
|
|
23
|
-
service: QuarantineService;
|
|
24
|
-
writes: QuarantineUpsertInput[];
|
|
25
|
-
listed: string[];
|
|
26
|
-
} => {
|
|
27
|
-
const writes: QuarantineUpsertInput[] = [];
|
|
28
|
-
const listed: string[] = [];
|
|
29
|
-
const repository = {
|
|
30
|
-
listByAccountConfigId: async (accountConfigId: string) => {
|
|
31
|
-
listed.push(accountConfigId);
|
|
32
|
-
return [] as QuarantineItem[];
|
|
33
|
-
},
|
|
34
|
-
upsert: async (input: QuarantineUpsertInput) => {
|
|
35
|
-
writes.push(input);
|
|
36
|
-
},
|
|
37
|
-
} satisfies IQuarantineRepository;
|
|
38
|
-
|
|
39
|
-
const mailboxSpecialUseService = {
|
|
40
|
-
listByMailboxId: async () => specialUse.map((use) => ({ specialUse: use })),
|
|
41
|
-
} as unknown as IMailboxSpecialUseRepository;
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
service: new QuarantineService(
|
|
45
|
-
repository,
|
|
46
|
-
mailboxSpecialUseService,
|
|
47
|
-
"sha-abc",
|
|
48
|
-
noopLog,
|
|
49
|
-
),
|
|
50
|
-
writes,
|
|
51
|
-
listed,
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const context = {
|
|
56
|
-
accountId: "acct-1",
|
|
57
|
-
accountConfigId: "cfg-1",
|
|
58
|
-
mailboxId: "mbx-1",
|
|
59
|
-
mailboxPath: "INBOX",
|
|
60
|
-
uidValidity: 1_712_000_000,
|
|
61
|
-
attempts: 2,
|
|
62
|
-
};
|
|
36
|
+
afterEach(() => store.close());
|
|
63
37
|
|
|
64
38
|
const failure = {
|
|
65
39
|
stage: "BodyParse" as const,
|
|
@@ -67,47 +41,48 @@ const failure = {
|
|
|
67
41
|
message: "the parser said no",
|
|
68
42
|
};
|
|
69
43
|
|
|
44
|
+
const record = (uid: number, mailboxPath = "Spam") =>
|
|
45
|
+
service.record(
|
|
46
|
+
{
|
|
47
|
+
accountId: account.accountId,
|
|
48
|
+
accountConfigId: account.accountConfigId,
|
|
49
|
+
mailboxId: spam.mailboxId,
|
|
50
|
+
mailboxPath,
|
|
51
|
+
uidValidity: 1_712_000_000,
|
|
52
|
+
attempts: 2,
|
|
53
|
+
},
|
|
54
|
+
uid,
|
|
55
|
+
failure,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const rows = () =>
|
|
59
|
+
store.quarantine.listByAccountConfigId(account.accountConfigId);
|
|
60
|
+
|
|
70
61
|
describe("QuarantineService.record", () => {
|
|
71
|
-
it("
|
|
72
|
-
|
|
73
|
-
await
|
|
74
|
-
|
|
62
|
+
it("keeps one record per uid however often the message is set aside", async () => {
|
|
63
|
+
await record(40217);
|
|
64
|
+
await record(40217);
|
|
65
|
+
|
|
66
|
+
const recorded = (await rows()).filter((row) => row.uid === 40217);
|
|
67
|
+
assert.equal(recorded.length, 1);
|
|
68
|
+
assert.equal(recorded[0].uidValidity, 1_712_000_000);
|
|
69
|
+
assert.equal(recorded[0].workerVersion, "sha-abc");
|
|
70
|
+
assert.equal(recorded[0].failurePartPath, undefined);
|
|
75
71
|
});
|
|
76
72
|
|
|
77
|
-
it("
|
|
78
|
-
|
|
79
|
-
await service.record(context, 40217, failure);
|
|
80
|
-
assert.deepEqual(
|
|
81
|
-
{
|
|
82
|
-
mailboxId: writes[0]?.mailboxId,
|
|
83
|
-
uidValidity: writes[0]?.uidValidity,
|
|
84
|
-
uid: writes[0]?.uid,
|
|
85
|
-
},
|
|
86
|
-
{ mailboxId: "mbx-1", uidValidity: 1_712_000_000, uid: 40217 },
|
|
87
|
-
);
|
|
88
|
-
});
|
|
73
|
+
it("reads the folder's role off the server's SPECIAL-USE", async () => {
|
|
74
|
+
await record(7);
|
|
89
75
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
await service.record(context, 40217, failure);
|
|
93
|
-
assert.ok(!("quarantineId" in (writes[0] as object)));
|
|
76
|
+
const [row] = (await rows()).filter((entry) => entry.uid === 7);
|
|
77
|
+
assert.equal(row.mailboxRole, "Junk");
|
|
94
78
|
});
|
|
95
79
|
|
|
96
|
-
it("
|
|
97
|
-
|
|
98
|
-
await service.record(
|
|
99
|
-
{ ...context, mailboxPath: "Clients/Acme" },
|
|
100
|
-
1,
|
|
101
|
-
failure,
|
|
102
|
-
);
|
|
103
|
-
assert.ok(!("failurePartPath" in (writes[0] as object)));
|
|
104
|
-
assert.ok(!("mailboxRole" in (writes[0] as object)));
|
|
105
|
-
});
|
|
80
|
+
it("loads what it recorded as the set a sync round skips", async () => {
|
|
81
|
+
await record(8);
|
|
106
82
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
assert.equal(writes[0]?.mailboxRole, "Junk");
|
|
83
|
+
const loaded = await service.load(account.accountConfigId);
|
|
84
|
+
assert.ok(loaded.has(spam.mailboxId, 1_712_000_000, 8));
|
|
85
|
+
assert.ok(!loaded.has(spam.mailboxId, 1_712_000_001, 8));
|
|
111
86
|
});
|
|
112
87
|
});
|
|
113
88
|
|
|
@@ -125,67 +100,34 @@ describe("resolveMailboxRole", () => {
|
|
|
125
100
|
});
|
|
126
101
|
});
|
|
127
102
|
|
|
128
|
-
describe("QuarantinedUids", () => {
|
|
129
|
-
const entries = [
|
|
130
|
-
{ mailboxId: "mbx-1", uidValidity: 10, uid: 5 },
|
|
131
|
-
] as QuarantineItem[];
|
|
132
|
-
|
|
133
|
-
it("matches a uid on the same mailbox and UIDVALIDITY", () => {
|
|
134
|
-
assert.equal(new QuarantinedUids(entries).has("mbx-1", 10, 5), true);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("does not match the same uid on a new UIDVALIDITY, which is a different message", () => {
|
|
138
|
-
assert.equal(new QuarantinedUids(entries).has("mbx-1", 11, 5), false);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it("does not match the same uid in another mailbox", () => {
|
|
142
|
-
assert.equal(new QuarantinedUids(entries).has("mbx-2", 10, 5), false);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
103
|
describe("shapeFromMessageData", () => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
},
|
|
168
|
-
],
|
|
169
|
-
bodyPartParameter: [
|
|
170
|
-
{
|
|
171
|
-
bodyPartId: "bp-0",
|
|
172
|
-
parameterName: "charset",
|
|
173
|
-
parameterValue: "iso-8859-1",
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
} as unknown as MessageData;
|
|
177
|
-
|
|
178
|
-
it("rebuilds the tree from the rows metadata sync already wrote", () => {
|
|
179
|
-
assert.deepEqual(shapeFromMessageData(message, data).structure, [
|
|
180
|
-
{ depth: 0, contentType: "multipart/alternative" },
|
|
181
|
-
{ depth: 1, contentType: "text/plain" },
|
|
182
|
-
]);
|
|
183
|
-
});
|
|
104
|
+
it("describes the message from the rows metadata sync wrote", async () => {
|
|
105
|
+
const imap = new FakeImapServer();
|
|
106
|
+
imap.addFolder("Spam");
|
|
107
|
+
const raw = eml({
|
|
108
|
+
"Message-ID": "<xyz@example.com>",
|
|
109
|
+
"Content-Type": "text/plain; charset=iso-8859-1",
|
|
110
|
+
"Content-Transfer-Encoding": "quoted-printable",
|
|
111
|
+
});
|
|
112
|
+
const messageId = await arriveAt(store, imap, account, spam, raw);
|
|
113
|
+
|
|
114
|
+
const shape = shapeFromMessageData(
|
|
115
|
+
await store.message.get(messageId),
|
|
116
|
+
await store.envelope.getMessageData(messageId),
|
|
117
|
+
(
|
|
118
|
+
await store.threadMessage.findByMessageId(
|
|
119
|
+
account.accountConfigId,
|
|
120
|
+
messageId,
|
|
121
|
+
)
|
|
122
|
+
)?.messageIdHeader,
|
|
123
|
+
);
|
|
184
124
|
|
|
185
|
-
|
|
186
|
-
|
|
125
|
+
assert.equal(shape.structure.length, 1);
|
|
126
|
+
assert.equal(shape.structure[0].depth, 0);
|
|
127
|
+
assert.match(shape.structure[0].contentType, /^text\/plain$/i);
|
|
187
128
|
assert.equal(shape.charset, "iso-8859-1");
|
|
188
|
-
assert.equal(shape.transferEncoding, "
|
|
189
|
-
assert.equal(shape.
|
|
129
|
+
assert.equal(shape.transferEncoding?.toLowerCase(), "quoted-printable");
|
|
130
|
+
assert.equal(shape.sizeBytes, raw.length);
|
|
131
|
+
assert.ok(shape.messageIdHash);
|
|
190
132
|
});
|
|
191
133
|
});
|
package/src/quarantine.ts
CHANGED
|
@@ -76,8 +76,9 @@ const EMPTY_SHAPE: QuarantineMessageShape = { structure: [] };
|
|
|
76
76
|
* has just refused.
|
|
77
77
|
*/
|
|
78
78
|
export const shapeFromMessageData = (
|
|
79
|
-
message: MessageItem,
|
|
79
|
+
message: Pick<MessageItem, "rfc822Size">,
|
|
80
80
|
data: MessageData,
|
|
81
|
+
messageIdHeader: string | undefined,
|
|
81
82
|
): QuarantineMessageShape => {
|
|
82
83
|
const parts = [...data.bodyPart].sort((a, b) =>
|
|
83
84
|
a.partPath.localeCompare(b.partPath, "en", { numeric: true }),
|
|
@@ -100,7 +101,7 @@ export const shapeFromMessageData = (
|
|
|
100
101
|
depth: partPathDepth(part.partPath),
|
|
101
102
|
contentType: `${part.mediaType}/${part.mediaSubtype}`,
|
|
102
103
|
})),
|
|
103
|
-
...hashOf(
|
|
104
|
+
...hashOf(messageIdHeader),
|
|
104
105
|
};
|
|
105
106
|
};
|
|
106
107
|
|