|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { test } from 'vitest'; |
3 | 3 | import { WEB_DESKTOP_DEVICE } from '../../../src/__tests__/test-utils/device-fixtures.ts'; |
4 | | -import type { WebProvider } from '@agent-device/platform-web'; |
| 4 | +import { createAgentBrowserWebProvider, type WebProvider } from '@agent-device/platform-web'; |
| 5 | +import { withCommandExecutorOverride } from '@agent-device/host-kit/command'; |
| 6 | +import { mkdtempForTestSync } from '../../../src/__tests__/test-utils/tmp-dir.ts'; |
| 7 | +import { |
| 8 | + installFakeManagedAgentBrowser, |
| 9 | + withNodeRuntime, |
| 10 | +} from '../../../src/__tests__/test-utils/web-managed-agent-browser.ts'; |
5 | 11 | import { createProviderScenarioHarness } from './harness.ts'; |
6 | 12 |
|
7 | 13 | test('web provider is scoped through the request router and dispatch path', async () => { |
@@ -200,3 +206,85 @@ test('web provider is scoped through the request router and dispatch path', asyn |
200 | 206 | await harness.close(); |
201 | 207 | } |
202 | 208 | }); |
| 209 | + |
| 210 | +test('non-dense browser refs survive snapshot storage and routed fill and click', async () => { |
| 211 | + const calls: string[][] = []; |
| 212 | + const values: Record<string, string> = { '@e2': '', '@e3': '' }; |
| 213 | + let signedIn = false; |
| 214 | + const stateDir = mkdtempForTestSync('web-ref-route-'); |
| 215 | + installFakeManagedAgentBrowser(stateDir); |
| 216 | + await withNodeRuntime({ version: '24.0.0' }, async () => { |
| 217 | + const provider = await createAgentBrowserWebProvider({ stateDir }); |
| 218 | + await withCommandExecutorOverride( |
| 219 | + async (_command, args) => { |
| 220 | + const [command, ref, text] = args.slice(1); |
| 221 | + let data: unknown = {}; |
| 222 | + if (command === 'snapshot') { |
| 223 | + data = { |
| 224 | + snapshot: [ |
| 225 | + '- textbox "Username" [ref=e2]', |
| 226 | + '- textbox "Passcode" [ref=e3]', |
| 227 | + '- button "Sign in" [ref=e4]', |
| 228 | + ].join('\n'), |
| 229 | + refs: { |
| 230 | + e2: { role: 'textbox', name: 'Username' }, |
| 231 | + e3: { role: 'textbox', name: 'Passcode' }, |
| 232 | + e4: { role: 'button', name: 'Sign in' }, |
| 233 | + }, |
| 234 | + }; |
| 235 | + } else if (command === 'fill') { |
| 236 | + calls.push(['fill', ref!, text!]); |
| 237 | + values[ref!] = text!; |
| 238 | + } else if (command === 'click') { |
| 239 | + calls.push(['click', ref!]); |
| 240 | + signedIn = ref === '@e4'; |
| 241 | + } else { |
| 242 | + assert.ok(command === 'open' || command === 'close', `Unexpected command: ${args}`); |
| 243 | + } |
| 244 | + return { stdout: JSON.stringify({ success: true, data }), stderr: '', exitCode: 0 }; |
| 245 | + }, |
| 246 | + async () => { |
| 247 | + const harness = await createProviderScenarioHarness({ |
| 248 | + deviceInventoryProvider: async () => [WEB_DESKTOP_DEVICE], |
| 249 | + platformRuntime: true, |
| 250 | + webProvider: () => provider, |
| 251 | + }); |
| 252 | + try { |
| 253 | + const open = await harness.callCommand('open', ['https://example.test/login'], { |
| 254 | + platform: 'web', |
| 255 | + }); |
| 256 | + assert.equal(open.json.error, undefined); |
| 257 | + const snapshot = await harness.callCommand('snapshot', [], { platform: 'web' }); |
| 258 | + assert.equal(snapshot.json.error, undefined); |
| 259 | + const nodes = snapshot.json.result.data.nodes; |
| 260 | + assert.deepEqual( |
| 261 | + nodes.map((node: { label: string; ref: string }) => [node.label, node.ref]), |
| 262 | + [ |
| 263 | + ['Username', 'e2'], |
| 264 | + ['Passcode', 'e3'], |
| 265 | + ['Sign in', 'e4'], |
| 266 | + ], |
| 267 | + ); |
| 268 | + const fill = await harness.callCommand('fill', [`@${nodes[0].ref}`, 'Ada']); |
| 269 | + assert.equal(fill.json.error, undefined); |
| 270 | + assert.deepEqual(values, { '@e2': 'Ada', '@e3': '' }); |
| 271 | + |
| 272 | + const refreshed = await harness.callCommand('snapshot'); |
| 273 | + assert.equal(refreshed.json.error, undefined); |
| 274 | + const button = refreshed.json.result.data.nodes.find( |
| 275 | + (node: { label: string }) => node.label === 'Sign in', |
| 276 | + ); |
| 277 | + const click = await harness.callCommand('click', [`@${button.ref}`]); |
| 278 | + assert.equal(click.json.error, undefined); |
| 279 | + assert.equal(signedIn, true); |
| 280 | + assert.deepEqual(calls, [ |
| 281 | + ['fill', '@e2', 'Ada'], |
| 282 | + ['click', '@e4'], |
| 283 | + ]); |
| 284 | + } finally { |
| 285 | + await harness.close(); |
| 286 | + } |
| 287 | + }, |
| 288 | + ); |
| 289 | + }); |
| 290 | +}); |
0 commit comments