@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,433 +0,0 @@
1
- /**
2
- * Issue #1033 stage 0.3: an invitation arriving in the mail becomes a card
3
- * beside the message, never a calendar entry. These drive `BodySyncService`
4
- * end to end — read-path body materialization into `applyPostStoreSteps` — so
5
- * the first-seen gate is asserted against the real pass, not a helper.
6
- */
7
-
8
- import assert from "node:assert/strict";
9
- import { describe, it } from "node:test";
10
- import { MemoryCalendarStore } from "@remit/calendar-service/memory-store";
11
- import type {
12
- CalendarSuggestionItem,
13
- FilterItem,
14
- IAddressRepository,
15
- ICalendarUnitOfWork,
16
- IEnvelopeRepository,
17
- IFilterRepository,
18
- IMessageRepository,
19
- IThreadMessageRepository,
20
- } from "@remit/data-ports";
21
- import {
22
- CalendarInviteMethod,
23
- CalendarSuggestionSource,
24
- CalendarSuggestionState,
25
- } from "@remit/domain-enums";
26
- import type { StorageService } from "@remit/storage-service";
27
- import { BodySyncService } from "./body-sync.js";
28
- import type { IImapConnection } from "./types.js";
29
-
30
- const UID = "invite-4711@example.test";
31
-
32
- const VCALENDAR = (sequence: number, method: string): string =>
33
- [
34
- "BEGIN:VCALENDAR",
35
- "VERSION:2.0",
36
- "PRODID:-//Example Corp//Scheduler//EN",
37
- `METHOD:${method}`,
38
- "BEGIN:VEVENT",
39
- `UID:${UID}`,
40
- "DTSTAMP:20260801T090000Z",
41
- `SEQUENCE:${sequence}`,
42
- "DTSTART:20260901T080000Z",
43
- "DTEND:20260901T090000Z",
44
- "SUMMARY:Quarterly review",
45
- "ORGANIZER:mailto:organizer@example.test",
46
- "ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:user@example.test",
47
- "END:VEVENT",
48
- "END:VCALENDAR",
49
- ].join("\r\n");
50
-
51
- const INVITATION_EML = (sequence = 0, method = "REQUEST"): Buffer =>
52
- Buffer.from(
53
- [
54
- "From: Organizer <organizer@example.test>",
55
- "To: user@example.test",
56
- "Subject: Quarterly review",
57
- 'Content-Type: multipart/alternative; boundary="bnd"',
58
- "",
59
- "--bnd",
60
- "Content-Type: text/plain",
61
- "",
62
- "You are invited.",
63
- "--bnd",
64
- 'Content-Type: text/calendar; method=REQUEST; charset="UTF-8"',
65
- "",
66
- VCALENDAR(sequence, method),
67
- "--bnd--",
68
- "",
69
- ].join("\r\n"),
70
- );
71
-
72
- /** DTSTART with neither a Z nor a TZID: RFC 5545 floating time. */
73
- const FLOATING_INVITATION_EML = Buffer.from(
74
- [
75
- "From: Organizer <organizer@example.test>",
76
- "To: user@example.test",
77
- "Subject: Quarterly review",
78
- 'Content-Type: multipart/alternative; boundary="bnd"',
79
- "",
80
- "--bnd",
81
- "Content-Type: text/plain",
82
- "",
83
- "You are invited.",
84
- "--bnd",
85
- "Content-Type: text/calendar; method=REQUEST",
86
- "",
87
- [
88
- "BEGIN:VCALENDAR",
89
- "VERSION:2.0",
90
- "PRODID:-//Example Corp//Scheduler//EN",
91
- "METHOD:REQUEST",
92
- "BEGIN:VEVENT",
93
- `UID:${UID}`,
94
- "DTSTAMP:20260801T090000Z",
95
- "SEQUENCE:0",
96
- "DTSTART:20260901T100000",
97
- "DTEND:20260901T110000",
98
- "SUMMARY:Quarterly review",
99
- "ORGANIZER:mailto:organizer@example.test",
100
- "END:VEVENT",
101
- "END:VCALENDAR",
102
- ].join("\r\n"),
103
- "--bnd--",
104
- "",
105
- ].join("\r\n"),
106
- );
107
-
108
- /** The shape `dismiss{muteSender:true}` writes: a standing rule, no action. */
109
- const MUTE_RULE = (sender: string): FilterItem =>
110
- ({
111
- filterId: `mute-${sender}`,
112
- accountConfigId: "cfg-1",
113
- name: `Muted invitations from ${sender}`,
114
- scope: "Standing",
115
- state: "Active",
116
- matchOperator: "And",
117
- literalClauses: [{ field: "From", value: sender }],
118
- actionLabelId: "None",
119
- actionMailboxId: "None",
120
- }) as unknown as FilterItem;
121
-
122
- const PLAIN_EML = Buffer.from(
123
- [
124
- "From: Someone <someone@example.test>",
125
- "To: user@example.test",
126
- "Subject: Hello",
127
- "Content-Type: text/plain",
128
- "",
129
- "body",
130
- ].join("\r\n"),
131
- );
132
-
133
- interface Harness {
134
- service: BodySyncService;
135
- suggestions: Map<string, CalendarSuggestionItem>;
136
- }
137
-
138
- const buildHarness = ({
139
- bodyStorageKey,
140
- timezone = "",
141
- withCalendarConfig = true,
142
- mutedSenderFilters = [] as FilterItem[],
143
- }: {
144
- bodyStorageKey?: string;
145
- timezone?: string;
146
- withCalendarConfig?: boolean;
147
- mutedSenderFilters?: FilterItem[];
148
- } = {}): Harness => {
149
- const store = new MemoryCalendarStore();
150
- const suggestions = store.calendarSuggestion;
151
-
152
- const messageService = {
153
- get: async (messageId: string) => ({
154
- messageId,
155
- mailboxId: "mb-inbox",
156
- uid: 1,
157
- bodyStorageKey,
158
- }),
159
- update: async () => {},
160
- } as unknown as IMessageRepository;
161
-
162
- const threadMessageService = {
163
- findAllByMessageId: async () => [
164
- {
165
- threadMessageId: "tm-1",
166
- sentDate: 1,
167
- mailboxId: "mb-inbox",
168
- isRead: false,
169
- isDeleted: false,
170
- hasStars: false,
171
- hasAttachment: false,
172
- },
173
- ],
174
- update: async () => {},
175
- } as unknown as IThreadMessageRepository;
176
-
177
- const storageService = {
178
- // The read-miss the forced re-sync rides in on: the row says the body is
179
- // stored, the object is gone, so the pass re-fetches and re-enters
180
- // applyPostStoreSteps with `bodyStorageKey` already set.
181
- retrieve: async () => {
182
- throw Object.assign(new Error("gone"), { name: "NoSuchKey" });
183
- },
184
- storeMessageBody: async () => ({ uri: "s3://bodies/stored" }),
185
- storeParsedBody: async () => {},
186
- listBodyParts: async () => [],
187
- } as unknown as StorageService;
188
-
189
- const addressService = {
190
- getAddress: async () => ({ flags: {} }),
191
- incrementInboundCount: async () => {},
192
- } as unknown as IAddressRepository;
193
-
194
- const envelopeService = {
195
- listBodyParts: async () => [],
196
- } as unknown as IEnvelopeRepository;
197
-
198
- // The producer provisions the default collection through the unit of work,
199
- // so this stands in for it — `create` is idempotent on a derived id, which
200
- // is why the producer may call it on every calendar-carrying message.
201
- const collections = new Map<string, { timezone: string }>();
202
- const calendarUnitOfWork = {
203
- transaction: <T>(
204
- run: (repos: {
205
- calendarCollection: {
206
- create: (input: {
207
- accountConfigId: string;
208
- }) => Promise<{ calendarId: string; timezone: string }>;
209
- };
210
- }) => Promise<T>,
211
- ) =>
212
- run({
213
- calendarCollection: {
214
- create: async (input: { accountConfigId: string }) => {
215
- const existing = collections.get(input.accountConfigId);
216
- if (existing) {
217
- return { calendarId: "cal-1", timezone: existing.timezone };
218
- }
219
- collections.set(input.accountConfigId, { timezone });
220
- return { calendarId: "cal-1", timezone };
221
- },
222
- },
223
- }),
224
- } as unknown as ICalendarUnitOfWork;
225
-
226
- const filterService = {
227
- listByAccountAndState: async () => mutedSenderFilters,
228
- } as unknown as Pick<IFilterRepository, "listByAccountAndState">;
229
-
230
- const service = new BodySyncService(
231
- messageService,
232
- storageService,
233
- threadMessageService,
234
- addressService,
235
- envelopeService,
236
- { info: () => {}, error: () => {} },
237
- undefined,
238
- undefined,
239
- undefined,
240
- undefined,
241
- withCalendarConfig
242
- ? {
243
- calendarSuggestionService: suggestions,
244
- calendarUnitOfWork,
245
- filterService,
246
- }
247
- : undefined,
248
- );
249
-
250
- return { service, suggestions: store.suggestions };
251
- };
252
-
253
- const readBody = async (
254
- service: BodySyncService,
255
- eml: Buffer,
256
- messageId = "m-1",
257
- ) => {
258
- const connection = {
259
- openBox: async () => {},
260
- fetchMessageBody: async () => eml,
261
- } as unknown as IImapConnection;
262
- return service.fetchAndGetBody(
263
- messageId,
264
- "acc-1",
265
- "cfg-1",
266
- { fullPath: "INBOX", placement: "inbox" },
267
- async () => connection,
268
- );
269
- };
270
-
271
- describe("mail-derived calendar suggestions (issue #1033)", () => {
272
- it("offers an invitation as one Pending suggestion", async () => {
273
- const harness = buildHarness();
274
-
275
- await readBody(harness.service, INVITATION_EML());
276
-
277
- const rows = [...harness.suggestions.values()];
278
- assert.equal(rows.length, 1);
279
- assert.equal(rows[0]?.state, CalendarSuggestionState.Pending);
280
- assert.equal(rows[0]?.icalUid, UID);
281
- assert.equal(rows[0]?.method, CalendarInviteMethod.Request);
282
- assert.equal(rows[0]?.summary, "Quarterly review");
283
- assert.equal(rows[0]?.organizer, "organizer@example.test");
284
- assert.equal(rows[0]?.source, CalendarSuggestionSource.IcalendarPart);
285
- });
286
-
287
- it("stores the invitation's bytes so accepting can write them unchanged", async () => {
288
- const harness = buildHarness();
289
-
290
- await readBody(harness.service, INVITATION_EML());
291
-
292
- const row = [...harness.suggestions.values()][0];
293
- assert.match(row?.icalData ?? "", /BEGIN:VCALENDAR/);
294
- assert.match(row?.icalData ?? "", /UID:invite-4711@example\.test/);
295
- });
296
-
297
- it("reads a floating start in the collection's own zone", async () => {
298
- // An invitation whose DTSTART names no zone is RFC 5545 floating time.
299
- // Read in UTC it lands two hours out for an Amsterdam calendar, and the
300
- // user is shown the wrong hour with nothing saying so.
301
- const harness = buildHarness({ timezone: "Europe/Amsterdam" });
302
-
303
- await readBody(harness.service, FLOATING_INVITATION_EML);
304
-
305
- const row = [...harness.suggestions.values()][0];
306
- assert.equal(row?.dtStart, "2026-09-01T10:00:00+02:00");
307
- assert.equal(row?.zoneCertainty, "Local");
308
- });
309
-
310
- it("offers no card for a sender the user muted", async () => {
311
- // The standing rule `dismiss{muteSender:true}` writes has exactly one
312
- // reader, and this is it: the index-time filter pipeline skips a rule
313
- // with no label and no move, so without this the button does nothing.
314
- const harness = buildHarness({
315
- mutedSenderFilters: [MUTE_RULE("organizer@example.test")],
316
- });
317
-
318
- await readBody(harness.service, INVITATION_EML());
319
-
320
- assert.equal(harness.suggestions.size, 0);
321
- });
322
-
323
- it("still offers a card when the rule names a different sender", async () => {
324
- const harness = buildHarness({
325
- mutedSenderFilters: [MUTE_RULE("someone-else@example.test")],
326
- });
327
-
328
- await readBody(harness.service, INVITATION_EML());
329
-
330
- assert.equal(harness.suggestions.size, 1);
331
- });
332
-
333
- it("still offers a card when the rule about this sender does something", async () => {
334
- // A rule that labels or moves this sender's mail is an ordinary filter
335
- // the pipeline acts on, not the user refusing their invitations.
336
- const harness = buildHarness({
337
- mutedSenderFilters: [
338
- {
339
- ...MUTE_RULE("organizer@example.test"),
340
- actionLabelId: "label-1",
341
- } as FilterItem,
342
- ],
343
- });
344
-
345
- await readBody(harness.service, INVITATION_EML());
346
-
347
- assert.equal(harness.suggestions.size, 1);
348
- });
349
-
350
- it("offers nothing for a message carrying no calendar part", async () => {
351
- const harness = buildHarness();
352
-
353
- await readBody(harness.service, PLAIN_EML);
354
-
355
- assert.equal(harness.suggestions.size, 0);
356
- });
357
-
358
- it("offers nothing on a forced body re-sync", async () => {
359
- // The owner's ruling on #497/#1011: a card is offered at first sight,
360
- // the same moment the account's filters run. A forced re-sync re-enters
361
- // this pass with the body already stored, and re-offering there would
362
- // resurrect a card the user dismissed.
363
- const harness = buildHarness({ bodyStorageKey: "s3://bodies/m-1" });
364
-
365
- const result = await readBody(harness.service, INVITATION_EML());
366
-
367
- // The pass genuinely ran — the body was re-fetched and re-stored — and
368
- // still offered nothing.
369
- assert.equal(result.storedAt, "s3://bodies/m-1");
370
- assert.equal(harness.suggestions.size, 0);
371
- });
372
-
373
- it("supersedes the earlier revision when a higher SEQUENCE arrives", async () => {
374
- const harness = buildHarness();
375
-
376
- await readBody(harness.service, INVITATION_EML(0), "m-1");
377
- await readBody(harness.service, INVITATION_EML(1), "m-2");
378
-
379
- const rows = [...harness.suggestions.values()];
380
- assert.equal(rows.length, 2);
381
- assert.deepEqual(
382
- rows.map((row) => `${row.messageId}:${row.state}`).sort(),
383
- ["m-1:Superseded", "m-2:Pending"],
384
- );
385
- });
386
-
387
- it("offers a cancellation as its own card, writing no calendar object", async () => {
388
- const harness = buildHarness();
389
-
390
- await readBody(harness.service, INVITATION_EML(1, "CANCEL"));
391
-
392
- const row = [...harness.suggestions.values()][0];
393
- assert.equal(row?.method, CalendarInviteMethod.Cancel);
394
- assert.equal(row?.state, CalendarSuggestionState.Pending);
395
- assert.equal(row?.acceptedCalendarObjectId, "");
396
- });
397
-
398
- it("stores the body even when the message carries unreadable iCalendar", async () => {
399
- const harness = buildHarness();
400
- const broken = Buffer.from(
401
- [
402
- "From: Organizer <organizer@example.test>",
403
- "To: user@example.test",
404
- "Subject: Broken",
405
- 'Content-Type: multipart/alternative; boundary="bnd"',
406
- "",
407
- "--bnd",
408
- "Content-Type: text/plain",
409
- "",
410
- "hi",
411
- "--bnd",
412
- "Content-Type: text/calendar",
413
- "",
414
- "BEGIN:VCALENDAR",
415
- "--bnd--",
416
- "",
417
- ].join("\r\n"),
418
- );
419
-
420
- const result = await readBody(harness.service, broken);
421
-
422
- assert.equal(result.storedAt, "newly-stored");
423
- assert.equal(harness.suggestions.size, 0);
424
- });
425
-
426
- it("is a no-op when body sync was built without a CalendarSuggestionConfig", async () => {
427
- const harness = buildHarness({ withCalendarConfig: false });
428
-
429
- await readBody(harness.service, INVITATION_EML());
430
-
431
- assert.equal(harness.suggestions.size, 0);
432
- });
433
- });