-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathe2e-cli.test.ts
More file actions
190 lines (160 loc) · 4.67 KB
/
e2e-cli.test.ts
File metadata and controls
190 lines (160 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, test, expect } from 'bun:test'
import { spawn } from 'child_process'
import stripAnsi from 'strip-ansi'
import path from 'path'
import { isSDKBuilt, ensureCliTestEnv } from './test-utils'
const CLI_PATH = path.join(__dirname, '../index.tsx')
const TIMEOUT_MS = 10000
const sdkBuilt = isSDKBuilt()
ensureCliTestEnv()
function runCLI(
args: string[],
): Promise<{ stdout: string; stderr: string; exitCode: number | null }> {
return new Promise((resolve, reject) => {
const proc = spawn('bun', ['run', CLI_PATH, ...args], {
cwd: path.join(__dirname, '../..'),
stdio: 'pipe',
})
let stdout = ''
let stderr = ''
proc.stdout?.on('data', (data) => {
stdout += data.toString()
})
proc.stderr?.on('data', (data) => {
stderr += data.toString()
})
const timeout = setTimeout(() => {
proc.kill('SIGTERM')
reject(new Error('Process timeout'))
}, TIMEOUT_MS)
proc.on('exit', (code) => {
clearTimeout(timeout)
resolve({ stdout, stderr, exitCode: code })
})
proc.on('error', (err) => {
clearTimeout(timeout)
reject(err)
})
})
}
describe.skipIf(!sdkBuilt)('CLI End-to-End Tests', () => {
test(
'CLI shows help with --help flag',
async () => {
const { stdout, stderr, exitCode } = await runCLI(['--help'])
const cleanOutput = stripAnsi(stdout + stderr)
expect(cleanOutput).toContain('--agent')
expect(cleanOutput).toContain('Usage:')
expect(exitCode).toBe(0)
},
TIMEOUT_MS,
)
test(
'CLI shows help with -h flag',
async () => {
const { stdout, stderr, exitCode } = await runCLI(['-h'])
const cleanOutput = stripAnsi(stdout + stderr)
expect(cleanOutput).toContain('--agent')
expect(exitCode).toBe(0)
},
TIMEOUT_MS,
)
test(
'CLI shows version with --version flag',
async () => {
const { stdout, stderr, exitCode } = await runCLI(['--version'])
const cleanOutput = stripAnsi(stdout + stderr)
expect(cleanOutput).toMatch(/\d+\.\d+\.\d+|dev/)
expect(exitCode).toBe(0)
},
TIMEOUT_MS,
)
test(
'CLI shows version with -v flag',
async () => {
const { stdout, stderr, exitCode } = await runCLI(['-v'])
const cleanOutput = stripAnsi(stdout + stderr)
expect(cleanOutput).toMatch(/\d+\.\d+\.\d+|dev/)
expect(exitCode).toBe(0)
},
TIMEOUT_MS,
)
test(
'CLI accepts --agent flag',
async () => {
// Note: This will timeout and exit because we can't interact with stdin
// But we can verify it starts without errors
const proc = spawn('bun', ['run', CLI_PATH, '--agent', 'ask'], {
cwd: path.join(__dirname, '../..'),
stdio: 'pipe',
})
let started = false
await new Promise<void>((resolve) => {
const timeout = setTimeout(() => {
resolve()
}, 2000) // Increased timeout for CI environments
// Check both stdout and stderr - CLI may output to either
proc.stdout?.once('data', () => {
started = true
clearTimeout(timeout)
resolve()
})
proc.stderr?.once('data', () => {
started = true
clearTimeout(timeout)
resolve()
})
})
proc.kill('SIGTERM')
expect(started).toBe(true)
},
TIMEOUT_MS,
)
test(
'CLI accepts --clear-logs flag',
async () => {
const proc = spawn('bun', ['run', CLI_PATH, '--clear-logs'], {
cwd: path.join(__dirname, '../..'),
stdio: 'pipe',
})
let started = false
await new Promise<void>((resolve) => {
const timeout = setTimeout(() => {
resolve()
}, 2000) // Increased timeout for CI environments
// Check both stdout and stderr - CLI may output to either
proc.stdout?.once('data', () => {
started = true
clearTimeout(timeout)
resolve()
})
proc.stderr?.once('data', () => {
started = true
clearTimeout(timeout)
resolve()
})
})
proc.kill('SIGTERM')
expect(started).toBe(true)
},
TIMEOUT_MS,
)
test(
'CLI handles invalid flags gracefully',
async () => {
const { stderr, exitCode } = await runCLI(['--invalid-flag'])
// Commander should show an error
expect(exitCode).not.toBe(0)
expect(stripAnsi(stderr)).toContain('error')
},
TIMEOUT_MS,
)
})
// Show message when SDK tests are skipped
if (!sdkBuilt) {
describe('SDK Build Required', () => {
test.skip('Build SDK for E2E tests: cd sdk && bun run build', () => {
// This test is skipped to show the build instruction
})
})
}