import { afterEach, describe, expect, test, vi } from 'vitest' import { assertEnv } from '../src/modules/env/assertEnv.js' import { checkDatabaseName, databaseNameFromUri, databaseNamesFor } from '../src/modules/env/databaseName.js' import { iaplEnvRules } from '../src/modules/env/iaplEnvRules.js' const base = { DATABASE_URI: 'mongodb://db/x', NEXT_PUBLIC_SERVER_URL: 'http://localhost:3000', PAYLOAD_SECRET: 'a-long-random-test-secret-of-32-chars' } 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', } function check(env: Record, storage: NonNullable[0]>['storage'] = true) { vi.unstubAllEnvs() for (const [k, v] of Object.entries(env)) {vi.stubEnv(k, v)} return () => assertEnv(iaplEnvRules({ storage }), { env }) } afterEach(() => vi.unstubAllEnvs()) describe('iaplEnvRules({ storage })', () => { test('production: every R2_* is required', () => { expect(check({ ...base, NODE_ENV: 'production' })).toThrow(/R2_BUCKET is required/) expect(check({ ...base, ...r2, NODE_ENV: 'production' })).not.toThrow() }) test('production: R2_PUBLIC_URL is optional by default (media served by the app)', () => { const { R2_PUBLIC_URL: _unused, ...credentials } = r2 expect(check({ ...base, ...credentials, NODE_ENV: 'production' })).not.toThrow() // still validated when set expect(check({ ...base, ...r2, NODE_ENV: 'production', R2_PUBLIC_URL: '/media' })).toThrow(/absolute URL in production/) }) test('requirePublicUrl: true makes R2_PUBLIC_URL required with the credentials', () => { const { R2_PUBLIC_URL: _unused, ...credentials } = r2 expect(check({ ...base, ...credentials, NODE_ENV: 'production' }, { requirePublicUrl: true })).toThrow(/R2_PUBLIC_URL is required/) expect(check({ ...base, ...r2, NODE_ENV: 'production' }, { requirePublicUrl: true })).not.toThrow() expect(check({ ...base, NODE_ENV: 'development' }, { requirePublicUrl: true })).not.toThrow() }) test('development: no R2 at all is fine (local disk)', () => { expect(check({ ...base, NODE_ENV: 'development' })).not.toThrow() }) test('development: R2_PUBLIC_URL alone is the read-only media mode', () => { expect(check({ ...base, NODE_ENV: 'development', R2_PUBLIC_URL: 'https://media.acme.pl' })).not.toThrow() }) test('development: a partial R2 set is an error (all or nothing)', () => { expect(check({ ...base, NODE_ENV: 'development', R2_BUCKET: 'b' })).toThrow(/R2_ACCESS_KEY_ID is required/) }) }) describe('database naming rule', () => { test('names come from the domain without dots', () => { expect(databaseNamesFor('intecion.com')).toEqual({ dev: 'intecioncom_dev', prod: 'intecioncom_prod' }) expect(databaseNamesFor('https://www.architektimprez.pl/')).toEqual({ dev: 'architektimprezpl_dev', prod: 'architektimprezpl_prod' }) }) test('the name is read from Mongo and Postgres URIs', () => { expect(databaseNameFromUri('mongodb://u:p@mongo:27017/intecioncom_prod?authSource=admin')).toBe('intecioncom_prod') expect(databaseNameFromUri('mongodb+srv://u:p@cluster.example.net/intecioncom_dev')).toBe('intecioncom_dev') expect(databaseNameFromUri('postgres://u:p@db:5432/intecioncom_prod')).toBe('intecioncom_prod') expect(databaseNameFromUri('file:./dev.db')).toBeNull() expect(databaseNameFromUri('postgres://u:p@db:5432')).toBeNull() }) test('checkDatabaseName warns on a mismatch and skips local or unknown domains', () => { expect(checkDatabaseName({ databaseUri: 'postgres://db/intecioncom_prod', domain: 'https://intecion.com' })).toBeNull() expect(checkDatabaseName({ databaseUri: 'postgres://db/payload', domain: 'intecion.com' })).toMatch(/expected "intecioncom_prod"/) expect(checkDatabaseName({ databaseUri: 'postgres://db/payload', domain: 'http://localhost:3000' })).toBeNull() expect(checkDatabaseName({ databaseUri: 'postgres://db/payload' })).toBeNull() }) test('iaplEnvRules({ databaseName }) only warns — assertEnv still passes', () => { const warn = vi.fn() const env = { DATABASE_URI: 'mongodb://mongo/payload', NEXT_PUBLIC_SERVER_URL: 'https://intecion.com', PAYLOAD_SECRET: 's' } expect(() => assertEnv(iaplEnvRules({ databaseName: { domain: 'intecion.com', warn } }), { env })).not.toThrow() expect(warn).toHaveBeenCalledWith(expect.stringMatching(/DATABASE_URI: database "payload"[\s\S]*intecioncom_prod/)) warn.mockClear() assertEnv(iaplEnvRules({ databaseName: { domain: 'www.intecion.com', warn } }), { env: { ...env, DATABASE_URI: 'mongodb://mongo/intecioncom_dev' } }) expect(warn).not.toHaveBeenCalled() // true: the domain comes from NEXT_PUBLIC_SERVER_URL in process.env vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'https://architektimprez.pl') assertEnv(iaplEnvRules({ databaseName: { warn } }), { env: { ...env, DATABASE_URI: 'postgres://db/architektimprez_prod' } }) expect(warn).toHaveBeenCalledWith(expect.stringMatching(/expected "architektimprezpl_prod"/)) vi.unstubAllEnvs() }) }) describe('PAYLOAD_SECRET minimum length', () => { const env = { DATABASE_URI: 'mongodb://db/x', NEXT_PUBLIC_SERVER_URL: 'https://acme.pl', PAYLOAD_SECRET: 'short-secret' } afterEach(() => vi.unstubAllEnvs()) test('a short secret fails in production', () => { vi.stubEnv('NODE_ENV', 'production') expect(() => assertEnv(iaplEnvRules(), { env })).toThrow(/PAYLOAD_SECRET must be at least 32 characters in production \(got 12\)/) expect(() => assertEnv(iaplEnvRules(), { env: { ...env, PAYLOAD_SECRET: 'x'.repeat(32) } })).not.toThrow() }) test('a short secret passes outside production', () => { vi.stubEnv('NODE_ENV', 'development') expect(() => assertEnv(iaplEnvRules(), { env })).not.toThrow() }) test('secretMinLength: 0 turns the check off; a custom minimum is used', () => { vi.stubEnv('NODE_ENV', 'production') expect(() => assertEnv(iaplEnvRules({ secretMinLength: 0 }), { env })).not.toThrow() expect(() => assertEnv(iaplEnvRules({ secretMinLength: 64 }), { env: { ...env, PAYLOAD_SECRET: 'x'.repeat(40) } })).toThrow(/at least 64 characters/) expect(() => assertEnv(iaplEnvRules({ secretMinLength: 8 }), { env })).not.toThrow() expect(() => assertEnv(iaplEnvRules({ secretVar: 'MY_SECRET' }), { env: { ...env, MY_SECRET: 'short' } })).toThrow(/MY_SECRET must be at least 32/) }) })