import type { Config } from 'payload' import { s3Storage } from '@payloadcms/storage-s3' import { afterEach, describe, expect, test, vi } from 'vitest' import { buildR2Storage } from '../src/modules/storage/buildR2Storage.js' const r2 = { R2_ACCESS_KEY_ID: 'k', R2_BUCKET: 'b', R2_ENDPOINT: 'https://acc.r2.cloudflarestorage.com', R2_PUBLIC_URL: 'https://media.acme.pl', R2_SECRET_ACCESS_KEY: 's', } const HANDLER = '@payloadcms/storage-s3/client#S3ClientUploadHandler' function stub(env: Record) { vi.unstubAllEnvs() for (const key of Object.keys(r2)) {vi.stubEnv(key, '')} for (const [k, v] of Object.entries(env)) {vi.stubEnv(k, v)} } const baseConfig = (): Config => ({ collections: [{ slug: 'media', fields: [], upload: true }] }) as unknown as Config /** Admin component paths the import map is generated from. */ function adminDependencies(config: Config): string[] { const deps = (config.admin?.dependencies ?? {}) as Record return Object.values(deps).map((d) => d.path ?? '') } afterEach(() => { vi.unstubAllEnvs() vi.restoreAllMocks() }) describe('buildR2Storage — import map independent of env (1.12.2)', () => { test('no R2 env: storage-s3 registered disabled, its client handler still in admin dependencies', async () => { stub({ NODE_ENV: 'development' }) const config = await buildR2Storage(['media'], { s3Storage })(baseConfig()) expect(adminDependencies(config)).toContain(HANDLER) // still local disk: no adapter, no disableLocalStorage const media = config.collections?.find((c) => c.slug === 'media') expect((media?.upload as { disableLocalStorage?: boolean }).disableLocalStorage).toBeUndefined() }) test('full R2 env: same handler (the import maps match)', async () => { stub({ ...r2, NODE_ENV: 'production' }) const config = await buildR2Storage(['media'], { s3Storage })(baseConfig()) expect(adminDependencies(config)).toContain(HANDLER) }) test('no adapter injected and no R2 env: plain passthrough (local-disk projects)', async () => { stub({ NODE_ENV: 'development' }) const config = await buildR2Storage(['media'])(baseConfig()) expect(adminDependencies(config)).not.toContain(HANDLER) }) }) describe('buildR2Storage — read-only media (R2_PUBLIC_URL only, outside production)', () => { type Hooks = { afterRead: ((a: { doc: Record }) => Record)[] beforeOperation: ((a: { args: { req?: { file?: unknown } }; operation: string }) => unknown)[] } async function mediaHooks(env: Record): Promise { vi.spyOn(console, 'warn').mockImplementation(() => {}) stub(env) const config = await buildR2Storage(['media'], { s3Storage })(baseConfig()) return config.collections?.find((c) => c.slug === 'media')?.hooks as unknown as Hooks | undefined } test('URLs point at the public domain, sizes included', async () => { const hooks = await mediaHooks({ NODE_ENV: 'development', R2_PUBLIC_URL: 'https://media.acme.pl/' }) const doc = hooks!.afterRead[0]({ doc: { filename: 'logo plik.png', sizes: { thumbnail: { filename: 'logo-400x300.png', url: '/api/media/file/logo-400x300.png' } }, thumbnailURL: '/api/media/file/logo-400x300.png', url: '/api/media/file/logo plik.png', }, }) expect(doc.url).toBe('https://media.acme.pl/logo%20plik.png') expect(doc.thumbnailURL).toBe('https://media.acme.pl/logo-400x300.png') expect((doc.sizes as Record).thumbnail.url).toBe('https://media.acme.pl/logo-400x300.png') }) test('uploads and deletes are refused, metadata edits pass', async () => { const hooks = await mediaHooks({ NODE_ENV: 'development', R2_PUBLIC_URL: 'https://media.acme.pl' }) const guard = hooks!.beforeOperation[0] expect(() => guard({ args: { req: { file: {} } }, operation: 'create' })).toThrow(/cannot be uploaded or deleted/) expect(() => guard({ args: { req: {} }, operation: 'delete' })).toThrow(/cannot be uploaded or deleted/) expect(() => guard({ args: { req: {} }, operation: 'update' })).not.toThrow() }) test('not in production, not with credentials, not with a relative URL', async () => { expect(await mediaHooks({ NODE_ENV: 'production', R2_PUBLIC_URL: 'https://media.acme.pl' })).toBeUndefined() expect(await mediaHooks({ NODE_ENV: 'development', R2_PUBLIC_URL: '/media' })).toBeUndefined() const partial = await mediaHooks({ NODE_ENV: 'development', R2_BUCKET: 'b', R2_PUBLIC_URL: 'https://media.acme.pl' }) expect(partial).toBeUndefined() }) })