|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, test } from 'vitest'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { WebDriverClient } from './webdriver-client.ts'; |
| 5 | + |
| 6 | +const realFetch = globalThis.fetch; |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + globalThis.fetch = realFetch; |
| 10 | +}); |
| 11 | + |
| 12 | +// #1658: `fill` blind-settles and types when the keyboard route is unsupported. |
| 13 | +// Anything else reaching that branch would degrade a real provider failure into |
| 14 | +// a blind text entry, so the split is pinned against the actual wire shapes. |
| 15 | +test('is_keyboard_shown reports an unimplemented route as unsupported', async () => { |
| 16 | + // Appium's own answer for a route the driver does not have. |
| 17 | + const client = await connectedClient( |
| 18 | + webDriverResponse(404, { value: { error: 'unknown command', message: 'Unknown command' } }), |
| 19 | + ); |
| 20 | + |
| 21 | + assert.equal(await client.isKeyboardShown(), 'unsupported'); |
| 22 | +}); |
| 23 | + |
| 24 | +test('is_keyboard_shown reports a non-boolean answer as unsupported', async () => { |
| 25 | + const client = await connectedClient(webDriverResponse(200, { value: null })); |
| 26 | + |
| 27 | + assert.equal(await client.isKeyboardShown(), 'unsupported'); |
| 28 | +}); |
| 29 | + |
| 30 | +// The status is identical to an unimplemented route's, so a status-only test |
| 31 | +// would read a dead session as a missing feature and keep typing into it. |
| 32 | +test('is_keyboard_shown propagates a dead session, which shares the 404', async () => { |
| 33 | + const client = await connectedClient( |
| 34 | + webDriverResponse(404, { |
| 35 | + value: { |
| 36 | + error: 'invalid session id', |
| 37 | + message: 'A session is either terminated or not started', |
| 38 | + }, |
| 39 | + }), |
| 40 | + ); |
| 41 | + |
| 42 | + await assert.rejects(client.isKeyboardShown(), (error: AppError) => { |
| 43 | + assert.match(error.message, /terminated or not started/); |
| 44 | + return true; |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +test('is_keyboard_shown propagates a grid failure', async () => { |
| 49 | + const client = await connectedClient( |
| 50 | + webDriverResponse(503, { value: { message: 'provider backend unavailable' } }), |
| 51 | + ); |
| 52 | + |
| 53 | + await assert.rejects(client.isKeyboardShown(), (error: AppError) => { |
| 54 | + assert.match(error.message, /provider backend unavailable/); |
| 55 | + return true; |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +// The readiness poll advertises a 2s budget; without the override each probe |
| 60 | +// would inherit the client's 30s default and one hung request could hold it |
| 61 | +// there. A stub that answers only the abort proves the caller's bound is the |
| 62 | +// one in force — were it ignored, this test would hang to its own timeout. |
| 63 | +test('is_keyboard_shown honors a caller timeout shorter than the client default', async () => { |
| 64 | + const client = await connectedClient( |
| 65 | + async (_input, init) => |
| 66 | + await new Promise<Response>((_resolve, reject) => { |
| 67 | + init?.signal?.addEventListener('abort', () => reject(init.signal?.reason as Error)); |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + await assert.rejects(client.isKeyboardShown(50), (error: Error) => { |
| 72 | + assert.match(`${error.name} ${error.message}`, /timeout|abort/i); |
| 73 | + return true; |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +// The focused element is the only signal that can say WHICH field took focus, |
| 78 | +// so `fill` fails closed when it is unavailable. Each wire shape it |
| 79 | +// distinguishes is pinned here. |
| 80 | +test('activeElement returns the focused element id and rect', async () => { |
| 81 | + const client = await connectedClient( |
| 82 | + webDriverRoutes({ |
| 83 | + '/element/active': { body: { value: { [W3C_ELEMENT_KEY]: 'el-1' } } }, |
| 84 | + '/rect': { body: { value: { x: 20, y: 300, width: 350, height: 44 } } }, |
| 85 | + }), |
| 86 | + ); |
| 87 | + |
| 88 | + assert.deepEqual(await client.activeElement(), { |
| 89 | + id: 'el-1', |
| 90 | + rect: { x: 20, y: 300, width: 350, height: 44 }, |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +// A form with nothing focused yet is an expected state, not a broken driver — |
| 95 | +// the caller must keep polling rather than give up. |
| 96 | +test('activeElement reports nothing focused as none', async () => { |
| 97 | + const client = await connectedClient( |
| 98 | + webDriverRoutes({ |
| 99 | + '/element/active': { |
| 100 | + status: 404, |
| 101 | + body: { value: { error: 'no such element', message: 'nothing focused' } }, |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + assert.equal(await client.activeElement(), 'none'); |
| 107 | +}); |
| 108 | + |
| 109 | +// Focus moved between the two round trips. A race the caller can simply retry |
| 110 | +// out of, not a failure worth ending the fill on. |
| 111 | +test('activeElement reports an element that vanished before its rect as none', async () => { |
| 112 | + const client = await connectedClient( |
| 113 | + webDriverRoutes({ |
| 114 | + '/element/active': { body: { value: { [W3C_ELEMENT_KEY]: 'el-1' } } }, |
| 115 | + '/rect': { |
| 116 | + status: 404, |
| 117 | + body: { value: { error: 'no such element', message: 'stale element' } }, |
| 118 | + }, |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + assert.equal(await client.activeElement(), 'none'); |
| 123 | +}); |
| 124 | + |
| 125 | +// A grid that answers 200/null is not implementing the route: the spec reports |
| 126 | +// "nothing focused" as a `no such element` error. Reading null as "nothing |
| 127 | +// focused" would fail every fill on such a grid instead of falling back. |
| 128 | +test('activeElement reports a non-element answer as unsupported', async () => { |
| 129 | + const client = await connectedClient( |
| 130 | + webDriverRoutes({ '/element/active': { body: { value: null } } }), |
| 131 | + ); |
| 132 | + |
| 133 | + assert.equal(await client.activeElement(), 'unsupported'); |
| 134 | +}); |
| 135 | + |
| 136 | +test('activeElement reports an unimplemented route as unsupported', async () => { |
| 137 | + const client = await connectedClient( |
| 138 | + webDriverRoutes({ |
| 139 | + '/element/active': { |
| 140 | + status: 404, |
| 141 | + body: { value: { error: 'unknown command', message: 'Unknown command' } }, |
| 142 | + }, |
| 143 | + }), |
| 144 | + ); |
| 145 | + |
| 146 | + assert.equal(await client.activeElement(), 'unsupported'); |
| 147 | +}); |
| 148 | + |
| 149 | +// Same discipline as the keyboard route: a dead session shares the 404 and must |
| 150 | +// never be mistaken for a driver that simply lacks the feature. |
| 151 | +test('activeElement propagates a dead session, which shares the 404', async () => { |
| 152 | + const client = await connectedClient( |
| 153 | + webDriverRoutes({ |
| 154 | + '/element/active': { |
| 155 | + status: 404, |
| 156 | + body: { |
| 157 | + value: { error: 'invalid session id', message: 'A session is either terminated' }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }), |
| 161 | + ); |
| 162 | + |
| 163 | + await assert.rejects(client.activeElement(), (error: AppError) => { |
| 164 | + assert.match(error.message, /terminated/); |
| 165 | + return true; |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
| 169 | +// `timeoutMs` bounds the OPERATION, not each request in it. Given the full |
| 170 | +// budget twice, these two sequential round trips would together take double it |
| 171 | +// — a probe handed the 1.5s left of a 2s readiness deadline would run 3s and |
| 172 | +// overrun the deadline it came from. Real time is unavoidable here: the defect |
| 173 | +// IS elapsed transport time across two calls. |
| 174 | +test('activeElement bounds its two sequential requests by one shared budget', async () => { |
| 175 | + const firstCallMs = 80; |
| 176 | + const budgetMs = 200; |
| 177 | + let rectRequestBudgetMs: number | undefined; |
| 178 | + const client = await connectedClient(async (input, init) => { |
| 179 | + const url = String(input instanceof Request ? input.url : input); |
| 180 | + if (url.includes('/rect')) { |
| 181 | + // Never answers; only its own abort ends it, so how long it was allowed |
| 182 | + // to run is exactly the budget it was handed. |
| 183 | + const startedAt = Date.now(); |
| 184 | + return await new Promise<Response>((_resolve, reject) => { |
| 185 | + init?.signal?.addEventListener('abort', () => { |
| 186 | + rectRequestBudgetMs = Date.now() - startedAt; |
| 187 | + reject(init.signal?.reason as Error); |
| 188 | + }); |
| 189 | + }); |
| 190 | + } |
| 191 | + await new Promise((resolve) => setTimeout(resolve, firstCallMs)); |
| 192 | + return new Response(JSON.stringify({ value: { [W3C_ELEMENT_KEY]: 'el-1' } }), { |
| 193 | + status: 200, |
| 194 | + headers: { 'Content-Type': 'application/json' }, |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + await assert.rejects(client.activeElement(budgetMs)); |
| 199 | + |
| 200 | + assert.ok(rectRequestBudgetMs !== undefined, 'the rect request should have been made'); |
| 201 | + // It must get what the first call left (~120ms), never a fresh 200ms. |
| 202 | + assert.ok( |
| 203 | + rectRequestBudgetMs < budgetMs - firstCallMs / 2, |
| 204 | + `rect request was allowed ${rectRequestBudgetMs}ms of a shared ${budgetMs}ms budget already ${firstCallMs}ms spent`, |
| 205 | + ); |
| 206 | +}); |
| 207 | + |
| 208 | +const W3C_ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf'; |
| 209 | + |
| 210 | +/** Dispatches by URL suffix, since `activeElement` makes two round trips. */ |
| 211 | +function webDriverRoutes( |
| 212 | + routes: Record<string, { status?: number; body: unknown }>, |
| 213 | +): typeof globalThis.fetch { |
| 214 | + const keys = Object.keys(routes).sort((a, b) => b.length - a.length); |
| 215 | + return async (input) => { |
| 216 | + const url = String(input instanceof Request ? input.url : input); |
| 217 | + const key = keys.find((candidate) => url.includes(candidate)); |
| 218 | + const route = key |
| 219 | + ? routes[key]! |
| 220 | + : { status: 500, body: { value: { message: `unrouted ${url}` } } }; |
| 221 | + return new Response(JSON.stringify(route.body), { |
| 222 | + status: route.status ?? 200, |
| 223 | + headers: { 'Content-Type': 'application/json' }, |
| 224 | + }); |
| 225 | + }; |
| 226 | +} |
| 227 | + |
| 228 | +/** A client with a live session id, so the probes reach the session routes. */ |
| 229 | +async function connectedClient(respond: typeof globalThis.fetch): Promise<WebDriverClient> { |
| 230 | + globalThis.fetch = webDriverResponse(200, { |
| 231 | + value: { sessionId: 'wd-1', capabilities: {} }, |
| 232 | + }); |
| 233 | + const client = new WebDriverClient({ |
| 234 | + clientVersion: '0.0.0-test', |
| 235 | + endpoint: 'http://cloud-webdriver.test/wd/hub/', |
| 236 | + requestPolicy: { timeoutMs: 30_000, retryAttempts: 0 }, |
| 237 | + }); |
| 238 | + await client.createSession({ platformName: 'iOS' }); |
| 239 | + globalThis.fetch = respond; |
| 240 | + return client; |
| 241 | +} |
| 242 | + |
| 243 | +function webDriverResponse(status: number, body: unknown): typeof globalThis.fetch { |
| 244 | + return async () => |
| 245 | + new Response(JSON.stringify(body), { |
| 246 | + status, |
| 247 | + headers: { 'Content-Type': 'application/json' }, |
| 248 | + }); |
| 249 | +} |
0 commit comments