Skip to content

Commit 8ad3ee4

Browse files
committed
test(cli): add tests for checkCiEnvVars in env-helpers
1 parent bfd25be commit 8ad3ee4

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

packages/cli/test/unit/commands/fix/env-helpers.test.mts

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Test Coverage:
1010
* - Environment variable instruction generation with exact var names
1111
* - Instruction formatting and consistency validation
12+
* - CI environment variable checking
1213
*
1314
* Testing Approach:
1415
* Uses direct function invocation without mocks since env-helpers.mts provides pure
@@ -20,11 +21,47 @@
2021
* - src/commands/fix/handle-fix.mts - Main fix command handler that uses env helpers
2122
*/
2223

23-
import { describe, expect, it } from 'vitest'
24+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2425

25-
import { getCiEnvInstructions } from '../../../../src/commands/fix/env-helpers.mts'
26+
// Mock @socketsecurity/lib/env/ci.
27+
const mockGetCI = vi.hoisted(() => vi.fn())
28+
vi.mock('@socketsecurity/lib/env/ci', () => ({
29+
getCI: mockGetCI,
30+
}))
31+
32+
// Mock @socketsecurity/lib/env/socket-cli.
33+
const mockGetSocketCliGithubToken = vi.hoisted(() => vi.fn())
34+
vi.mock('@socketsecurity/lib/env/socket-cli', async importOriginal => {
35+
const actual =
36+
(await importOriginal()) as typeof import('@socketsecurity/lib/env/socket-cli')
37+
return {
38+
...actual,
39+
getSocketCliGithubToken: mockGetSocketCliGithubToken,
40+
}
41+
})
42+
43+
// Mock SOCKET_CLI_GIT_USER_EMAIL.
44+
const mockGitEmail = vi.hoisted(() => ({ SOCKET_CLI_GIT_USER_EMAIL: '' }))
45+
vi.mock('../../../../src/env/socket-cli-git-user-email.mts', () => mockGitEmail)
46+
47+
// Mock SOCKET_CLI_GIT_USER_NAME.
48+
const mockGitUser = vi.hoisted(() => ({ SOCKET_CLI_GIT_USER_NAME: '' }))
49+
vi.mock('../../../../src/env/socket-cli-git-user-name.mts', () => mockGitUser)
50+
51+
import {
52+
checkCiEnvVars,
53+
getCiEnvInstructions,
54+
} from '../../../../src/commands/fix/env-helpers.mts'
2655

2756
describe('env-helpers', () => {
57+
beforeEach(() => {
58+
vi.clearAllMocks()
59+
mockGetCI.mockReturnValue(false)
60+
mockGetSocketCliGithubToken.mockReturnValue(undefined)
61+
mockGitEmail.SOCKET_CLI_GIT_USER_EMAIL = ''
62+
mockGitUser.SOCKET_CLI_GIT_USER_NAME = ''
63+
})
64+
2865
describe('getCiEnvInstructions', () => {
2966
it('should return instructions with exact env var names', () => {
3067
const instructions = getCiEnvInstructions()
@@ -50,4 +87,76 @@ describe('env-helpers', () => {
5087
expect(lines[4]).toContain('SOCKET_CLI_GIT_USER_EMAIL=')
5188
})
5289
})
90+
91+
describe('checkCiEnvVars', () => {
92+
it('should return all missing when no env vars are set', () => {
93+
mockGetCI.mockReturnValue(false)
94+
mockGetSocketCliGithubToken.mockReturnValue(undefined)
95+
mockGitEmail.SOCKET_CLI_GIT_USER_EMAIL = ''
96+
mockGitUser.SOCKET_CLI_GIT_USER_NAME = ''
97+
98+
const result = checkCiEnvVars()
99+
100+
expect(result.missing).toHaveLength(4)
101+
expect(result.present).toHaveLength(0)
102+
expect(result.missing).toContain('CI')
103+
expect(result.missing).toContain('SOCKET_CLI_GIT_USER_EMAIL')
104+
expect(result.missing).toContain('SOCKET_CLI_GIT_USER_NAME')
105+
expect(result.missing).toContain(
106+
'SOCKET_CLI_GITHUB_TOKEN (or GITHUB_TOKEN)',
107+
)
108+
})
109+
110+
it('should return CI as present when in CI environment', () => {
111+
mockGetCI.mockReturnValue(true)
112+
113+
const result = checkCiEnvVars()
114+
115+
expect(result.present).toContain('CI')
116+
expect(result.missing).not.toContain('CI')
117+
})
118+
119+
it('should return GitHub token as present when set', () => {
120+
mockGetSocketCliGithubToken.mockReturnValue('ghp_test_token')
121+
122+
const result = checkCiEnvVars()
123+
124+
expect(result.present).toContain(
125+
'SOCKET_CLI_GITHUB_TOKEN (or GITHUB_TOKEN)',
126+
)
127+
expect(result.missing).not.toContain(
128+
'SOCKET_CLI_GITHUB_TOKEN (or GITHUB_TOKEN)',
129+
)
130+
})
131+
132+
it('should return git user name as present when set', () => {
133+
mockGitUser.SOCKET_CLI_GIT_USER_NAME = 'test-user'
134+
135+
const result = checkCiEnvVars()
136+
137+
expect(result.present).toContain('SOCKET_CLI_GIT_USER_NAME')
138+
expect(result.missing).not.toContain('SOCKET_CLI_GIT_USER_NAME')
139+
})
140+
141+
it('should return git email as present when set', () => {
142+
mockGitEmail.SOCKET_CLI_GIT_USER_EMAIL = 'test@example.com'
143+
144+
const result = checkCiEnvVars()
145+
146+
expect(result.present).toContain('SOCKET_CLI_GIT_USER_EMAIL')
147+
expect(result.missing).not.toContain('SOCKET_CLI_GIT_USER_EMAIL')
148+
})
149+
150+
it('should return all present when all env vars are set', () => {
151+
mockGetCI.mockReturnValue(true)
152+
mockGetSocketCliGithubToken.mockReturnValue('ghp_test_token')
153+
mockGitUser.SOCKET_CLI_GIT_USER_NAME = 'test-user'
154+
mockGitEmail.SOCKET_CLI_GIT_USER_EMAIL = 'test@example.com'
155+
156+
const result = checkCiEnvVars()
157+
158+
expect(result.missing).toHaveLength(0)
159+
expect(result.present).toHaveLength(4)
160+
})
161+
})
53162
})

0 commit comments

Comments
 (0)