import { afterAll, describe, expect, it } from 'vitest'; import { loadConfig } from '../src/config.ts'; import { migrate } from '../src/db/migrate.ts'; import { closeTestPool, getTestPool } from './helpers/db.ts'; afterAll(closeTestPool); describe('database', () => { it('migrate is idempotent', async () => { const pool = await getTestPool(); await migrate(pool); await migrate(pool); const { rows } = await pool.query(`SELECT to_regclass('public.events') AS t`); expect(rows[0].t).toBe('events'); }); }); describe('config', () => { it('loadConfig rejects missing DATABASE_URL', () => { expect(() => loadConfig({ INGEST_HMAC_SECRET: 'x'.repeat(32) })).toThrow(/DATABASE_URL/); }); it('loadConfig applies defaults', () => { const c = loadConfig({ DATABASE_URL: 'postgres://x', INGEST_HMAC_SECRET: 'x'.repeat(32) }); expect(c.PORT).toBe(3001); expect(c.SFTP_INTERVAL_MIN).toBe(15); expect(c.sftp).toBeNull(); }); }); describe('TRUST_PROXY', () => { const base = { DATABASE_URL: 'postgres://x', INGEST_HMAC_SECRET: 'x'.repeat(32) }; it('accepts true/false', () => { expect(loadConfig({ ...base, TRUST_PROXY: 'true' }).TRUST_PROXY).toBe(true); expect(loadConfig({ ...base, TRUST_PROXY: 'false' }).TRUST_PROXY).toBe(false); }); it('accepts the reverse proxy address(es), so only those may set X-Forwarded-For', () => { expect(loadConfig({ ...base, TRUST_PROXY: '172.16.10.20' }).TRUST_PROXY).toEqual(['172.16.10.20']); expect(loadConfig({ ...base, TRUST_PROXY: '172.16.10.20, 10.0.0.0/8' }).TRUST_PROXY).toEqual(['172.16.10.20', '10.0.0.0/8']); }); });