Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

sandbox-install.test.ts

70 lines2.6 KB

1import { execFileSync, spawnSync } from 'node:child_process'
2import { describe, expect, it } from 'vitest'
3import { HARNESSES, buildSandbox } from './sandbox-triage'
4import type { HarnessName } from './sandbox-triage'
5​
6/**
7 * Guards the sandbox CLI-install commands.
8 *
9 * The bug this locks down: the setup step used to splice each `installCommand`
10 * into a bigger command (`<cmd> || sudo -n env "PATH=$PATH" <cmd>`). Grok's
11 * installer starts with `(`, and a subshell is not a valid argument to
12 * `sudo`/`env`, so `sh` failed at PARSE time — `sh: syntax error: unexpected
13 * "("`, exit 2 — and the install never ran at all.
14 */
15​
16const HARNESS_NAMES = Object.keys(HARNESSES) as Array<HarnessName>
17​
18/** A real POSIX shell to parse with; `sh` on Linux/macOS, git-bash's on Windows. */
19const SHELL = ['sh', 'bash'].find((candidate) => {
20 const probe = spawnSync(candidate, ['-c', 'exit 0'], { stdio: 'ignore' })
21 return probe.status === 0
22})
23​
24/** Setup commands the sandbox definition actually runs, in order. */
25function setupCommands(harness: HarnessName): Array<string> {
26 const { workspace } = buildSandbox({
27 harness,
28 // Any sandboxed provider: `local` skips the install (host CLI is on PATH).
29 provider: 'docker',
30 repo: 'TanStack/ai',
31 threadId: 'test',
32 })
33 const setup = workspace?.setup
34 if (typeof setup !== 'function') {
35 throw new Error('expected a setup builder function')
36 }
37 const commands: Array<string> = []
38 setup({
39 serial: (command) => commands.push(command),
40 parallel: (group) => commands.push(...group),
41 })
42 return commands
44​
45describe('sandbox CLI install commands', () => {
46 it.each(HARNESS_NAMES)('%s installs and verifies its CLI', (harness) => {
47 const command = HARNESSES[harness].installCommand
48 expect(command).toBeTruthy()
49 // Every install must RUN the CLI: npm treats the platform-specific native
50 // binary (an optional dep) as best-effort, so a "successful" install can
51 // still leave a CLI that dies at run time.
52 expect(command).toMatch(/--version/)
53 })
54​
55 it.each(HARNESS_NAMES)('%s runs its command unwrapped', (harness) => {
56 expect(setupCommands(harness)).toEqual([HARNESSES[harness].installCommand])
57 })
58​
59 it.runIf(SHELL !== undefined).each(HARNESS_NAMES)(
60 '%s parses as a shell command',
61 (harness) => {
62 const command = HARNESSES[harness].installCommand
63 if (SHELL === undefined || command === null) return
64 // `-n`: parse only, run nothing. Throws with the shell's syntax error.
65 expect(() =>
66 execFileSync(SHELL, ['-n', '-c', command], { stdio: 'pipe' }),
67 ).not.toThrow()
68 },
69 )
70})