mirror of
https://github.com/anthropic-experimental/sandbox-runtime.git
synced 2026-05-22 20:39:42 +08:00
- Add test/utils/which.test.ts for Bun environment testing - Add test/utils/which-node-test.mjs for Node.js fallback testing - Update test/sandbox/linux-dependency-error.test.ts to mock globalThis.Bun.which directly instead of using mock.module - Update test/sandbox/seccomp-filter.test.ts to use whichSync
55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
/**
|
|
* Test whichSync behavior in Node.js environment
|
|
* Run with: node --experimental-vm-modules test/utils/which-node.test.mjs
|
|
*/
|
|
|
|
import assert from 'node:assert'
|
|
import { spawnSync } from 'node:child_process'
|
|
|
|
// Verify we're running in Node.js, not Bun
|
|
assert.strictEqual(
|
|
typeof globalThis.Bun,
|
|
'undefined',
|
|
'This test must run in Node.js, not Bun'
|
|
)
|
|
|
|
console.log('Running whichSync Node.js fallback tests...')
|
|
|
|
// Build the project first to get the JS output
|
|
const buildResult = spawnSync('npm', ['run', 'build'], {
|
|
cwd: process.cwd(),
|
|
stdio: 'inherit',
|
|
})
|
|
|
|
if (buildResult.status !== 0) {
|
|
console.error('Build failed')
|
|
process.exit(1)
|
|
}
|
|
|
|
// Dynamically import the built module
|
|
const { whichSync } = await import('../../dist/utils/which.js')
|
|
|
|
// Test 1: Should find existing executable
|
|
const lsPath = whichSync('ls')
|
|
assert.ok(lsPath !== null, 'whichSync should find ls')
|
|
assert.ok(lsPath.includes('/ls'), `Expected path to contain /ls, got: ${lsPath}`)
|
|
console.log('✓ Found ls at:', lsPath)
|
|
|
|
// Test 2: Should return null for non-existent executable
|
|
const nonExistent = whichSync('this-command-definitely-does-not-exist-12345')
|
|
assert.strictEqual(nonExistent, null, 'Should return null for non-existent command')
|
|
console.log('✓ Returns null for non-existent command')
|
|
|
|
// Test 3: Should find bash
|
|
const bashPath = whichSync('bash')
|
|
assert.ok(bashPath !== null, 'whichSync should find bash')
|
|
console.log('✓ Found bash at:', bashPath)
|
|
|
|
// Test 4: Verify spawnSync is being used (by checking behavior matches which command)
|
|
const whichResult = spawnSync('which', ['ls'], { encoding: 'utf8' })
|
|
const expectedPath = whichResult.stdout.trim()
|
|
assert.strictEqual(lsPath, expectedPath, 'whichSync output should match which command')
|
|
console.log('✓ Output matches which command')
|
|
|
|
console.log('\n✅ All Node.js fallback tests passed!')
|