@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.
Files changed (123) hide show
  1. package/package.json +21 -6
  2. package/src/body-part-mapper.ts +51 -1
  3. package/src/body-sync-queue.test.ts +39 -99
  4. package/src/body-sync-terminal.test.ts +115 -277
  5. package/src/body-sync-terminal.ts +10 -12
  6. package/src/body-sync.test.ts +925 -0
  7. package/src/body-sync.ts +156 -235
  8. package/src/classification-backfill.test.ts +197 -527
  9. package/src/classify-message.test.ts +11 -74
  10. package/src/external-move.test.ts +102 -209
  11. package/src/external-move.ts +13 -0
  12. package/src/filter-operations.test.ts +319 -0
  13. package/src/filter-operations.ts +377 -0
  14. package/src/filter-update.test.ts +313 -0
  15. package/src/filters/filter-config.test.ts +55 -197
  16. package/src/filters/filter-config.ts +4 -39
  17. package/src/filters/match.test.ts +1 -38
  18. package/src/filters/match.ts +3 -52
  19. package/src/filters/pipeline.test.ts +158 -289
  20. package/src/filters/pipeline.ts +56 -170
  21. package/src/flag-push-terminal.ts +9 -11
  22. package/src/flag-push.test.ts +76 -372
  23. package/src/flag-queue.test.ts +197 -0
  24. package/src/folder-operations.test.ts +273 -0
  25. package/src/folder-operations.ts +217 -0
  26. package/src/forwarded-message.test.ts +122 -0
  27. package/src/imapflow-connect-retry.test.ts +76 -113
  28. package/src/imapflow-connection.test.ts +34 -0
  29. package/src/imapflow-connection.ts +5 -4
  30. package/src/index.ts +59 -12
  31. package/src/list-id-backfill.test.ts +133 -499
  32. package/src/mailbox-cursor.test.ts +81 -189
  33. package/src/mailbox-delete-intent.test.ts +78 -205
  34. package/src/mailbox-intent.ts +5 -3
  35. package/src/mailbox-management.test.ts +107 -220
  36. package/src/mailbox-management.ts +1 -2
  37. package/src/mailbox-presence.test.ts +36 -34
  38. package/src/mailbox-queue.ts +2 -20
  39. package/src/mailbox-rename-intent.test.ts +156 -325
  40. package/src/mailbox-rename-subtree.test.ts +205 -582
  41. package/src/mailbox-sync.test.ts +182 -744
  42. package/src/message-move.test.ts +726 -0
  43. package/src/message-move.ts +227 -57
  44. package/src/message-read.ts +87 -0
  45. package/src/message-save-retry.test.ts +90 -0
  46. package/src/message-sync-flags.test.ts +10 -1
  47. package/src/message-sync.test.ts +762 -0
  48. package/src/message-sync.ts +409 -239
  49. package/src/mime-walker.ts +16 -15
  50. package/src/organize-job.ts +93 -0
  51. package/src/outbox-attachment.test.ts +97 -209
  52. package/src/outbox-failed-recovery.test.ts +198 -269
  53. package/src/outbox-queue.test.ts +24 -0
  54. package/src/outbox-queue.ts +25 -8
  55. package/src/ownership.ts +140 -0
  56. package/src/placement-move-terminal.ts +9 -12
  57. package/src/placement-move.ts +33 -6
  58. package/src/placement-settled.ts +28 -4
  59. package/src/placement.test.ts +404 -0
  60. package/src/quarantine.test.ts +90 -148
  61. package/src/quarantine.ts +3 -2
  62. package/src/refusal.ts +29 -0
  63. package/src/restore-source-placement.ts +0 -4
  64. package/src/spam-report.test.ts +238 -710
  65. package/src/sync-warning.test.ts +213 -0
  66. package/src/sync-warning.ts +56 -0
  67. package/src/test-helpers/fake-imap.ts +690 -0
  68. package/src/test-helpers/test-queue.ts +56 -0
  69. package/src/test-helpers/test-store.ts +299 -0
  70. package/src/thread-root.test.ts +29 -0
  71. package/src/thread-root.ts +51 -0
  72. package/src/types/mailparser-augment.d.ts +8 -0
  73. package/src/adversarial-mime.e2e.test.ts +0 -307
  74. package/src/body-sync-auto-read-writeonce.test.ts +0 -219
  75. package/src/body-sync-calendar-suggestion.test.ts +0 -433
  76. package/src/body-sync-category-denormalize.test.ts +0 -352
  77. package/src/body-sync-category-override.test.ts +0 -501
  78. package/src/body-sync-category-writeonce.test.ts +0 -234
  79. package/src/body-sync-filter.test.ts +0 -237
  80. package/src/body-sync-message-gone.test.ts +0 -139
  81. package/src/body-sync-placement-flags.test.ts +0 -347
  82. package/src/body-sync-placement-writeonce.test.ts +0 -337
  83. package/src/body-sync-quarantine.test.ts +0 -276
  84. package/src/body-sync-resync.test.ts +0 -605
  85. package/src/body-sync-unclassifiable-once.test.ts +0 -260
  86. package/src/body-sync-unsubscribed-auto-read.test.ts +0 -176
  87. package/src/filters/anchor-drift.ts +0 -55
  88. package/src/filters/message-embedder.test.ts +0 -39
  89. package/src/filters/message-embedder.ts +0 -33
  90. package/src/flag-push-terminal.test.ts +0 -209
  91. package/src/flag-queue-stars.test.ts +0 -171
  92. package/src/imapflow-connection.e2e.test.ts +0 -335
  93. package/src/imapflow-connection.integ.test.ts +0 -316
  94. package/src/message-delete-junk-reconcile.test.ts +0 -117
  95. package/src/message-delete-no-trash.test.ts +0 -245
  96. package/src/message-delete-unsettled-placement.test.ts +0 -396
  97. package/src/message-move-copy.test.ts +0 -562
  98. package/src/message-move-empty-trash.test.ts +0 -326
  99. package/src/message-move-same-mailbox.test.ts +0 -139
  100. package/src/message-move-unsettled-placement.test.ts +0 -378
  101. package/src/message-sync-address-spoof.test.ts +0 -293
  102. package/src/message-sync-category.test.ts +0 -142
  103. package/src/message-sync-changedsince.test.ts +0 -840
  104. package/src/message-sync-junk-harvest.test.ts +0 -244
  105. package/src/message-sync-quarantine.test.ts +0 -416
  106. package/src/message-sync-repoint.test.ts +0 -419
  107. package/src/outbox-queue-settled-row.test.ts +0 -140
  108. package/src/pass-through-unit-of-work.ts +0 -17
  109. package/src/placement-move-terminal.test.ts +0 -300
  110. package/src/placement-move-unsettled.test.ts +0 -275
  111. package/src/placement-transition-race.test.ts +0 -482
  112. package/src/stale-message-reconcile.test.ts +0 -82
  113. package/src/stale-message-reconcile.ts +0 -55
  114. package/src/test-helpers/isolated-mailbox.ts +0 -44
  115. package/src/test-helpers/mailfuzz-connection.ts +0 -27
  116. package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +0 -21
  117. package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +0 -31
  118. package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +0 -33
  119. package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +0 -42
  120. package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +0 -25
  121. package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +0 -15
  122. package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +0 -17
  123. package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +0 -18
