|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { detectConfigSchemaKeys } from '../../../../src/core/scan/detectConfigSchemaKeys.js'; |
| 3 | + |
| 4 | +describe('detectConfigSchemaKeys', () => { |
| 5 | + it('extracts schema keys from a Zod .parse(process.env) loader', () => { |
| 6 | + const content = `const envVars = z.object({ |
| 7 | + CLIENT_ID_GITHUB: z.string(), |
| 8 | + CLIENT_SECRET_GITHUB: z.string().optional(), |
| 9 | + }).parse(process.env);`; |
| 10 | + |
| 11 | + expect(detectConfigSchemaKeys(content).sort()).toEqual([ |
| 12 | + 'CLIENT_ID_GITHUB', |
| 13 | + 'CLIENT_SECRET_GITHUB', |
| 14 | + ]); |
| 15 | + }); |
| 16 | + |
| 17 | + it('supports .safeParse(process.env)', () => { |
| 18 | + const content = |
| 19 | + 'schema.safeParse(process.env);\nconst obj = { DATABASE_URL: 1 };'; |
| 20 | + |
| 21 | + expect(detectConfigSchemaKeys(content)).toEqual(['DATABASE_URL']); |
| 22 | + }); |
| 23 | + |
| 24 | + it('supports envalid cleanEnv(process.env, {...})', () => { |
| 25 | + const content = |
| 26 | + 'const env = cleanEnv(process.env, { PORT: port(), API_KEY: str() });'; |
| 27 | + |
| 28 | + expect(detectConfigSchemaKeys(content).sort()).toEqual(['API_KEY', 'PORT']); |
| 29 | + }); |
| 30 | + |
| 31 | + it('supports direct whole-object assignment (= process.env)', () => { |
| 32 | + const content = |
| 33 | + 'const env = process.env;\nconst cfg = { SECRET_TOKEN: env.SECRET_TOKEN };'; |
| 34 | + |
| 35 | + expect(detectConfigSchemaKeys(content)).toEqual(['SECRET_TOKEN']); |
| 36 | + }); |
| 37 | + |
| 38 | + it('supports spread of the whole env object ({ ...process.env })', () => { |
| 39 | + const content = 'const cfg = { ...process.env, EXTRA_FLAG: true };'; |
| 40 | + |
| 41 | + expect(detectConfigSchemaKeys(content)).toEqual(['EXTRA_FLAG']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('recognises quoted keys', () => { |
| 45 | + const content = |
| 46 | + 'z.object({ "CLIENT_SECRET_GITHUB": z.string() }).parse(process.env);'; |
| 47 | + |
| 48 | + expect(detectConfigSchemaKeys(content)).toEqual(['CLIENT_SECRET_GITHUB']); |
| 49 | + }); |
| 50 | + |
| 51 | + it('deduplicates repeated keys', () => { |
| 52 | + const content = |
| 53 | + 'schema.parse(process.env);\nconst a = { API_KEY: 1 };\nconst b = { API_KEY: 2 };'; |
| 54 | + |
| 55 | + expect(detectConfigSchemaKeys(content)).toEqual(['API_KEY']); |
| 56 | + }); |
| 57 | + |
| 58 | + it('ignores non-UPPER_SNAKE keys', () => { |
| 59 | + const content = |
| 60 | + 'z.object({ clientId: z.string(), someKey: z.string() }).parse(process.env);'; |
| 61 | + |
| 62 | + expect(detectConfigSchemaKeys(content)).toEqual([]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns [] when the file only reads a single process.env.X key', () => { |
| 66 | + const content = |
| 67 | + 'const key = process.env.API_KEY;\nconst obj = { OTHER_KEY: 1 };'; |
| 68 | + |
| 69 | + // `process.env.API_KEY` must NOT count as whole-object consumption. |
| 70 | + expect(detectConfigSchemaKeys(content)).toEqual([]); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns [] when the file does not touch process.env at all', () => { |
| 74 | + const content = 'const config = { DATABASE_URL: 1, API_KEY: 2 };'; |
| 75 | + |
| 76 | + expect(detectConfigSchemaKeys(content)).toEqual([]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('returns [] for a whole-env loader with no UPPER_SNAKE object keys', () => { |
| 80 | + const content = 'const env = process.env;\nexport default env;'; |
| 81 | + |
| 82 | + expect(detectConfigSchemaKeys(content)).toEqual([]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('detects the loader across newlines between .parse( and process.env', () => { |
| 86 | + const content = |
| 87 | + 'const cfg = z\n .object({ API_KEY: z.string() })\n .parse(\n process.env,\n );'; |
| 88 | + |
| 89 | + expect(detectConfigSchemaKeys(content)).toEqual(['API_KEY']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('extracts only object keys, not the accessor key, in a mixed file', () => { |
| 93 | + // The file both reads a single process.env.NODE_ENV and defines a schema. |
| 94 | + const content = |
| 95 | + 'const runtime = process.env.NODE_ENV;\nconst cfg = schema.parse(process.env);\nconst obj = { DB_HOST: 1 };'; |
| 96 | + |
| 97 | + // NODE_ENV is an accessor (no `KEY:`), so only the object key DB_HOST is declared. |
| 98 | + expect(detectConfigSchemaKeys(content)).toEqual(['DB_HOST']); |
| 99 | + }); |
| 100 | + |
| 101 | + it('handles keys containing digits (e.g. OAUTH2_TOKEN, S3_BUCKET)', () => { |
| 102 | + const content = |
| 103 | + 'cleanEnv(process.env, { OAUTH2_TOKEN: str(), S3_BUCKET: str() });'; |
| 104 | + |
| 105 | + expect(detectConfigSchemaKeys(content).sort()).toEqual([ |
| 106 | + 'OAUTH2_TOKEN', |
| 107 | + 'S3_BUCKET', |
| 108 | + ]); |
| 109 | + }); |
| 110 | +}); |
0 commit comments