@remit/mailbox-service 0.0.49 → 0.0.51

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.
@@ -0,0 +1,721 @@
1
+ import assert from "node:assert/strict";
2
+ import { describe, it } from "node:test";
3
+ import type {
4
+ CreateOutboxAttachmentInput,
5
+ IOutboxAttachmentRepository,
6
+ IOutboxMessageRepository,
7
+ OutboxAttachmentCap,
8
+ OutboxAttachmentItem,
9
+ OutboxMessageItem,
10
+ ReserveOutboxAttachmentResult,
11
+ } from "@remit/data-ports";
12
+ import { holdsRoom } from "@remit/data-ports";
13
+ import { ForbiddenError, NotFoundError } from "@remit/data-ports/errors";
14
+ import {
15
+ OutboxAttachmentRejectionReason,
16
+ OutboxMessageStatus,
17
+ } from "@remit/domain-enums";
18
+ import {
19
+ createMockStorageService,
20
+ type StorageService,
21
+ UPLOAD_URL_TTL_SECONDS,
22
+ } from "@remit/storage-service";
23
+ import {
24
+ OUTBOX_ATTACHMENT_MAX_COUNT,
25
+ OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES,
26
+ OutboxAttachmentService,
27
+ } from "./outbox-attachment.js";
28
+
29
+ const ACCOUNT_CONFIG_ID = "cfg-679";
30
+ const ACCOUNT_ID = "acc-679";
31
+ const DRAFT_ID = "draft-679";
32
+
33
+ /**
34
+ * An in-memory stand-in for the repository, with the one property that matters:
35
+ * `reserve` counts and inserts without yielding, the way a database transaction
36
+ * does. Everything else may interleave freely.
37
+ */
38
+ const createRepository = (): IOutboxAttachmentRepository & {
39
+ rows: Map<string, OutboxAttachmentItem>;
40
+ } => {
41
+ const rows = new Map<string, OutboxAttachmentItem>();
42
+ let sequence = 0;
43
+
44
+ const forDraft = (
45
+ accountConfigId: string,
46
+ outboxMessageId: string,
47
+ ): OutboxAttachmentItem[] =>
48
+ [...rows.values()].filter(
49
+ (row) =>
50
+ row.accountConfigId === accountConfigId &&
51
+ row.outboxMessageId === outboxMessageId,
52
+ );
53
+
54
+ return {
55
+ rows,
56
+ reserve: async (
57
+ input: CreateOutboxAttachmentInput,
58
+ cap: OutboxAttachmentCap,
59
+ ): Promise<ReserveOutboxAttachmentResult> => {
60
+ // No await inside: the whole point of the real implementation is that
61
+ // counting and inserting are one atomic step.
62
+ const live = forDraft(
63
+ input.accountConfigId,
64
+ input.outboxMessageId,
65
+ ).filter((row) => holdsRoom(row, cap.nowSeconds));
66
+ const usedBytes = live.reduce((total, row) => total + row.sizeBytes, 0);
67
+ if (live.length >= cap.maxCount) {
68
+ return { outcome: "OverCountCap", usedBytes };
69
+ }
70
+ if (usedBytes + input.sizeBytes > cap.maxTotalBytes) {
71
+ return { outcome: "OverByteCap", usedBytes };
72
+ }
73
+ sequence += 1;
74
+ const item: OutboxAttachmentItem = {
75
+ ...input,
76
+ state: "Pending",
77
+ createdAt: sequence,
78
+ updatedAt: sequence,
79
+ };
80
+ rows.set(item.outboxAttachmentId, item);
81
+ return { outcome: "Reserved", item };
82
+ },
83
+ get: async (accountConfigId, outboxAttachmentId) => {
84
+ const row = rows.get(outboxAttachmentId);
85
+ if (!row || row.accountConfigId !== accountConfigId) {
86
+ throw new NotFoundError(`No outbox attachment ${outboxAttachmentId}`);
87
+ }
88
+ return row;
89
+ },
90
+ listByOutboxMessage: async (accountConfigId, outboxMessageId) =>
91
+ forDraft(accountConfigId, outboxMessageId),
92
+ markStored: async (accountConfigId, outboxAttachmentId, sizeBytes) => {
93
+ const row = rows.get(outboxAttachmentId);
94
+ if (!row || row.accountConfigId !== accountConfigId) return null;
95
+ if (row.state !== "Pending") return null;
96
+ const next: OutboxAttachmentItem = {
97
+ ...row,
98
+ state: "Stored",
99
+ sizeBytes,
100
+ reservationExpiresAt: 0,
101
+ };
102
+ rows.set(outboxAttachmentId, next);
103
+ return next;
104
+ },
105
+ deleteLapsedReservations: async (
106
+ accountConfigId: string,
107
+ outboxMessageId: string,
108
+ nowSeconds: number,
109
+ ) => {
110
+ const gone: string[] = [];
111
+ for (const row of [...rows.values()]) {
112
+ if (
113
+ row.accountConfigId === accountConfigId &&
114
+ row.outboxMessageId === outboxMessageId &&
115
+ row.state === "Pending" &&
116
+ row.reservationExpiresAt < nowSeconds
117
+ ) {
118
+ rows.delete(row.outboxAttachmentId);
119
+ gone.push(row.outboxAttachmentId);
120
+ }
121
+ }
122
+ return gone;
123
+ },
124
+ deleteMany: async (accountConfigId, ids) => {
125
+ for (const id of ids) {
126
+ if (rows.get(id)?.accountConfigId === accountConfigId) rows.delete(id);
127
+ }
128
+ },
129
+ deleteByOutboxMessage: async (accountConfigId, outboxMessageId) => {
130
+ for (const row of forDraft(accountConfigId, outboxMessageId)) {
131
+ rows.delete(row.outboxAttachmentId);
132
+ }
133
+ },
134
+ };
135
+ };
136
+
137
+ const build = (
138
+ status: OutboxMessageItem["status"] = OutboxMessageStatus.draft,
139
+ now?: () => number,
140
+ ) => {
141
+ const storage = createMockStorageService();
142
+ const repository = createRepository();
143
+ const outboxMessageService = {
144
+ get: async (
145
+ accountConfigId: string,
146
+ _id: string,
147
+ mode?: "read" | "act",
148
+ ) => {
149
+ if (accountConfigId !== ACCOUNT_CONFIG_ID) {
150
+ assert.equal(mode, "act");
151
+ throw new ForbiddenError("not yours");
152
+ }
153
+ return {
154
+ outboxMessageId: DRAFT_ID,
155
+ accountId: ACCOUNT_ID,
156
+ accountConfigId,
157
+ status,
158
+ } as OutboxMessageItem;
159
+ },
160
+ } as unknown as IOutboxMessageRepository;
161
+
162
+ return {
163
+ service: new OutboxAttachmentService({
164
+ outboxMessageService,
165
+ outboxAttachmentService: repository,
166
+ storage,
167
+ now,
168
+ }),
169
+ storage,
170
+ repository,
171
+ };
172
+ };
173
+
174
+ const mint = (
175
+ service: OutboxAttachmentService,
176
+ overrides: Partial<{
177
+ accountConfigId: string;
178
+ filename: string;
179
+ contentType: string;
180
+ sizeBytes: number;
181
+ }> = {},
182
+ ) =>
183
+ service.mint({
184
+ accountConfigId: overrides.accountConfigId ?? ACCOUNT_CONFIG_ID,
185
+ outboxMessageId: DRAFT_ID,
186
+ filename: overrides.filename ?? "notes.txt",
187
+ contentType: overrides.contentType ?? "text/plain",
188
+ sizeBytes: overrides.sizeBytes ?? 10,
189
+ });
190
+
191
+ const uploadFor = (
192
+ storage: StorageService,
193
+ outboxAttachmentId: string,
194
+ sizeBytes: number,
195
+ ) =>
196
+ storage.storeOutboxAttachment({
197
+ accountConfigId: ACCOUNT_CONFIG_ID,
198
+ accountId: ACCOUNT_ID,
199
+ outboxMessageId: DRAFT_ID,
200
+ outboxAttachmentId,
201
+ content: Buffer.alloc(sizeBytes),
202
+ });
203
+
204
+ describe("reserving room on a draft", () => {
205
+ it("writes a Pending row and hands back a URL bound to its size", async () => {
206
+ const { service, repository } = build();
207
+
208
+ const result = await mint(service, {
209
+ filename: "invoice.pdf",
210
+ contentType: "application/pdf",
211
+ sizeBytes: 2048,
212
+ });
213
+
214
+ assert.equal(result.outcome, "Minted");
215
+ if (result.outcome !== "Minted") return;
216
+ assert.equal(result.reservation.filename, "invoice.pdf");
217
+ assert.equal(result.reservation.contentType, "application/pdf");
218
+ assert.match(result.reservation.uploadUrl, /max=2048/);
219
+
220
+ const row = repository.rows.get(result.reservation.outboxAttachmentId);
221
+ assert.equal(row?.state, "Pending");
222
+ assert.equal(row?.sizeBytes, 2048);
223
+ // The key on the row is the one the URL addresses — one identity, not two.
224
+ assert.ok(row?.storageKey.endsWith(result.reservation.outboxAttachmentId));
225
+ });
226
+
227
+ it("refuses a declared size over the cap", async () => {
228
+ const { service } = build();
229
+
230
+ const result = await mint(service, {
231
+ filename: "huge.bin",
232
+ sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES + 1,
233
+ });
234
+
235
+ assert.equal(result.outcome, "Rejected");
236
+ if (result.outcome !== "Rejected") return;
237
+ assert.equal(
238
+ result.rejection.reason,
239
+ OutboxAttachmentRejectionReason.FileTooLarge,
240
+ );
241
+ });
242
+
243
+ it("counts a reservation nobody has uploaded against yet", async () => {
244
+ const { service } = build();
245
+ await mint(service, {
246
+ sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
247
+ });
248
+
249
+ const result = await mint(service, { sizeBytes: 4096 });
250
+
251
+ assert.equal(result.outcome, "Rejected");
252
+ if (result.outcome !== "Rejected") return;
253
+ assert.equal(
254
+ result.rejection.reason,
255
+ OutboxAttachmentRejectionReason.MessageTooLarge,
256
+ );
257
+ assert.equal(
258
+ result.rejection.usedBytes,
259
+ OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
260
+ );
261
+ });
262
+
263
+ it("refuses once the draft holds the most files a message can", async () => {
264
+ const { service } = build();
265
+ for (let index = 0; index < OUTBOX_ATTACHMENT_MAX_COUNT; index += 1) {
266
+ await mint(service, { sizeBytes: 1 });
267
+ }
268
+
269
+ const result = await mint(service, { sizeBytes: 1 });
270
+
271
+ assert.equal(result.outcome, "Rejected");
272
+ if (result.outcome !== "Rejected") return;
273
+ assert.equal(
274
+ result.rejection.reason,
275
+ OutboxAttachmentRejectionReason.TooManyAttachments,
276
+ );
277
+ });
278
+
279
+ it("refuses a file declared as empty, and a filename that sanitizes away", async () => {
280
+ const { service } = build();
281
+
282
+ const empty = await mint(service, { sizeBytes: 0 });
283
+ assert.equal(
284
+ empty.outcome === "Rejected" && empty.rejection.reason,
285
+ OutboxAttachmentRejectionReason.EmptyFile,
286
+ );
287
+
288
+ const unnamed = await mint(service, { filename: "../.." });
289
+ assert.equal(
290
+ unnamed.outcome === "Rejected" && unnamed.rejection.reason,
291
+ OutboxAttachmentRejectionReason.UnusableFilename,
292
+ );
293
+ });
294
+
295
+ it("records a filename stripped of its path and a media type it cannot read", async () => {
296
+ const { service } = build();
297
+
298
+ const result = await mint(service, {
299
+ filename: "../../etc/passwd",
300
+ contentType: "",
301
+ });
302
+
303
+ assert.equal(result.outcome, "Minted");
304
+ if (result.outcome !== "Minted") return;
305
+ assert.equal(result.reservation.filename, "passwd");
306
+ assert.equal(result.reservation.contentType, "application/octet-stream");
307
+ });
308
+
309
+ it("stops holding room once the reservation lapses", async () => {
310
+ const clock = { seconds: 1_000_000 };
311
+ const { service } = build(OutboxMessageStatus.draft, () => clock.seconds);
312
+ await mint(service, {
313
+ sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
314
+ });
315
+
316
+ assert.equal(
317
+ (await mint(service, { sizeBytes: 4096 })).outcome,
318
+ "Rejected",
319
+ );
320
+
321
+ clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
322
+
323
+ assert.equal((await mint(service, { sizeBytes: 4096 })).outcome, "Minted");
324
+ });
325
+
326
+ it("denies a mint against a draft owned by someone else", async () => {
327
+ const { service, repository } = build();
328
+
329
+ await assert.rejects(
330
+ () => mint(service, { accountConfigId: "cfg-stranger" }),
331
+ ForbiddenError,
332
+ );
333
+ assert.equal(repository.rows.size, 0);
334
+ });
335
+
336
+ it("refuses to reserve on a message that has left draft", async () => {
337
+ const { service } = build(OutboxMessageStatus.queued);
338
+
339
+ await assert.rejects(() => mint(service), {
340
+ name: "ConflictError",
341
+ message: /no longer take an attachment/,
342
+ });
343
+ });
344
+ });
345
+
346
+ describe("the cap under concurrency", () => {
347
+ /**
348
+ * Nothing in this service serializes anything, and nothing needs to: the
349
+ * repository counts and inserts in one transaction, so parallel mints — in
350
+ * this process or six others — are ordered by the database. These would fail
351
+ * if `reserve` ever grew an await between its count and its insert.
352
+ */
353
+ it("holds the byte cap when files are dropped in together", async () => {
354
+ const { service } = build();
355
+ const fiveMegabytes = 5 * 1024 * 1024;
356
+
357
+ const results = await Promise.all(
358
+ Array.from({ length: 6 }, () =>
359
+ mint(service, { sizeBytes: fiveMegabytes }),
360
+ ),
361
+ );
362
+
363
+ assert.equal(
364
+ results.filter((result) => result.outcome === "Minted").length,
365
+ 5,
366
+ );
367
+ const rejected = results.filter((result) => result.outcome === "Rejected");
368
+ assert.equal(rejected.length, 1);
369
+ assert.equal(
370
+ rejected[0].outcome === "Rejected" && rejected[0].rejection.reason,
371
+ OutboxAttachmentRejectionReason.MessageTooLarge,
372
+ );
373
+ });
374
+
375
+ it("holds the file-count ceiling when files are dropped in together", async () => {
376
+ const { service, repository } = build();
377
+
378
+ const results = await Promise.all(
379
+ Array.from({ length: OUTBOX_ATTACHMENT_MAX_COUNT + 5 }, () =>
380
+ mint(service, { sizeBytes: 1 }),
381
+ ),
382
+ );
383
+
384
+ assert.equal(
385
+ results.filter((result) => result.outcome === "Minted").length,
386
+ OUTBOX_ATTACHMENT_MAX_COUNT,
387
+ );
388
+ assert.equal(repository.rows.size, OUTBOX_ATTACHMENT_MAX_COUNT);
389
+ });
390
+ });
391
+
392
+ describe("completing an attachment", () => {
393
+ it("believes storage about the size and moves the row to Stored", async () => {
394
+ const { service, storage, repository } = build();
395
+ const minted = await mint(service, { sizeBytes: 512 });
396
+ assert.equal(minted.outcome, "Minted");
397
+ if (minted.outcome !== "Minted") return;
398
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 512);
399
+
400
+ const completed = await service.complete({
401
+ accountConfigId: ACCOUNT_CONFIG_ID,
402
+ outboxMessageId: DRAFT_ID,
403
+ outboxAttachmentId: minted.reservation.outboxAttachmentId,
404
+ });
405
+
406
+ assert.equal(completed.outcome, "Completed");
407
+ if (completed.outcome !== "Completed") return;
408
+ assert.equal(completed.attachment.sizeBytes, 512);
409
+ assert.equal(completed.attachment.state, "Stored");
410
+ assert.equal(
411
+ repository.rows.get(minted.reservation.outboxAttachmentId)
412
+ ?.reservationExpiresAt,
413
+ 0,
414
+ );
415
+ });
416
+
417
+ it("answers a repeated completion the same way, not with an error", async () => {
418
+ const { service, storage } = build();
419
+ const minted = await mint(service, { sizeBytes: 64 });
420
+ assert.equal(minted.outcome, "Minted");
421
+ if (minted.outcome !== "Minted") return;
422
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 64);
423
+
424
+ const input = {
425
+ accountConfigId: ACCOUNT_CONFIG_ID,
426
+ outboxMessageId: DRAFT_ID,
427
+ outboxAttachmentId: minted.reservation.outboxAttachmentId,
428
+ };
429
+ const first = await service.complete(input);
430
+ const second = await service.complete(input);
431
+
432
+ assert.equal(first.outcome, "Completed");
433
+ assert.equal(second.outcome, "Completed");
434
+ assert.deepEqual(
435
+ first.outcome === "Completed" && first.attachment,
436
+ second.outcome === "Completed" && second.attachment,
437
+ );
438
+ });
439
+
440
+ it("never completes an attachment whose object is absent", async () => {
441
+ const { service } = build();
442
+ const minted = await mint(service, { sizeBytes: 512 });
443
+ assert.equal(minted.outcome, "Minted");
444
+ if (minted.outcome !== "Minted") return;
445
+
446
+ const completed = await service.complete({
447
+ accountConfigId: ACCOUNT_CONFIG_ID,
448
+ outboxMessageId: DRAFT_ID,
449
+ outboxAttachmentId: minted.reservation.outboxAttachmentId,
450
+ });
451
+
452
+ assert.equal(
453
+ completed.outcome === "Rejected" && completed.rejection.reason,
454
+ OutboxAttachmentRejectionReason.UploadMissing,
455
+ );
456
+ });
457
+
458
+ it("refuses a wrong-sized object, removes it, and gives the room back", async () => {
459
+ const { service, storage, repository } = build();
460
+ const minted = await mint(service, { sizeBytes: 512 });
461
+ assert.equal(minted.outcome, "Minted");
462
+ if (minted.outcome !== "Minted") return;
463
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 4096);
464
+
465
+ const completed = await service.complete({
466
+ accountConfigId: ACCOUNT_CONFIG_ID,
467
+ outboxMessageId: DRAFT_ID,
468
+ outboxAttachmentId: minted.reservation.outboxAttachmentId,
469
+ });
470
+
471
+ assert.equal(completed.outcome, "Rejected");
472
+ if (completed.outcome !== "Rejected") return;
473
+ assert.equal(
474
+ completed.rejection.reason,
475
+ OutboxAttachmentRejectionReason.SizeMismatch,
476
+ );
477
+ // The room the reservation held is reported as released, not as still held.
478
+ assert.equal(completed.rejection.usedBytes, 0);
479
+ assert.equal(repository.rows.size, 0);
480
+ assert.equal(
481
+ await storage.statOutboxAttachment(
482
+ ACCOUNT_CONFIG_ID,
483
+ ACCOUNT_ID,
484
+ DRAFT_ID,
485
+ minted.reservation.outboxAttachmentId,
486
+ ),
487
+ null,
488
+ );
489
+ });
490
+
491
+ it("refuses a completion once the reservation has lapsed", async () => {
492
+ const clock = { seconds: 3_000_000 };
493
+ const { service, storage } = build(
494
+ OutboxMessageStatus.draft,
495
+ () => clock.seconds,
496
+ );
497
+ const minted = await mint(service, { sizeBytes: 64 });
498
+ assert.equal(minted.outcome, "Minted");
499
+ if (minted.outcome !== "Minted") return;
500
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 64);
501
+
502
+ clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
503
+
504
+ const completed = await service.complete({
505
+ accountConfigId: ACCOUNT_CONFIG_ID,
506
+ outboxMessageId: DRAFT_ID,
507
+ outboxAttachmentId: minted.reservation.outboxAttachmentId,
508
+ });
509
+
510
+ assert.equal(
511
+ completed.outcome === "Rejected" && completed.rejection.reason,
512
+ OutboxAttachmentRejectionReason.ReservationExpired,
513
+ );
514
+ });
515
+ });
516
+
517
+ describe("removing and discarding", () => {
518
+ it("retainOnly drops the rows and the bytes it was not told to keep", async () => {
519
+ const { service, storage, repository } = build();
520
+ const kept = await mint(service, { filename: "keep.txt", sizeBytes: 8 });
521
+ const dropped = await mint(service, { filename: "drop.txt", sizeBytes: 8 });
522
+ assert.equal(kept.outcome, "Minted");
523
+ assert.equal(dropped.outcome, "Minted");
524
+ if (kept.outcome !== "Minted" || dropped.outcome !== "Minted") return;
525
+ await uploadFor(storage, dropped.reservation.outboxAttachmentId, 8);
526
+
527
+ await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
528
+ kept.reservation.outboxAttachmentId,
529
+ ]);
530
+
531
+ assert.deepEqual(
532
+ [...repository.rows.keys()],
533
+ [kept.reservation.outboxAttachmentId],
534
+ );
535
+ assert.equal(
536
+ await storage.statOutboxAttachment(
537
+ ACCOUNT_CONFIG_ID,
538
+ ACCOUNT_ID,
539
+ DRAFT_ID,
540
+ dropped.reservation.outboxAttachmentId,
541
+ ),
542
+ null,
543
+ );
544
+ });
545
+
546
+ it("discardAll takes every row and every object", async () => {
547
+ const { service, storage, repository } = build();
548
+ const minted = await mint(service, { sizeBytes: 8 });
549
+ assert.equal(minted.outcome, "Minted");
550
+ if (minted.outcome !== "Minted") return;
551
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 8);
552
+
553
+ await service.discardAll(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID);
554
+
555
+ assert.equal(repository.rows.size, 0);
556
+ assert.deepEqual(
557
+ await storage.listOutboxAttachments(
558
+ ACCOUNT_CONFIG_ID,
559
+ ACCOUNT_ID,
560
+ DRAFT_ID,
561
+ ),
562
+ [],
563
+ );
564
+ });
565
+ });
566
+
567
+ describe("retainOnly, which is what attachmentIds drives", () => {
568
+ const mintTwo = async (service: OutboxAttachmentService) => {
569
+ const first = await mint(service, { filename: "one.txt", sizeBytes: 8 });
570
+ const second = await mint(service, { filename: "two.txt", sizeBytes: 8 });
571
+ assert.equal(first.outcome, "Minted");
572
+ assert.equal(second.outcome, "Minted");
573
+ if (first.outcome !== "Minted" || second.outcome !== "Minted") {
574
+ throw new Error("unreachable");
575
+ }
576
+ return [first.reservation, second.reservation] as const;
577
+ };
578
+
579
+ it("keeps everything named and removes everything else, bytes included", async () => {
580
+ const { service, storage, repository } = build();
581
+ const [keep, drop] = await mintTwo(service);
582
+ await uploadFor(storage, keep.outboxAttachmentId, 8);
583
+ await uploadFor(storage, drop.outboxAttachmentId, 8);
584
+
585
+ await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
586
+ keep.outboxAttachmentId,
587
+ ]);
588
+
589
+ assert.deepEqual([...repository.rows.keys()], [keep.outboxAttachmentId]);
590
+ assert.ok(
591
+ await storage.statOutboxAttachment(
592
+ ACCOUNT_CONFIG_ID,
593
+ ACCOUNT_ID,
594
+ DRAFT_ID,
595
+ keep.outboxAttachmentId,
596
+ ),
597
+ );
598
+ assert.equal(
599
+ await storage.statOutboxAttachment(
600
+ ACCOUNT_CONFIG_ID,
601
+ ACCOUNT_ID,
602
+ DRAFT_ID,
603
+ drop.outboxAttachmentId,
604
+ ),
605
+ null,
606
+ );
607
+ });
608
+
609
+ it("an empty list is a real instruction: everything goes", async () => {
610
+ const { service, storage, repository } = build();
611
+ const [first] = await mintTwo(service);
612
+ await uploadFor(storage, first.outboxAttachmentId, 8);
613
+
614
+ await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, []);
615
+
616
+ assert.equal(repository.rows.size, 0);
617
+ assert.deepEqual(
618
+ await storage.listOutboxAttachments(
619
+ ACCOUNT_CONFIG_ID,
620
+ ACCOUNT_ID,
621
+ DRAFT_ID,
622
+ ),
623
+ [],
624
+ );
625
+ });
626
+
627
+ it("naming every id changes nothing", async () => {
628
+ const { service, repository } = build();
629
+ const [first, second] = await mintTwo(service);
630
+
631
+ await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
632
+ first.outboxAttachmentId,
633
+ second.outboxAttachmentId,
634
+ ]);
635
+
636
+ assert.equal(repository.rows.size, 2);
637
+ });
638
+
639
+ it("an id the draft never held is a no-op, not a removal of the rest", async () => {
640
+ const { service, repository } = build();
641
+ const [first, second] = await mintTwo(service);
642
+
643
+ await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
644
+ first.outboxAttachmentId,
645
+ second.outboxAttachmentId,
646
+ "never-existed",
647
+ ]);
648
+
649
+ assert.equal(repository.rows.size, 2);
650
+ });
651
+ });
652
+
653
+ describe("a reservation that lapses without ever completing", () => {
654
+ it("is reaped, so the sweep can collect the bytes it was vouching for", async () => {
655
+ const clock = { seconds: 5_000_000 };
656
+ const { service, storage, repository } = build(
657
+ OutboxMessageStatus.draft,
658
+ () => clock.seconds,
659
+ );
660
+ const minted = await mint(service, { sizeBytes: 32 });
661
+ assert.equal(minted.outcome, "Minted");
662
+ if (minted.outcome !== "Minted") return;
663
+ // Uploaded, never confirmed — the shape a closed tab leaves behind.
664
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 32);
665
+
666
+ clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
667
+
668
+ // Before the reap the row still names the object, which is enough for the
669
+ // sweep to leave it alone forever.
670
+ const live = await service.reapAndListLive(ACCOUNT_CONFIG_ID, DRAFT_ID);
671
+
672
+ assert.deepEqual(live, []);
673
+ assert.equal(repository.rows.size, 0);
674
+ });
675
+
676
+ it("is not shown to the composer as a file the draft holds", async () => {
677
+ const clock = { seconds: 6_000_000 };
678
+ const { service } = build(OutboxMessageStatus.draft, () => clock.seconds);
679
+ await mint(service, { sizeBytes: 32 });
680
+
681
+ assert.equal(
682
+ (await service.listFor(ACCOUNT_CONFIG_ID, DRAFT_ID)).length,
683
+ 1,
684
+ );
685
+
686
+ clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
687
+
688
+ assert.deepEqual(await service.listFor(ACCOUNT_CONFIG_ID, DRAFT_ID), []);
689
+ });
690
+
691
+ it("a confirmed attachment cannot be overwritten through its old URL", async () => {
692
+ const { service, storage } = build();
693
+ const minted = await mint(service, { sizeBytes: 16 });
694
+ assert.equal(minted.outcome, "Minted");
695
+ if (minted.outcome !== "Minted") return;
696
+ await uploadFor(storage, minted.reservation.outboxAttachmentId, 16);
697
+
698
+ assert.equal(
699
+ await service.hasLiveReservation(
700
+ ACCOUNT_CONFIG_ID,
701
+ minted.reservation.outboxAttachmentId,
702
+ ),
703
+ true,
704
+ );
705
+
706
+ await service.complete({
707
+ accountConfigId: ACCOUNT_CONFIG_ID,
708
+ outboxMessageId: DRAFT_ID,
709
+ outboxAttachmentId: minted.reservation.outboxAttachmentId,
710
+ });
711
+
712
+ // The URL stays signed for the rest of its window; the row is what refuses.
713
+ assert.equal(
714
+ await service.hasLiveReservation(
715
+ ACCOUNT_CONFIG_ID,
716
+ minted.reservation.outboxAttachmentId,
717
+ ),
718
+ false,
719
+ );
720
+ });
721
+ });