@antelopejs/dms-media 0.6.0 → 0.6.1

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 (74) hide show
  1. package/README.md +3 -3
  2. package/dist/db/models/mediaFolders.model.d.ts +0 -3
  3. package/dist/db/models/mediaFolders.model.js +0 -29
  4. package/dist/db/models/mediaFolders.model.js.map +1 -1
  5. package/dist/db/tables/mediaDeclarations.table.d.ts +0 -4
  6. package/dist/db/tables/mediaDeclarations.table.js +0 -16
  7. package/dist/db/tables/mediaDeclarations.table.js.map +1 -1
  8. package/dist/declared/assets.d.ts +0 -2
  9. package/dist/declared/assets.js +12 -140
  10. package/dist/declared/assets.js.map +1 -1
  11. package/dist/declared/folders.d.ts +1 -5
  12. package/dist/declared/folders.js +14 -80
  13. package/dist/declared/folders.js.map +1 -1
  14. package/dist/declared/index.d.ts +0 -2
  15. package/dist/declared/index.js +0 -2
  16. package/dist/declared/index.js.map +1 -1
  17. package/dist/declared/records.d.ts +2 -30
  18. package/dist/declared/records.js +3 -33
  19. package/dist/declared/records.js.map +1 -1
  20. package/dist/derivatives.js +5 -17
  21. package/dist/derivatives.js.map +1 -1
  22. package/dist/implementations/interface-dms-media/index.d.ts +1 -3
  23. package/dist/implementations/interface-dms-media/index.js +0 -2
  24. package/dist/implementations/interface-dms-media/index.js.map +1 -1
  25. package/dist/routes/media/delivery-stream.js +7 -21
  26. package/dist/routes/media/delivery-stream.js.map +1 -1
  27. package/dist/routes/media/upload.js +2 -5
  28. package/dist/routes/media/upload.js.map +1 -1
  29. package/dist/test/antelope.test.js +4 -4
  30. package/dist/test/integration/media-declared-content.test.js +17 -317
  31. package/dist/test/integration/media-declared-content.test.js.map +1 -1
  32. package/dist/test/integration/media-linked-subfolders.test.js +2 -2
  33. package/dist/test/integration/media-linked-subfolders.test.js.map +1 -1
  34. package/dist/test/integration/media-storage-origin-unreachable.test.js +79 -0
  35. package/dist/test/integration/media-storage-origin-unreachable.test.js.map +1 -0
  36. package/dist/test/unit/declared.test.js +0 -66
  37. package/dist/test/unit/declared.test.js.map +1 -1
  38. package/dist/test/unit/library/realtime.test.js +1 -0
  39. package/dist/test/unit/library/realtime.test.js.map +1 -1
  40. package/dist/validation/declared.schema.d.ts +7 -36
  41. package/dist/validation/declared.schema.js +1 -12
  42. package/dist/validation/declared.schema.js.map +1 -1
  43. package/frontend-vue/package.json +1 -1
  44. package/package.json +8 -8
  45. package/skills/dms-media/SKILL.md +11 -24
  46. package/src/db/models/mediaFolders.model.ts +0 -44
  47. package/src/db/tables/mediaDeclarations.table.ts +3 -19
  48. package/src/declared/assets.ts +19 -210
  49. package/src/declared/folders.ts +22 -135
  50. package/src/declared/index.ts +0 -2
  51. package/src/declared/records.ts +8 -82
  52. package/src/derivatives.ts +7 -17
  53. package/src/implementations/interface-dms-media/index.ts +1 -8
  54. package/src/routes/media/delivery-stream.ts +12 -28
  55. package/src/routes/media/upload.ts +4 -6
  56. package/src/test/antelope.test.ts +4 -4
  57. package/src/test/integration/media-declared-content.test.ts +17 -360
  58. package/src/test/integration/media-linked-subfolders.test.ts +2 -2
  59. package/src/test/integration/media-storage-origin-unreachable.test.ts +110 -0
  60. package/src/test/unit/declared.test.ts +0 -87
  61. package/src/test/unit/library/realtime.test.ts +2 -0
  62. package/src/validation/declared.schema.ts +0 -13
  63. package/dist/declared/readiness.d.ts +0 -2
  64. package/dist/declared/readiness.js +0 -52
  65. package/dist/declared/readiness.js.map +0 -1
  66. package/dist/declared/retire.d.ts +0 -3
  67. package/dist/declared/retire.js +0 -118
  68. package/dist/declared/retire.js.map +0 -1
  69. package/dist/test/integration/media-declared-retire.test.js +0 -186
  70. package/dist/test/integration/media-declared-retire.test.js.map +0 -1
  71. package/src/declared/readiness.ts +0 -64
  72. package/src/declared/retire.ts +0 -206
  73. package/src/test/integration/media-declared-retire.test.ts +0 -257
  74. /package/dist/test/integration/{media-declared-retire.test.d.ts → media-storage-origin-unreachable.test.d.ts} +0 -0
