|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { WEB_DESKTOP_DEVICE } from '../../../src/__tests__/test-utils/index.ts'; |
| 4 | +import type { WebProvider } from '../../../src/platforms/web/provider.ts'; |
| 5 | +import { createProviderScenarioHarness } from './harness.ts'; |
| 6 | + |
| 7 | +test('web provider is scoped through the request router and dispatch path', async () => { |
| 8 | + const calls: string[] = []; |
| 9 | + const webProvider: WebProvider = { |
| 10 | + async open(target) { |
| 11 | + calls.push(`open:${target}`); |
| 12 | + }, |
| 13 | + async close(target) { |
| 14 | + calls.push(`close:${target ?? ''}`); |
| 15 | + }, |
| 16 | + async snapshot(options) { |
| 17 | + calls.push(`snapshot:${options?.scope ?? ''}`); |
| 18 | + return { |
| 19 | + nodes: [ |
| 20 | + { |
| 21 | + index: 0, |
| 22 | + type: 'section', |
| 23 | + role: 'main', |
| 24 | + label: 'main', |
| 25 | + rect: { x: 0, y: 0, width: 320, height: 240 }, |
| 26 | + depth: 0, |
| 27 | + }, |
| 28 | + { |
| 29 | + index: 1, |
| 30 | + type: 'button', |
| 31 | + role: 'button', |
| 32 | + label: 'Launch', |
| 33 | + rect: { x: 10, y: 20, width: 80, height: 32 }, |
| 34 | + hittable: true, |
| 35 | + depth: 1, |
| 36 | + parentIndex: 0, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }; |
| 40 | + }, |
| 41 | + async screenshot() { |
| 42 | + calls.push('screenshot'); |
| 43 | + }, |
| 44 | + async click(x, y) { |
| 45 | + calls.push(`click:${x}:${y}`); |
| 46 | + }, |
| 47 | + async fill(x, y, text) { |
| 48 | + calls.push(`fill:${x}:${y}:${text}`); |
| 49 | + }, |
| 50 | + async typeText(text) { |
| 51 | + calls.push(`type:${text}`); |
| 52 | + }, |
| 53 | + async scroll(direction) { |
| 54 | + calls.push(`scroll:${direction}`); |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + const harness = await createProviderScenarioHarness({ |
| 59 | + deviceInventoryProvider: async () => [WEB_DESKTOP_DEVICE], |
| 60 | + webProvider: ({ device, session }) => { |
| 61 | + calls.push(`scope:${session?.name ?? 'none'}:${device.id}`); |
| 62 | + return webProvider; |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + try { |
| 67 | + const open = await harness.callCommand( |
| 68 | + 'open', |
| 69 | + ['https://example.test'], |
| 70 | + { platform: 'web' }, |
| 71 | + { meta: { requestId: 'req-web-open' } }, |
| 72 | + ); |
| 73 | + assert.equal(open.json.error, undefined); |
| 74 | + |
| 75 | + const snapshot = await harness.callCommand( |
| 76 | + 'snapshot', |
| 77 | + [], |
| 78 | + { platform: 'web', snapshotScope: 'main' }, |
| 79 | + { meta: { requestId: 'req-web-snapshot' } }, |
| 80 | + ); |
| 81 | + |
| 82 | + assert.deepEqual(snapshot.json.result.data.nodes, [ |
| 83 | + { |
| 84 | + index: 0, |
| 85 | + type: 'section', |
| 86 | + role: 'main', |
| 87 | + label: 'main', |
| 88 | + rect: { x: 0, y: 0, width: 320, height: 240 }, |
| 89 | + depth: 0, |
| 90 | + parentIndex: undefined, |
| 91 | + ref: 'e1', |
| 92 | + }, |
| 93 | + { |
| 94 | + index: 1, |
| 95 | + type: 'button', |
| 96 | + role: 'button', |
| 97 | + label: 'Launch', |
| 98 | + rect: { x: 10, y: 20, width: 80, height: 32 }, |
| 99 | + hittable: true, |
| 100 | + depth: 1, |
| 101 | + parentIndex: 0, |
| 102 | + ref: 'e2', |
| 103 | + }, |
| 104 | + ]); |
| 105 | + assert.deepEqual(calls, [ |
| 106 | + 'scope:none:agent-browser-chrome', |
| 107 | + 'open:https://example.test', |
| 108 | + 'scope:default:agent-browser-chrome', |
| 109 | + 'snapshot:main', |
| 110 | + ]); |
| 111 | + } finally { |
| 112 | + await harness.close(); |
| 113 | + } |
| 114 | +}); |
0 commit comments