@adonis-agora/media 0.28.1 → 0.29.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
1
  # @adonis-agora/media
2
2
 
3
+ ## 0.29.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#258](https://github.com/DavideCarvalho/adonis-agora-media/pull/258) [`cb90771`](https://github.com/DavideCarvalho/adonis-agora-media/commit/cb90771a3e30c2e08b6342b51978af124d172c95) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - **`media.fake()` / `media.restore()`** — fake the media service in app tests, like `drive.fake()` and `mail.fake()`.
8
+
9
+ `import media from '@adonis-agora/media/services/main'` captures the `MediaManager` once at boot, so `app.container.swap(MediaManager, …)` never reaches code that already imported it, and apps fell back to monkeypatching `media.library.attach = …`, which leaks between suites. The swap now happens inside the manager:
10
+
11
+ ```ts
12
+ const fake = media.fake()
13
+ await uploadAvatar()
14
+ fake.assertCalled('attach', (input) => input.collection === 'avatar')
15
+ assert.equal(fake.calls.signedUrl[0]?.[1], 300) // TTL
16
+ fake.stub('deliver', async () => ({ kind: 'redirect', url: 'https://cdn.test/x' }))
17
+ media.restore()
18
+ ```
19
+
20
+ - While faked, `media.library` (and `store`, `storage`, `attachments`, `uploads`, `resumable`, `direct`) run the real library code, with the configured collections and delivery settings, over an `InMemoryMediaStore`, an `InMemoryDisk` for every disk name and a `FakeImageProcessor`.
21
+ - The returned `MediaFake` records every `media.library` call with typed arguments (`fake.calls.<method>`), has `assertCalled` / `assertNotCalled`, and `stub(method, fn)` / `unstub(method)` to force a result or an error.
22
+ - `media.fake()` again starts a fresh fake; `media.restore()` is a no-op without one.
23
+ - New exports from `@adonis-agora/media/testing`: `MediaFake`, `MEDIA_FAKE_METHODS`, `lazyInMemoryDiskResolver`, and the types `MediaFakeOptions`, `MediaFakeMethod`, `MediaFakeCall`, `MediaFakeCalls`, `MediaFakeStub`.
24
+
25
+ `MediaManager.storage`, `library`, `attachments`, `store` and `uploads` are now getters instead of readonly fields. Reading them is unchanged; nothing could assign them before (they were `readonly`).
26
+
27
+ ## 0.28.2
28
+
29
+ ### Patch Changes
30
+
31
+ - [#246](https://github.com/DavideCarvalho/adonis-agora-media/pull/246) [`2e21558`](https://github.com/DavideCarvalho/adonis-agora-media/commit/2e215589fab2c7373bd7f34e7f97d807dc587a4f) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - `S3Disk.deleteMany` now works on MinIO versions that reject `DeleteObjects` with `MissingContentMD5`: it falls back to single deletes and remembers.
32
+
3
33
  ## 0.28.1
4
34
 
5
35
  ### Patch Changes
package/README.md CHANGED
@@ -360,6 +360,49 @@ const library = new MediaLibrary({
360
360
  In a real app you can also `drive.fake()` from `@adonisjs/drive` to back the disks with an in-memory
361
361
  fake while keeping the rest of the wiring intact.
362
362
 
363
+ ### Faking the `media` service in app tests
364
+
365
+ `import media from '@adonis-agora/media/services/main'` captures the manager once at boot, so
366
+ `app.container.swap(MediaManager, ...)` never reaches code that already imported it — and
367
+ monkeypatching `media.library.attach = ...` leaks between suites. Use `media.fake()` instead, the
368
+ same idea as `drive.fake()` / `mail.fake()`:
369
+
370
+ ```ts
371
+ import media from '@adonis-agora/media/services/main'
372
+
373
+ test('uploads the avatar', async ({ assert, cleanup }) => {
374
+ const fake = media.fake() // every `media.*` now runs in memory
375
+ cleanup(() => media.restore())
376
+
377
+ await client.post('/avatar').file('avatar', path).loginAs(user)
378
+
379
+ // Real library semantics (collections, whitelist, single-file replace, delivery) over an
380
+ // in-memory store, in-memory disks and a fake image processor:
381
+ const [record] = await media.library.list('User', user.id, 'avatar')
382
+ assert.equal(fake.disk().files.size, 1)
383
+
384
+ // Every `media.library` call is recorded with its typed arguments:
385
+ fake.assertCalled('attach', (input) => input.collection === 'avatar')
386
+ assert.equal(fake.calls.signedUrl[0]?.[1], 300) // the TTL passed to signedUrl
387
+ fake.assertNotCalled('delete')
388
+
389
+ // Force a result or an error the in-memory library would not produce:
390
+ fake.stub('deliver', async () => ({ kind: 'redirect', url: 'https://cdn.test/x' }))
391
+ fake.stub('attach', () => {
392
+ throw new Error('S3 is down')
393
+ })
394
+ })
395
+ ```
396
+
397
+ - `media.fake(options?)` — `{ visibility?: 'private' | 'public' | 'unknown', store?, imageProcessor? }`.
398
+ Keeps the configured collections and delivery settings; every disk name resolves to an
399
+ `InMemoryDisk` (`fake.disk(name)`). Calling it again starts a fresh fake.
400
+ - `fake.calls.<method>` — args tuples per call, for `attach`, `attachExisting`, `list`, `find`,
401
+ `delete`, `url`, `deliver`, `signedUrl`, `ensureConversion`, `transform` (`for(...)` records as
402
+ the method it calls). `fake.assertCalled` / `fake.assertNotCalled` take an optional predicate.
403
+ - `fake.stub(method, fn)` / `fake.unstub(method)` — stubbed calls are still recorded.
404
+ - `media.restore()` puts the real manager back (safe to call without a fake, or twice).
405
+
363
406
  ## Links
364
407
 
365
408
  - Repo: https://github.com/DavideCarvalho/adonis-media