@remit/mailbox-service 0.0.112 → 0.0.113

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 (270) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +126 -0
  78. package/dist/mailbox-queue.js +214 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +284 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -1,726 +0,0 @@
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
- } from "@remit/data-ports";
8
- import { MessagePlacementUnsettledError } from "@remit/data-ports/errors";
9
- import { composeFolderRoleAppointmentName } from "@remit/data-ports/folder-role";
10
- import { deriveAddressId, deriveCopyMessageId } from "@remit/data-ports/id";
11
- import {
12
- CanonicalMailboxRole,
13
- MessageClassificationState,
14
- } from "@remit/domain-enums";
15
- import { BodySyncService } from "./body-sync.js";
16
- import {
17
- MessageMoveService,
18
- NoTrashMailboxError,
19
- StaleTrashAppointmentError,
20
- UnconfirmedTrashMailboxError,
21
- } from "./message-move.js";
22
- import { restoreSourcePlacement } from "./restore-source-placement.js";
23
- import { SyncWarningService } from "./sync-warning.js";
24
- import { eml, FakeImapServer } from "./test-helpers/fake-imap.js";
25
- import {
26
- arriveAt,
27
- captureQueue,
28
- openStore,
29
- type QueueCapture,
30
- type QueuedEvent,
31
- type Store,
32
- seedAccount,
33
- seedMailbox,
34
- seedMessage,
35
- silentLog,
36
- syncFolder,
37
- } from "./test-helpers/test-store.js";
38
-
39
- type TrashSetup = "flagged" | "named" | "none" | "appointed" | "stale";
40
-
41
- let store: Store;
42
- let capture: QueueCapture;
43
- let queue: QueuedEvent[];
44
- let imap: FakeImapServer;
45
- let account: AccountItem;
46
- let inbox: MailboxItem;
47
- let archive: MailboxItem;
48
- let junk: MailboxItem;
49
- let trash: MailboxItem | undefined;
50
-
51
- beforeEach(async () => {
52
- capture = captureQueue();
53
- queue = capture.sent;
54
- store = await openStore();
55
- imap = new FakeImapServer();
56
- });
57
-
58
- afterEach(async () => {
59
- capture.restore();
60
- await store.close();
61
- });
62
-
63
- const appoint = (mailboxId: string) =>
64
- store.accountSetting.upsert({
65
- accountConfigId: account.accountConfigId,
66
- name: composeFolderRoleAppointmentName(
67
- account.accountId,
68
- CanonicalMailboxRole.Trash,
69
- ),
70
- value: { kind: "String", value: mailboxId },
71
- });
72
-
73
- const world = async (setup: TrashSetup = "flagged") => {
74
- account = await seedAccount(store);
75
- inbox = await seedMailbox(store, account, "INBOX");
76
- archive = await seedMailbox(store, account, "Archive");
77
- junk = await seedMailbox(store, account, "Junk", { specialUse: "Junk" });
78
- trash = undefined;
79
- if (setup === "flagged" || setup === "stale") {
80
- trash = await seedMailbox(store, account, "INBOX/Trash", {
81
- specialUse: "Trash",
82
- });
83
- }
84
- if (setup === "named") {
85
- trash = await seedMailbox(store, account, "Trash");
86
- }
87
- if (setup === "appointed") {
88
- await seedMailbox(store, account, "Deleted");
89
- trash = await seedMailbox(store, account, "Bin");
90
- await appoint(trash.mailboxId);
91
- }
92
- if (setup === "stale") {
93
- await appoint("mbx-appointed-and-gone");
94
- }
95
- for (const mailbox of await store.mailbox.listAllByAccount(
96
- account.accountId,
97
- )) {
98
- imap.addFolder(mailbox.fullPath);
99
- }
100
- };
101
-
102
- const service = () =>
103
- new MessageMoveService({
104
- messageService: store.message,
105
- mailboxService: store.mailbox,
106
- mailboxSpecialUseService: store.mailboxSpecialUse,
107
- threadMessageService: store.threadMessage,
108
- addressService: store.address,
109
- sqsQueueUrl: "http://localhost:9324/000000000000/remit-messages.fifo",
110
- moveSettleTimeoutMs: 200,
111
- moveSettlePollMs: 10,
112
- });
113
-
114
- const rowIn = async (
115
- mailbox: MailboxItem,
116
- uid: number,
117
- message: Partial<CreateMessageInput> = {},
118
- ) =>
119
- (await seedMessage(store, account, mailbox, uid, { message })).message
120
- .messageId;
121
-
122
- const midMove = async (uid = 42) => {
123
- const messageId = await rowIn(inbox, uid);
124
- await service().moveMessage(
125
- account.accountConfigId,
126
- messageId,
127
- archive.mailboxId,
128
- account.accountId,
129
- );
130
- queue.length = 0;
131
- return messageId;
132
- };
133
-
134
- const midDelete = async (uid = 42) => {
135
- const messageId = await rowIn(inbox, uid);
136
- await service().deleteMessages(
137
- account.accountConfigId,
138
- [messageId],
139
- account.accountId,
140
- );
141
- queue.length = 0;
142
- return messageId;
143
- };
144
-
145
- const settleLater = (messageId: string, uid = 907) =>
146
- setTimeout(() => {
147
- void store.message.updateUid(messageId, uid, archive.mailboxId);
148
- }, 30);
149
-
150
- const row = (messageId: string) => store.message.get(messageId);
151
-
152
- describe("moving a message", () => {
153
- it("moves locally first and enqueues the IMAP move from the folder it is in", async () => {
154
- await world();
155
- const messageId = await rowIn(inbox, 337);
156
-
157
- await service().moveMessages(
158
- account.accountConfigId,
159
- [messageId],
160
- archive.mailboxId,
161
- account.accountId,
162
- );
163
-
164
- const moved = await row(messageId);
165
- assert.equal(moved.mailboxId, archive.mailboxId);
166
- assert.equal(moved.syncStatus, "pending");
167
- assert.deepEqual(
168
- queue.map((event) => [
169
- event.body.type,
170
- event.body.sourceMailboxPath,
171
- event.body.uid,
172
- ]),
173
- [["MESSAGE_MOVE", "INBOX", 337]],
174
- );
175
- });
176
-
177
- it("does nothing for a move into the folder the message is already in", async () => {
178
- await world();
179
- const messageId = await rowIn(inbox, 337);
180
- const before = await row(messageId);
181
-
182
- await service().moveMessages(
183
- account.accountConfigId,
184
- [messageId],
185
- inbox.mailboxId,
186
- account.accountId,
187
- );
188
-
189
- assert.equal(queue.length, 0);
190
- assert.deepEqual(await row(messageId), before);
191
- });
192
-
193
- it("binds the confirmed folder and uid once an in-flight move settles", async () => {
194
- await world();
195
- const messageId = await midMove();
196
-
197
- settleLater(messageId);
198
- await service().moveMessage(
199
- account.accountConfigId,
200
- messageId,
201
- inbox.mailboxId,
202
- account.accountId,
203
- );
204
-
205
- assert.deepEqual(
206
- queue.map((event) => [event.body.sourceMailboxPath, event.body.uid]),
207
- [["Archive", 907]],
208
- );
209
- });
210
-
211
- it("refuses and writes nothing while the earlier move never settles", async () => {
212
- await world();
213
- const messageId = await midMove();
214
- const before = await row(messageId);
215
-
216
- await assert.rejects(
217
- service().moveMessage(
218
- account.accountConfigId,
219
- messageId,
220
- inbox.mailboxId,
221
- account.accountId,
222
- ),
223
- (error: unknown) =>
224
- error instanceof MessagePlacementUnsettledError &&
225
- error.statusCode === 409 &&
226
- error.publicApiError?.details?.reason === "in_flight",
227
- );
228
-
229
- assert.equal(queue.length, 0);
230
- assert.deepEqual(await row(messageId), before);
231
- });
232
-
233
- it("never answers the same-folder skip from a folder the message has not reached", async () => {
234
- await world();
235
- const messageId = await midMove();
236
-
237
- await assert.rejects(
238
- service().moveMessage(
239
- account.accountConfigId,
240
- messageId,
241
- archive.mailboxId,
242
- account.accountId,
243
- ),
244
- MessagePlacementUnsettledError,
245
- );
246
- assert.equal(queue.length, 0);
247
- });
248
-
249
- it("moves no part of a batch while one row is in flight", async () => {
250
- await world();
251
- const settled = await rowIn(inbox, 11);
252
- const moving = await midMove();
253
-
254
- await assert.rejects(
255
- service().moveMessages(
256
- account.accountConfigId,
257
- [settled, moving],
258
- archive.mailboxId,
259
- account.accountId,
260
- ),
261
- MessagePlacementUnsettledError,
262
- );
263
-
264
- assert.equal(queue.length, 0);
265
- assert.equal((await row(settled)).mailboxId, inbox.mailboxId);
266
- });
267
-
268
- it("waits out a delete into Trash in flight rather than binding its borrowed uid", async () => {
269
- await world();
270
- const messageId = await midDelete();
271
- assert.equal((await row(messageId)).mailboxId, trash?.mailboxId);
272
-
273
- await assert.rejects(
274
- service().moveMessage(
275
- account.accountConfigId,
276
- messageId,
277
- inbox.mailboxId,
278
- account.accountId,
279
- ),
280
- MessagePlacementUnsettledError,
281
- );
282
- assert.equal(queue.length, 0);
283
- });
284
-
285
- it("moves a row a move gave up on without waiting", async () => {
286
- await world();
287
- const stranded = await midMove();
288
- await restoreSourcePlacement(
289
- {
290
- messageService: store.message,
291
- threadMessageService: store.threadMessage,
292
- },
293
- {
294
- accountConfigId: account.accountConfigId,
295
- messageId: stranded,
296
- sourceMailboxId: inbox.mailboxId,
297
- uid: 42,
298
- syncStatus: "abandoned",
299
- abandonedMutation: "move",
300
- },
301
- );
302
- assert.equal((await row(stranded)).syncStatus, "abandoned");
303
-
304
- const startedAt = Date.now();
305
- await service().moveMessage(
306
- account.accountConfigId,
307
- stranded,
308
- archive.mailboxId,
309
- account.accountId,
310
- );
311
-
312
- assert.ok(Date.now() - startedAt < 150);
313
- assert.equal((await row(stranded)).mailboxId, archive.mailboxId);
314
- });
315
-
316
- for (const action of ["move", "delete", "copy"] as const) {
317
- it(`refuses to ${action} a copy the server never confirms, writing nothing`, async () => {
318
- await world();
319
- const original = await rowIn(inbox, 5);
320
- await service().copyMessage(
321
- account.accountConfigId,
322
- original,
323
- archive.mailboxId,
324
- account.accountId,
325
- );
326
- queue.length = 0;
327
- const copy = deriveCopyMessageId(original, archive.mailboxId);
328
- const before = await row(copy);
329
-
330
- await assert.rejects(
331
- action === "delete"
332
- ? service().deleteMessages(
333
- account.accountConfigId,
334
- [copy],
335
- account.accountId,
336
- { permanent: true },
337
- )
338
- : service()[`${action}Message`](
339
- account.accountConfigId,
340
- copy,
341
- inbox.mailboxId,
342
- account.accountId,
343
- ),
344
- (error: unknown) =>
345
- error instanceof MessagePlacementUnsettledError &&
346
- error.statusCode === 409 &&
347
- error.publicApiError?.details?.reason === "in_flight",
348
- );
349
-
350
- assert.equal(queue.length, 0);
351
- assert.deepEqual(await row(copy), before);
352
- });
353
- }
354
- });
355
-
356
- describe("deleting a message", () => {
357
- const remove = (messageIds: string[], permanent = false) =>
358
- service().deleteMessages(
359
- account.accountConfigId,
360
- messageIds,
361
- account.accountId,
362
- permanent ? { permanent } : undefined,
363
- );
364
-
365
- it("moves it into a Trash the server flagged under a nested path", async () => {
366
- await world("flagged");
367
- const messageId = await rowIn(inbox, 5);
368
-
369
- await remove([messageId]);
370
-
371
- assert.equal((await row(messageId)).mailboxId, trash?.mailboxId);
372
- assert.deepEqual(
373
- queue.map((event) => [
374
- event.body.operation,
375
- event.body.destinationMailboxPath,
376
- ]),
377
- [["move_to_trash", "INBOX/Trash"]],
378
- );
379
- });
380
-
381
- it("files into a Trash that resolves by name alone", async () => {
382
- await world("named");
383
- const messageId = await rowIn(inbox, 5);
384
-
385
- await remove([messageId]);
386
-
387
- assert.equal((await row(messageId)).mailboxId, trash?.mailboxId);
388
- });
389
-
390
- for (const [setup, error, reason] of [
391
- ["none", NoTrashMailboxError, "none"],
392
- ["stale", StaleTrashAppointmentError, "stale"],
393
- ] as const) {
394
- it(`refuses and touches nothing when the Trash is ${reason}`, async () => {
395
- await world(setup);
396
- const messageId = await rowIn(inbox, 5);
397
- const before = await row(messageId);
398
-
399
- await assert.rejects(
400
- remove([messageId]),
401
- (thrown: unknown) =>
402
- thrown instanceof error &&
403
- thrown.statusCode === 409 &&
404
- thrown.publicApiError?.details?.reason === reason,
405
- );
406
-
407
- assert.equal(queue.length, 0);
408
- assert.deepEqual(await row(messageId), before);
409
- });
410
- }
411
-
412
- it("expunges only when asked for a permanent delete", async () => {
413
- await world("none");
414
- const messageId = await rowIn(inbox, 5);
415
-
416
- await remove([messageId], true);
417
-
418
- assert.deepEqual(
419
- queue.map((event) => event.body.operation),
420
- ["permanent_delete"],
421
- );
422
- });
423
-
424
- it("expunges a message already in a confirmed Trash", async () => {
425
- await world("flagged");
426
- const messageId = await rowIn(trash as MailboxItem, 5);
427
-
428
- await remove([messageId]);
429
-
430
- assert.deepEqual(
431
- queue.map((event) => event.body.operation),
432
- ["permanent_delete"],
433
- );
434
- });
435
-
436
- it("refuses to expunge from a Trash that only resolves by name", async () => {
437
- await world("named");
438
- const messageId = await rowIn(trash as MailboxItem, 5);
439
-
440
- await assert.rejects(
441
- remove([messageId]),
442
- (thrown: unknown) =>
443
- thrown instanceof UnconfirmedTrashMailboxError &&
444
- thrown.publicApiError?.details?.reason === "unconfirmed",
445
- );
446
- assert.equal(queue.length, 0);
447
- });
448
-
449
- it("withholds a sender whose only other mail is in Junk once this one is in Trash", async () => {
450
- await world("flagged");
451
- const raw = eml({ From: "Spam <spam@example.com>" });
452
- await arriveAt(store, imap, account, junk, raw);
453
- const kept = await arriveAt(
454
- store,
455
- imap,
456
- account,
457
- inbox,
458
- eml({ From: "Spam <spam@example.com>", "Message-ID": "<b@x>" }),
459
- );
460
- const sender = () =>
461
- store.address.getAddress(
462
- account.accountConfigId,
463
- deriveAddressId(account.accountConfigId, "spam@example.com"),
464
- );
465
- assert.equal((await sender()).flags.junkOnly, undefined);
466
-
467
- await remove([kept]);
468
-
469
- assert.equal((await sender()).flags.junkOnly?.value, true);
470
- });
471
-
472
- it("binds the confirmed pair once an in-flight move settles", async () => {
473
- await world("flagged");
474
- const messageId = await midMove();
475
-
476
- settleLater(messageId);
477
- await remove([messageId]);
478
-
479
- assert.deepEqual(
480
- queue.map((event) => [event.body.mailboxPath, event.body.uid]),
481
- [["Archive", 907]],
482
- );
483
- });
484
-
485
- it("expunges no part of a batch while one row is in flight, and all of it once it settles", async () => {
486
- await world("flagged");
487
- const settled = await rowIn(inbox, 11);
488
- const moving = await midMove();
489
-
490
- await assert.rejects(
491
- remove([settled, moving], true),
492
- MessagePlacementUnsettledError,
493
- );
494
- assert.equal(queue.length, 0);
495
-
496
- settleLater(moving);
497
- await remove([settled, moving], true);
498
-
499
- assert.deepEqual(
500
- queue.map((event) => [event.body.messageId, event.body.uid]).sort(),
501
- [
502
- [moving, 907],
503
- [settled, 11],
504
- ].sort(),
505
- );
506
- });
507
- });
508
-
509
- describe("copying a message", () => {
510
- const copy = (messageIds: string[], destination: MailboxItem) =>
511
- service().copyMessages(
512
- account.accountConfigId,
513
- messageIds,
514
- destination.mailboxId,
515
- account.accountId,
516
- );
517
-
518
- const COPIED = eml({
519
- "Message-ID": "<copied@example.com>",
520
- "List-Id": "LinkedIn Invitations <invitations.linkedin.com>",
521
- });
522
-
523
- const source = async () => {
524
- const messageId = await arriveAt(store, imap, account, inbox, COPIED);
525
- await new BodySyncService(
526
- store.message,
527
- store.storage(),
528
- store.threadMessage,
529
- store.address,
530
- store.envelope,
531
- new SyncWarningService(store.syncWarning),
532
- silentLog,
533
- ).syncBodies(
534
- [messageId],
535
- account.accountId,
536
- account.accountConfigId,
537
- {
538
- mailboxId: inbox.mailboxId,
539
- fullPath: inbox.fullPath,
540
- placement: "inbox",
541
- uidValidity: inbox.uidValidity,
542
- },
543
- async () => imap.connection(),
544
- );
545
- return messageId;
546
- };
547
-
548
- const threads = async (messageId: string) =>
549
- store.threadMessage.findAllByMessageId(account.accountConfigId, messageId);
550
-
551
- it("derives the copy's id from source and destination and carries its derived fields", async () => {
552
- await world();
553
- const original = await source();
554
-
555
- await copy([original], archive);
556
-
557
- const copyId = deriveCopyMessageId(original, archive.mailboxId);
558
- const copied = await row(copyId);
559
- assert.equal(copied.mailboxId, archive.mailboxId);
560
- assert.equal(
561
- copied.classificationState,
562
- MessageClassificationState.Examined,
563
- );
564
- assert.equal((await threads(copyId))[0].listId, "invitations.linkedin.com");
565
- assert.deepEqual(
566
- queue.map((event) => event.body.type),
567
- ["MESSAGE_COPY"],
568
- );
569
- });
570
-
571
- it("converges a replayed copy on one row", async () => {
572
- await world();
573
- const original = await source();
574
-
575
- await copy([original], archive);
576
- await copy([original], archive);
577
-
578
- const copyId = deriveCopyMessageId(original, archive.mailboxId);
579
- assert.equal((await threads(copyId)).length, 1);
580
- assert.equal(
581
- (await store.message.listAllByMailbox(archive.mailboxId)).length,
582
- 1,
583
- );
584
- });
585
-
586
- it("adds no third row when the destination folder syncs the copy", async () => {
587
- await world();
588
- const original = await source();
589
- await copy([original], archive);
590
- const copyId = deriveCopyMessageId(original, archive.mailboxId);
591
- const uid = imap.deliver("Archive", COPIED);
592
- await store.message.updateUid(copyId, uid, archive.mailboxId);
593
-
594
- await syncFolder(store, account, archive, imap);
595
-
596
- assert.equal(
597
- (await store.message.listAllByMailbox(archive.mailboxId)).length,
598
- 1,
599
- );
600
- assert.equal((await row(original)).mailboxId, inbox.mailboxId);
601
- assert.equal((await threads(copyId)).length, 1);
602
- });
603
-
604
- it("deletes the copy or the original without touching the other", async () => {
605
- await world();
606
- const original = await source();
607
- await copy([original], archive);
608
- const copyId = deriveCopyMessageId(original, archive.mailboxId);
609
- await store.message.updateUid(copyId, 77, archive.mailboxId);
610
-
611
- await service().deleteMessages(
612
- account.accountConfigId,
613
- [copyId],
614
- account.accountId,
615
- { permanent: true },
616
- );
617
-
618
- assert.deepEqual(await threads(copyId), []);
619
- assert.equal((await threads(original)).length, 1);
620
- });
621
-
622
- it("binds the confirmed pair once an in-flight move settles, and refuses a batch until then", async () => {
623
- await world();
624
- const settled = await rowIn(inbox, 11);
625
- const moving = await midMove();
626
-
627
- await assert.rejects(
628
- copy([settled, moving], junk),
629
- MessagePlacementUnsettledError,
630
- );
631
- assert.equal(queue.length, 0);
632
-
633
- settleLater(moving);
634
- await service().copyMessage(
635
- account.accountConfigId,
636
- moving,
637
- inbox.mailboxId,
638
- account.accountId,
639
- );
640
-
641
- assert.deepEqual(
642
- queue.map((event) => [event.body.sourceMailboxPath, event.body.uid]),
643
- [["Archive", 907]],
644
- );
645
- });
646
- });
647
-
648
- describe("emptying the Trash", () => {
649
- const empty = () =>
650
- service().emptyTrash(account.accountConfigId, account.accountId);
651
-
652
- it("empties the appointed Trash, never the folder called Deleted", async () => {
653
- await world("appointed");
654
- const binned = await rowIn(trash as MailboxItem, 1);
655
-
656
- const { deletedCount } = await empty();
657
-
658
- assert.equal(deletedCount, 1);
659
- assert.equal((await row(binned)).status, "deleting");
660
- assert.deepEqual(
661
- queue.map((event) => [
662
- event.body.type,
663
- event.body.trashMailboxId,
664
- event.body.trashUidValidity,
665
- ]),
666
- [["EMPTY_TRASH", trash?.mailboxId, trash?.uidValidity]],
667
- );
668
- });
669
-
670
- for (const [setup, error, reason] of [
671
- ["none", NoTrashMailboxError, "none"],
672
- ["named", UnconfirmedTrashMailboxError, "unconfirmed"],
673
- ["stale", StaleTrashAppointmentError, "stale"],
674
- ] as const) {
675
- it(`refuses when the Trash is ${reason}`, async () => {
676
- await world(setup);
677
- const kept = trash ? await rowIn(trash, 1) : undefined;
678
-
679
- await assert.rejects(
680
- empty(),
681
- (thrown: unknown) =>
682
- thrown instanceof error &&
683
- thrown.publicApiError?.details?.reason === reason,
684
- );
685
-
686
- assert.equal(queue.length, 0);
687
- if (kept) assert.equal((await row(kept)).status, "active");
688
- });
689
- }
690
-
691
- it("marks settled and sync-pending rows but leaves a move into Trash alone", async () => {
692
- await world("flagged");
693
- const bin = trash as MailboxItem;
694
- const settled = await rowIn(bin, 1);
695
- const pending = await arriveAt(store, imap, account, bin, eml({}));
696
- const arriving = await rowIn(inbox, 10);
697
- await service().moveMessage(
698
- account.accountConfigId,
699
- arriving,
700
- bin.mailboxId,
701
- account.accountId,
702
- );
703
- queue.length = 0;
704
-
705
- const { deletedCount } = await empty();
706
-
707
- assert.equal(deletedCount, 2);
708
- assert.equal((await row(settled)).status, "deleting");
709
- assert.equal((await row(pending)).status, "deleting");
710
- assert.equal((await row(arriving)).status, "moving");
711
- });
712
-
713
- it("reports the same count when pressed twice before the worker runs", async () => {
714
- await world("flagged");
715
- const bin = trash as MailboxItem;
716
- await rowIn(bin, 1);
717
- await rowIn(bin, 2);
718
-
719
- const first = await empty();
720
- const second = await empty();
721
-
722
- assert.equal(first.deletedCount, 2);
723
- assert.equal(second.deletedCount, 2);
724
- assert.equal(queue.length, 2);
725
- });
726
- });