2026-05-20 11:40:40 +08:00
import { describe , it , expect , vi , beforeEach } from 'vitest'
import { createSandboxedBashTool } from './sandboxed-bash'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
import { spawn } from 'node:child_process'
import { EventEmitter } from 'node:events'
vi . mock ( '@anthropic-ai/sandbox-runtime' )
vi . mock ( 'node:child_process' )
describe ( 'createSandboxedBashTool' , () => {
const mockCwd = '/mock/cwd'
const mockSessionManager = {
getSkillEnvs : vi.fn (),
}
beforeEach (() => {
vi . clearAllMocks ()
mockSessionManager . getSkillEnvs . mockReturnValue ({ TEST_ENV : 'test-value' })
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- SandboxManager.wrapWithSandbox is a mocked async function from a 3rd party library
;( SandboxManager . wrapWithSandbox as any ). mockImplementation (
async ( cmd : string ) => `sandboxed- ${ cmd } ` ,
)
2026-05-20 11:40:40 +08:00
})
const createMockSignal = () => {
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- signal needs to be partially compatible with AbortSignal but we need to emit events for testing
2026-05-20 11:40:40 +08:00
const signal = new EventEmitter () as any
2026-05-20 13:49:59 +08:00
signal . addEventListener = vi . fn (( event : string , cb : ( ev : Event ) => unknown ) => {
2026-05-20 11:40:40 +08:00
signal . on ( event , cb )
})
2026-05-20 13:49:59 +08:00
signal . removeEventListener = vi . fn (( event : string , cb : ( ev : Event ) => unknown ) => {
2026-05-20 11:40:40 +08:00
signal . off ( event , cb )
})
signal . aborted = false
return signal
}
const createMockOnUpdate = () => vi . fn ()
it ( 'should execute a command in the sandbox with injected env vars' , async () => {
2026-05-21 11:50:09 +08:00
const tool = createSandboxedBashTool ( mockCwd , mockSessionManager . getSkillEnvs ())
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
const mockStdout = new EventEmitter ()
const mockStderr = new EventEmitter ()
const mockChild = Object . assign ( new EventEmitter (), {
stdout : mockStdout ,
stderr : mockStderr ,
pid : 123 ,
kill : vi.fn (),
})
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn is mocked to return a partial ChildProcess
2026-05-20 11:40:40 +08:00
;( spawn as any ). mockReturnValue ( mockChild )
const onUpdate = createMockOnUpdate ()
const signal = createMockSignal ()
2026-05-20 13:49:59 +08:00
// pi-coding-agent AgentTool.execute signature: (toolCallId, params, signal?, onUpdate?)
const executePromise = tool . execute ( '1' , { command : 'echo hello' }, signal , onUpdate )
2026-05-20 11:40:40 +08:00
await vi . waitFor (() => expect ( spawn ). toHaveBeenCalled ())
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
expect ( SandboxManager . wrapWithSandbox ). toHaveBeenCalledWith ( 'echo hello' )
2026-05-20 13:49:59 +08:00
expect ( spawn ). toHaveBeenCalledWith (
'bash' ,
[ '-c' , 'sandboxed-echo hello' ],
expect . objectContaining ({
2026-05-20 21:45:26 +08:00
env : expect.objectContaining ({
TEST_ENV : 'test-value' ,
}),
2026-05-20 13:49:59 +08:00
}),
)
2026-05-20 11:40:40 +08:00
// Simulate stdout data
mockStdout . emit ( 'data' , Buffer . from ( 'hello output' ))
mockChild . emit ( 'close' , 0 )
const result = await executePromise
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ToolResponse content is an array of content blocks
2026-05-20 11:40:40 +08:00
expect (( result as any ). content [ 0 ]. text ). toContain ( 'hello output' )
})
it ( 'should handle process errors' , async () => {
2026-05-21 11:50:09 +08:00
const tool = createSandboxedBashTool ( mockCwd , mockSessionManager . getSkillEnvs ())
2026-05-20 11:40:40 +08:00
const mockChild = new EventEmitter ()
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
2026-05-20 11:40:40 +08:00
;( spawn as any ). mockReturnValue ( mockChild )
const signal = createMockSignal ()
2026-05-20 13:49:59 +08:00
const executePromise = tool . execute ( '1' , { command : 'fail' }, signal , createMockOnUpdate ())
2026-05-20 11:40:40 +08:00
await vi . waitFor (() => expect ( spawn ). toHaveBeenCalled ())
mockChild . emit ( 'error' , new Error ( 'spawn failed' ))
await expect ( executePromise ). rejects . toThrow ( 'spawn failed' )
})
it ( 'should handle non-zero exit codes' , async () => {
2026-05-21 11:50:09 +08:00
const tool = createSandboxedBashTool ( mockCwd , mockSessionManager . getSkillEnvs ())
2026-05-20 11:40:40 +08:00
const mockChild = Object . assign ( new EventEmitter (), {
stdout : new EventEmitter (),
stderr : new EventEmitter (),
pid : 1 ,
})
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
2026-05-20 11:40:40 +08:00
;( spawn as any ). mockReturnValue ( mockChild )
const signal = createMockSignal ()
2026-05-20 13:49:59 +08:00
const executePromise = tool . execute ( '1' , { command : 'exit 1' }, signal , createMockOnUpdate ())
2026-05-20 11:40:40 +08:00
await vi . waitFor (() => expect ( spawn ). toHaveBeenCalled ())
mockChild . emit ( 'close' , 1 )
await expect ( executePromise ). rejects . toThrow ( /exited with code 1/ )
})
it ( 'should handle timeouts' , async () => {
vi . useFakeTimers ()
2026-05-21 11:50:09 +08:00
const tool = createSandboxedBashTool ( mockCwd , mockSessionManager . getSkillEnvs ())
2026-05-20 11:40:40 +08:00
const mockChild = Object . assign ( new EventEmitter (), {
stdout : new EventEmitter (),
stderr : new EventEmitter (),
pid : 456 ,
kill : vi.fn (),
})
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
2026-05-20 11:40:40 +08:00
;( spawn as any ). mockReturnValue ( mockChild )
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type is boolean
2026-05-20 11:40:40 +08:00
const killSpy = vi . spyOn ( process , 'kill' ). mockImplementation (() => true as any )
const signal = createMockSignal ()
2026-05-20 13:49:59 +08:00
const executePromise = tool . execute (
'1' ,
{ command : 'sleep 10' , timeout : 2 },
signal ,
createMockOnUpdate (),
)
2026-05-20 11:40:40 +08:00
await vi . waitFor (() => expect ( spawn ). toHaveBeenCalled ())
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
await vi . advanceTimersByTimeAsync ( 2100 )
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
expect ( killSpy ). toHaveBeenCalledWith ( - 456 , 'SIGKILL' )
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
mockChild . emit ( 'close' , null )
await expect ( executePromise ). rejects . toThrow ( /timed out/ )
vi . useRealTimers ()
killSpy . mockRestore ()
})
it ( 'should handle abort signals' , async () => {
2026-05-21 11:50:09 +08:00
const tool = createSandboxedBashTool ( mockCwd , mockSessionManager . getSkillEnvs ())
2026-05-20 11:40:40 +08:00
const mockChild = Object . assign ( new EventEmitter (), {
stdout : new EventEmitter (),
stderr : new EventEmitter (),
pid : 789 ,
kill : vi.fn (),
})
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
2026-05-20 11:40:40 +08:00
;( spawn as any ). mockReturnValue ( mockChild )
2026-05-20 13:49:59 +08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type is boolean
2026-05-20 11:40:40 +08:00
const killSpy = vi . spyOn ( process , 'kill' ). mockImplementation (() => true as any )
const signal = createMockSignal ()
2026-05-20 13:49:59 +08:00
const executePromise = tool . execute ( '1' , { command : 'long-run' }, signal , createMockOnUpdate ())
2026-05-20 11:40:40 +08:00
await vi . waitFor (() => expect ( spawn ). toHaveBeenCalled ())
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
signal . aborted = true
signal . emit ( 'abort' )
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
expect ( killSpy ). toHaveBeenCalledWith ( - 789 , 'SIGKILL' )
2026-05-20 13:49:59 +08:00
2026-05-20 11:40:40 +08:00
mockChild . emit ( 'close' , null )
await expect ( executePromise ). rejects . toThrow ( /aborted/ )
killSpy . mockRestore ()
})
})