|
| 1 | +/** |
| 2 | + * Unit tests for environment constants module. |
| 3 | + * |
| 4 | + * Purpose: |
| 5 | + * Tests environment variable re-exports and the ENV proxy behavior. |
| 6 | + * |
| 7 | + * Test Coverage: |
| 8 | + * - Environment variable re-exports |
| 9 | + * - ENV proxy behavior in VITEST mode |
| 10 | + * - processEnv export |
| 11 | + * - Build metadata getters |
| 12 | + * |
| 13 | + * Related Files: |
| 14 | + * - src/constants/env.mts (implementation) |
| 15 | + */ |
| 16 | + |
| 17 | +import process from 'node:process' |
| 18 | + |
| 19 | +import { describe, expect, it } from 'vitest' |
| 20 | + |
| 21 | +import { |
| 22 | + CI, |
| 23 | + ENV, |
| 24 | + getCdxgenVersion, |
| 25 | + getCliHomepage, |
| 26 | + getCliName, |
| 27 | + getCliVersion, |
| 28 | + getCliVersionHash, |
| 29 | + getCoanaVersion, |
| 30 | + getPyCliVersion, |
| 31 | + getPythonBuildTag, |
| 32 | + getPythonVersion, |
| 33 | + getSocketPatchVersion, |
| 34 | + getSynpVersion, |
| 35 | + HOME, |
| 36 | + isPublishedBuild, |
| 37 | + isSentryBuild, |
| 38 | + processEnv, |
| 39 | + SOCKET_CLI_DEBUG, |
| 40 | + VITEST, |
| 41 | +} from '../../../src/constants/env.mts' |
| 42 | + |
| 43 | +describe('constants/env', () => { |
| 44 | + describe('environment variable re-exports', () => { |
| 45 | + it('exports CI', () => { |
| 46 | + // CI should be defined (boolean or undefined). |
| 47 | + expect(typeof CI === 'boolean' || CI === undefined).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + it('exports HOME', () => { |
| 51 | + // HOME should be a string or undefined. |
| 52 | + expect(typeof HOME === 'string' || HOME === undefined).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + it('exports VITEST', () => { |
| 56 | + // VITEST should be true in test environment. |
| 57 | + expect(VITEST).toBe(true) |
| 58 | + }) |
| 59 | + |
| 60 | + it('exports SOCKET_CLI_DEBUG', () => { |
| 61 | + // SOCKET_CLI_DEBUG should be a boolean or undefined. |
| 62 | + expect( |
| 63 | + typeof SOCKET_CLI_DEBUG === 'boolean' || SOCKET_CLI_DEBUG === undefined, |
| 64 | + ).toBe(true) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('processEnv export', () => { |
| 69 | + it('exports process.env reference', () => { |
| 70 | + expect(processEnv).toBe(process.env) |
| 71 | + }) |
| 72 | + |
| 73 | + it('allows reading environment variables', () => { |
| 74 | + // PATH should exist in process.env. |
| 75 | + expect(typeof processEnv['PATH']).toBe('string') |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + describe('build metadata getters', () => { |
| 80 | + it('getCdxgenVersion returns a string', () => { |
| 81 | + const version = getCdxgenVersion() |
| 82 | + expect(typeof version).toBe('string') |
| 83 | + }) |
| 84 | + |
| 85 | + it('getCliHomepage returns a string', () => { |
| 86 | + const homepage = getCliHomepage() |
| 87 | + expect(typeof homepage).toBe('string') |
| 88 | + }) |
| 89 | + |
| 90 | + it('getCliName returns a string', () => { |
| 91 | + const name = getCliName() |
| 92 | + expect(typeof name).toBe('string') |
| 93 | + expect(name.length).toBeGreaterThan(0) |
| 94 | + }) |
| 95 | + |
| 96 | + it('getCliVersion returns a string', () => { |
| 97 | + const version = getCliVersion() |
| 98 | + expect(typeof version).toBe('string') |
| 99 | + }) |
| 100 | + |
| 101 | + it('getCliVersionHash returns a string', () => { |
| 102 | + const hash = getCliVersionHash() |
| 103 | + expect(typeof hash).toBe('string') |
| 104 | + }) |
| 105 | + |
| 106 | + it('getCoanaVersion returns a string', () => { |
| 107 | + const version = getCoanaVersion() |
| 108 | + expect(typeof version).toBe('string') |
| 109 | + }) |
| 110 | + |
| 111 | + it('getPyCliVersion returns a string', () => { |
| 112 | + const version = getPyCliVersion() |
| 113 | + expect(typeof version).toBe('string') |
| 114 | + }) |
| 115 | + |
| 116 | + it('getPythonBuildTag returns a string', () => { |
| 117 | + const tag = getPythonBuildTag() |
| 118 | + expect(typeof tag).toBe('string') |
| 119 | + }) |
| 120 | + |
| 121 | + it('getPythonVersion returns a string', () => { |
| 122 | + const version = getPythonVersion() |
| 123 | + expect(typeof version).toBe('string') |
| 124 | + }) |
| 125 | + |
| 126 | + it('getSocketPatchVersion returns a string', () => { |
| 127 | + const version = getSocketPatchVersion() |
| 128 | + expect(typeof version).toBe('string') |
| 129 | + }) |
| 130 | + |
| 131 | + it('getSynpVersion returns a string', () => { |
| 132 | + const version = getSynpVersion() |
| 133 | + expect(typeof version).toBe('string') |
| 134 | + }) |
| 135 | + |
| 136 | + it('isPublishedBuild returns a boolean', () => { |
| 137 | + expect(typeof isPublishedBuild()).toBe('boolean') |
| 138 | + }) |
| 139 | + |
| 140 | + it('isSentryBuild returns a boolean', () => { |
| 141 | + expect(typeof isSentryBuild()).toBe('boolean') |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + describe('ENV proxy', () => { |
| 146 | + it('is an object', () => { |
| 147 | + expect(typeof ENV).toBe('object') |
| 148 | + }) |
| 149 | + |
| 150 | + it('provides access to VITEST', () => { |
| 151 | + // In test env, VITEST comes from process.env and is a string 'true'. |
| 152 | + expect(ENV.VITEST).toBeTruthy() |
| 153 | + }) |
| 154 | + |
| 155 | + it('allows reading env variables via get trap', () => { |
| 156 | + // In VITEST mode, the proxy should read from process.env. |
| 157 | + const pathValue = ENV['PATH' as keyof typeof ENV] |
| 158 | + expect(typeof pathValue).toBe('string') |
| 159 | + }) |
| 160 | + |
| 161 | + it('allows checking property existence via has trap', () => { |
| 162 | + expect('VITEST' in ENV).toBe(true) |
| 163 | + expect('HOME' in ENV).toBe(true) |
| 164 | + }) |
| 165 | + |
| 166 | + it('returns own keys via ownKeys trap', () => { |
| 167 | + const keys = Object.keys(ENV) |
| 168 | + expect(Array.isArray(keys)).toBe(true) |
| 169 | + expect(keys.length).toBeGreaterThan(0) |
| 170 | + }) |
| 171 | + |
| 172 | + it('returns property descriptors via getOwnPropertyDescriptor trap', () => { |
| 173 | + const descriptor = Object.getOwnPropertyDescriptor(ENV, 'VITEST') |
| 174 | + expect(descriptor).toBeDefined() |
| 175 | + // In test env, VITEST comes from process.env and is a string 'true'. |
| 176 | + expect(descriptor?.value).toBeTruthy() |
| 177 | + }) |
| 178 | + |
| 179 | + it('allows setting values in VITEST mode via set trap', () => { |
| 180 | + const testKey = 'TEST_ENV_VAR_FOR_TESTING' |
| 181 | + const originalValue = process.env[testKey] |
| 182 | + |
| 183 | + // Set value via ENV proxy. |
| 184 | + ;(ENV as any)[testKey] = 'test-value' |
| 185 | + |
| 186 | + // Verify it was set in process.env. |
| 187 | + expect(process.env[testKey]).toBe('test-value') |
| 188 | + |
| 189 | + // Clean up. |
| 190 | + if (originalValue === undefined) { |
| 191 | + delete process.env[testKey] |
| 192 | + } else { |
| 193 | + process.env[testKey] = originalValue |
| 194 | + } |
| 195 | + }) |
| 196 | + |
| 197 | + it('includes INLINED_* properties from snapshot', () => { |
| 198 | + const keys = Object.keys(ENV) |
| 199 | + const inlinedKeys = keys.filter(k => k.startsWith('INLINED_')) |
| 200 | + // Should have some inlined keys from the build metadata. |
| 201 | + expect(inlinedKeys.length).toBeGreaterThan(0) |
| 202 | + }) |
| 203 | + |
| 204 | + it('provides access to INLINED_SOCKET_CLI_NAME', () => { |
| 205 | + const name = ENV.INLINED_SOCKET_CLI_NAME |
| 206 | + expect(typeof name).toBe('string') |
| 207 | + expect(name.length).toBeGreaterThan(0) |
| 208 | + }) |
| 209 | + |
| 210 | + it('provides access to INLINED_SOCKET_CLI_VERSION', () => { |
| 211 | + const version = ENV.INLINED_SOCKET_CLI_VERSION |
| 212 | + expect(typeof version).toBe('string') |
| 213 | + }) |
| 214 | + }) |
| 215 | +}) |
0 commit comments