Skip to content

Commit 5900658

Browse files
committed
refactor(test/helpers/cli-execution): drop 5 dead funcs + colocate 6 internal types (-219 LOC)
Dead (5 functions never called anywhere): - executeBatchCliCommands, executeCliJson, executeCliWithRetry, executeCliWithTiming, expectCliError, expectCliSuccess Colocated (used only as internal signatures, no external import): - CliExecutionResult, CliExecutionOptions (return/param types of executeCliCommand) - SocketJsonOk, SocketJsonErr, SocketJsonContract (members of the contract validated by validateSocketJsonContract) - CliInScratchOptions (param type of executeCliInScratch)
1 parent f5f659a commit 5900658

1 file changed

Lines changed: 6 additions & 225 deletions

File tree

packages/cli/test/helpers/cli-execution.mts

Lines changed: 6 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { SpawnOptions } from '@socketsecurity/lib-stable/spawn/types'
1919
/**
2020
* Result from CLI execution with enhanced utilities.
2121
*/
22-
export interface CliExecutionResult {
22+
interface CliExecutionResult {
2323
/**
2424
* Exit code from the CLI command.
2525
*/
@@ -54,7 +54,7 @@ export interface CliExecutionResult {
5454
/**
5555
* Options for CLI execution.
5656
*/
57-
export interface CliExecutionOptions extends SpawnOptions {
57+
interface CliExecutionOptions extends SpawnOptions {
5858
/**
5959
* Whether to automatically add --config {} to isolate from user config
6060
* (default: true)
@@ -74,38 +74,6 @@ export interface CliExecutionOptions extends SpawnOptions {
7474
timeout?: number | undefined
7575
}
7676

77-
/**
78-
* Batch execute multiple CLI commands in sequence.
79-
*
80-
* @example
81-
* ```typescript
82-
* const results = await executeBatchCliCommands([
83-
* ['config', 'get', 'apiToken'],
84-
* ['config', 'get', 'defaultOrg'],
85-
* ])
86-
* ```
87-
*
88-
* @param commands - Array of command argument arrays.
89-
* @param options - Execution options applied to all commands.
90-
*
91-
* @returns Array of CLI execution results
92-
*/
93-
export async function executeBatchCliCommands(
94-
commands: string[][],
95-
options?: CliExecutionOptions | undefined,
96-
): Promise<CliExecutionResult[]> {
97-
const results: CliExecutionResult[] = []
98-
99-
for (let i = 0, { length } = commands; i < length; i += 1) {
100-
const args = commands[i]!
101-
// eslint-disable-next-line no-await-in-loop
102-
const result = await executeCliCommand(args, options)
103-
results.push(result)
104-
}
105-
106-
return results
107-
}
108-
10977
/**
11078
* Execute Socket CLI command with enhanced result handling.
11179
*
@@ -172,193 +140,6 @@ export async function executeCliCommand(
172140
}
173141
}
174142

175-
/**
176-
* Execute Socket CLI command and parse JSON output.
177-
*
178-
* @example
179-
* ```typescript
180-
* const { data, result } = await executeCliJson(['scan', 'create'])
181-
* expect(data.id).toBeDefined()
182-
* ```
183-
*
184-
* @param args - Command arguments (--json flag added automatically)
185-
* @param options - Execution options.
186-
*
187-
* @returns Parsed JSON data and execution result
188-
*/
189-
export async function executeCliJson<T = unknown>(
190-
args: string[],
191-
options?: CliExecutionOptions | undefined,
192-
): Promise<{ data: T; result: CliExecutionResult }> {
193-
const finalArgs = args.includes('--json') ? args : [...args, '--json']
194-
195-
const result = await executeCliCommand(finalArgs, options)
196-
197-
try {
198-
const data = JSON.parse(result.stdout) as T
199-
return { data, result }
200-
} catch (e) {
201-
throw new Error(
202-
`Failed to parse JSON from CLI output\nCommand: ${args.join(' ')}\nstdout: ${result.stdout}\nError: ${e instanceof Error ? e.message : String(e)}`,
203-
)
204-
}
205-
}
206-
207-
/**
208-
* Execute Socket CLI command with multiple retry attempts. Useful for commands
209-
* that may have transient failures.
210-
*
211-
* @example
212-
* ```typescript
213-
* const result = await executeCliWithRetry(['scan', 'create'], 3, 2000)
214-
* ```
215-
*
216-
* @param args - Command arguments.
217-
* @param maxRetries - Maximum number of retry attempts (default: 3)
218-
* @param retryDelay - Delay between retries in ms (default: 1000)
219-
* @param options - Execution options.
220-
*
221-
* @returns CLI execution result
222-
*/
223-
export async function executeCliWithRetry(
224-
args: string[],
225-
maxRetries = 3,
226-
retryDelay = 1000,
227-
options?: CliExecutionOptions | undefined,
228-
): Promise<CliExecutionResult> {
229-
let lastError: Error | undefined
230-
let attempts = 0
231-
232-
while (attempts < maxRetries) {
233-
attempts++
234-
try {
235-
// eslint-disable-next-line no-await-in-loop
236-
return await executeCliCommand(args, options)
237-
} catch (e) {
238-
lastError = e instanceof Error ? e : new Error(String(e))
239-
240-
if (attempts < maxRetries) {
241-
// Wait before retrying
242-
// eslint-disable-next-line no-await-in-loop
243-
await new Promise(resolve => setTimeout(resolve, retryDelay))
244-
}
245-
}
246-
}
247-
248-
throw new Error(
249-
`CLI command failed after ${maxRetries} attempts\nCommand: ${args.join(' ')}\nLast error: ${lastError?.message}`,
250-
)
251-
}
252-
253-
/**
254-
* Execute CLI command and capture timing information.
255-
*
256-
* @example
257-
* ```typescript
258-
* const { result, duration } = await executeCli WithTiming(['scan', 'create'])
259-
* expect(duration).toBeLessThan(5000)
260-
* ```
261-
*
262-
* @param args - Command arguments.
263-
* @param options - Execution options.
264-
*
265-
* @returns Execution result with timing information
266-
*/
267-
export async function executeCliWithTiming(
268-
args: string[],
269-
options?: CliExecutionOptions | undefined,
270-
): Promise<{
271-
result: CliExecutionResult
272-
duration: number
273-
startTime: number
274-
endTime: number
275-
}> {
276-
const startTime = Date.now()
277-
const result = await executeCliCommand(args, options)
278-
const endTime = Date.now()
279-
const duration = endTime - startTime
280-
281-
return {
282-
duration,
283-
endTime,
284-
result,
285-
startTime,
286-
}
287-
}
288-
289-
/**
290-
* Execute Socket CLI command expecting failure (non-zero exit code).
291-
*
292-
* @example
293-
* ```typescript
294-
* const result = await expectCliError(['scan'], 1)
295-
* expect(result.stderr).toContain('error')
296-
* ```
297-
*
298-
* @param args - Command arguments.
299-
* @param expectedCode - Expected exit code (default: any non-zero)
300-
* @param options - Execution options.
301-
*
302-
* @returns CLI execution result
303-
*
304-
* @throws Error if command succeeds
305-
*/
306-
export async function expectCliError(
307-
args: string[],
308-
expectedCode?: number | undefined,
309-
options?: CliExecutionOptions | undefined,
310-
): Promise<CliExecutionResult> {
311-
const result = await executeCliCommand(args, options)
312-
313-
if (result.status) {
314-
throw new Error(
315-
`Expected CLI command to fail but it succeeded\nCommand: ${args.join(' ')}\nstdout: ${result.stdout}`,
316-
)
317-
}
318-
319-
if (expectedCode !== undefined && result.code !== expectedCode) {
320-
throw new Error(
321-
`Expected exit code ${expectedCode} but got ${result.code}\nCommand: ${args.join(' ')}\nstderr: ${result.stderr}`,
322-
)
323-
}
324-
325-
return result
326-
}
327-
328-
/**
329-
* Execute Socket CLI command expecting success (exit code 0).
330-
*
331-
* @example
332-
* ```typescript
333-
* const result = await expectCliSuccess(['wrapper', '--help'])
334-
* expect(result.stdout).toContain('Usage')
335-
* ```
336-
*
337-
* @param args - Command arguments.
338-
* @param options - Execution options.
339-
*
340-
* @returns CLI execution result
341-
*
342-
* @throws Error if command fails
343-
*/
344-
export async function expectCliSuccess(
345-
args: string[],
346-
options?: CliExecutionOptions | undefined,
347-
): Promise<CliExecutionResult> {
348-
const result = await executeCliCommand(args, {
349-
expectedExitCode: 0,
350-
...options,
351-
})
352-
353-
if (!result.status) {
354-
throw new Error(
355-
`Expected CLI command to succeed but got exit code ${result.code}\nCommand: ${args.join(' ')}\nstdout: ${result.stdout}\nstderr: ${result.stderr}`,
356-
)
357-
}
358-
359-
return result
360-
}
361-
362143
/**
363144
* Shape of the Socket CLI's `--json` response contract. Mirrors the
364145
* `validate_json` shell helper that was in `test/smoke.sh` so e2e tests can
@@ -371,19 +152,19 @@ export async function expectCliSuccess(
371152
* optional; `cause`/`code` are optional but, when `code` is present, it
372153
* must be a number.
373154
*/
374-
export interface SocketJsonOk<T = unknown> {
155+
interface SocketJsonOk<T = unknown> {
375156
ok: true
376157
data: T
377158
message?: string | undefined
378159
}
379-
export interface SocketJsonErr {
160+
interface SocketJsonErr {
380161
ok: false
381162
data?: unknown | undefined
382163
message: string
383164
cause?: string | undefined
384165
code?: number | undefined
385166
}
386-
export type SocketJsonContract<T = unknown> = SocketJsonOk<T> | SocketJsonErr
167+
type SocketJsonContract<T = unknown> = SocketJsonOk<T> | SocketJsonErr
387168

388169
/**
389170
* Validate that `stdout` is JSON matching the Socket CLI's `--json` contract,
@@ -443,7 +224,7 @@ export function validateSocketJsonContract<T = unknown>(
443224
/**
444225
* Options for {@link executeCliInScratch}.
445226
*/
446-
export interface CliInScratchOptions extends CliExecutionOptions {
227+
interface CliInScratchOptions extends CliExecutionOptions {
447228
/**
448229
* Files to seed into the scratch cwd before running. Keyed by relative path;
449230
* each value is the file body written verbatim. Use for fixtures the

0 commit comments

Comments
 (0)