Skip to content

Commit eaebd75

Browse files
inshu5757ravixalgorithmclaude
authored
fix: correct the environment variable summary count (#170)
* fix: correct environment variable summary count * fix(test): clear required env vars case-insensitively * test(check-env): cast the env literal so tsc passes ProcessEnv requires NODE_ENV, which the test leaves out on purpose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(check-env): type the env literal as a string map so tsc passes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: ravixalgorithm <11aravipratapsingh@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6bd1bff commit eaebd75

2 files changed

Lines changed: 110 additions & 2 deletions

File tree

‎__tests__/check-env.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
});

‎scripts/check-env.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ if (state.optionalPresent.length === 0) {
254254

255255
console.log('\n' + '='.repeat(70));
256256
const criticalMissing = state.missing.filter(m => m.severity === 'critical').length;
257-
const totalRequired = Object.keys(requiredVars).length;
258-
const presentRequired = state.present.length;
257+
const totalRequired = Object.keys(requiredVars).length + 1;
258+
const presentRequired = totalRequired - criticalMissing;
259259

260260
console.log(`Summary: ${presentRequired}/${totalRequired} required variables present`);
261261
console.log(`Optional variables: ${state.optionalPresent.length}/${Object.keys(optionalVars).length} enabled`);

0 commit comments

Comments
 (0)