|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { resolveWebProvider, withWebProvider, type WebProvider } from '../provider.ts'; |
| 5 | + |
| 6 | +// The scope's un-composed fallback is the single policy for a request that reaches a web device |
| 7 | +// without a composed provider: it refuses with `web-provider-missing` instead of borrowing a host |
| 8 | +// it was never given (#3794). |
| 9 | +test('an un-scoped web provider resolves to a refusal naming the missing composition', async () => { |
| 10 | + const provider = resolveWebProvider(); |
| 11 | + const operations: readonly [string, () => Promise<unknown>][] = [ |
| 12 | + ['open', () => provider.open('https://example.test')], |
| 13 | + ['close', () => provider.close()], |
| 14 | + ['snapshot', () => provider.snapshot()], |
| 15 | + ['screenshot', () => provider.screenshot('/tmp/out.png')], |
| 16 | + ['setViewport', () => provider.setViewport(800, 600)], |
| 17 | + ['click', () => provider.click(1, 2)], |
| 18 | + ['fill', () => provider.fill(1, 2, 'text')], |
| 19 | + ['typeText', () => provider.typeText('text')], |
| 20 | + ['scroll', () => provider.scroll('down')], |
| 21 | + ]; |
| 22 | + for (const [operation, call] of operations) { |
| 23 | + const error = await call().then( |
| 24 | + () => undefined, |
| 25 | + (error: unknown) => error, |
| 26 | + ); |
| 27 | + assert.ok(error instanceof AppError, `${operation} refuses with an AppError`); |
| 28 | + assert.equal(error.details?.reason, 'web-provider-missing', operation); |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +test('a composed request provider answers instead of the fallback refusal', async () => { |
| 33 | + const opened: string[] = []; |
| 34 | + const provider: WebProvider = { |
| 35 | + open: async (target) => { |
| 36 | + opened.push(target); |
| 37 | + }, |
| 38 | + close: async () => {}, |
| 39 | + snapshot: async () => ({ nodes: [] }), |
| 40 | + screenshot: async () => {}, |
| 41 | + setViewport: async () => {}, |
| 42 | + click: async () => {}, |
| 43 | + fill: async () => {}, |
| 44 | + typeText: async () => {}, |
| 45 | + scroll: async () => {}, |
| 46 | + }; |
| 47 | + await withWebProvider(provider, async () => { |
| 48 | + await resolveWebProvider().open('https://example.test'); |
| 49 | + }); |
| 50 | + assert.deepEqual(opened, ['https://example.test']); |
| 51 | +}); |
0 commit comments