forked from google-gemini/gemini-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowserAgentFactory.test.ts
More file actions
287 lines (246 loc) · 8.78 KB
/
browserAgentFactory.test.ts
File metadata and controls
287 lines (246 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
createBrowserAgentDefinition,
cleanupBrowserAgent,
} from './browserAgentFactory.js';
import { injectAutomationOverlay } from './automationOverlay.js';
import { makeFakeConfig } from '../../test-utils/config.js';
import type { Config } from '../../config/config.js';
import type { MessageBus } from '../../confirmation-bus/message-bus.js';
import type { BrowserManager } from './browserManager.js';
// Create mock browser manager
const mockBrowserManager = {
ensureConnection: vi.fn().mockResolvedValue(undefined),
getDiscoveredTools: vi.fn().mockResolvedValue([
// Semantic tools
{ name: 'take_snapshot', description: 'Take snapshot' },
{ name: 'click', description: 'Click element' },
{ name: 'fill', description: 'Fill form field' },
{ name: 'navigate_page', description: 'Navigate to URL' },
// Visual tools (from --experimental-vision)
{ name: 'click_at', description: 'Click at coordinates' },
]),
callTool: vi.fn().mockResolvedValue({ content: [] }),
close: vi.fn().mockResolvedValue(undefined),
};
// Mock dependencies
vi.mock('./browserManager.js', () => ({
BrowserManager: vi.fn(() => mockBrowserManager),
}));
vi.mock('./automationOverlay.js', () => ({
injectAutomationOverlay: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('../../utils/debugLogger.js', () => ({
debugLogger: {
log: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
import {
buildBrowserSystemPrompt,
BROWSER_AGENT_NAME,
} from './browserAgentDefinition.js';
describe('browserAgentFactory', () => {
let mockConfig: Config;
let mockMessageBus: MessageBus;
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(injectAutomationOverlay).mockClear();
// Reset mock implementations
mockBrowserManager.ensureConnection.mockResolvedValue(undefined);
mockBrowserManager.getDiscoveredTools.mockResolvedValue([
// Semantic tools
{ name: 'take_snapshot', description: 'Take snapshot' },
{ name: 'click', description: 'Click element' },
{ name: 'fill', description: 'Fill form field' },
{ name: 'navigate_page', description: 'Navigate to URL' },
// Visual tools (from --experimental-vision)
{ name: 'click_at', description: 'Click at coordinates' },
]);
mockBrowserManager.close.mockResolvedValue(undefined);
mockConfig = makeFakeConfig({
agents: {
overrides: {
browser_agent: {
enabled: true,
},
},
browser: {
headless: false,
},
},
});
mockMessageBus = {
publish: vi.fn().mockResolvedValue(undefined),
subscribe: vi.fn(),
unsubscribe: vi.fn(),
} as unknown as MessageBus;
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('createBrowserAgentDefinition', () => {
it('should ensure browser connection', async () => {
await createBrowserAgentDefinition(mockConfig, mockMessageBus);
expect(mockBrowserManager.ensureConnection).toHaveBeenCalled();
});
it('should inject automation overlay when not in headless mode', async () => {
await createBrowserAgentDefinition(mockConfig, mockMessageBus);
expect(injectAutomationOverlay).toHaveBeenCalledWith(mockBrowserManager);
});
it('should not inject automation overlay when in headless mode', async () => {
const headlessConfig = makeFakeConfig({
agents: {
overrides: {
browser_agent: {
enabled: true,
},
},
browser: {
headless: true,
},
},
});
await createBrowserAgentDefinition(headlessConfig, mockMessageBus);
expect(injectAutomationOverlay).not.toHaveBeenCalled();
});
it('should return agent definition with discovered tools', async () => {
const { definition } = await createBrowserAgentDefinition(
mockConfig,
mockMessageBus,
);
expect(definition.name).toBe(BROWSER_AGENT_NAME);
// 5 MCP tools + 1 type_text composite tool (no analyze_screenshot without visualModel)
expect(definition.toolConfig?.tools).toHaveLength(6);
});
it('should return browser manager for cleanup', async () => {
const { browserManager } = await createBrowserAgentDefinition(
mockConfig,
mockMessageBus,
);
expect(browserManager).toBeDefined();
});
it('should call printOutput when provided', async () => {
const printOutput = vi.fn();
await createBrowserAgentDefinition(
mockConfig,
mockMessageBus,
printOutput,
);
expect(printOutput).toHaveBeenCalled();
});
it('should create definition with correct structure', async () => {
const { definition } = await createBrowserAgentDefinition(
mockConfig,
mockMessageBus,
);
expect(definition.kind).toBe('local');
expect(definition.inputConfig).toBeDefined();
expect(definition.outputConfig).toBeDefined();
expect(definition.promptConfig).toBeDefined();
});
it('should exclude visual prompt section when visualModel is not configured', async () => {
const { definition } = await createBrowserAgentDefinition(
mockConfig,
mockMessageBus,
);
const systemPrompt = definition.promptConfig?.systemPrompt ?? '';
expect(systemPrompt).not.toContain('analyze_screenshot');
expect(systemPrompt).not.toContain('VISUAL IDENTIFICATION');
});
it('should include visual prompt section when visualModel is configured', async () => {
const configWithVision = makeFakeConfig({
agents: {
overrides: {
browser_agent: {
enabled: true,
},
},
browser: {
headless: false,
visualModel: 'gemini-2.5-flash-preview',
},
},
});
const { definition } = await createBrowserAgentDefinition(
configWithVision,
mockMessageBus,
);
const systemPrompt = definition.promptConfig?.systemPrompt ?? '';
expect(systemPrompt).toContain('analyze_screenshot');
expect(systemPrompt).toContain('VISUAL IDENTIFICATION');
});
it('should include analyze_screenshot tool when visualModel is configured', async () => {
const configWithVision = makeFakeConfig({
agents: {
overrides: {
browser_agent: {
enabled: true,
},
},
browser: {
headless: false,
visualModel: 'gemini-2.5-flash-preview',
},
},
});
const { definition } = await createBrowserAgentDefinition(
configWithVision,
mockMessageBus,
);
// 5 MCP tools + 1 type_text + 1 analyze_screenshot
expect(definition.toolConfig?.tools).toHaveLength(7);
const toolNames =
definition.toolConfig?.tools
?.filter(
(t): t is { name: string } => typeof t === 'object' && 'name' in t,
)
.map((t) => t.name) ?? [];
expect(toolNames).toContain('analyze_screenshot');
});
});
describe('cleanupBrowserAgent', () => {
it('should call close on browser manager', async () => {
await cleanupBrowserAgent(
mockBrowserManager as unknown as BrowserManager,
);
expect(mockBrowserManager.close).toHaveBeenCalled();
});
it('should handle errors during cleanup gracefully', async () => {
const errorManager = {
close: vi.fn().mockRejectedValue(new Error('Close failed')),
} as unknown as BrowserManager;
// Should not throw
await expect(cleanupBrowserAgent(errorManager)).resolves.toBeUndefined();
});
});
});
describe('buildBrowserSystemPrompt', () => {
it('should include visual section when vision is enabled', () => {
const prompt = buildBrowserSystemPrompt(true);
expect(prompt).toContain('VISUAL IDENTIFICATION');
expect(prompt).toContain('analyze_screenshot');
expect(prompt).toContain('click_at');
});
it('should exclude visual section when vision is disabled', () => {
const prompt = buildBrowserSystemPrompt(false);
expect(prompt).not.toContain('VISUAL IDENTIFICATION');
expect(prompt).not.toContain('analyze_screenshot');
});
it('should always include core sections regardless of vision', () => {
for (const visionEnabled of [true, false]) {
const prompt = buildBrowserSystemPrompt(visionEnabled);
expect(prompt).toContain('PARALLEL TOOL CALLS');
expect(prompt).toContain('OVERLAY/POPUP HANDLING');
expect(prompt).toContain('COMPLEX WEB APPS');
expect(prompt).toContain('TERMINAL FAILURES');
expect(prompt).toContain('complete_task');
}
});
});