|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +const scriptPath = fileURLToPath(new URL('../scripts/check-env.mjs', import.meta.url)); |
| 9 | +const requiredEnvNames = [ |
| 10 | + 'MONGODB_URI', |
| 11 | + 'BETTER_AUTH_SECRET', |
| 12 | + 'BETTER_AUTH_URL', |
| 13 | + 'FINNHUB_API_KEYS', |
| 14 | + 'FINNHUB_API_KEY', |
| 15 | + 'NEXT_PUBLIC_FINNHUB_API_KEY', |
| 16 | +]; |
| 17 | +const requiredEnvNamesLowercase = new Set(requiredEnvNames.map((name) => name.toLowerCase())); |
| 18 | +const regularRequiredEnv = { |
| 19 | + MONGODB_URI: 'mongodb://example.test/openstock', |
| 20 | + BETTER_AUTH_SECRET: 'test-secret', |
| 21 | + BETTER_AUTH_URL: 'http://localhost:3000', |
| 22 | +}; |
| 23 | +const finnhubKeyNames = [ |
| 24 | + 'FINNHUB_API_KEYS', |
| 25 | + 'FINNHUB_API_KEY', |
| 26 | + 'NEXT_PUBLIC_FINNHUB_API_KEY', |
| 27 | +] as const; |
| 28 | + |
| 29 | +function clearRequiredEnvVars(env: Record<string, string | undefined>) { |
| 30 | + for (const name of Object.keys(env)) { |
| 31 | + if (requiredEnvNamesLowercase.has(name.toLowerCase())) delete env[name]; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +describe('scripts/check-env.mjs', () => { |
| 36 | + let workingDirectory: string; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + workingDirectory = mkdtempSync(path.join(tmpdir(), 'openstock-check-env-')); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + rmSync(workingDirectory, { recursive: true, force: true }); |
| 44 | + }); |
| 45 | + |
| 46 | + function runChecker(overrides: Record<string, string> = {}) { |
| 47 | + const env = { ...process.env }; |
| 48 | + clearRequiredEnvVars(env); |
| 49 | + Object.assign(env, overrides); |
| 50 | + // Keep dotenv from reading a .env file from the repository or the caller's cwd. |
| 51 | + env.DOTENV_CONFIG_PATH = path.join(workingDirectory, 'no-project-env'); |
| 52 | + |
| 53 | + const result = spawnSync(process.execPath, [scriptPath], { |
| 54 | + cwd: workingDirectory, |
| 55 | + env, |
| 56 | + encoding: 'utf8', |
| 57 | + }); |
| 58 | + |
| 59 | + expect(result.error).toBeUndefined(); |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + it('clears required environment variables regardless of case and keeps unrelated variables', () => { |
| 64 | + // Not ProcessEnv: that type requires NODE_ENV, and this test checks the exact keys left over |
| 65 | + const env: Record<string, string | undefined> = { |
| 66 | + mOnGoDb_UrI: 'inherited-mongodb-uri', |
| 67 | + better_auth_secret: 'inherited-auth-secret', |
| 68 | + Better_Auth_Url: 'inherited-auth-url', |
| 69 | + finnhub_api_keys: 'inherited-finnhub-keys', |
| 70 | + Finnhub_Api_Key: 'inherited-finnhub-key', |
| 71 | + next_public_finnhub_api_key: 'inherited-public-finnhub-key', |
| 72 | + UNRELATED_SETTING: 'keep-me', |
| 73 | + }; |
| 74 | + |
| 75 | + clearRequiredEnvVars(env); |
| 76 | + |
| 77 | + expect(env).toEqual({ UNRELATED_SETTING: 'keep-me' }); |
| 78 | + }); |
| 79 | + |
| 80 | + it.each(finnhubKeyNames)('reports 4/4 when %s is set', (finnhubKeyName) => { |
| 81 | + const result = runChecker({ |
| 82 | + ...regularRequiredEnv, |
| 83 | + [finnhubKeyName]: 'test-finnhub-key', |
| 84 | + }); |
| 85 | + |
| 86 | + expect(result.stdout).toContain('Summary: 4/4 required variables present'); |
| 87 | + expect(result.status).toBe(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it('reports 3/4 and lists Finnhub aliases when all Finnhub keys are missing', () => { |
| 91 | + const result = runChecker(regularRequiredEnv); |
| 92 | + |
| 93 | + expect(result.stdout).toContain('Summary: 3/4 required variables present'); |
| 94 | + expect(result.stdout).toContain('FINNHUB_API_KEYS or FINNHUB_API_KEY or NEXT_PUBLIC_FINNHUB_API_KEY'); |
| 95 | + expect(result.status).toBe(1); |
| 96 | + }); |
| 97 | + |
| 98 | + it('reports 0/4 and lists all missing required checks', () => { |
| 99 | + const result = runChecker(); |
| 100 | + |
| 101 | + expect(result.stdout).toContain('Summary: 0/4 required variables present'); |
| 102 | + expect(result.stdout).toContain('MONGODB_URI'); |
| 103 | + expect(result.stdout).toContain('BETTER_AUTH_SECRET'); |
| 104 | + expect(result.stdout).toContain('BETTER_AUTH_URL'); |
| 105 | + expect(result.stdout).toContain('FINNHUB_API_KEYS or FINNHUB_API_KEY or NEXT_PUBLIC_FINNHUB_API_KEY'); |
| 106 | + expect(result.status).toBe(1); |
| 107 | + }); |
| 108 | +}); |
0 commit comments