@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 56 lines 1.79 KB
Raw
1 /**
2 * Output sanitization utilities for stable test comparisons.
3 */
4
5 import type { E2ETestResult } from './types';
6
7 /**
8 * Sanitize command output for stable comparisons.
9 * Removes ANSI escape codes, normalizes line endings, and trims leading/trailing
10 * whitespace around the first/last meaningful output.
11 *
12 * @param output - Raw command output (stdout or stderr)
13 * @returns Sanitized output suitable for assertions and snapshots
14 */
15 export function sanitizeOutput(output: string): string {
16 return (
17 output
18 // Remove ANSI escape codes (colors, formatting)
19 .replace(/\x1b\[[0-9;]*m/g, '')
20 // Normalize line endings (Windows → Unix)
21 .replace(/\r\n/g, '\n')
22 // Trim leading/trailing whitespace from entire output
23 .trim()
24 );
25 }
26
27 /**
28 * Parse JSON from a command's stdout, providing a clear error message on failure.
29 *
30 * When the CLI errors out (e.g. 401), stdout is empty and `JSON.parse("")` throws
31 * a cryptic `SyntaxError: Unexpected EOF`. This helper surfaces the actual exit code
32 * and stderr so the root cause is immediately visible.
33 */
34 export function parseJsonStdout(result: E2ETestResult): unknown {
35 const raw = sanitizeOutput(result.stdout);
36 if (raw === '') {
37 const stderrPreview = sanitizeOutput(result.stderr).slice(0, 300);
38 throw new Error(
39 `stdout was empty — cannot parse JSON.\n` +
40 ` exitCode: ${result.exitCode}\n` +
41 ` stderr: ${stderrPreview || '(empty)'}`
42 );
43 }
44 try {
45 return JSON.parse(raw);
46 } catch (cause) {
47 const stderrPreview = sanitizeOutput(result.stderr).slice(0, 300);
48 throw new Error(
49 `stdout was not valid JSON.\n` +
50 ` exitCode: ${result.exitCode}\n` +
51 ` stdout: ${raw.slice(0, 300)}\n` +
52 ` stderr: ${stderrPreview || '(empty)'}`,
53 { cause }
54 );
55 }
56 }