|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { createAgentBrowserWebProvider } from './agent-browser-provider.ts'; |
| 4 | +import { withCommandExecutorOverride, type ExecResult } from '../../utils/exec.ts'; |
| 5 | +import { AppError } from '../../utils/errors.ts'; |
| 6 | + |
| 7 | +type AgentBrowserCall = { |
| 8 | + cmd: string; |
| 9 | + args: string[]; |
| 10 | +}; |
| 11 | + |
| 12 | +test('agent-browser provider maps supported operations to session-scoped JSON commands', async () => { |
| 13 | + const calls: AgentBrowserCall[] = []; |
| 14 | + const provider = createAgentBrowserWebProvider({ session: 'web-session' }); |
| 15 | + |
| 16 | + await withCommandExecutorOverride(recordingExecutor(calls), async () => { |
| 17 | + await provider.open('https://example.test'); |
| 18 | + await provider.screenshot('/tmp/page.png', { fullscreen: true }); |
| 19 | + await provider.click(10.4, 20.6); |
| 20 | + await provider.fill(11, 22, 'Ada'); |
| 21 | + await provider.typeText('hello'); |
| 22 | + await provider.scroll('down', { pixels: 400 }); |
| 23 | + await provider.close(); |
| 24 | + }); |
| 25 | + |
| 26 | + assert.deepEqual( |
| 27 | + calls.map((call) => call.args), |
| 28 | + [ |
| 29 | + ['open', 'https://example.test', '--json', '--session', 'web-session'], |
| 30 | + ['screenshot', '--full', '/tmp/page.png', '--json', '--session', 'web-session'], |
| 31 | + ['mouse', 'move', '10', '21', '--json', '--session', 'web-session'], |
| 32 | + ['mouse', 'down', '--json', '--session', 'web-session'], |
| 33 | + ['mouse', 'up', '--json', '--session', 'web-session'], |
| 34 | + ['mouse', 'move', '11', '22', '--json', '--session', 'web-session'], |
| 35 | + ['mouse', 'down', '--json', '--session', 'web-session'], |
| 36 | + ['mouse', 'up', '--json', '--session', 'web-session'], |
| 37 | + ['press', 'Control+a', '--json', '--session', 'web-session'], |
| 38 | + ['keyboard', 'type', 'Ada', '--json', '--session', 'web-session'], |
| 39 | + ['keyboard', 'type', 'hello', '--json', '--session', 'web-session'], |
| 40 | + ['scroll', 'down', '400', '--json', '--session', 'web-session'], |
| 41 | + ['close', '--json', '--session', 'web-session'], |
| 42 | + ], |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | +test('agent-browser provider normalizes snapshot refs, labels, values, parents, and rects', async () => { |
| 47 | + const calls: AgentBrowserCall[] = []; |
| 48 | + const provider = createAgentBrowserWebProvider({ session: 'web-session' }); |
| 49 | + |
| 50 | + const snapshot = await withCommandExecutorOverride( |
| 51 | + async (cmd, args) => { |
| 52 | + calls.push({ cmd, args }); |
| 53 | + if (args[0] === 'snapshot') { |
| 54 | + return jsonResult({ |
| 55 | + success: true, |
| 56 | + data: { |
| 57 | + refs: { |
| 58 | + e1: { role: 'heading', name: 'Welcome' }, |
| 59 | + e2: { role: 'textbox', value: 'Ada' }, |
| 60 | + e3: { role: 'button', name: 'Save' }, |
| 61 | + e4: { role: 'link', name: 'Docs' }, |
| 62 | + }, |
| 63 | + snapshot: [ |
| 64 | + 'heading "Welcome" [ref=e1]', |
| 65 | + ' textbox "Name" value="Ada" [ref=e2]', |
| 66 | + ' button "Save" [ref=e3]', |
| 67 | + ' link "Docs" [ref=e4]', |
| 68 | + ].join('\n'), |
| 69 | + truncated: false, |
| 70 | + }, |
| 71 | + }); |
| 72 | + } |
| 73 | + if (args.slice(0, 3).join(' ') === 'get box @e3') { |
| 74 | + return jsonResult({ success: false, error: 'No box for element' }); |
| 75 | + } |
| 76 | + if (args[0] === 'get' && args[1] === 'box') { |
| 77 | + const ref = args[2]; |
| 78 | + const offset = ref === '@e1' ? 0 : ref === '@e2' ? 10 : 30; |
| 79 | + return jsonResult({ |
| 80 | + success: true, |
| 81 | + data: { x: offset + 1, y: offset + 2, width: 100, height: 20 }, |
| 82 | + }); |
| 83 | + } |
| 84 | + return jsonResult({ success: true, data: {} }); |
| 85 | + }, |
| 86 | + async () => await provider.snapshot({ interactiveOnly: true, depth: 4, scope: '#main' }), |
| 87 | + ); |
| 88 | + |
| 89 | + assert.deepEqual(calls[0]?.args, [ |
| 90 | + 'snapshot', |
| 91 | + '--interactive', |
| 92 | + '--compact', |
| 93 | + '--depth', |
| 94 | + '4', |
| 95 | + '--selector', |
| 96 | + '#main', |
| 97 | + '--json', |
| 98 | + '--session', |
| 99 | + 'web-session', |
| 100 | + ]); |
| 101 | + assert.deepEqual(snapshot.nodes, [ |
| 102 | + { |
| 103 | + index: 0, |
| 104 | + role: 'heading', |
| 105 | + label: 'Welcome', |
| 106 | + value: undefined, |
| 107 | + depth: 0, |
| 108 | + enabled: undefined, |
| 109 | + focused: undefined, |
| 110 | + rect: { x: 1, y: 2, width: 100, height: 20 }, |
| 111 | + }, |
| 112 | + { |
| 113 | + index: 1, |
| 114 | + role: 'textbox', |
| 115 | + label: 'Name', |
| 116 | + value: 'Ada', |
| 117 | + depth: 1, |
| 118 | + enabled: undefined, |
| 119 | + focused: undefined, |
| 120 | + parentIndex: 0, |
| 121 | + rect: { x: 11, y: 12, width: 100, height: 20 }, |
| 122 | + }, |
| 123 | + { |
| 124 | + index: 2, |
| 125 | + role: 'button', |
| 126 | + label: 'Save', |
| 127 | + value: undefined, |
| 128 | + depth: 1, |
| 129 | + enabled: undefined, |
| 130 | + focused: undefined, |
| 131 | + parentIndex: 0, |
| 132 | + }, |
| 133 | + { |
| 134 | + index: 3, |
| 135 | + role: 'link', |
| 136 | + label: 'Docs', |
| 137 | + value: undefined, |
| 138 | + depth: 1, |
| 139 | + enabled: undefined, |
| 140 | + focused: undefined, |
| 141 | + parentIndex: 0, |
| 142 | + rect: { x: 31, y: 32, width: 100, height: 20 }, |
| 143 | + }, |
| 144 | + ]); |
| 145 | +}); |
| 146 | + |
| 147 | +test('agent-browser provider surfaces stale ref failures during snapshot geometry lookup', async () => { |
| 148 | + const provider = createAgentBrowserWebProvider({ session: 'web-session' }); |
| 149 | + |
| 150 | + await assert.rejects( |
| 151 | + () => |
| 152 | + withCommandExecutorOverride( |
| 153 | + async (_cmd, args) => { |
| 154 | + if (args[0] === 'snapshot') { |
| 155 | + return jsonResult({ |
| 156 | + success: true, |
| 157 | + data: { |
| 158 | + refs: { e1: { role: 'button', name: 'Save' } }, |
| 159 | + snapshot: 'button "Save" [ref=e1]', |
| 160 | + }, |
| 161 | + }); |
| 162 | + } |
| 163 | + return jsonResult({ success: false, error: 'Stale ref @e1' }); |
| 164 | + }, |
| 165 | + async () => await provider.snapshot(), |
| 166 | + ), |
| 167 | + (error: unknown) => |
| 168 | + error instanceof AppError && |
| 169 | + error.code === 'COMMAND_FAILED' && |
| 170 | + error.message === 'Stale ref @e1', |
| 171 | + ); |
| 172 | +}); |
| 173 | + |
| 174 | +test('agent-browser provider adds doctor guidance for missing binary and invalid JSON', async () => { |
| 175 | + const provider = createAgentBrowserWebProvider(); |
| 176 | + |
| 177 | + await assert.rejects( |
| 178 | + () => |
| 179 | + withCommandExecutorOverride( |
| 180 | + async () => { |
| 181 | + throw new AppError('TOOL_MISSING', 'agent-browser not found'); |
| 182 | + }, |
| 183 | + async () => await provider.open('https://example.test'), |
| 184 | + ), |
| 185 | + (error: unknown) => |
| 186 | + error instanceof AppError && |
| 187 | + error.code === 'TOOL_MISSING' && |
| 188 | + error.details?.hint === |
| 189 | + 'Install agent-browser and run `agent-browser doctor --offline --quick` to verify the local browser setup.', |
| 190 | + ); |
| 191 | + |
| 192 | + await assert.rejects( |
| 193 | + () => |
| 194 | + withCommandExecutorOverride( |
| 195 | + async () => ({ stdout: 'not-json', stderr: '', exitCode: 0 }), |
| 196 | + async () => await provider.open('https://example.test'), |
| 197 | + ), |
| 198 | + (error: unknown) => |
| 199 | + error instanceof AppError && |
| 200 | + error.code === 'COMMAND_FAILED' && |
| 201 | + error.message === 'agent-browser returned invalid JSON' && |
| 202 | + typeof error.details?.hint === 'string', |
| 203 | + ); |
| 204 | +}); |
| 205 | + |
| 206 | +function recordingExecutor(calls: AgentBrowserCall[]) { |
| 207 | + return async (cmd: string, args: string[]): Promise<ExecResult> => { |
| 208 | + calls.push({ cmd, args }); |
| 209 | + return jsonResult({ success: true, data: {} }); |
| 210 | + }; |
| 211 | +} |
| 212 | + |
| 213 | +function jsonResult(value: unknown): ExecResult { |
| 214 | + return { stdout: JSON.stringify(value), stderr: '', exitCode: 0 }; |
| 215 | +} |
0 commit comments