overrides.test.ts84 lines · main
| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | |
| 3 | import { getEnabledFeaturesOverrideDisabledList } from './overrides' |
| 4 | |
| 5 | describe('getEnabledFeaturesOverrideDisabledList', () => { |
| 6 | let warnSpy: ReturnType<typeof vi.spyOn> |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 10 | }) |
| 11 | |
| 12 | afterEach(() => { |
| 13 | warnSpy.mockRestore() |
| 14 | }) |
| 15 | |
| 16 | it('returns empty list when no env vars are set', () => { |
| 17 | expect(getEnabledFeaturesOverrideDisabledList({})).toEqual([]) |
| 18 | }) |
| 19 | |
| 20 | it('disables a feature when its env var is "false"', () => { |
| 21 | expect( |
| 22 | getEnabledFeaturesOverrideDisabledList({ ENABLED_FEATURES_LOGS_TEMPLATES: 'false' }) |
| 23 | ).toEqual(['logs:templates']) |
| 24 | }) |
| 25 | |
| 26 | it('does not disable features whose env var is "true"', () => { |
| 27 | expect( |
| 28 | getEnabledFeaturesOverrideDisabledList({ ENABLED_FEATURES_LOGS_TEMPLATES: 'true' }) |
| 29 | ).toEqual([]) |
| 30 | }) |
| 31 | |
| 32 | it('accepts booleans case-insensitively and trims whitespace', () => { |
| 33 | const result = getEnabledFeaturesOverrideDisabledList({ |
| 34 | ENABLED_FEATURES_LOGS_TEMPLATES: 'FALSE', |
| 35 | ENABLED_FEATURES_LOGS_METADATA: ' False ', |
| 36 | }) |
| 37 | expect(result.sort()).toEqual(['logs:metadata', 'logs:templates']) |
| 38 | }) |
| 39 | |
| 40 | it('maps snake_case and kebab-case keys to the env name convention', () => { |
| 41 | const result = getEnabledFeaturesOverrideDisabledList({ |
| 42 | ENABLED_FEATURES_BRANDING_LARGE_LOGO: 'false', |
| 43 | ENABLED_FEATURES_DOCS_SELF_HOSTING: 'false', |
| 44 | }) |
| 45 | expect(result.sort()).toEqual(['branding:large_logo', 'docs:self-hosting']) |
| 46 | }) |
| 47 | |
| 48 | it('warns and ignores non-boolean values', () => { |
| 49 | expect( |
| 50 | getEnabledFeaturesOverrideDisabledList({ ENABLED_FEATURES_LOGS_TEMPLATES: 'maybe' }) |
| 51 | ).toEqual([]) |
| 52 | expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('must be "true" or "false"')) |
| 53 | }) |
| 54 | |
| 55 | it('warns and ignores env vars prefixed but not matching a known feature', () => { |
| 56 | expect( |
| 57 | getEnabledFeaturesOverrideDisabledList({ ENABLED_FEATURES_NOT_A_FEATURE: 'false' }) |
| 58 | ).toEqual([]) |
| 59 | expect(warnSpy).toHaveBeenCalledWith( |
| 60 | expect.stringContaining('does not match any known feature') |
| 61 | ) |
| 62 | }) |
| 63 | |
| 64 | it('does not warn for reserved ENABLED_FEATURES_OVERRIDE_DISABLE_ALL', () => { |
| 65 | expect( |
| 66 | getEnabledFeaturesOverrideDisabledList({ ENABLED_FEATURES_OVERRIDE_DISABLE_ALL: 'true' }) |
| 67 | ).toEqual([]) |
| 68 | expect(warnSpy).not.toHaveBeenCalled() |
| 69 | }) |
| 70 | |
| 71 | it('ignores env vars outside the prefix without warning', () => { |
| 72 | expect( |
| 73 | getEnabledFeaturesOverrideDisabledList({ NODE_ENV: 'production', PATH: '/usr/bin' }) |
| 74 | ).toEqual([]) |
| 75 | expect(warnSpy).not.toHaveBeenCalled() |
| 76 | }) |
| 77 | |
| 78 | it('treats an empty string value as unset', () => { |
| 79 | expect(getEnabledFeaturesOverrideDisabledList({ ENABLED_FEATURES_LOGS_TEMPLATES: '' })).toEqual( |
| 80 | [] |
| 81 | ) |
| 82 | expect(warnSpy).not.toHaveBeenCalled() |
| 83 | }) |
| 84 | }) |