import { describe, expect, it } from 'bun:test'; import { buildLocalDevConfig, LOCAL_DEV_SECRET } from './dev-env'; describe('buildLocalDevConfig', () => { it('applies local-first defaults for the dev bootstrapper', () => { const config = buildLocalDevConfig({}); expect(config.bindHost).toBe('127.0.0.1'); expect(config.port).toBe('3000'); expect(config.publicOrigin).toBe('http://localhost:3000'); expect(config.env.BETTER_AUTH_BASE_URL).toBe('http://localhost:3000'); expect(config.env.BETTER_AUTH_TRUSTED_ORIGINS).toBe('http://localhost:3000,http://127.0.0.1:3000'); expect(config.env.BETTER_AUTH_SECRET).toBe(LOCAL_DEV_SECRET); expect(config.env.DATABASE_URL).toBe('file:data/fiscal.sqlite'); expect(config.env.NEXT_PUBLIC_API_URL).toBe(''); expect(config.env.WORKFLOW_TARGET_WORLD).toBe('local'); expect(config.env.WORKFLOW_LOCAL_DATA_DIR).toBe('.workflow-data'); expect(config.overrides.secretFallbackUsed).toBe(true); }); it('preserves a custom local sqlite path and merges trusted origins', () => { const config = buildLocalDevConfig({ BETTER_AUTH_SECRET: 'real-secret', BETTER_AUTH_TRUSTED_ORIGINS: 'https://fiscal.b11studio.xyz,http://localhost:3000', DATABASE_URL: 'file:data/dev.sqlite', WORKFLOW_TARGET_WORLD: 'local' }); expect(config.env.BETTER_AUTH_SECRET).toBe('real-secret'); expect(config.env.BETTER_AUTH_TRUSTED_ORIGINS) .toBe('http://localhost:3000,http://127.0.0.1:3000,https://fiscal.b11studio.xyz'); expect(config.env.DATABASE_URL).toBe('file:data/dev.sqlite'); expect(config.overrides.databaseChanged).toBe(false); expect(config.overrides.workflowChanged).toBe(false); }); it('trusts both localhost and 127.0.0.1 for loopback public origins', () => { const config = buildLocalDevConfig({ DEV_PUBLIC_HOST: '127.0.0.1', PORT: '3412' }); expect(config.publicOrigin).toBe('http://127.0.0.1:3412'); expect(config.env.BETTER_AUTH_TRUSTED_ORIGINS).toBe('http://127.0.0.1:3412,http://localhost:3412'); }); it('respects an explicit public origin override', () => { const config = buildLocalDevConfig({ DEV_PUBLIC_ORIGIN: 'https://local.fiscal.test:4444/', PORT: '4000' }); expect(config.port).toBe('4000'); expect(config.publicOrigin).toBe('https://local.fiscal.test:4444'); expect(config.env.BETTER_AUTH_BASE_URL).toBe('https://local.fiscal.test:4444'); expect(config.env.BETTER_AUTH_TRUSTED_ORIGINS).toBe('https://local.fiscal.test:4444'); }); it('falls back from deployment-shaped env values to local-safe ones', () => { const config = buildLocalDevConfig({ BETTER_AUTH_BASE_URL: 'https://fiscal.b11studio.xyz', BETTER_AUTH_SECRET: 'replace-with-a-long-random-secret', DATABASE_URL: 'file:/app/data/fiscal.sqlite', NEXT_PUBLIC_API_URL: 'https://fiscal.b11studio.xyz', WORKFLOW_POSTGRES_URL: 'postgres://workflow:workflow@workflow-postgres:5432/workflow', WORKFLOW_TARGET_WORLD: '@workflow/world-postgres' }); expect(config.env.BETTER_AUTH_BASE_URL).toBe('http://localhost:3000'); expect(config.env.BETTER_AUTH_SECRET).toBe(LOCAL_DEV_SECRET); expect(config.env.DATABASE_URL).toBe('file:data/fiscal.sqlite'); expect(config.env.NEXT_PUBLIC_API_URL).toBe(''); expect(config.env.WORKFLOW_TARGET_WORLD).toBe('local'); expect(config.overrides.apiBaseChanged).toBe(true); expect(config.overrides.authOriginChanged).toBe(true); expect(config.overrides.databaseChanged).toBe(true); expect(config.overrides.workflowChanged).toBe(true); }); });