Skip to content

Commit f5519a8

Browse files
committed
refactor(env): separate test-only env helpers
1 parent 9152f91 commit f5519a8

File tree

16 files changed

+303
-221
lines changed

16 files changed

+303
-221
lines changed

cli/src/__tests__/utils/env.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, test, expect, afterEach } from 'bun:test'
22

3-
import { getCliEnv, createTestCliEnv } from '../../utils/env'
3+
import { createTestCliEnv } from '../../testing/env'
4+
import { getCliEnv } from '../../utils/env'
45

56
describe('cli/utils/env', () => {
67
describe('getCliEnv', () => {

cli/src/testing/env.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Test-only CLI env fixtures.
3+
*/
4+
5+
import { createTestBaseEnv } from '@codebuff/common/testing/env-process'
6+
7+
import type { CliEnv } from '../types/env'
8+
9+
/**
10+
* Create a test CliEnv with optional overrides.
11+
* Composes from createTestBaseEnv for DRY.
12+
*/
13+
export const createTestCliEnv = (overrides: Partial<CliEnv> = {}): CliEnv => ({
14+
...createTestBaseEnv(),
15+
16+
// CLI-specific defaults
17+
KITTY_WINDOW_ID: undefined,
18+
SIXEL_SUPPORT: undefined,
19+
ZED_NODE_ENV: undefined,
20+
ZED_TERM: undefined,
21+
ZED_SHELL: undefined,
22+
COLORTERM: undefined,
23+
VSCODE_THEME_KIND: undefined,
24+
VSCODE_COLOR_THEME_KIND: undefined,
25+
VSCODE_GIT_IPC_HANDLE: undefined,
26+
VSCODE_PID: undefined,
27+
VSCODE_CWD: undefined,
28+
VSCODE_NLS_CONFIG: undefined,
29+
CURSOR_PORT: undefined,
30+
CURSOR: undefined,
31+
JETBRAINS_REMOTE_RUN: undefined,
32+
IDEA_INITIAL_DIRECTORY: undefined,
33+
IDE_CONFIG_DIR: undefined,
34+
JB_IDE_CONFIG_DIR: undefined,
35+
VISUAL: undefined,
36+
EDITOR: undefined,
37+
CODEBUFF_CLI_EDITOR: undefined,
38+
CODEBUFF_EDITOR: undefined,
39+
OPEN_TUI_THEME: undefined,
40+
OPENTUI_THEME: undefined,
41+
CODEBUFF_IS_BINARY: undefined,
42+
CODEBUFF_CLI_VERSION: undefined,
43+
CODEBUFF_CLI_TARGET: undefined,
44+
CODEBUFF_RG_PATH: undefined,
45+
CODEBUFF_SCROLL_MULTIPLIER: undefined,
46+
...overrides,
47+
})
48+

cli/src/utils/env.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* process env with CLI-specific vars for terminal/IDE detection.
66
*/
77

8-
import { getBaseEnv, createTestBaseEnv } from '@codebuff/common/env-process'
8+
import { getBaseEnv } from '@codebuff/common/env-process'
99

1010
import type { CliEnv } from '../types/env'
1111

@@ -66,43 +66,3 @@ export const getCliEnv = (): CliEnv => ({
6666
* or when you need to set environment variables at runtime.
6767
*/
6868
export const getSystemProcessEnv = (): NodeJS.ProcessEnv => process.env
69-
70-
/**
71-
* Create a test CliEnv with optional overrides.
72-
* Composes from createTestBaseEnv() for DRY.
73-
*/
74-
export const createTestCliEnv = (overrides: Partial<CliEnv> = {}): CliEnv => ({
75-
...createTestBaseEnv(),
76-
77-
// CLI-specific defaults
78-
KITTY_WINDOW_ID: undefined,
79-
SIXEL_SUPPORT: undefined,
80-
ZED_NODE_ENV: undefined,
81-
ZED_TERM: undefined,
82-
ZED_SHELL: undefined,
83-
COLORTERM: undefined,
84-
VSCODE_THEME_KIND: undefined,
85-
VSCODE_COLOR_THEME_KIND: undefined,
86-
VSCODE_GIT_IPC_HANDLE: undefined,
87-
VSCODE_PID: undefined,
88-
VSCODE_CWD: undefined,
89-
VSCODE_NLS_CONFIG: undefined,
90-
CURSOR_PORT: undefined,
91-
CURSOR: undefined,
92-
JETBRAINS_REMOTE_RUN: undefined,
93-
IDEA_INITIAL_DIRECTORY: undefined,
94-
IDE_CONFIG_DIR: undefined,
95-
JB_IDE_CONFIG_DIR: undefined,
96-
VISUAL: undefined,
97-
EDITOR: undefined,
98-
CODEBUFF_CLI_EDITOR: undefined,
99-
CODEBUFF_EDITOR: undefined,
100-
OPEN_TUI_THEME: undefined,
101-
OPENTUI_THEME: undefined,
102-
CODEBUFF_IS_BINARY: undefined,
103-
CODEBUFF_CLI_VERSION: undefined,
104-
CODEBUFF_CLI_TARGET: undefined,
105-
CODEBUFF_RG_PATH: undefined,
106-
CODEBUFF_SCROLL_MULTIPLIER: undefined,
107-
...overrides,
108-
})

common/src/__tests__/env-ci.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, test, expect, afterEach } from 'bun:test'
22

3-
import { getCiEnv, ciEnv, isCI, createTestCiEnv } from '../env-ci'
3+
import { getCiEnv, ciEnv, isCI } from '../env-ci'
4+
import { createTestCiEnv } from '../testing/env-ci'
45

56
describe('env-ci', () => {
67
describe('getCiEnv', () => {

common/src/__tests__/env-process.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { describe, test, expect, beforeEach, afterEach } from 'bun:test'
1+
import { describe, test, expect, afterEach } from 'bun:test'
22

3-
import {
4-
getProcessEnv,
5-
processEnv,
6-
createTestProcessEnv,
7-
} from '../env-process'
3+
import { getProcessEnv, processEnv } from '../env-process'
4+
import { createTestProcessEnv } from '../testing/env-process'
85

96
describe('env-process', () => {
107
describe('getProcessEnv', () => {

common/src/env-ci.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,3 @@ export const isCI = (): boolean => {
3333
const env = getCiEnv()
3434
return env.CI === 'true' || env.CI === '1' || env.GITHUB_ACTIONS === 'true'
3535
}
36-
37-
/**
38-
* Create a test CiEnv with optional overrides.
39-
*/
40-
export const createTestCiEnv = (overrides: Partial<CiEnv> = {}): CiEnv => ({
41-
CI: undefined,
42-
GITHUB_ACTIONS: undefined,
43-
RENDER: undefined,
44-
IS_PULL_REQUEST: undefined,
45-
CODEBUFF_GITHUB_TOKEN: undefined,
46-
CODEBUFF_API_KEY: 'test-api-key',
47-
...overrides,
48-
})

common/src/env-process.ts

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* environment variables like SHELL, HOME, TERM, etc.
77
*
88
* Usage:
9-
* - Import `getBaseProcessEnv` for base OS-level vars only
9+
* - Import `getBaseEnv` for base OS-level vars only
1010
* - Import `getProcessEnv` for the full ProcessEnv (base + extensions)
11-
* - In tests, use `createTestBaseProcessEnv` or `createTestProcessEnv`
1211
*/
1312

1413
import type { BaseEnv, ProcessEnv } from './types/contracts/env'
@@ -34,30 +33,6 @@ export const getBaseEnv = (): BaseEnv => ({
3433
PATH: process.env.PATH,
3534
})
3635

37-
/**
38-
* Create test defaults for BaseEnv.
39-
* Package-specific test helpers should spread this.
40-
*/
41-
export const createTestBaseEnv = (
42-
overrides: Partial<BaseEnv> = {},
43-
): BaseEnv => ({
44-
SHELL: undefined,
45-
COMSPEC: undefined,
46-
HOME: '/home/test',
47-
USERPROFILE: undefined,
48-
APPDATA: undefined,
49-
XDG_CONFIG_HOME: undefined,
50-
TERM: 'xterm-256color',
51-
TERM_PROGRAM: undefined,
52-
TERM_BACKGROUND: undefined,
53-
TERMINAL_EMULATOR: undefined,
54-
COLORFGBG: undefined,
55-
NODE_ENV: 'test',
56-
NODE_PATH: undefined,
57-
PATH: '/usr/bin',
58-
...overrides,
59-
})
60-
6136
/**
6237
* Get full process environment values (base + all extensions).
6338
* Returns a snapshot of the current process.env values for the ProcessEnv type.
@@ -117,60 +92,3 @@ export const getProcessEnv = (): ProcessEnv => ({
11792
* Use this for production code, inject mocks in tests.
11893
*/
11994
export const processEnv: ProcessEnv = getProcessEnv()
120-
121-
/**
122-
* Create a test ProcessEnv with optional overrides.
123-
* Composes from createTestBaseProcessEnv for DRY.
124-
*/
125-
export const createTestProcessEnv = (
126-
overrides: Partial<ProcessEnv> = {},
127-
): ProcessEnv => ({
128-
...createTestBaseEnv(),
129-
130-
// Terminal-specific
131-
KITTY_WINDOW_ID: undefined,
132-
SIXEL_SUPPORT: undefined,
133-
ZED_NODE_ENV: undefined,
134-
135-
// VS Code family detection
136-
VSCODE_THEME_KIND: undefined,
137-
VSCODE_COLOR_THEME_KIND: undefined,
138-
VSCODE_GIT_IPC_HANDLE: undefined,
139-
VSCODE_PID: undefined,
140-
VSCODE_CWD: undefined,
141-
VSCODE_NLS_CONFIG: undefined,
142-
143-
// Cursor editor detection
144-
CURSOR_PORT: undefined,
145-
CURSOR: undefined,
146-
147-
// JetBrains IDE detection
148-
JETBRAINS_REMOTE_RUN: undefined,
149-
IDEA_INITIAL_DIRECTORY: undefined,
150-
IDE_CONFIG_DIR: undefined,
151-
JB_IDE_CONFIG_DIR: undefined,
152-
153-
// Editor preferences
154-
VISUAL: undefined,
155-
EDITOR: undefined,
156-
CODEBUFF_CLI_EDITOR: undefined,
157-
CODEBUFF_EDITOR: undefined,
158-
159-
// Theme preferences
160-
OPEN_TUI_THEME: undefined,
161-
OPENTUI_THEME: undefined,
162-
163-
// Codebuff CLI-specific
164-
CODEBUFF_IS_BINARY: undefined,
165-
CODEBUFF_CLI_VERSION: undefined,
166-
CODEBUFF_CLI_TARGET: undefined,
167-
CODEBUFF_RG_PATH: undefined,
168-
CODEBUFF_WASM_DIR: undefined,
169-
170-
// Build/CI flags
171-
VERBOSE: undefined,
172-
OVERRIDE_TARGET: undefined,
173-
OVERRIDE_PLATFORM: undefined,
174-
OVERRIDE_ARCH: undefined,
175-
...overrides,
176-
})

common/src/env.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
1-
import {
2-
clientEnvSchema,
3-
clientProcessEnv,
4-
type ClientInput,
5-
} from './env-schema'
1+
import { clientEnvSchema, clientProcessEnv } from './env-schema'
62

7-
const isTestRuntime =
8-
process.env.NODE_ENV === 'test' || process.env.BUN_ENV === 'test'
9-
10-
const TEST_ENV_DEFAULTS: ClientInput = {
11-
NEXT_PUBLIC_CB_ENVIRONMENT: 'test',
12-
NEXT_PUBLIC_CODEBUFF_APP_URL: 'http://localhost:3000',
13-
NEXT_PUBLIC_SUPPORT_EMAIL: 'support@codebuff.com',
14-
NEXT_PUBLIC_POSTHOG_API_KEY: 'test-posthog-key',
15-
NEXT_PUBLIC_POSTHOG_HOST_URL: 'https://us.i.posthog.com',
16-
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: 'pk_test_placeholder',
17-
NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL:
18-
'https://billing.stripe.com/p/login/test_placeholder',
19-
NEXT_PUBLIC_GOOGLE_SITE_VERIFICATION_ID: 'test-verification',
20-
NEXT_PUBLIC_WEB_PORT: '3000',
21-
}
22-
23-
const envInput = isTestRuntime
24-
? { ...TEST_ENV_DEFAULTS, ...clientProcessEnv }
25-
: clientProcessEnv
26-
27-
const parsedEnv = clientEnvSchema.safeParse(envInput)
3+
const parsedEnv = clientEnvSchema.safeParse(clientProcessEnv)
284
if (!parsedEnv.success) {
295
throw parsedEnv.error
306
}
317

328
export const env = parsedEnv.data
339

34-
// Populate process.env with defaults during tests so direct access works
35-
if (isTestRuntime) {
36-
for (const [key, value] of Object.entries(TEST_ENV_DEFAULTS)) {
37-
if (!process.env[key] && typeof value === 'string') {
38-
process.env[key] = value
39-
}
40-
}
41-
}
42-
4310
// Only log environment in non-production
4411
if (env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') {
4512
console.log('Using environment:', env.NEXT_PUBLIC_CB_ENVIRONMENT)

common/src/testing/env-ci.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Test-only CiEnv fixtures.
3+
*/
4+
5+
import type { CiEnv } from '../types/contracts/env'
6+
7+
/**
8+
* Create a test CiEnv with optional overrides.
9+
*/
10+
export const createTestCiEnv = (overrides: Partial<CiEnv> = {}): CiEnv => ({
11+
CI: undefined,
12+
GITHUB_ACTIONS: undefined,
13+
RENDER: undefined,
14+
IS_PULL_REQUEST: undefined,
15+
CODEBUFF_GITHUB_TOKEN: undefined,
16+
CODEBUFF_API_KEY: 'test-api-key',
17+
...overrides,
18+
})
19+

0 commit comments

Comments
 (0)