test-preload.ts29 lines · main
| 1 | /** |
| 2 | * Bun test preload (wired via bunfig.toml `[test].preload`). Runs ONCE before |
| 3 | * any test module is imported. |
| 4 | * |
| 5 | * Why this exists: `src/env.ts` parses `process.env` exactly once at module |
| 6 | * load (`export const env = loadEnv(envSchema)`) and the result is frozen for |
| 7 | * the process lifetime. Individual test files that set |
| 8 | * `process.env.BRIVEN_BETTER_AUTH_SECRET` at their top only work when that file |
| 9 | * runs alone — in a full `bun test` run the FIRST file to import env.ts |
| 10 | * (transitively, via almost anything) freezes the secret to whatever was set |
| 11 | * at that moment. If that first file didn't set it, `env.BRIVEN_BETTER_AUTH_SECRET` |
| 12 | * is `undefined` for the WHOLE run, and S2.8's fail-closed `secretBytes()` |
| 13 | * correctly throws — breaking every cli-jwt / Bearer-auth test downstream. |
| 14 | * |
| 15 | * Setting it here, before any module loads, mirrors production (the secret is |
| 16 | * always present at boot) so env.ts captures it regardless of file order. We |
| 17 | * use `??=` so a real secret provided by CI/the environment is never |
| 18 | * overwritten. |
| 19 | */ |
| 20 | process.env.BRIVEN_BETTER_AUTH_SECRET ??= 'test-better-auth-secret-0123456789abcdef'; |
| 21 | |
| 22 | // Same freeze problem for the control-plane DB URL: `src/db/client.ts` getDb() |
| 23 | // reads `env.BRIVEN_DATABASE_URL` and throws "not configured" when empty. Route |
| 24 | // tests (auth-cli, me, projects) set a deliberately-unreachable URL at file-top |
| 25 | // so getDb() constructs (and the route returns 200-or-500 on the dead socket), |
| 26 | // but that only works for whichever file imports env.ts first. Set it here so |
| 27 | // the value is captured regardless of order. Port 5 is intentionally dead — no |
| 28 | // test should ever actually reach a database through this URL. |
| 29 | process.env.BRIVEN_DATABASE_URL ??= 'postgres://test:test@127.0.0.1:5/test'; |