@@ -14,7 +14,6 @@ import {
14
14
  } from "@antelopejs/interface-dms-media";
15
15
  import type { AxiosInstance } from "axios";
16
16
  import { expect } from "chai";
17
- import sharp from "sharp";
18
17
  import {
19
18
  MediaAssetModel,
20
19
  MediaDeclarationModel,
@@ -54,19 +53,6 @@ async function rejectionOf(promise: Promise<unknown>): Promise<Error> {
54
53
  throw new Error("Expected the declaration to be refused");
55
54
  }
56
55
 
57
- function squarePng(size: number): Promise<Buffer> {
58
- return sharp({
59
- create: {
60
- width: size,
61
- height: size,
62
- channels: 3,
63
- background: { r: 240, g: 160, b: 0 },
64
- },
65
- })
66
- .png()
67
- .toBuffer();
68
- }
69
-
70
56
  describe("[integration] media declared content — folders and files shipped as code", () => {
71
57
  let client: AxiosInstance;
72
58
  let tempDir: string;
@@ -144,7 +130,7 @@ describe("[integration] media declared content — folders and files shipped as
144
130
  }).to.deep.equal(before);
145
131
  });
146
132
 
147
- it("isolates the same declaration keys and updates in two tenant instances", async () => {
133
+ it("isolates the same declaration keys in two tenant instances", async () => {
148
134
  const tenantId = "declared-isolation";
149
135
  const schema = Schema.get(TENANT_SCHEMA_NAME);
150
136
  if (!schema) throw new Error("Tenant schema is missing");
@@ -170,26 +156,8 @@ describe("[integration] media declared content — folders and files shipped as
170
156
  const firstFile = await ImportMediaAsset(file);
171
157
  const secondFile = await ImportMediaAsset({ ...file, tenantId });
172
158
  expect(firstFile.id).to.not.equal(secondFile.id);
173
- expect(
174
- (
175
- await EnsureMediaFolder({
176
- ...folder,
177
- tenantId,
178
- name: "Other tenant folder",
179
- })
180
- ).status,
181
- ).to.equal("updated");
182
- expect(
183
- (
184
- await ImportMediaAsset({
185
- ...file,
186
- tenantId,
187
- source: { bytes: Buffer.from("Other tenant content") },
188
- })
189
- ).status,
190
- ).to.equal("updated");
191
- expect((await EnsureMediaFolder(folder)).status).to.equal("unchanged");
192
- expect((await ImportMediaAsset(file)).status).to.equal("unchanged");
159
+ expect((await EnsureMediaFolder(folder)).status).to.equal("kept");
160
+ expect((await ImportMediaAsset(file)).status).to.equal("kept");
193
161
  expect(
194
162
  await GetModel(MediaAssetModel, tenantId).get(firstFile.id as string),
195
163
  ).to.equal(undefined);
@@ -235,89 +203,6 @@ describe("[integration] media declared content — folders and files shipped as
235
203
  }
236
204
  });
237
205
 
238
- it("rejects moving a declared ancestor under its descendant", async () => {
239
- const root = {
240
- tenantId: DEFAULT_TENANT_ID,
241
- key: "declared.cycle-root",
242
- name: "Cycle root",
243
- };
244
- const parent = await EnsureMediaFolder(root);
245
- const child = await EnsureMediaFolder({
246
- tenantId: DEFAULT_TENANT_ID,
247
- key: "declared.cycle-child",
248
- name: "Cycle child",
249
- parent: root.key,
250
- });
251
- const error = await rejectionOf(
252
- EnsureMediaFolder({ ...root, parent: "declared.cycle-child" }),
253
- );
254
- expect(error.message).to.contain("inside itself");
255
- expect((await folders().get(parent.id as string))?.path).to.deep.equal(
256
- [],
257
- );
258
- expect((await folders().get(child.id as string))?.path).to.deep.equal([
259
- parent.id,
260
- ]);
261
- const destination = await EnsureMediaFolder({
262
- tenantId: DEFAULT_TENANT_ID,
263
- key: "declared.move-target",
264
- name: "Move target",
265
- });
266
- expect(
267
- (await EnsureMediaFolder({ ...root, parent: "declared.move-target" }))
268
- .status,
269
- ).to.equal("updated");
270
- expect((await folders().get(child.id as string))?.path).to.deep.equal([
271
- destination.id,
272
- parent.id,
273
- ]);
274
- expect((await EnsureMediaFolder(root)).status).to.equal("updated");
275
- expect(
276
- (await folders().get(parent.id as string))?.parentId ?? undefined,
277
- ).to.equal(undefined);
278
- expect((await folders().get(child.id as string))?.path).to.deep.equal([
279
- parent.id,
280
- ]);
281
- });
282
-
283
- it("preserves a member rename racing a folder refresh", async () => {
284
- const declaration = {
285
- tenantId: DEFAULT_TENANT_ID,
286
- key: "declared.folder-race",
287
- name: "Folder before race",
288
- };
289
- const first = await EnsureMediaFolder(declaration);
290
- // oxlint-disable-next-line typescript/unbound-method -- Rebound with call(this) below and restored in finally.
291
- const original = MediaFolderModel.prototype.get;
292
- let renamed = false;
293
- MediaFolderModel.prototype.get = async function (id) {
294
- const snapshot = await original.call(this, id);
295
- if (id === first.id && !renamed) {
296
- renamed = true;
297
- await client.post(`/api/media/folders/${id}/rename`, {
298
- name: "Member folder name",
299
- });
300
- }
301
- return snapshot;
302
- };
303
- try {
304
- const result = await EnsureMediaFolder({
305
- ...declaration,
306
- name: "Code folder name",
307
- visibility: "public",
308
- });
309
- expect(result).to.deep.equal({ status: "preserved", id: first.id });
310
- const folder = await folders().get(first.id as string);
311
- expect(folder?.name).to.equal("Member folder name");
312
- expect(folder?.visibility).to.equal("private");
313
- expect((await EnsureMediaFolder(declaration)).status).to.equal(
314
- "preserved",
315
- );
316
- } finally {
317
- MediaFolderModel.prototype.get = original;
318
- }
319
- });
320
-
321
206
  it("creates a folder once, however often it is declared", async () => {
322
207
  const declaration = {
323
208
  tenantId: DEFAULT_TENANT_ID,
@@ -327,7 +212,7 @@ describe("[integration] media declared content — folders and files shipped as
327
212
  const first = await EnsureMediaFolder(declaration);
328
213
  const second = await EnsureMediaFolder(declaration);
329
214
  expect(first.status).to.equal("created");
330
- expect(second).to.deep.equal({ status: "unchanged", id: first.id });
215
+ expect(second).to.deep.equal({ status: "kept", id: first.id });
331
216
  const folder = await folders().get(first.id as string);
332
217
  expect(folder?.name).to.equal("Declared procedures");
333
218
  expect(folder?.parentId ?? undefined).to.equal(undefined);
@@ -372,21 +257,7 @@ describe("[integration] media declared content — folders and files shipped as
372
257
  ).to.have.length(1);
373
258
  });
374
259
 
375
- it("refreshes a folder still as declared", async () => {
376
- const result = await EnsureMediaFolder({
377
- tenantId: DEFAULT_TENANT_ID,
378
- key: CHILD_KEY,
379
- name: "Safety rules",
380
- parent: ROOT_KEY,
381
- });
382
- expect(result.status).to.equal("updated");
383
- const folder = await folders().get(result.id as string);
384
- expect(folder?.name).to.equal("Safety rules");
385
- expect(folder?.visibility).to.equal("private");
386
- expect(await readFolderAcl(result.id as string)).to.equal(undefined);
387
- });
388
-
389
- it("leaves a folder a member renamed as they made it", async () => {
260
+ it("never changes a folder declared before, even renamed by a member", async () => {
390
261
  const declared = await EnsureMediaFolder({
391
262
  tenantId: DEFAULT_TENANT_ID,
392
263
  key: "declared.renamed",
@@ -402,31 +273,11 @@ describe("[integration] media declared content — folders and files shipped as
402
273
  key: "declared.renamed",
403
274
  name: "Declared templates v2",
404
275
  });
405
- expect(result).to.deep.equal({ status: "preserved", id: declared.id });
276
+ expect(result).to.deep.equal({ status: "kept", id: declared.id });
406
277
  const folder = await folders().get(declared.id as string);
407
278
  expect(folder?.name).to.equal("Our templates");
408
279
  });
409
280
 
410
- it("does not bring back a folder a member deleted", async () => {
411
- const declared = await EnsureMediaFolder({
412
- tenantId: DEFAULT_TENANT_ID,
413
- key: "declared.deleted",
414
- name: "Declared archive",
415
- });
416
- const removal = await client.delete(`/api/media/folders/${declared.id}`);
417
- expect(removal.status).to.equal(HTTP_OK);
418
- const result = await EnsureMediaFolder({
419
- tenantId: DEFAULT_TENANT_ID,
420
- key: "declared.deleted",
421
- name: "Declared archive",
422
- });
423
- expect(result).to.deep.equal({ status: "deleted" });
424
- const all = await folders().getAll();
425
- expect(all.some((folder) => folder.name === "Declared archive")).to.equal(
426
- false,
427
- );
428
- });
429
-
430
281
  it("refuses an invalid name, an unknown parent and a taken name", async () => {
431
282
  const badName = await rejectionOf(
432
283
  EnsureMediaFolder({
@@ -457,52 +308,6 @@ describe("[integration] media declared content — folders and files shipped as
457
308
  });
458
309
 
459
310
  describe("files", () => {
460
- for (const replaceBytes of [false, true]) {
461
- it(`preserves a member rename racing an asset ${replaceBytes ? "content" : "metadata"} refresh`, async () => {
462
- const declaration = {
463
- tenantId: DEFAULT_TENANT_ID,
464
- key: `declared.asset-race.${replaceBytes}`,
465
- folder: { key: ROOT_KEY },
466
- name: `race-${replaceBytes}.txt`,
467
- mimetype: "text/plain",
468
- source: { bytes: Buffer.from("Original content") },
469
- };
470
- const first = await ImportMediaAsset(declaration);
471
- const before = await assets().get(first.id as string);
472
- // oxlint-disable-next-line typescript/unbound-method -- Rebound with call(this) below and restored in finally.
473
- const original = MediaAssetModel.prototype.get;
474
- let reads = 0;
475
- MediaAssetModel.prototype.get = async function (id) {
476
- const snapshot = await original.call(this, id);
477
- // Race the last snapshot used to publish, not just the initial verdict.
478
- if (id === first.id && ++reads === 2) {
479
- await client.post(`/api/media/assets/${id}/update`, {
480
- name: `member-${replaceBytes}.txt`,
481
- });
482
- }
483
- return snapshot;
484
- };
485
- try {
486
- const result = await ImportMediaAsset({
487
- ...declaration,
488
- name: `code-${replaceBytes}.txt`,
489
- source: replaceBytes
490
- ? { bytes: Buffer.from("Replacement content") }
491
- : declaration.source,
492
- });
493
- expect(result).to.deep.equal({ status: "preserved", id: first.id });
494
- const after = await assets().get(first.id as string);
495
- expect(after?.name).to.equal(`member-${replaceBytes}.txt`);
496
- expect(after?.storageKey).to.equal(before?.storageKey);
497
- expect((await ImportMediaAsset(declaration)).status).to.equal(
498
- "preserved",
499
- );
500
- } finally {
501
- MediaAssetModel.prototype.get = original;
502
- }
503
- });
504
- }
505
-
506
311
  it("imports bytes once, with their dimensions and an upload event", async () => {
507
312
  const declaration = {
508
313
  tenantId: DEFAULT_TENANT_ID,
@@ -516,7 +321,7 @@ describe("[integration] media declared content — folders and files shipped as
516
321
  const first = await ImportMediaAsset(declaration);
517
322
  const second = await ImportMediaAsset(declaration);
518
323
  expect(first.status).to.equal("created");
519
- expect(second).to.deep.equal({ status: "unchanged", id: first.id });
324
+ expect(second).to.deep.equal({ status: "kept", id: first.id });
520
325
  const asset = await assets().get(first.id as string);
521
326
  expect(asset?.name).to.equal("logo.png");
522
327
  expect(asset?.alt).to.equal("Helio logo");
@@ -581,145 +386,6 @@ describe("[integration] media declared content — folders and files shipped as
581
386
  expect(refused.message).to.contain("only accepts image/*");
582
387
  });
583
388
 
584
- it("refreshes the content of a file still as declared", async () => {
585
- const declaration = {
586
- tenantId: DEFAULT_TENANT_ID,
587
- key: "declared.banner",
588
- folder: { key: ROOT_KEY },
589
- name: "banner.png",
590
- mimetype: PNG_MIMETYPE,
591
- };
592
- const first = await ImportMediaAsset({
593
- ...declaration,
594
- source: { bytes: await squarePng(2) },
595
- });
596
- const before = await assets().get(first.id as string);
597
- const second = await ImportMediaAsset({
598
- ...declaration,
599
- source: { bytes: await squarePng(3) },
600
- });
601
- expect(second).to.deep.equal({ status: "updated", id: first.id });
602
- const after = await assets().get(first.id as string);
603
- expect(after?.width).to.equal(3);
604
- expect(after?.storageKey).to.not.equal(before?.storageKey);
605
- const third = await ImportMediaAsset({
606
- ...declaration,
607
- source: { bytes: await squarePng(3) },
608
- });
609
- expect(third.status).to.equal("unchanged");
610
- });
611
-
612
- it("does not read the source while the caller's version is unchanged", async () => {
613
- let loads = 0;
614
- const load = (text: string) => () => {
615
- loads++;
616
- return Buffer.from(text);
617
- };
618
- const declaration = {
619
- tenantId: DEFAULT_TENANT_ID,
620
- key: "declared.generated",
621
- folder: { key: ROOT_KEY },
622
- name: "generated.txt",
623
- mimetype: "text/plain",
624
- };
625
- const first = await ImportMediaAsset({
626
- ...declaration,
627
- version: "v1",
628
- source: { load: load("Generated v1") },
629
- });
630
- expect(first.status).to.equal("created");
631
- expect(loads).to.equal(1);
632
- // Same version: neither the loader nor a file path that does not exist is read.
633
- expect(
634
- await ImportMediaAsset({
635
- ...declaration,
636
- version: "v1",
637
- source: { load: load("never read") },
638
- }),
639
- ).to.deep.equal({ status: "unchanged", id: first.id });
640
- expect(
641
- (
642
- await ImportMediaAsset({
643
- ...declaration,
644
- version: "v1",
645
- source: { filePath: join(tempDir, "never-written.txt") },
646
- })
647
- ).status,
648
- ).to.equal("unchanged");
649
- expect(loads).to.equal(1);
650
- // New metadata under the same version refreshes without reading the bytes.
651
- const renamed = await ImportMediaAsset({
652
- ...declaration,
653
- version: "v1",
654
- name: "generated-renamed.txt",
655
- source: { load: load("never read") },
656
- });
657
- expect(renamed).to.deep.equal({ status: "updated", id: first.id });
658
- expect(loads).to.equal(1);
659
- const updated = await ImportMediaAsset({
660
- ...declaration,
661
- version: "v2",
662
- name: "generated-renamed.txt",
663
- source: { load: load("Generated v2") },
664
- });
665
- expect(updated).to.deep.equal({ status: "updated", id: first.id });
666
- expect(loads).to.equal(2);
667
- const asset = await assets().get(first.id as string);
668
- expect(asset?.size).to.equal("Generated v2".length);
669
- });
670
-
671
- it("reads a new version once, and not again when its bytes did not change", async () => {
672
- let loads = 0;
673
- const declaration = {
674
- tenantId: DEFAULT_TENANT_ID,
675
- key: "declared.rebuilt",
676
- folder: { key: ROOT_KEY },
677
- name: "rebuilt.txt",
678
- mimetype: "text/plain",
679
- source: {
680
- load: async () => {
681
- loads++;
682
- return Buffer.from("Same bytes");
683
- },
684
- },
685
- };
686
- const first = await ImportMediaAsset({ ...declaration, version: "a" });
687
- const rebuilt = await ImportMediaAsset({ ...declaration, version: "b" });
688
- expect(rebuilt).to.deep.equal({ status: "unchanged", id: first.id });
689
- expect(loads).to.equal(2);
690
- expect(
691
- (await ImportMediaAsset({ ...declaration, version: "b" })).status,
692
- ).to.equal("unchanged");
693
- expect(loads).to.equal(2);
694
- });
695
-
696
- it("does not read the source of a file a member changed or deleted", async () => {
697
- let loads = 0;
698
- const declaration = {
699
- tenantId: DEFAULT_TENANT_ID,
700
- key: "declared.untouchable",
701
- folder: { key: ROOT_KEY },
702
- name: "untouchable.txt",
703
- mimetype: "text/plain",
704
- source: {
705
- load: () => {
706
- loads++;
707
- return Buffer.from(`Untouchable ${loads}`);
708
- },
709
- },
710
- };
711
- const first = await ImportMediaAsset(declaration);
712
- await client.post(`/api/media/assets/${first.id}/update`, {
713
- name: "member-owned.txt",
714
- });
715
- expect((await ImportMediaAsset(declaration)).status).to.equal(
716
- "preserved",
717
- );
718
- await client.delete(`/api/media/assets/${first.id}`);
719
- expect((await ImportMediaAsset(declaration)).status).to.equal("deleted");
720
- expect(loads).to.equal(1);
721
- });
722
-
723
389
  it("names the key when a lazy source fails or returns no bytes", async () => {
724
390
  const declaration = {
725
391
  tenantId: DEFAULT_TENANT_ID,
@@ -754,7 +420,7 @@ describe("[integration] media declared content — folders and files shipped as
754
420
  expect(wrong.message).to.contain("must return a Uint8Array");
755
421
  });
756
422
 
757
- it("leaves a file a member renamed as they made it", async () => {
423
+ it("never changes a file declared before, nor reads its source again", async () => {
758
424
  const declaration = {
759
425
  tenantId: DEFAULT_TENANT_ID,
760
426
  key: "declared.manual",
@@ -768,32 +434,23 @@ describe("[integration] media declared content — folders and files shipped as
768
434
  name: "our-manual.txt",
769
435
  });
770
436
  expect(rename.status).to.equal(HTTP_OK);
437
+ let loads = 0;
771
438
  const result = await ImportMediaAsset({
772
439
  ...declaration,
773
- source: { bytes: Buffer.from("Manual v2") },
440
+ source: {
441
+ load: () => {
442
+ loads++;
443
+ return Buffer.from("Manual v2");
444
+ },
445
+ },
774
446
  });
775
- expect(result).to.deep.equal({ status: "preserved", id: first.id });
447
+ expect(result).to.deep.equal({ status: "kept", id: first.id });
448
+ expect(loads, "the source of a kept file is not read").to.equal(0);
776
449
  const asset = await assets().get(first.id as string);
777
450
  expect(asset?.name).to.equal("our-manual.txt");
778
451
  expect(asset?.size).to.equal("Manual v1".length);
779
452
  });
780
453
 
781
- it("does not bring back a file a member deleted", async () => {
782
- const declaration = {
783
- tenantId: DEFAULT_TENANT_ID,
784
- key: "declared.flyer",
785
- folder: { key: ROOT_KEY },
786
- name: "flyer.txt",
787
- mimetype: "text/plain",
788
- source: { bytes: Buffer.from("Flyer") },
789
- };
790
- const first = await ImportMediaAsset(declaration);
791
- const removal = await client.delete(`/api/media/assets/${first.id}`);
792
- expect(removal.status).to.equal(HTTP_OK);
793
- const result = await ImportMediaAsset(declaration);
794
- expect(result).to.deep.equal({ status: "deleted" });
795
- });
796
-
797
454
  it("refuses an unknown folder, a refused type and an unreadable image", async () => {
798
455
  const unknown = await rejectionOf(
799
456
  ImportMediaAsset({
@@ -193,7 +193,7 @@ describe("[integration] media linked subfolders — bindings that allow them", (
193
193
  name: "Project C",
194
194
  parent: { binding: OPEN_BINDING_ID },
195
195
  });
196
- expect(again).to.deep.equal({ status: "unchanged", id: project.id });
196
+ expect(again).to.deep.equal({ status: "kept", id: project.id });
197
197
  const nested = await EnsureMediaFolder({
198
198
  tenantId: DEFAULT_TENANT_ID,
199
199
  key: "subfolders.project-c.roof",
@@ -208,7 +208,7 @@ describe("[integration] media linked subfolders — bindings that allow them", (
208
208
  name: "Roof",
209
209
  parent: "subfolders.project-c",
210
210
  });
211
- expect(sameParent).to.deep.equal({ status: "unchanged", id: nested.id });
211
+ expect(sameParent).to.deep.equal({ status: "kept", id: nested.id });
212
212
  const photo = await ImportMediaAsset({
213
213
  tenantId: DEFAULT_TENANT_ID,
214
214
  key: "subfolders.project-c.roof.photo",
@@ -0,0 +1,110 @@
1
+ import type { AxiosInstance } from "axios";
2
+ import { expect } from "chai";
3
+ import sharp from "sharp";
4
+ import { rawGet, uploadAsset } from "../helpers/assets";
5
+ import { authorizedClient, getBaseUrl } from "../helpers/http";
6
+ import { ensureOwnerSession } from "../helpers/owner";
7
+
8
+ const HTTP_OK = 200;
9
+ const HTTP_FOUND = 302;
10
+ const STORAGE_PATH_PREFIX = "/file-storage/";
11
+ const THUMB_WIDTH = 300;
12
+ const PHOTO_NAME = "wide.png";
13
+ const PHOTO_WIDTH = 640;
14
+ const PHOTO_HEIGHT = 320;
15
+ const SVG_NAME = "badge.svg";
16
+ const SVG_SOURCE =
17
+ '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"><rect width="20" height="10" fill="#0a7"/></svg>';
18
+
19
+ function widePng(): Promise<Buffer> {
20
+ return sharp({
21
+ create: {
22
+ width: PHOTO_WIDTH,
23
+ height: PHOTO_HEIGHT,
24
+ channels: 3,
25
+ background: { r: 0, g: 120, b: 200 },
26
+ },
27
+ })
28
+ .png()
29
+ .toBuffer();
30
+ }
31
+
32
+ function requestUrl(input: Parameters<typeof fetch>[0]): URL {
33
+ if (input instanceof Request) return new URL(input.url);
34
+ return new URL(input);
35
+ }
36
+
37
+ /**
38
+ * A backend behind a private network or an egress policy cannot call the
39
+ * storage's public URL (here the API's own /file-storage routes). Clients still
40
+ * reach it, as a browser does through the dashboard; only the server's fetch
41
+ * is cut, so every server-side read and write must go through the storage
42
+ * interface.
43
+ */
44
+ describe("[integration] media when the server cannot reach the storage's public URL", () => {
45
+ const serverFetch = globalThis.fetch;
46
+ let client: AxiosInstance;
47
+ let token: string;
48
+ let folderId: string;
49
+
50
+ before(async function () {
51
+ this.timeout(40_000);
52
+ const session = await ensureOwnerSession();
53
+ token = session.accessToken;
54
+ client = authorizedClient(token);
55
+ const folder = await client.post("/api/media/folders", {
56
+ name: "Unreachable storage origin",
57
+ });
58
+ expect(folder.status).to.equal(HTTP_OK);
59
+ folderId = folder.data.id;
60
+ globalThis.fetch = (input, init) =>
61
+ requestUrl(input).pathname.startsWith(STORAGE_PATH_PREFIX)
62
+ ? Promise.reject(new TypeError("fetch failed: egress blocked"))
63
+ : serverFetch(input, init);
64
+ });
65
+
66
+ after(async () => {
67
+ globalThis.fetch = serverFetch;
68
+ await client.delete(`/api/media/folders/${folderId}`);
69
+ });
70
+
71
+ it("reads an uploaded image's size at confirmation and renders its thumbnail", async () => {
72
+ const asset = await uploadAsset(
73
+ client,
74
+ folderId,
75
+ PHOTO_NAME,
76
+ "image/png",
77
+ await widePng(),
78
+ );
79
+ expect(asset.width).to.equal(PHOTO_WIDTH);
80
+ expect(asset.height).to.equal(PHOTO_HEIGHT);
81
+
82
+ const thumb = await rawGet(`/media/${asset.id}/thumb/${PHOTO_NAME}`, token);
83
+ expect(thumb.status).to.equal(HTTP_FOUND);
84
+ const stored = await serverFetch(thumb.location as string);
85
+ expect(stored.status).to.equal(HTTP_OK);
86
+ const metadata = await sharp(
87
+ Buffer.from(await stored.arrayBuffer()),
88
+ ).metadata();
89
+ expect(metadata.width).to.equal(THUMB_WIDTH);
90
+ });
91
+
92
+ it("streams an SVG through the API", async () => {
93
+ const asset = await uploadAsset(
94
+ client,
95
+ folderId,
96
+ SVG_NAME,
97
+ "image/svg+xml",
98
+ Buffer.from(SVG_SOURCE),
99
+ );
100
+ const response = await serverFetch(
101
+ `${getBaseUrl()}/media/${asset.id}/${SVG_NAME}`,
102
+ { headers: { Authorization: `Bearer ${token}` }, redirect: "manual" },
103
+ );
104
+ expect(response.status).to.equal(HTTP_OK);
105
+ expect(response.headers.get("content-length")).to.equal(
106
+ String(Buffer.byteLength(SVG_SOURCE)),
107
+ );
108
+ expect(await response.text()).to.equal(SVG_SOURCE);
109
+ });
110
+ });