|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import { test } from 'vitest'; |
| 2 | +import { test, vi } from 'vitest'; |
3 | 3 | import { buildGesturePlan } from '@agent-device/contracts/interaction'; |
| 4 | +import { AppError } from '@agent-device/kernel/errors'; |
4 | 5 | import { createCloudWebDriverCapabilities } from './capabilities.ts'; |
5 | 6 | import type { WebDriverClient, W3CActionSequence } from './webdriver-client.ts'; |
6 | 7 | import { createWebDriverInteractor } from './webdriver-interactor.ts'; |
7 | 8 |
|
| 9 | +// #1658: `fill` used to send its keys in the request right after the tap. A |
| 10 | +// WebView input takes first responder asynchronously, so on a web login form |
| 11 | +// the keys landed with nothing focused and `fill` still reported success. |
| 12 | +test('fill withholds keys until the tapped field raises the keyboard', async () => { |
| 13 | + const world = createTextEntryWorld(); |
| 14 | + // Hidden at the tap, then up two polls later: the transition our tap caused. |
| 15 | + world.keyboardShown = [false, false, false, true]; |
| 16 | + |
| 17 | + const result = await runFill(world); |
| 18 | + |
| 19 | + assert.equal(result?.textEntryReadiness, 'keyboard-shown'); |
| 20 | + assert.deepEqual(world.transcript, [ |
| 21 | + 'keyboard', |
| 22 | + 'tap', |
| 23 | + 'keyboard', |
| 24 | + 'keyboard', |
| 25 | + 'keyboard', |
| 26 | + 'keys', |
| 27 | + ]); |
| 28 | +}); |
| 29 | + |
| 30 | +test('fill settles when the keyboard was already up before the tap', async () => { |
| 31 | + const world = createTextEntryWorld(); |
| 32 | + // Back-to-back fills (email then password): a keyboard that never went down |
| 33 | + // cannot witness focus moving to the NEW field, so no poll can prove anything |
| 34 | + // — polling it would only re-observe evidence about the previous field. |
| 35 | + world.keyboardShown = [true]; |
| 36 | + |
| 37 | + const result = await runFill(world); |
| 38 | + |
| 39 | + assert.equal(result?.textEntryReadiness, 'settled-keyboard-up'); |
| 40 | + assert.deepEqual(world.transcript, ['keyboard', 'tap', 'keys']); |
| 41 | +}); |
| 42 | + |
| 43 | +test('fill settles when the driver cannot report keyboard state', async () => { |
| 44 | + const world = createTextEntryWorld(); |
| 45 | + world.keyboardSupported = false; |
| 46 | + |
| 47 | + const result = await runFill(world); |
| 48 | + |
| 49 | + // Evidence, never a precondition: an unanswerable keyboard must not fail the fill. |
| 50 | + assert.equal(result?.textEntryReadiness, 'settled-unknown'); |
| 51 | + assert.deepEqual(world.transcript, ['keyboard', 'tap', 'keys']); |
| 52 | +}); |
| 53 | + |
| 54 | +test('fill still types when no keyboard ever appears, and says so', async () => { |
| 55 | + const world = createTextEntryWorld(); |
| 56 | + world.keyboardShown = [false]; |
| 57 | + |
| 58 | + const result = await runFill(world); |
| 59 | + |
| 60 | + assert.equal(result?.textEntryReadiness, 'not-observed'); |
| 61 | + assert.equal(world.transcript.at(-1), 'keys'); |
| 62 | +}); |
| 63 | + |
| 64 | +/** |
| 65 | + * Drives `fill` on a fake clock: the readiness wait is real production time on |
| 66 | + * a device and must never be real time here (docs/agents/testing.md). Advancing |
| 67 | + * past the whole readiness budget is safe for every case — once readiness |
| 68 | + * resolves there are no pending timers left to fire. |
| 69 | + */ |
| 70 | +async function runFill(world: ReturnType<typeof createTextEntryWorld>) { |
| 71 | + vi.useFakeTimers(); |
| 72 | + try { |
| 73 | + const interactor = createWebDriverInteractor({ |
| 74 | + client: world.client, |
| 75 | + backend: 'xctest', |
| 76 | + capabilities: createCloudWebDriverCapabilities({ provider: 'test', platform: 'ios' }), |
| 77 | + }); |
| 78 | + const pending = interactor.fill(12, 24, '[email protected]'); |
| 79 | + await vi.advanceTimersByTimeAsync(5_000); |
| 80 | + return await pending; |
| 81 | + } finally { |
| 82 | + vi.useRealTimers(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Records the request order `fill` produces. The ordering IS the fix: a `keys` |
| 88 | + * entry that follows `tap` with no keyboard evidence between them is the bug. |
| 89 | + */ |
| 90 | +function createTextEntryWorld() { |
| 91 | + const world = { |
| 92 | + transcript: [] as string[], |
| 93 | + keyboardShown: [] as boolean[], |
| 94 | + keyboardSupported: true, |
| 95 | + client: undefined as unknown as WebDriverClient, |
| 96 | + }; |
| 97 | + world.client = { |
| 98 | + performActions: async () => { |
| 99 | + world.transcript.push('tap'); |
| 100 | + }, |
| 101 | + releaseActions: async () => {}, |
| 102 | + sendKeys: async () => { |
| 103 | + world.transcript.push('keys'); |
| 104 | + }, |
| 105 | + isKeyboardShown: async () => { |
| 106 | + world.transcript.push('keyboard'); |
| 107 | + if (!world.keyboardSupported) { |
| 108 | + throw new AppError('COMMAND_FAILED', 'Unknown command: is_keyboard_shown'); |
| 109 | + } |
| 110 | + // The last programmed reading is what the keyboard stays at. |
| 111 | + return world.keyboardShown.length > 1 |
| 112 | + ? world.keyboardShown.shift()! |
| 113 | + : world.keyboardShown[0]!; |
| 114 | + }, |
| 115 | + } as unknown as WebDriverClient; |
| 116 | + return world; |
| 117 | +} |
| 118 | + |
8 | 119 | test('endpoint plans become one timed W3C pointer move', async () => { |
9 | 120 | const performed: W3CActionSequence[][] = []; |
10 | 121 | let released = false; |
|
0 commit comments