|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'; |
| 8 | +import { CoderAgentExecutor } from './executor.js'; |
| 9 | +import type { |
| 10 | + ExecutionEventBus, |
| 11 | + RequestContext, |
| 12 | + TaskStore, |
| 13 | +} from '@a2a-js/sdk/server'; |
| 14 | +import { EventEmitter } from 'node:events'; |
| 15 | +import { requestStorage } from '../http/requestStorage.js'; |
| 16 | + |
| 17 | +// Mocks for constructor dependencies |
| 18 | +vi.mock('../config/config.js', () => ({ |
| 19 | + loadConfig: vi.fn().mockReturnValue({ |
| 20 | + getSessionId: () => 'test-session', |
| 21 | + getTargetDir: () => '/tmp', |
| 22 | + getCheckpointingEnabled: () => false, |
| 23 | + }), |
| 24 | + loadEnvironment: vi.fn(), |
| 25 | + setTargetDir: vi.fn().mockReturnValue('/tmp'), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock('../config/settings.js', () => ({ |
| 29 | + loadSettings: vi.fn().mockReturnValue({}), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('../config/extension.js', () => ({ |
| 33 | + loadExtensions: vi.fn().mockReturnValue([]), |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock('../http/requestStorage.js', () => ({ |
| 37 | + requestStorage: { |
| 38 | + getStore: vi.fn(), |
| 39 | + }, |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock('./task.js', () => { |
| 43 | + const mockTaskInstance = (taskId: string, contextId: string) => ({ |
| 44 | + id: taskId, |
| 45 | + contextId, |
| 46 | + taskState: 'working', |
| 47 | + acceptUserMessage: vi |
| 48 | + .fn() |
| 49 | + .mockImplementation(async function* (context, aborted) { |
| 50 | + const isConfirmation = ( |
| 51 | + context.userMessage.parts as Array<{ kind: string }> |
| 52 | + ).some((p) => p.kind === 'confirmation'); |
| 53 | + // Hang only for main user messages (text), allow confirmations to finish quickly |
| 54 | + if (!isConfirmation && aborted) { |
| 55 | + await new Promise((resolve) => { |
| 56 | + aborted.addEventListener('abort', resolve, { once: true }); |
| 57 | + }); |
| 58 | + } |
| 59 | + yield { type: 'content', value: 'hello' }; |
| 60 | + }), |
| 61 | + acceptAgentMessage: vi.fn().mockResolvedValue(undefined), |
| 62 | + scheduleToolCalls: vi.fn().mockResolvedValue(undefined), |
| 63 | + waitForPendingTools: vi.fn().mockResolvedValue(undefined), |
| 64 | + getAndClearCompletedTools: vi.fn().mockReturnValue([]), |
| 65 | + addToolResponsesToHistory: vi.fn(), |
| 66 | + sendCompletedToolsToLlm: vi.fn().mockImplementation(async function* () {}), |
| 67 | + cancelPendingTools: vi.fn(), |
| 68 | + setTaskStateAndPublishUpdate: vi.fn(), |
| 69 | + dispose: vi.fn(), |
| 70 | + getMetadata: vi.fn().mockResolvedValue({}), |
| 71 | + geminiClient: { |
| 72 | + initialize: vi.fn().mockResolvedValue(undefined), |
| 73 | + }, |
| 74 | + toSDKTask: () => ({ |
| 75 | + id: taskId, |
| 76 | + contextId, |
| 77 | + kind: 'task', |
| 78 | + status: { state: 'working', timestamp: new Date().toISOString() }, |
| 79 | + metadata: {}, |
| 80 | + history: [], |
| 81 | + artifacts: [], |
| 82 | + }), |
| 83 | + }); |
| 84 | + |
| 85 | + const MockTask = vi.fn().mockImplementation(mockTaskInstance); |
| 86 | + (MockTask as unknown as { create: Mock }).create = vi |
| 87 | + .fn() |
| 88 | + .mockImplementation(async (taskId: string, contextId: string) => |
| 89 | + mockTaskInstance(taskId, contextId), |
| 90 | + ); |
| 91 | + |
| 92 | + return { Task: MockTask }; |
| 93 | +}); |
| 94 | + |
| 95 | +describe('CoderAgentExecutor', () => { |
| 96 | + let executor: CoderAgentExecutor; |
| 97 | + let mockTaskStore: TaskStore; |
| 98 | + let mockEventBus: ExecutionEventBus; |
| 99 | + |
| 100 | + beforeEach(() => { |
| 101 | + vi.clearAllMocks(); |
| 102 | + mockTaskStore = { |
| 103 | + save: vi.fn().mockResolvedValue(undefined), |
| 104 | + load: vi.fn().mockResolvedValue(undefined), |
| 105 | + delete: vi.fn().mockResolvedValue(undefined), |
| 106 | + list: vi.fn().mockResolvedValue([]), |
| 107 | + } as unknown as TaskStore; |
| 108 | + |
| 109 | + mockEventBus = new EventEmitter() as unknown as ExecutionEventBus; |
| 110 | + mockEventBus.publish = vi.fn(); |
| 111 | + mockEventBus.finished = vi.fn(); |
| 112 | + |
| 113 | + executor = new CoderAgentExecutor(mockTaskStore); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should distinguish between primary and secondary execution', async () => { |
| 117 | + const taskId = 'test-task'; |
| 118 | + const contextId = 'test-context'; |
| 119 | + |
| 120 | + const mockSocket = new EventEmitter(); |
| 121 | + const requestContext = { |
| 122 | + userMessage: { |
| 123 | + messageId: 'msg-1', |
| 124 | + taskId, |
| 125 | + contextId, |
| 126 | + parts: [{ kind: 'text', text: 'hi' }], |
| 127 | + metadata: { |
| 128 | + coderAgent: { kind: 'agent-settings', workspacePath: '/tmp' }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + } as unknown as RequestContext; |
| 132 | + |
| 133 | + // Mock requestStorage for primary |
| 134 | + (requestStorage.getStore as Mock).mockReturnValue({ |
| 135 | + req: { socket: mockSocket }, |
| 136 | + }); |
| 137 | + |
| 138 | + // First execution (Primary) |
| 139 | + const primaryPromise = executor.execute(requestContext, mockEventBus); |
| 140 | + |
| 141 | + // Give it enough time to reach line 490 in executor.ts |
| 142 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 143 | + |
| 144 | + expect( |
| 145 | + ( |
| 146 | + executor as unknown as { executingTasks: Set<string> } |
| 147 | + ).executingTasks.has(taskId), |
| 148 | + ).toBe(true); |
| 149 | + const wrapper = executor.getTask(taskId); |
| 150 | + expect(wrapper).toBeDefined(); |
| 151 | + |
| 152 | + // Mock requestStorage for secondary |
| 153 | + const secondarySocket = new EventEmitter(); |
| 154 | + (requestStorage.getStore as Mock).mockReturnValue({ |
| 155 | + req: { socket: secondarySocket }, |
| 156 | + }); |
| 157 | + |
| 158 | + const secondaryRequestContext = { |
| 159 | + userMessage: { |
| 160 | + messageId: 'msg-2', |
| 161 | + taskId, |
| 162 | + contextId, |
| 163 | + parts: [{ kind: 'confirmation', callId: '1', outcome: 'proceed' }], |
| 164 | + metadata: { |
| 165 | + coderAgent: { kind: 'agent-settings', workspacePath: '/tmp' }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + } as unknown as RequestContext; |
| 169 | + |
| 170 | + const secondaryPromise = executor.execute( |
| 171 | + secondaryRequestContext, |
| 172 | + mockEventBus, |
| 173 | + ); |
| 174 | + |
| 175 | + // Secondary execution should NOT add to executingTasks (already there) |
| 176 | + // and should return early after its loop |
| 177 | + await secondaryPromise; |
| 178 | + |
| 179 | + // Task should still be in executingTasks and NOT disposed |
| 180 | + expect( |
| 181 | + ( |
| 182 | + executor as unknown as { executingTasks: Set<string> } |
| 183 | + ).executingTasks.has(taskId), |
| 184 | + ).toBe(true); |
| 185 | + expect(wrapper?.task.dispose).not.toHaveBeenCalled(); |
| 186 | + |
| 187 | + // Now simulate secondary socket closure - it should NOT affect primary |
| 188 | + secondarySocket.emit('end'); |
| 189 | + expect( |
| 190 | + ( |
| 191 | + executor as unknown as { executingTasks: Set<string> } |
| 192 | + ).executingTasks.has(taskId), |
| 193 | + ).toBe(true); |
| 194 | + expect(wrapper?.task.dispose).not.toHaveBeenCalled(); |
| 195 | + |
| 196 | + // Set to terminal state to verify disposal on finish |
| 197 | + wrapper!.task.taskState = 'completed'; |
| 198 | + |
| 199 | + // Now close primary socket |
| 200 | + mockSocket.emit('end'); |
| 201 | + |
| 202 | + await primaryPromise; |
| 203 | + |
| 204 | + expect( |
| 205 | + ( |
| 206 | + executor as unknown as { executingTasks: Set<string> } |
| 207 | + ).executingTasks.has(taskId), |
| 208 | + ).toBe(false); |
| 209 | + expect(wrapper?.task.dispose).toHaveBeenCalled(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should evict task from cache when it reaches terminal state', async () => { |
| 213 | + const taskId = 'test-task-terminal'; |
| 214 | + const contextId = 'test-context'; |
| 215 | + |
| 216 | + const mockSocket = new EventEmitter(); |
| 217 | + (requestStorage.getStore as Mock).mockReturnValue({ |
| 218 | + req: { socket: mockSocket }, |
| 219 | + }); |
| 220 | + |
| 221 | + const requestContext = { |
| 222 | + userMessage: { |
| 223 | + messageId: 'msg-1', |
| 224 | + taskId, |
| 225 | + contextId, |
| 226 | + parts: [{ kind: 'text', text: 'hi' }], |
| 227 | + metadata: { |
| 228 | + coderAgent: { kind: 'agent-settings', workspacePath: '/tmp' }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + } as unknown as RequestContext; |
| 232 | + |
| 233 | + const primaryPromise = executor.execute(requestContext, mockEventBus); |
| 234 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 235 | + |
| 236 | + const wrapper = executor.getTask(taskId)!; |
| 237 | + expect(wrapper).toBeDefined(); |
| 238 | + // Simulate terminal state |
| 239 | + wrapper.task.taskState = 'completed'; |
| 240 | + |
| 241 | + // Finish primary execution |
| 242 | + mockSocket.emit('end'); |
| 243 | + await primaryPromise; |
| 244 | + |
| 245 | + expect(executor.getTask(taskId)).toBeUndefined(); |
| 246 | + expect(wrapper.task.dispose).toHaveBeenCalled(); |
| 247 | + }); |
| 248 | +}); |
0 commit comments