@@ -1,276 +0,0 @@
1
- /**
2
- * The boundary this feature lives or dies on (issue #72).
3
- *
4
- * A message the parser refuses is set aside and stops being requeued. Storage
5
- * and database failures — which the same per-message frame catches — keep
6
- * propagating to the requeue path, because recording one as a quarantine would
7
- * tell the user that mail Remit could not reach was mail Remit could not read,
8
- * and let go of it. That discrimination is the `error instanceof BodyParseError`
9
- * branch in `syncBodies`' stream loop, and these tests drive it there.
10
- *
11
- * The parse cases inject the `BodyParseError` at the storage write rather than
12
- * feeding mailparser bytes it refuses: mailparser accepts every byte sequence a
13
- * test can construct, and the branch under test asks only what type of error
14
- * reached it. That only `parseMessageBody` ever constructs one — so an instance
15
- * of it really is proof the message and not the infrastructure failed — is
16
- * pinned in `body-parse.test.ts`.
17
- */
18
-
19
- import assert from "node:assert/strict";
20
- import { Readable } from "node:stream";
21
- import { describe, it } from "node:test";
22
- import type {
23
- IAddressRepository,
24
- IEnvelopeRepository,
25
- IMailboxSpecialUseRepository,
26
- IMessageRepository,
27
- IQuarantineRepository,
28
- IThreadMessageRepository,
29
- MessageItem,
30
- QuarantineItem,
31
- QuarantineUpsertInput,
32
- UpdateMessageInput,
33
- } from "@remit/data-ports";
34
- import { NotFoundError } from "@remit/data-ports/errors";
35
- import { MessageCategory } from "@remit/domain-enums";
36
- import type { StorageService } from "@remit/storage-service";
37
- import { BodyParseError } from "./body-parse.js";
38
- import { BodySyncService } from "./body-sync.js";
39
- import { QuarantineService } from "./quarantine.js";
40
- import type { IImapConnection } from "./types.js";
41
-
42
- const WELL_FORMED = Buffer.from(
43
- [
44
- "From: someone@example.com",
45
- "To: me@example.com",
46
- "Subject: hello",
47
- "Content-Type: text/plain",
48
- "",
49
- "body",
50
- ].join("\r\n"),
51
- );
52
-
53
- const buildHarness = (options: {
54
- existing?: QuarantineItem[];
55
- storeBodyFails?: unknown;
56
- storeParsedFails?: unknown;
57
- upsertFails?: boolean;
58
- }) => {
59
- const writes: QuarantineUpsertInput[] = [];
60
- const messageUpdates: string[] = [];
61
-
62
- const message: MessageItem = {
63
- messageId: "m-1",
64
- mailboxId: "mbx-1",
65
- uid: 40217,
66
- rfc822Size: 2048,
67
- messageIdHeader: "<abc@example.com>",
68
- category: MessageCategory.uncategorized,
69
- classificationState: "NotExamined",
70
- } as MessageItem;
71
-
72
- const messageService = {
73
- get: async () => message,
74
- update: async (messageId: string, _input: UpdateMessageInput) => {
75
- messageUpdates.push(messageId);
76
- },
77
- } as unknown as IMessageRepository;
78
-
79
- const storageService = {
80
- retrieve: async () => {
81
- throw new Error("the sync path never reads a body back");
82
- },
83
- storeMessageBodyStream: async () => {
84
- if (options.storeBodyFails !== undefined) throw options.storeBodyFails;
85
- return { uri: "s3://bodies/m-1" };
86
- },
87
- storeParsedBody: async () => {
88
- if (options.storeParsedFails !== undefined)
89
- throw options.storeParsedFails;
90
- },
91
- listBodyParts: async () => [],
92
- } as unknown as StorageService;
93
-
94
- const envelopeService = {
95
- getMessageData: async () => ({ bodyPart: [], bodyPartParameter: [] }),
96
- listBodyParts: async () => [],
97
- } as unknown as IEnvelopeRepository;
98
-
99
- const addressService = {
100
- getAddress: async () => {
101
- throw new NotFoundError("Address not found");
102
- },
103
- incrementInboundCount: async () => {},
104
- } as unknown as IAddressRepository;
105
-
106
- const repository = {
107
- listByAccountConfigId: async () => options.existing ?? [],
108
- upsert: async (input: QuarantineUpsertInput) => {
109
- if (options.upsertFails) throw new Error("database unavailable");
110
- writes.push(input);
111
- },
112
- } satisfies IQuarantineRepository;
113
-
114
- const service = new BodySyncService(
115
- messageService,
116
- storageService,
117
- {
118
- findAllByMessageId: async () => [
119
- {
120
- threadMessageId: "tm-1",
121
- sentDate: 1,
122
- mailboxId: "mbx-1",
123
- isRead: false,
124
- isDeleted: false,
125
- hasStars: false,
126
- hasAttachment: false,
127
- },
128
- ],
129
- update: async () => {},
130
- } as unknown as IThreadMessageRepository,
131
- addressService,
132
- envelopeService,
133
- { info: () => {}, error: () => {}, debug: () => {} },
134
- undefined,
135
- undefined,
136
- {
137
- quarantineService: new QuarantineService(
138
- repository,
139
- {
140
- listByMailboxId: async () => [],
141
- } as unknown as IMailboxSpecialUseRepository,
142
- "sha-abc",
143
- { info: () => {}, warn: () => {} },
144
- ),
145
- mailboxId: "mbx-1",
146
- uidValidity: 1_712_000_000,
147
- attempts: 2,
148
- },
149
- );
150
-
151
- return { service, writes, messageUpdates };
152
- };
153
-
154
- const connection = () =>
155
- ({
156
- openBox: async () => {},
157
- async *fetchMessageBodies(uids: number[]) {
158
- for (const uid of uids) {
159
- yield { uid, source: Readable.from([WELL_FORMED]) };
160
- }
161
- },
162
- }) as unknown as IImapConnection;
163
-
164
- const sync = (service: BodySyncService) =>
165
- service.syncBodies(
166
- ["m-1"],
167
- "acc-1",
168
- "cfg-1",
169
- { fullPath: "INBOX", placement: "inbox" },
170
- async () => connection(),
171
- );
172
-
173
- const parseRefusal = () =>
174
- new BodyParseError(new Error("boundary never closed"));
175
-
176
- describe("body sync quarantines a message the parser refuses", () => {
177
- it("records the failure instead of requeueing the message forever", async () => {
178
- const harness = buildHarness({ storeBodyFails: parseRefusal() });
179
-
180
- const result = await sync(harness.service);
181
-
182
- assert.equal(harness.writes.length, 1);
183
- assert.deepEqual(result.failedMessageIds, []);
184
- });
185
-
186
- it("names the message by uid and UIDVALIDITY, so the record is idempotent", async () => {
187
- const harness = buildHarness({ storeBodyFails: parseRefusal() });
188
-
189
- await sync(harness.service);
190
-
191
- assert.equal(harness.writes[0]?.uid, 40217);
192
- assert.equal(harness.writes[0]?.uidValidity, 1_712_000_000);
193
- assert.equal(harness.writes[0]?.failureStage, "BodyParse");
194
- });
195
-
196
- it("does not mark the message synced — it was set aside, not applied", async () => {
197
- const harness = buildHarness({ storeBodyFails: parseRefusal() });
198
-
199
- const result = await sync(harness.service);
200
-
201
- assert.deepEqual(result.syncedMessageIds, []);
202
- assert.deepEqual(harness.messageUpdates, []);
203
- });
204
- });
205
-
206
- describe("body sync leaves infrastructure failures alone", () => {
207
- it("requeues a storage failure rather than calling the message unreadable", async () => {
208
- const harness = buildHarness({
209
- storeBodyFails: new Error("S3 503 SlowDown"),
210
- });
211
-
212
- const result = await sync(harness.service);
213
-
214
- assert.deepEqual(harness.writes, []);
215
- assert.deepEqual(result.failedMessageIds, ["m-1"]);
216
- });
217
-
218
- it("requeues a database failure the same way, parse already past", async () => {
219
- const throughput = new Error("ProvisionedThroughputExceededException");
220
- throughput.name = "ProvisionedThroughputExceededException";
221
- const harness = buildHarness({ storeParsedFails: throughput });
222
-
223
- // The body parsed cleanly and the failure came after it, so nothing here
224
- // may say the message is unreadable.
225
- const result = await sync(harness.service);
226
-
227
- assert.deepEqual(harness.writes, []);
228
- assert.deepEqual(result.failedMessageIds, ["m-1"]);
229
- });
230
- });
231
-
232
- describe("body sync contains a failure to write the record", () => {
233
- it("requeues the message instead of aborting the batch", async () => {
234
- const harness = buildHarness({
235
- storeBodyFails: parseRefusal(),
236
- upsertFails: true,
237
- });
238
-
239
- // Writing the record is database work, so its failure is infrastructure:
240
- // the message keeps its place in the queue rather than being let go of
241
- // with nothing written down, and the rest of the batch is unaffected.
242
- const result = await sync(harness.service);
243
-
244
- assert.deepEqual(harness.writes, []);
245
- assert.deepEqual(result.failedMessageIds, ["m-1"]);
246
- });
247
- });
248
-
249
- describe("body sync skips what is already quarantined", () => {
250
- it("does not fetch or re-parse a uid already set aside", async () => {
251
- const harness = buildHarness({
252
- existing: [
253
- {
254
- mailboxId: "mbx-1",
255
- uidValidity: 1_712_000_000,
256
- uid: 40217,
257
- } as QuarantineItem,
258
- ],
259
- });
260
-
261
- const result = await harness.service.syncBodies(
262
- ["m-1"],
263
- "acc-1",
264
- "cfg-1",
265
- { fullPath: "INBOX", placement: "inbox" },
266
- async () => {
267
- throw new Error("a quarantined message must not be fetched again");
268
- },
269
- );
270
-
271
- assert.equal(result.skippedCount, 1);
272
- assert.deepEqual(result.failedMessageIds, []);
273
- assert.deepEqual(harness.writes, []);
274
- assert.deepEqual(harness.messageUpdates, []);
275
- });
276
- });