|
| 1 | +import { isEnvVarEnabled } from './env.js'; |
| 2 | +import { ui } from './logging.js'; |
| 3 | + |
| 4 | +describe('isEnvVarEnabled', () => { |
| 5 | + beforeEach(() => { |
| 6 | + vi.unstubAllEnvs(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should consider missing variable disabled', () => { |
| 10 | + expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should consider "true" enabled', () => { |
| 14 | + vi.stubEnv('CP_VERBOSE', 'true'); |
| 15 | + expect(isEnvVarEnabled('CP_VERBOSE')).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should consider "false" disabled', () => { |
| 19 | + vi.stubEnv('CP_VERBOSE', 'false'); |
| 20 | + expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should consider "1" enabled', () => { |
| 24 | + vi.stubEnv('CP_VERBOSE', '1'); |
| 25 | + expect(isEnvVarEnabled('CP_VERBOSE')).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should consider "0" disabled', () => { |
| 29 | + vi.stubEnv('CP_VERBOSE', '0'); |
| 30 | + expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should log a warning for unexpected values', () => { |
| 34 | + vi.stubEnv('CP_VERBOSE', 'unexpected'); |
| 35 | + expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false); |
| 36 | + expect(ui()).toHaveLogged( |
| 37 | + 'warn', |
| 38 | + 'Environment variable CP_VERBOSE expected to be a boolean (true/false/1/0), but received value unexpected. Treating it as disabled.', |
| 39 | + ); |
| 40 | + }); |
| 41 | +}); |
0 commit comments