mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
fix(agent): resolve lint and type errors in sandboxed bash tests
This commit is contained in:
@@ -16,16 +16,19 @@ describe('createSandboxedBashTool', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
mockSessionManager.getSkillEnvs.mockReturnValue({ TEST_ENV: 'test-value' })
|
mockSessionManager.getSkillEnvs.mockReturnValue({ TEST_ENV: 'test-value' })
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// 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}`)
|
;(SandboxManager.wrapWithSandbox as any).mockImplementation(
|
||||||
|
async (cmd: string) => `sandboxed-${cmd}`,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const createMockSignal = () => {
|
const createMockSignal = () => {
|
||||||
|
// 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
|
||||||
const signal = new EventEmitter() as any
|
const signal = new EventEmitter() as any
|
||||||
signal.addEventListener = vi.fn((event: string, cb: any) => {
|
signal.addEventListener = vi.fn((event: string, cb: (ev: Event) => unknown) => {
|
||||||
signal.on(event, cb)
|
signal.on(event, cb)
|
||||||
})
|
})
|
||||||
signal.removeEventListener = vi.fn((event: string, cb: any) => {
|
signal.removeEventListener = vi.fn((event: string, cb: (ev: Event) => unknown) => {
|
||||||
signal.off(event, cb)
|
signal.off(event, cb)
|
||||||
})
|
})
|
||||||
signal.aborted = false
|
signal.aborted = false
|
||||||
@@ -35,6 +38,7 @@ describe('createSandboxedBashTool', () => {
|
|||||||
const createMockOnUpdate = () => vi.fn()
|
const createMockOnUpdate = () => vi.fn()
|
||||||
|
|
||||||
it('should execute a command in the sandbox with injected env vars', async () => {
|
it('should execute a command in the sandbox with injected env vars', async () => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockSessionManager is a partial mock of DatabaseSessionManager
|
||||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
||||||
|
|
||||||
const mockStdout = new EventEmitter()
|
const mockStdout = new EventEmitter()
|
||||||
@@ -46,38 +50,44 @@ describe('createSandboxedBashTool', () => {
|
|||||||
kill: vi.fn(),
|
kill: vi.fn(),
|
||||||
})
|
})
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn is mocked to return a partial ChildProcess
|
||||||
;(spawn as any).mockReturnValue(mockChild)
|
;(spawn as any).mockReturnValue(mockChild)
|
||||||
|
|
||||||
const onUpdate = createMockOnUpdate()
|
const onUpdate = createMockOnUpdate()
|
||||||
const signal = createMockSignal()
|
const signal = createMockSignal()
|
||||||
|
|
||||||
const executePromise = tool.execute('1', { command: 'echo hello' }, signal, onUpdate, {} as any)
|
// pi-coding-agent AgentTool.execute signature: (toolCallId, params, signal?, onUpdate?)
|
||||||
|
const executePromise = tool.execute('1', { command: 'echo hello' }, signal, onUpdate)
|
||||||
|
|
||||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||||
|
|
||||||
expect(SandboxManager.wrapWithSandbox).toHaveBeenCalledWith('echo hello')
|
expect(SandboxManager.wrapWithSandbox).toHaveBeenCalledWith('echo hello')
|
||||||
expect(spawn).toHaveBeenCalledWith('bash', ['-c', 'sandboxed-echo hello'], expect.objectContaining({
|
expect(spawn).toHaveBeenCalledWith(
|
||||||
env: { TEST_ENV: 'test-value' }
|
'bash',
|
||||||
}))
|
['-c', 'sandboxed-echo hello'],
|
||||||
|
expect.objectContaining({
|
||||||
|
env: { TEST_ENV: 'test-value' },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// Simulate stdout data
|
// Simulate stdout data
|
||||||
mockStdout.emit('data', Buffer.from('hello output'))
|
mockStdout.emit('data', Buffer.from('hello output'))
|
||||||
mockChild.emit('close', 0)
|
mockChild.emit('close', 0)
|
||||||
|
|
||||||
const result = await executePromise
|
const result = await executePromise
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ToolResponse content is an array of content blocks
|
||||||
expect((result as any).content[0].text).toContain('hello output')
|
expect((result as any).content[0].text).toContain('hello output')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle process errors', async () => {
|
it('should handle process errors', async () => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockSessionManager is a partial mock
|
||||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
||||||
const mockChild = new EventEmitter()
|
const mockChild = new EventEmitter()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||||
;(spawn as any).mockReturnValue(mockChild)
|
;(spawn as any).mockReturnValue(mockChild)
|
||||||
|
|
||||||
const signal = createMockSignal()
|
const signal = createMockSignal()
|
||||||
const executePromise = tool.execute('1', { command: 'fail' }, signal, createMockOnUpdate(), {} as any)
|
const executePromise = tool.execute('1', { command: 'fail' }, signal, createMockOnUpdate())
|
||||||
|
|
||||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||||
mockChild.emit('error', new Error('spawn failed'))
|
mockChild.emit('error', new Error('spawn failed'))
|
||||||
@@ -86,17 +96,18 @@ describe('createSandboxedBashTool', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should handle non-zero exit codes', async () => {
|
it('should handle non-zero exit codes', async () => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockSessionManager is a partial mock
|
||||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
||||||
const mockChild = Object.assign(new EventEmitter(), {
|
const mockChild = Object.assign(new EventEmitter(), {
|
||||||
stdout: new EventEmitter(),
|
stdout: new EventEmitter(),
|
||||||
stderr: new EventEmitter(),
|
stderr: new EventEmitter(),
|
||||||
pid: 1,
|
pid: 1,
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||||
;(spawn as any).mockReturnValue(mockChild)
|
;(spawn as any).mockReturnValue(mockChild)
|
||||||
|
|
||||||
const signal = createMockSignal()
|
const signal = createMockSignal()
|
||||||
const executePromise = tool.execute('1', { command: 'exit 1' }, signal, createMockOnUpdate(), {} as any)
|
const executePromise = tool.execute('1', { command: 'exit 1' }, signal, createMockOnUpdate())
|
||||||
|
|
||||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||||
mockChild.emit('close', 1)
|
mockChild.emit('close', 1)
|
||||||
@@ -106,6 +117,7 @@ describe('createSandboxedBashTool', () => {
|
|||||||
|
|
||||||
it('should handle timeouts', async () => {
|
it('should handle timeouts', async () => {
|
||||||
vi.useFakeTimers()
|
vi.useFakeTimers()
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockSessionManager is a partial mock
|
||||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
||||||
const mockChild = Object.assign(new EventEmitter(), {
|
const mockChild = Object.assign(new EventEmitter(), {
|
||||||
stdout: new EventEmitter(),
|
stdout: new EventEmitter(),
|
||||||
@@ -113,12 +125,18 @@ describe('createSandboxedBashTool', () => {
|
|||||||
pid: 456,
|
pid: 456,
|
||||||
kill: vi.fn(),
|
kill: vi.fn(),
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||||
;(spawn as any).mockReturnValue(mockChild)
|
;(spawn as any).mockReturnValue(mockChild)
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type is boolean
|
||||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||||
|
|
||||||
const signal = createMockSignal()
|
const signal = createMockSignal()
|
||||||
const executePromise = tool.execute('1', { command: 'sleep 10', timeout: 2 }, signal, createMockOnUpdate(), {} as any)
|
const executePromise = tool.execute(
|
||||||
|
'1',
|
||||||
|
{ command: 'sleep 10', timeout: 2 },
|
||||||
|
signal,
|
||||||
|
createMockOnUpdate(),
|
||||||
|
)
|
||||||
|
|
||||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||||
|
|
||||||
@@ -134,6 +152,7 @@ describe('createSandboxedBashTool', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should handle abort signals', async () => {
|
it('should handle abort signals', async () => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockSessionManager is a partial mock
|
||||||
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
const tool = createSandboxedBashTool(mockCwd, mockSessionManager as any)
|
||||||
const mockChild = Object.assign(new EventEmitter(), {
|
const mockChild = Object.assign(new EventEmitter(), {
|
||||||
stdout: new EventEmitter(),
|
stdout: new EventEmitter(),
|
||||||
@@ -141,12 +160,13 @@ describe('createSandboxedBashTool', () => {
|
|||||||
pid: 789,
|
pid: 789,
|
||||||
kill: vi.fn(),
|
kill: vi.fn(),
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- spawn returns mocked ChildProcess
|
||||||
;(spawn as any).mockReturnValue(mockChild)
|
;(spawn as any).mockReturnValue(mockChild)
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- process.kill return type is boolean
|
||||||
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => true as any)
|
||||||
|
|
||||||
const signal = createMockSignal()
|
const signal = createMockSignal()
|
||||||
const executePromise = tool.execute('1', { command: 'long-run' }, signal, createMockOnUpdate(), {} as any)
|
const executePromise = tool.execute('1', { command: 'long-run' }, signal, createMockOnUpdate())
|
||||||
|
|
||||||
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
await vi.waitFor(() => expect(spawn).toHaveBeenCalled())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user