Skip to content

Commit a07bc47

Browse files
committed
fix(tests): update supported files tests for SDK v4 getSupportedFiles(orgSlug)
1 parent 747120b commit a07bc47

File tree

1 file changed

+40
-83
lines changed

1 file changed

+40
-83
lines changed
Lines changed: 40 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
/**
22
* Unit tests for fetchSupportedScanFileNames.
33
*
4-
* Purpose:
5-
* Tests fetching supported manifest file names for scanning. Validates which files Socket can analyze.
6-
*
7-
* Test Coverage:
8-
* - Successful API operation
9-
* - SDK setup failure handling
10-
* - API call error scenarios
11-
* - Custom SDK options (API tokens, base URLs)
12-
* - Supported file types
13-
* - Ecosystem detection
14-
* - Null prototype usage for security
15-
*
16-
* Testing Approach:
17-
* Uses SDK test helpers to mock Socket API interactions. Validates comprehensive
18-
* error handling and API integration.
19-
*
20-
* Related Files:
21-
* - src/commands/SupportedScanFileNames.mts (implementation)
4+
* Tests fetching supported manifest file names for scanning.
5+
* Validates which files Socket can analyze via the SDK v4 getSupportedFiles API.
226
*/
237

248
import { describe, expect, it, vi } from 'vitest'
@@ -32,6 +16,7 @@ import {
3216
// Mock the dependencies.
3317
const mockHandleApiCall = vi.hoisted(() => vi.fn())
3418
const mockSetupSdk = vi.hoisted(() => vi.fn())
19+
const mockGetDefaultOrgSlug = vi.hoisted(() => vi.fn())
3520

3621
vi.mock('../../../../../src/utils/socket/api.mts', () => ({
3722
handleApiCall: mockHandleApiCall,
@@ -41,6 +26,10 @@ vi.mock('../../../../../src/utils/socket/sdk.mts', () => ({
4126
setupSdk: mockSetupSdk,
4227
}))
4328

29+
vi.mock('../../../../../src/commands/ci/fetch-default-org-slug.mts', () => ({
30+
getDefaultOrgSlug: mockGetDefaultOrgSlug,
31+
}))
32+
4433
describe('fetchSupportedScanFileNames', () => {
4534
it('fetches supported scan file names successfully', async () => {
4635
const { fetchSupportedScanFileNames } =
@@ -51,13 +40,14 @@ describe('fetchSupportedScanFileNames', () => {
5140
}
5241

5342
const { mockHandleApi, mockSdk } = await setupSdkMockSuccess(
54-
'getSupportedScanFiles',
43+
'getSupportedFiles',
5544
mockData,
5645
)
46+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'test-org' })
5747

5848
const result = await fetchSupportedScanFileNames()
5949

60-
expect(mockSdk.getSupportedScanFiles).toHaveBeenCalledWith()
50+
expect(mockSdk.getSupportedFiles).toHaveBeenCalledWith('test-org')
6151
expect(mockHandleApi).toHaveBeenCalledWith(expect.any(Promise), {
6252
description: 'supported scan file types',
6353
})
@@ -85,22 +75,39 @@ describe('fetchSupportedScanFileNames', () => {
8575
const { fetchSupportedScanFileNames } =
8676
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
8777

88-
await setupSdkMockError('getSupportedScanFiles', 'API error', 500)
78+
await setupSdkMockError('getSupportedFiles', 'API error', 500)
79+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'test-org' })
8980

9081
const result = await fetchSupportedScanFileNames()
9182

9283
expect(result.ok).toBe(false)
9384
expect(result.code).toBe(500)
9485
})
9586

87+
it('handles org slug failure', async () => {
88+
const { fetchSupportedScanFileNames } =
89+
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
90+
91+
await setupSdkMockSuccess('getSupportedFiles', {})
92+
mockGetDefaultOrgSlug.mockResolvedValue({
93+
ok: false,
94+
message: 'No org found',
95+
})
96+
97+
const result = await fetchSupportedScanFileNames()
98+
99+
expect(result.ok).toBe(false)
100+
})
101+
96102
it('passes custom SDK options', async () => {
97103
const { fetchSupportedScanFileNames } =
98104
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
99105

100106
const { mockSdk, mockSetupSdk } = await setupSdkMockSuccess(
101-
'getSupportedScanFiles',
107+
'getSupportedFiles',
102108
{},
103109
)
110+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'my-org' })
104111

105112
const options = {
106113
sdkOpts: {
@@ -112,17 +119,18 @@ describe('fetchSupportedScanFileNames', () => {
112119
await fetchSupportedScanFileNames(options)
113120

114121
expect(mockSetupSdk).toHaveBeenCalledWith(options.sdkOpts)
115-
expect(mockSdk.getSupportedScanFiles).toHaveBeenCalledWith()
122+
expect(mockSdk.getSupportedFiles).toHaveBeenCalledWith('my-org')
116123
})
117124

118125
it('passes custom spinner', async () => {
119126
const { fetchSupportedScanFileNames } =
120127
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
121128

122129
const { mockHandleApi } = await setupSdkMockSuccess(
123-
'getSupportedScanFiles',
130+
'getSupportedFiles',
124131
{},
125132
)
133+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'test-org' })
126134

127135
const mockSpinner = {
128136
start: vi.fn(),
@@ -131,11 +139,7 @@ describe('fetchSupportedScanFileNames', () => {
131139
fail: vi.fn(),
132140
}
133141

134-
const options = {
135-
spinner: mockSpinner,
136-
}
137-
138-
await fetchSupportedScanFileNames(options)
142+
await fetchSupportedScanFileNames({ spinner: mockSpinner })
139143

140144
expect(mockHandleApi).toHaveBeenCalledWith(expect.any(Promise), {
141145
description: 'supported scan file types',
@@ -147,10 +151,11 @@ describe('fetchSupportedScanFileNames', () => {
147151
const { fetchSupportedScanFileNames } =
148152
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
149153

150-
await setupSdkMockSuccess('getSupportedScanFiles', {
154+
await setupSdkMockSuccess('getSupportedFiles', {
151155
supportedFiles: [],
152156
ecosystems: [],
153157
})
158+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'test-org' })
154159

155160
const result = await fetchSupportedScanFileNames()
156161

@@ -159,62 +164,15 @@ describe('fetchSupportedScanFileNames', () => {
159164
expect(result.data?.ecosystems).toEqual([])
160165
})
161166

162-
it('handles comprehensive file types', async () => {
163-
const { fetchSupportedScanFileNames } =
164-
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
165-
166-
const comprehensiveFiles = [
167-
// JavaScript/Node.js
168-
'package.json',
169-
'package-lock.json',
170-
'yarn.lock',
171-
'pnpm-lock.yaml',
172-
// PHP
173-
'composer.json',
174-
'composer.lock',
175-
// Ruby
176-
'Gemfile',
177-
'Gemfile.lock',
178-
// Python
179-
'requirements.txt',
180-
'Pipfile',
181-
'Pipfile.lock',
182-
'poetry.lock',
183-
'pyproject.toml',
184-
// Go
185-
'go.mod',
186-
'go.sum',
187-
// Java
188-
'pom.xml',
189-
'build.gradle',
190-
// .NET
191-
'packages.config',
192-
'*.csproj',
193-
// Rust
194-
'Cargo.toml',
195-
'Cargo.lock',
196-
]
197-
198-
await setupSdkMockSuccess('getSupportedScanFiles', {
199-
supportedFiles: comprehensiveFiles,
200-
})
201-
202-
const result = await fetchSupportedScanFileNames()
203-
204-
expect(result.ok).toBe(true)
205-
expect(result.data?.supportedFiles).toContain('package.json')
206-
expect(result.data?.supportedFiles).toContain('Cargo.toml')
207-
expect(result.data?.supportedFiles).toContain('pom.xml')
208-
})
209-
210167
it('works without options parameter', async () => {
211168
const { fetchSupportedScanFileNames } =
212169
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
213170

214171
const { mockHandleApi, mockSetupSdk } = await setupSdkMockSuccess(
215-
'getSupportedScanFiles',
172+
'getSupportedFiles',
216173
{ supportedFiles: ['package.json'] },
217174
)
175+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'test-org' })
218176

219177
const result = await fetchSupportedScanFileNames()
220178

@@ -230,12 +188,11 @@ describe('fetchSupportedScanFileNames', () => {
230188
const { fetchSupportedScanFileNames } =
231189
await import('../../../../../src/commands/scan/fetch-supported-scan-file-names.mts')
232190

233-
const { mockSdk } = await setupSdkMockSuccess('getSupportedScanFiles', {})
191+
const { mockSdk } = await setupSdkMockSuccess('getSupportedFiles', {})
192+
mockGetDefaultOrgSlug.mockResolvedValue({ ok: true, data: 'test-org' })
234193

235-
// This tests that the function properly uses __proto__: null.
236194
await fetchSupportedScanFileNames()
237195

238-
// The function should work without prototype pollution issues.
239-
expect(mockSdk.getSupportedScanFiles).toHaveBeenCalled()
196+
expect(mockSdk.getSupportedFiles).toHaveBeenCalled()
240197
})
241198
})

0 commit comments

Comments
 (0)