UNPKG

@adonis-agora/media

Version:

Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a

152 lines • 6.05 kB
import { AssertionError } from 'node:assert'; import { InMemoryDisk } from './in_memory_disk.js'; /** * The {@link MediaLibrary} methods a {@link MediaFake} records and lets you {@link MediaFake.stub}. * `for(...)` is not listed: its binding calls `attach`/`attachExisting`/`list` through the fake, * so those are what get recorded. */ export const MEDIA_FAKE_METHODS = [ 'attach', 'attachExisting', 'list', 'page', 'rename', 'find', 'delete', 'url', 'deliver', 'signedUrl', 'ensureConversion', 'transform', ]; /** * In-memory disks created on demand for ANY name, so every disk a config or collection names * resolves without listing them up front. `undefined` resolves to `defaultDisk`. */ export function lazyInMemoryDiskResolver(defaultDisk, visibility = 'private') { const disks = {}; const resolve = (name) => { const key = name ?? defaultDisk; disks[key] ??= new InMemoryDisk(`memory://${key}`, visibility); return disks[key]; }; return { resolve, disks }; } function isFakeMethod(prop) { return typeof prop === 'string' && MEDIA_FAKE_METHODS.includes(prop); } /** * The handle `media.fake()` returns. While it is active, `media.library` (and `media.store`, * `media.storage`, `media.attachments`, …) run on real library code over in-memory doubles, and * every library call is recorded here. * * ```ts * const fake = media.fake() * await someCodeThatUploads() * fake.assertCalled('attach', (input) => input.collection === 'avatar') * assert.equal(fake.calls.signedUrl[0]?.[1], 300) // TTL * fake.stub('deliver', async () => ({ kind: 'redirect', url: 'https://x.test' })) * media.restore() * ``` * * Built by `MediaManager.fake()`; not meant to be constructed directly. */ export class MediaFake { /** Recorded calls per method — the args tuple of each call, in order. Stubbed calls included. */ calls; /** The faked library — the very object `media.library` returns while this fake is active. */ library; /** The store backing the fake (an `InMemoryMediaStore` unless one was passed). */ store; /** In-memory disks created so far, by name (every name resolves; this lists the ones touched). */ disks; #resolve; #stubs = new Map(); #onRestore; constructor(input) { this.store = input.store; this.disks = input.disks; this.#resolve = input.resolve; this.#onRestore = input.onRestore; this.calls = Object.fromEntries(MEDIA_FAKE_METHODS.map((method) => [method, []])); this.library = this.#record(input.library); } /** The in-memory disk behind `name` (default disk when omitted) — inspect `.files` for bytes. */ disk(name) { return this.#resolve(name); } /** * Replace one library method for the rest of this fake (calls are still recorded). Use it to * force a result or an error the in-memory library would not produce on its own. */ stub(method, implementation) { this.#stubs.set(method, implementation); return this; } /** Drop a {@link stub}, so the method runs the real in-memory library again. */ unstub(method) { this.#stubs.delete(method); return this; } /** Throws unless `method` was called (and, with `predicate`, called with matching args). */ assertCalled(method, predicate) { if (this.#matching(method, predicate).length > 0) return; throw new AssertionError({ message: predicate ? `Expected media.library.${method} to be called with matching arguments` : `Expected media.library.${method} to be called`, actual: this.calls[method], }); } /** Throws if `method` was called (or, with `predicate`, called with matching args). */ assertNotCalled(method, predicate) { const matching = this.#matching(method, predicate); if (matching.length === 0) return; throw new AssertionError({ message: predicate ? `Expected media.library.${method} not to be called with matching arguments` : `Expected media.library.${method} not to be called`, actual: matching, }); } /** Same as `media.restore()`, but a no-op once a newer fake has replaced this one. */ restore() { this.#onRestore(); } #matching(method, predicate) { const calls = this.calls[method]; return predicate ? calls.filter((args) => predicate(...args)) : calls; } /** * Wrap the library so the recorded methods log their args and honour stubs. A Proxy (rather than * a subclass) keeps the library's internal `this.x()` calls on the real object, so only calls * made from outside — the app's — are recorded. `for()` runs against the proxy so the binding's * `attach`/`list` are recorded too. */ #record(library) { const proxy = new Proxy(library, { get: (target, prop) => { if (prop === 'for') { return (ownerType, ownerId) => target.for.call(proxy, ownerType, ownerId); } if (!isFakeMethod(prop)) { // Bound to the real library: its methods read private fields, which a Proxy `this` lacks // (e.g. `media.access.image` calls the symbol-keyed `imageOfRecord`). const value = Reflect.get(target, prop, target); return typeof value === 'function' ? value.bind(target) : value; } return (...args) => { this.calls[prop].push(args); const stub = this.#stubs.get(prop); if (stub) return (async () => stub(...args))(); return target[prop].apply(target, args); }; }, }); return proxy; } } //# sourceMappingURL=media_fake.js.map