|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from 'bun:test' |
| 2 | +import { |
| 3 | + runProgrammaticStep, |
| 4 | + clearAgentGeneratorCache, |
| 5 | +} from '../run-programmatic-step' |
| 6 | +import { AgentState } from '@codebuff/common/types/session-state' |
| 7 | +import { AgentTemplate } from '../templates/types' |
| 8 | +import { WebSocket } from 'ws' |
| 9 | +import { mockFileContext, MockWebSocket } from './test-utils' |
| 10 | + |
| 11 | +describe('QuickJS Sandbox Generator', () => { |
| 12 | + let mockAgentState: AgentState |
| 13 | + let mockParams: any |
| 14 | + let mockTemplate: AgentTemplate |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + clearAgentGeneratorCache() |
| 18 | + |
| 19 | + // Reuse common test data structure |
| 20 | + mockAgentState = { |
| 21 | + agentId: 'test-agent-123', |
| 22 | + agentType: 'test-vm-agent', |
| 23 | + messageHistory: [], |
| 24 | + report: {}, |
| 25 | + agentContext: {}, |
| 26 | + subagents: [], |
| 27 | + stepsRemaining: 10, |
| 28 | + } |
| 29 | + |
| 30 | + // Base template structure - will be customized per test |
| 31 | + mockTemplate = { |
| 32 | + id: 'test-vm-agent', |
| 33 | + name: 'Test VM Agent', |
| 34 | + purpose: 'Test VM isolation', |
| 35 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 36 | + outputMode: 'report', |
| 37 | + includeMessageHistory: false, |
| 38 | + toolNames: ['update_report'], |
| 39 | + spawnableAgents: [], |
| 40 | + promptSchema: {}, |
| 41 | + systemPrompt: '', |
| 42 | + userInputPrompt: '', |
| 43 | + agentStepPrompt: '', |
| 44 | + initialAssistantMessage: '', |
| 45 | + initialAssistantPrefix: '', |
| 46 | + stepAssistantMessage: '', |
| 47 | + stepAssistantPrefix: '', |
| 48 | + handleStep: '', // Will be set per test |
| 49 | + } |
| 50 | + |
| 51 | + // Common params structure |
| 52 | + mockParams = { |
| 53 | + template: mockTemplate, |
| 54 | + prompt: 'Test prompt', |
| 55 | + params: { testParam: 'value' }, |
| 56 | + userId: 'test-user', |
| 57 | + userInputId: 'test-input', |
| 58 | + clientSessionId: 'test-session', |
| 59 | + fingerprintId: 'test-fingerprint', |
| 60 | + onResponseChunk: () => {}, |
| 61 | + agentType: 'test-vm-agent', |
| 62 | + fileContext: mockFileContext, |
| 63 | + assistantMessage: undefined, |
| 64 | + assistantPrefix: undefined, |
| 65 | + ws: new MockWebSocket() as unknown as WebSocket, |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + clearAgentGeneratorCache() |
| 71 | + }) |
| 72 | + |
| 73 | + test('should execute string-based generator in QuickJS sandbox', async () => { |
| 74 | + // Customize template for this test |
| 75 | + mockTemplate.handleStep = ` |
| 76 | + function* ({ agentState, prompt, params }) { |
| 77 | + yield { |
| 78 | + toolName: 'update_report', |
| 79 | + args: { |
| 80 | + json_update: { |
| 81 | + message: 'Hello from QuickJS sandbox!', |
| 82 | + prompt: prompt, |
| 83 | + agentId: agentState.agentId |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + ` |
| 89 | + mockParams.template = mockTemplate |
| 90 | + |
| 91 | + const result = await runProgrammaticStep(mockAgentState, mockParams) |
| 92 | + |
| 93 | + expect(result.agentState.report).toEqual({ |
| 94 | + message: 'Hello from QuickJS sandbox!', |
| 95 | + prompt: 'Test prompt', |
| 96 | + agentId: 'test-agent-123', |
| 97 | + }) |
| 98 | + expect(result.endTurn).toBe(true) |
| 99 | + }) |
| 100 | + |
| 101 | + test('should handle QuickJS sandbox errors gracefully', async () => { |
| 102 | + // Customize for error test |
| 103 | + mockTemplate.id = 'test-vm-agent-error' |
| 104 | + mockTemplate.name = 'Test VM Agent Error' |
| 105 | + mockTemplate.purpose = 'Test QuickJS error handling' |
| 106 | + mockTemplate.toolNames = [] |
| 107 | + mockTemplate.handleStep = ` |
| 108 | + function* ({ agentState, prompt, params }) { |
| 109 | + throw new Error('QuickJS error test') |
| 110 | + } |
| 111 | + ` |
| 112 | + |
| 113 | + mockAgentState.agentId = 'test-agent-error-123' |
| 114 | + mockAgentState.agentType = 'test-vm-agent-error' |
| 115 | + |
| 116 | + mockParams.template = mockTemplate |
| 117 | + mockParams.agentType = 'test-vm-agent-error' |
| 118 | + mockParams.params = {} |
| 119 | + |
| 120 | + const result = await runProgrammaticStep(mockAgentState, mockParams) |
| 121 | + |
| 122 | + expect(result.endTurn).toBe(true) |
| 123 | + expect(result.agentState.report.error).toContain( |
| 124 | + 'Error executing programmatic agent' |
| 125 | + ) |
| 126 | + }) |
| 127 | +}) |
0 commit comments