oira666_pi-browser-agent 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +101 -0
- package/package.json +28 -0
- package/src/broker/protocol.ts +127 -0
- package/src/broker/server.ts +331 -0
- package/src/broker/task-store.ts +192 -0
- package/src/index.ts +132 -0
- package/src/tools/_register.ts +89 -0
- package/src/tools/browser_agent_tools.ts +72 -0
- package/src/tools/browser_js_navigation_tools.ts +458 -0
- package/src/tools/browser_observability_tools.ts +167 -0
- package/src/tools/browser_task_tools.ts +383 -0
- package/src/tools/read_only/common.ts +173 -0
- package/src/tools/read_only/index.ts +312 -0
- package/src/util/paths.ts +19 -0
- package/src/util/truncate.ts +34 -0
- package/tests/broker.test.ts +241 -0
- package/tests/index.test.ts +153 -0
- package/tests/protocol.test.ts +48 -0
- package/tests/task-store.test.ts +35 -0
- package/tests/tools/browser-js-navigation-tools.test.ts +223 -0
- package/tests/tools/browser-observability-tools.test.ts +100 -0
- package/tests/tools/browser-read-only-tools.test.ts +225 -0
- package/tests/tools/browser-task-tools.test.ts +348 -0
- package/tests/tools/meta-tool.test.ts +110 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
|
|
4
|
+
import type { ResponseFrame } from '../../src/broker/protocol.ts';
|
|
5
|
+
import {
|
|
6
|
+
browserClearSiteDataDefinition,
|
|
7
|
+
browserEvaluateJsDefinition,
|
|
8
|
+
browserRunJsDefinition,
|
|
9
|
+
createBrowserClearSiteDataTool,
|
|
10
|
+
createBrowserCloseTabTool,
|
|
11
|
+
createBrowserEvaluateJsTool,
|
|
12
|
+
createBrowserJsNavigationToolFamily,
|
|
13
|
+
createBrowserNavigateTool,
|
|
14
|
+
createBrowserReloadExtensionTool,
|
|
15
|
+
createBrowserReloadTool,
|
|
16
|
+
createBrowserRunJsTool,
|
|
17
|
+
createBrowserSwitchTabTool,
|
|
18
|
+
} from '../../src/tools/browser_js_navigation_tools.ts';
|
|
19
|
+
|
|
20
|
+
function textOf(result: any): string {
|
|
21
|
+
return result.content[0]?.text || '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createBrokerStub(handler: (type: string, params: unknown, options: { timeoutMs?: number }) => Promise<ResponseFrame>) {
|
|
25
|
+
return {
|
|
26
|
+
async request(type: string, params: unknown, options: { timeoutMs?: number }) {
|
|
27
|
+
return await handler(type, params, options);
|
|
28
|
+
},
|
|
29
|
+
} as any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('tool family builders expose all expected JS/navigation/destructive tools', () => {
|
|
33
|
+
const broker = createBrokerStub(async () => ({ v: 1, kind: 'response', id: 'unused', ok: true, data: {} }));
|
|
34
|
+
const family = createBrowserJsNavigationToolFamily(broker).map((tool) => tool.name);
|
|
35
|
+
assert.deepEqual(family, [
|
|
36
|
+
'browser_evaluate_js',
|
|
37
|
+
'browser_run_js',
|
|
38
|
+
'browser_navigate',
|
|
39
|
+
'browser_switch_tab',
|
|
40
|
+
'browser_close_tab',
|
|
41
|
+
'browser_reload',
|
|
42
|
+
'browser_reload_extension',
|
|
43
|
+
'browser_clear_site_data',
|
|
44
|
+
]);
|
|
45
|
+
assert.equal('url' in (browserEvaluateJsDefinition.parameters as any).properties, false);
|
|
46
|
+
assert.equal('url' in (browserRunJsDefinition.parameters as any).properties, false);
|
|
47
|
+
assert.equal((browserClearSiteDataDefinition.parameters as any).properties.types.minItems, 1);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('browser_evaluate_js forwards timeout-aware requests, defaults to the active tab, and truncates large payloads', async () => {
|
|
51
|
+
let captured: { type: string; params: any; timeoutMs?: number } | null = null;
|
|
52
|
+
const broker = createBrokerStub(async (type, params, options) => {
|
|
53
|
+
captured = { type, params, timeoutMs: options.timeoutMs };
|
|
54
|
+
return {
|
|
55
|
+
v: 1,
|
|
56
|
+
kind: 'response',
|
|
57
|
+
id: '1',
|
|
58
|
+
ok: true,
|
|
59
|
+
data: { tabId: 7, value: 'x'.repeat(60_000) },
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const tool = createBrowserEvaluateJsTool(broker);
|
|
64
|
+
const result = await tool.execute('call-1', { expression: 'document.title', timeout_ms: 1234 });
|
|
65
|
+
|
|
66
|
+
const recorded = captured as any;
|
|
67
|
+
assert.ok(recorded);
|
|
68
|
+
assert.equal(recorded.type, 'browser_evaluate_js');
|
|
69
|
+
assert.equal(recorded.params.expression, 'document.title');
|
|
70
|
+
assert.equal(recorded.params.use_active_tab, true);
|
|
71
|
+
assert.equal(recorded.timeoutMs, 2234);
|
|
72
|
+
assert.equal(result.details.ok, true);
|
|
73
|
+
assert.equal(result.details.truncated, true);
|
|
74
|
+
assert.match(textOf(result), /Full output:/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('browser_run_js returns structured bridge failures instead of throwing', async () => {
|
|
78
|
+
const broker = createBrokerStub(async () => ({
|
|
79
|
+
v: 1,
|
|
80
|
+
kind: 'response',
|
|
81
|
+
id: '2',
|
|
82
|
+
ok: false,
|
|
83
|
+
error: { code: 'E_TIMEOUT', message: 'Timed out while evaluating JavaScript' },
|
|
84
|
+
}));
|
|
85
|
+
|
|
86
|
+
const tool = createBrowserRunJsTool(broker);
|
|
87
|
+
const result = await tool.execute('call-2', { code: 'return 1;' });
|
|
88
|
+
|
|
89
|
+
assert.equal(result.details.ok, false);
|
|
90
|
+
assert.equal((result.details as any).error.code, 'E_TIMEOUT');
|
|
91
|
+
assert.match(textOf(result), /browser_run_js failed/);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('navigation/destructive tool builders pass through successful bridge responses and default relevant tools to the active tab', async () => {
|
|
95
|
+
const calls: Array<{ type: string; params: any }> = [];
|
|
96
|
+
const broker = createBrokerStub(async (type, params) => {
|
|
97
|
+
calls.push({ type, params });
|
|
98
|
+
return {
|
|
99
|
+
v: 1,
|
|
100
|
+
kind: 'response',
|
|
101
|
+
id: type,
|
|
102
|
+
ok: true,
|
|
103
|
+
data: { tabId: 9, url: 'https://example.com', origin: 'https://example.com', closed: true, active: true, cleared: true },
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const navigate = await createBrowserNavigateTool(broker).execute('call-3', { url: 'https://example.com' });
|
|
108
|
+
assert.equal(navigate.details.ok, true);
|
|
109
|
+
assert.match(textOf(navigate), /Navigated tab 9/);
|
|
110
|
+
assert.equal(calls[0]?.params?.use_active_tab, true);
|
|
111
|
+
|
|
112
|
+
const switchTab = await createBrowserSwitchTabTool(broker).execute('call-4', { tab_id: 9 });
|
|
113
|
+
assert.match(textOf(switchTab), /Switched to tab 9/);
|
|
114
|
+
|
|
115
|
+
const closeTab = await createBrowserCloseTabTool(broker).execute('call-5', { tab_id: 9 });
|
|
116
|
+
assert.match(textOf(closeTab), /Closed tab 9/);
|
|
117
|
+
|
|
118
|
+
const reload = await createBrowserReloadTool(broker).execute('call-6', { tab_id: 9 });
|
|
119
|
+
assert.match(textOf(reload), /Reloaded tab 9/);
|
|
120
|
+
|
|
121
|
+
const clear = await createBrowserClearSiteDataTool(broker).execute('call-7', { origin: 'https://example.com' });
|
|
122
|
+
assert.match(textOf(clear), /Cleared site data/);
|
|
123
|
+
// Explicit origin/url must NOT be combined with use_active_tab: that would
|
|
124
|
+
// send conflicting selectors to the bridge.
|
|
125
|
+
assert.equal(calls.at(-1)?.params?.use_active_tab, undefined);
|
|
126
|
+
assert.equal(calls.at(-1)?.params?.origin, 'https://example.com');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('browser_clear_site_data defaults to the active tab only when no explicit target is given', async () => {
|
|
130
|
+
const calls: Array<{ type: string; params: any }> = [];
|
|
131
|
+
const broker = createBrokerStub(async (type, params) => {
|
|
132
|
+
calls.push({ type, params });
|
|
133
|
+
return {
|
|
134
|
+
v: 1,
|
|
135
|
+
kind: 'response',
|
|
136
|
+
id: type,
|
|
137
|
+
ok: true,
|
|
138
|
+
data: { origin: 'https://example.com', cleared: true },
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
await createBrowserClearSiteDataTool(broker).execute('call-c1', {});
|
|
143
|
+
assert.equal(calls.at(-1)?.params?.use_active_tab, true);
|
|
144
|
+
|
|
145
|
+
await createBrowserClearSiteDataTool(broker).execute('call-c2', { url: 'https://example.com/path' });
|
|
146
|
+
assert.equal(calls.at(-1)?.params?.use_active_tab, undefined);
|
|
147
|
+
assert.equal(calls.at(-1)?.params?.url, 'https://example.com/path');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('browser_reload_extension waits for a new bridge session after requesting reload', async () => {
|
|
151
|
+
let calls = 0;
|
|
152
|
+
const broker = {
|
|
153
|
+
async request(type: string) {
|
|
154
|
+
assert.equal(type, 'browser_reload_extension');
|
|
155
|
+
return {
|
|
156
|
+
v: 1,
|
|
157
|
+
kind: 'response',
|
|
158
|
+
id: 'ext-reload',
|
|
159
|
+
ok: true,
|
|
160
|
+
data: { extensionId: 'abc123', reloading: true },
|
|
161
|
+
} as ResponseFrame;
|
|
162
|
+
},
|
|
163
|
+
probeConnectivity() {
|
|
164
|
+
calls += 1;
|
|
165
|
+
return calls < 3
|
|
166
|
+
? { brokerReachable: true, brokerListening: true, bridgeConnected: true, bridgeSessionSerial: 1, url: 'ws://127.0.0.1:7878' }
|
|
167
|
+
: { brokerReachable: true, brokerListening: true, bridgeConnected: true, bridgeSessionSerial: 2, url: 'ws://127.0.0.1:7878' };
|
|
168
|
+
},
|
|
169
|
+
} as any;
|
|
170
|
+
|
|
171
|
+
const result = await createBrowserReloadExtensionTool(broker).execute('call-ext', { timeout_ms: 1_000 });
|
|
172
|
+
assert.equal(result.details.ok, true);
|
|
173
|
+
assert.match(textOf(result), /Reloaded browser extension abc123/);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('navigation tools reject malformed success payloads without required fields', async () => {
|
|
177
|
+
// Navigate response missing tabId should be surfaced as E_PROTOCOL.
|
|
178
|
+
const navBroker = createBrokerStub(async () => ({
|
|
179
|
+
v: 1, kind: 'response', id: 'n', ok: true, data: { url: 'https://example.com' },
|
|
180
|
+
}));
|
|
181
|
+
const navResult = await createBrowserNavigateTool(navBroker).execute('call-n', { url: 'https://example.com' });
|
|
182
|
+
assert.equal(navResult.details.ok, false);
|
|
183
|
+
assert.equal((navResult.details as any).error.code, 'E_PROTOCOL');
|
|
184
|
+
assert.match(textOf(navResult), /browser_navigate failed/);
|
|
185
|
+
|
|
186
|
+
// clear_site_data without origin in response should be E_PROTOCOL too.
|
|
187
|
+
const clearBroker = createBrokerStub(async () => ({
|
|
188
|
+
v: 1, kind: 'response', id: 'c', ok: true, data: { cleared: true },
|
|
189
|
+
}));
|
|
190
|
+
const clearResult = await createBrowserClearSiteDataTool(clearBroker).execute('call-c', { origin: 'https://example.com' });
|
|
191
|
+
assert.equal(clearResult.details.ok, false);
|
|
192
|
+
assert.equal((clearResult.details as any).error.code, 'E_PROTOCOL');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('browser_reload_extension surfaces reconnect timeout as a structured error result', async () => {
|
|
196
|
+
const broker = {
|
|
197
|
+
async request() {
|
|
198
|
+
return {
|
|
199
|
+
v: 1, kind: 'response', id: 'ext', ok: true, data: { extensionId: 'abc', reloading: true },
|
|
200
|
+
} as ResponseFrame;
|
|
201
|
+
},
|
|
202
|
+
// Always report the same bridge session serial — the tool should time out waiting for a new one.
|
|
203
|
+
probeConnectivity() {
|
|
204
|
+
return { brokerReachable: true, brokerListening: true, bridgeConnected: true, bridgeSessionSerial: 42, url: 'ws://127.0.0.1:7878' };
|
|
205
|
+
},
|
|
206
|
+
} as any;
|
|
207
|
+
|
|
208
|
+
const result = await createBrowserReloadExtensionTool(broker).execute('call-ext-t', { timeout_ms: 150 });
|
|
209
|
+
assert.equal(result.details.ok, false);
|
|
210
|
+
assert.equal((result.details as any).error.code, 'E_TIMEOUT');
|
|
211
|
+
assert.match(textOf(result), /browser_reload_extension failed/);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('tool builders convert transport exceptions into safe error payloads', async () => {
|
|
215
|
+
const broker = createBrokerStub(async () => {
|
|
216
|
+
throw { code: 'E_BRIDGE_DISCONNECTED', message: 'socket dropped' };
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const result = await createBrowserReloadTool(broker).execute('call-8', { tab_id: 9 });
|
|
220
|
+
assert.equal(result.details.ok, false);
|
|
221
|
+
assert.equal((result.details as any).error.code, 'E_BRIDGE_DISCONNECTED');
|
|
222
|
+
assert.match(textOf(result), /socket dropped/);
|
|
223
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
|
|
5
|
+
import { createBrowserGetConsoleLogsTool, createBrowserGetNetworkTool, createBufferedObservabilityToolDefinitions } from '../../src/tools/browser_observability_tools.ts';
|
|
6
|
+
|
|
7
|
+
function createBroker(overrides: Partial<any> = {}) {
|
|
8
|
+
return {
|
|
9
|
+
async request(_type: string, _params: unknown) {
|
|
10
|
+
return { ok: true, data: {} };
|
|
11
|
+
},
|
|
12
|
+
probeConnectivity() {
|
|
13
|
+
return { brokerListening: true, bridgeConnected: true, url: 'ws://127.0.0.1:7878' };
|
|
14
|
+
},
|
|
15
|
+
...overrides,
|
|
16
|
+
} as any;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function textOf(result: any): string {
|
|
20
|
+
return result.content[0]?.text || '';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
test('browser_get_console_logs tool forwards params and returns truncated previews with temp file spill', async () => {
|
|
24
|
+
let call: any = null;
|
|
25
|
+
const broker = createBroker({
|
|
26
|
+
async request(type: string, params: unknown) {
|
|
27
|
+
call = { type, params };
|
|
28
|
+
return {
|
|
29
|
+
ok: true,
|
|
30
|
+
data: {
|
|
31
|
+
tabId: 2,
|
|
32
|
+
total: 1,
|
|
33
|
+
returned: 1,
|
|
34
|
+
entries: Array.from({ length: 2500 }, (_, index) => ({ level: 'info', text: `line-${index}` })),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const tool = createBrowserGetConsoleLogsTool(broker);
|
|
41
|
+
const result = await tool.execute('call-1', { tab_id: 2, levels: ['info'], timeout_ms: 1234 });
|
|
42
|
+
|
|
43
|
+
assert.equal(call.type, 'browser_get_console_logs');
|
|
44
|
+
assert.deepEqual(call.params, { tab_id: 2, levels: ['info'], timeout_ms: 1234 });
|
|
45
|
+
assert.equal(result.details.ok, true);
|
|
46
|
+
assert.ok(result.details.fullOutputPath);
|
|
47
|
+
assert.equal((result.details as any).entries, undefined);
|
|
48
|
+
const spilled = await readFile(String(result.details.fullOutputPath), 'utf8');
|
|
49
|
+
assert.match(spilled, /line-2499/);
|
|
50
|
+
assert.match(textOf(result), /Console log query returned 1 entry/);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('browser_get_console_logs defaults to the active tab when no target is provided', async () => {
|
|
54
|
+
let call: any = null;
|
|
55
|
+
const tool = createBrowserGetConsoleLogsTool(createBroker({
|
|
56
|
+
async request(type: string, params: unknown) {
|
|
57
|
+
call = { type, params };
|
|
58
|
+
return { ok: true, data: { tabId: 7, total: 0, returned: 0, entries: [] } };
|
|
59
|
+
},
|
|
60
|
+
}));
|
|
61
|
+
|
|
62
|
+
await tool.execute('call-1', { last: 5 });
|
|
63
|
+
assert.deepEqual(call.params, { last: 5, use_active_tab: true });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('browser_get_network tool returns structured error results on bridge failure', async () => {
|
|
67
|
+
const tool = createBrowserGetNetworkTool(createBroker({
|
|
68
|
+
async request() {
|
|
69
|
+
throw { code: 'E_BRIDGE_DISCONNECTED', message: 'socket dropped' };
|
|
70
|
+
},
|
|
71
|
+
}));
|
|
72
|
+
|
|
73
|
+
const result = await tool.execute('call-1', { filter: { failed_only: true } });
|
|
74
|
+
assert.equal(result.details.ok, false);
|
|
75
|
+
assert.equal((result.details as any).error.code, 'E_BRIDGE_DISCONNECTED');
|
|
76
|
+
assert.equal((result.details as any).error.message, 'socket dropped');
|
|
77
|
+
assert.match(textOf(result), /browser_get_network failed: socket dropped/);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('browser_get_console_logs tool returns structured error results on bridge failure', async () => {
|
|
81
|
+
const tool = createBrowserGetConsoleLogsTool(createBroker({
|
|
82
|
+
async request() {
|
|
83
|
+
throw { code: 'E_BRIDGE_DISCONNECTED', message: 'socket dropped' };
|
|
84
|
+
},
|
|
85
|
+
}));
|
|
86
|
+
|
|
87
|
+
const result = await tool.execute('call-1', { last: 5 });
|
|
88
|
+
assert.equal(result.details.ok, false);
|
|
89
|
+
assert.equal((result.details as any).error.code, 'E_BRIDGE_DISCONNECTED');
|
|
90
|
+
assert.match(textOf(result), /browser_get_console_logs failed: socket dropped/);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('buffered observability tool-definition builders expose both tools', () => {
|
|
94
|
+
const definitions = createBufferedObservabilityToolDefinitions(createBroker());
|
|
95
|
+
assert.deepEqual(Object.keys(definitions).sort(), ['browser_get_console_logs', 'browser_get_network']);
|
|
96
|
+
assert.match(definitions.browser_get_network.description, /defaults to the active tab/i);
|
|
97
|
+
assert.match(definitions.browser_get_network.description, /Always pass filters/i);
|
|
98
|
+
assert.match(definitions.browser_get_console_logs.description, /defaults to the active tab/i);
|
|
99
|
+
assert.match(definitions.browser_get_console_logs.description, /Prefer passing filters/i);
|
|
100
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFile, rm } from 'node:fs/promises';
|
|
4
|
+
|
|
5
|
+
import { createReadOnlyBrowserToolMap, READ_ONLY_BROWSER_TOOL_NAMES } from '../../src/tools/read_only/index.ts';
|
|
6
|
+
|
|
7
|
+
function createBrokerHarness(handler: (type: string, params: unknown, options?: { timeoutMs?: number }) => any) {
|
|
8
|
+
return {
|
|
9
|
+
probeConnectivity() {
|
|
10
|
+
return {
|
|
11
|
+
brokerListening: true,
|
|
12
|
+
bridgeConnected: true,
|
|
13
|
+
url: 'ws://127.0.0.1:7777',
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
async request(type: string, params: unknown, options?: { timeoutMs?: number }) {
|
|
17
|
+
return await handler(type, params, options);
|
|
18
|
+
},
|
|
19
|
+
} as any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function textOf(result: any) {
|
|
23
|
+
return String(result.content?.[0]?.text || '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('read-only tool family exports exactly the planned tool names', () => {
|
|
27
|
+
assert.deepEqual(READ_ONLY_BROWSER_TOOL_NAMES, [
|
|
28
|
+
'browser_get_screenshot',
|
|
29
|
+
'browser_get_html',
|
|
30
|
+
'browser_get_dom_info',
|
|
31
|
+
'browser_get_computed_styles',
|
|
32
|
+
'browser_list_tabs',
|
|
33
|
+
'browser_wait_for',
|
|
34
|
+
'browser_get_accessibility_tree',
|
|
35
|
+
'browser_get_performance_metrics',
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('browser_get_html truncates oversized HTML and spills the full output to a temp file', async () => {
|
|
40
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async (type) => {
|
|
41
|
+
assert.equal(type, 'browser_get_html');
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
id: 'req-1',
|
|
45
|
+
kind: 'response',
|
|
46
|
+
v: 1,
|
|
47
|
+
data: {
|
|
48
|
+
url: 'https://example.com',
|
|
49
|
+
html: '<div>' + 'x'.repeat(60_000) + '</div>',
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
const result = await tools.get('browser_get_html')!.execute('call-1', { selector: '#app' });
|
|
55
|
+
assert.equal(result.details.ok, true);
|
|
56
|
+
assert.equal(result.details.truncated, true);
|
|
57
|
+
assert.match(textOf(result), /Full output:/);
|
|
58
|
+
|
|
59
|
+
const spillPath = String(result.details.fullOutputPath);
|
|
60
|
+
const spilled = await readFile(spillPath, 'utf8');
|
|
61
|
+
assert.match(spilled, /^<div>x+/);
|
|
62
|
+
await rm(spillPath, { force: true });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('browser_get_screenshot spills large inline payloads to a temp file', async () => {
|
|
66
|
+
const largeBase64 = Buffer.alloc(300 * 1024, 7).toString('base64');
|
|
67
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async (type) => {
|
|
68
|
+
assert.equal(type, 'browser_get_screenshot');
|
|
69
|
+
return {
|
|
70
|
+
ok: true,
|
|
71
|
+
id: 'req-2',
|
|
72
|
+
kind: 'response',
|
|
73
|
+
v: 1,
|
|
74
|
+
data: {
|
|
75
|
+
mime: 'image/jpeg',
|
|
76
|
+
data_base64: largeBase64,
|
|
77
|
+
width: 1280,
|
|
78
|
+
height: 720,
|
|
79
|
+
url: 'https://example.com',
|
|
80
|
+
title: 'Example',
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}));
|
|
84
|
+
|
|
85
|
+
const result = await tools.get('browser_get_screenshot')!.execute('call-1', {});
|
|
86
|
+
assert.equal(result.details.ok, true);
|
|
87
|
+
assert.equal(result.details.inlined, false);
|
|
88
|
+
assert.equal(result.details.spilledToFile, true);
|
|
89
|
+
assert.equal((result.details.result as any).data_base64, undefined);
|
|
90
|
+
|
|
91
|
+
const spillPath = String((result.details.result as any).path);
|
|
92
|
+
const spilled = await readFile(spillPath);
|
|
93
|
+
assert.equal(spilled.length, 300 * 1024);
|
|
94
|
+
await rm(spillPath, { force: true });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('browser_wait_for gives the bridge enough time to finish the requested wait window', async () => {
|
|
98
|
+
let seenTimeout: number | undefined;
|
|
99
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async (type, params, options) => {
|
|
100
|
+
assert.equal(type, 'browser_wait_for');
|
|
101
|
+
seenTimeout = options?.timeoutMs;
|
|
102
|
+
assert.deepEqual(params, { selector: '#ready', timeout_ms: 40_000 });
|
|
103
|
+
return {
|
|
104
|
+
ok: true,
|
|
105
|
+
id: 'req-3',
|
|
106
|
+
kind: 'response',
|
|
107
|
+
v: 1,
|
|
108
|
+
data: {
|
|
109
|
+
status: 'matched',
|
|
110
|
+
matched: { selector: '#ready' },
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}));
|
|
114
|
+
|
|
115
|
+
const result = await tools.get('browser_wait_for')!.execute('call-1', { selector: '#ready', timeout_ms: 40_000 });
|
|
116
|
+
assert.equal(result.details.ok, true);
|
|
117
|
+
assert.equal(seenTimeout, 41_500);
|
|
118
|
+
assert.match(textOf(result), /matched/);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('bridge transport failures become structured tool results', async () => {
|
|
122
|
+
const tools = createReadOnlyBrowserToolMap({
|
|
123
|
+
probeConnectivity() {
|
|
124
|
+
return { brokerListening: true, bridgeConnected: true, url: 'ws://127.0.0.1:7777' };
|
|
125
|
+
},
|
|
126
|
+
async request() {
|
|
127
|
+
throw Object.assign(new Error('boom'), { code: 'E_TIMEOUT', details: { timeoutMs: 1000 } });
|
|
128
|
+
},
|
|
129
|
+
} as any);
|
|
130
|
+
|
|
131
|
+
const result = await tools.get('browser_get_performance_metrics')!.execute('call-1', {});
|
|
132
|
+
assert.equal(result.details.ok, false);
|
|
133
|
+
assert.equal((result.details.error as any).code, 'E_TIMEOUT');
|
|
134
|
+
assert.match(textOf(result), /failed: boom/);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('plain broker timeout errors are surfaced as E_TIMEOUT instead of bridge disconnects', async () => {
|
|
138
|
+
const tools = createReadOnlyBrowserToolMap({
|
|
139
|
+
probeConnectivity() {
|
|
140
|
+
return { brokerListening: true, bridgeConnected: true, url: 'ws://127.0.0.1:7777' };
|
|
141
|
+
},
|
|
142
|
+
async request() {
|
|
143
|
+
throw new Error('Request timed out: browser_get_html');
|
|
144
|
+
},
|
|
145
|
+
} as any);
|
|
146
|
+
|
|
147
|
+
const result = await tools.get('browser_get_html')!.execute('call-1', {});
|
|
148
|
+
assert.equal(result.details.ok, false);
|
|
149
|
+
assert.equal((result.details.error as any).code, 'E_TIMEOUT');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
test('browser_get_html rejects malformed success payloads without html/content', async () => {
|
|
154
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async () => ({
|
|
155
|
+
ok: true,
|
|
156
|
+
id: 'req-h',
|
|
157
|
+
kind: 'response',
|
|
158
|
+
v: 1,
|
|
159
|
+
data: { url: 'https://example.com' },
|
|
160
|
+
})));
|
|
161
|
+
|
|
162
|
+
const result = await tools.get('browser_get_html')!.execute('call-1', {});
|
|
163
|
+
assert.equal(result.details.ok, false);
|
|
164
|
+
assert.equal((result.details.error as any).code, 'E_PROTOCOL');
|
|
165
|
+
assert.match(textOf(result), /did not include html/i);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('browser_get_screenshot rejects malformed success payloads without image data', async () => {
|
|
169
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async () => ({
|
|
170
|
+
ok: true,
|
|
171
|
+
id: 'req-4',
|
|
172
|
+
kind: 'response',
|
|
173
|
+
v: 1,
|
|
174
|
+
data: {
|
|
175
|
+
mime: 'image/jpeg',
|
|
176
|
+
title: 'Broken screenshot',
|
|
177
|
+
},
|
|
178
|
+
})));
|
|
179
|
+
|
|
180
|
+
const result = await tools.get('browser_get_screenshot')!.execute('call-1', {});
|
|
181
|
+
assert.equal(result.details.ok, false);
|
|
182
|
+
assert.equal((result.details.error as any).code, 'E_INTERNAL');
|
|
183
|
+
assert.match(textOf(result), /did not include image data/);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
test('browser_get_performance_metrics uses a longer timeout when web vitals are requested', async () => {
|
|
188
|
+
let seenTimeout: number | undefined;
|
|
189
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async (_type, _params, options) => {
|
|
190
|
+
seenTimeout = options?.timeoutMs;
|
|
191
|
+
return {
|
|
192
|
+
ok: true,
|
|
193
|
+
id: 'req-5',
|
|
194
|
+
kind: 'response',
|
|
195
|
+
v: 1,
|
|
196
|
+
data: { web_vitals: { lcp: 1000 } },
|
|
197
|
+
};
|
|
198
|
+
}));
|
|
199
|
+
|
|
200
|
+
const result = await tools.get('browser_get_performance_metrics')!.execute('call-1', { include: ['web_vitals'] });
|
|
201
|
+
assert.equal(result.details.ok, true);
|
|
202
|
+
assert.equal(seenTimeout, 27_500);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
test('browser_list_tabs normalizes wrapped bridge payloads', async () => {
|
|
207
|
+
const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async () => ({
|
|
208
|
+
ok: true,
|
|
209
|
+
id: 'req-6',
|
|
210
|
+
kind: 'response',
|
|
211
|
+
v: 1,
|
|
212
|
+
data: {
|
|
213
|
+
tabs: [
|
|
214
|
+
{ tabId: 11, title: 'One', url: 'https://one.test', active: false, pinned: false, is_agent_tab: false },
|
|
215
|
+
{ tabId: 12, title: 'Agent', url: 'chrome://newtab/', active: true, pinned: true, is_agent_tab: true },
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
})));
|
|
219
|
+
|
|
220
|
+
const result = await tools.get('browser_list_tabs')!.execute('call-1', {});
|
|
221
|
+
assert.equal(result.details.ok, true);
|
|
222
|
+
assert.equal((result.details.result as any[]).length, 2);
|
|
223
|
+
assert.equal((result.details.result as any[])[1].is_agent_tab, true);
|
|
224
|
+
assert.match(textOf(result), /Open browser tabs/);
|
|
225
|
+
});
|