|
7 | 7 | import { expect, test } from 'vitest'; |
8 | 8 | import fs from 'node:fs'; |
9 | 9 | import path from 'node:path'; |
| 10 | +import { AppError } from '@agent-device/kernel/errors'; |
10 | 11 | import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; |
11 | 12 | import { |
12 | 13 | localRuntimeOwner, |
@@ -38,25 +39,29 @@ function fillWithStdinText( |
38 | 39 | flags: Record<string, unknown> = {}, |
39 | 40 | fillFails?: (text: string) => Error, |
40 | 41 | text = SECRET, |
| 42 | + fillResult?: Record<string, unknown>, |
41 | 43 | ) { |
42 | 44 | return sendToWebSession( |
43 | 45 | session, |
44 | 46 | { command: 'fill', positionals: ['10', '20', text], flags: { textStdin: true, ...flags } }, |
45 | 47 | fillFails, |
| 48 | + fillResult, |
46 | 49 | ); |
47 | 50 | } |
48 | 51 |
|
49 | 52 | function sendToWebSession( |
50 | 53 | session: SessionState, |
51 | 54 | request: Pick<DaemonRequest, 'command' | 'positionals' | 'flags'>, |
52 | 55 | fillFails?: (text: string) => Error, |
| 56 | + fillResult?: Record<string, unknown>, |
53 | 57 | ) { |
54 | 58 | const sessionStore = makeSessionStore('agent-device-router-fill-text-stdin-'); |
55 | 59 | sessionStore.publish(session.name, session); |
56 | 60 | const typed: string[] = []; |
57 | 61 | const fillPoint = async (input: FillPointInput) => { |
58 | 62 | typed.push(input.text); |
59 | 63 | if (fillFails) throw fillFails(input.text); |
| 64 | + return fillResult; |
60 | 65 | }; |
61 | 66 | const owner = localRuntimeOwner('web'); |
62 | 67 | const baseFacts = createUnavailableRuntimeFactsForTest(WEB_DESKTOP_DEVICE, owner); |
@@ -312,3 +317,162 @@ test('a whitespace-only --text-stdin value collapses the error text it appears i |
312 | 317 | expect(result.ok).toBe(false); |
313 | 318 | if (!result.ok) expect(result.error.message).toBe('[REDACTED]'); |
314 | 319 | }); |
| 320 | + |
| 321 | +const FILL_TARGET = { |
| 322 | + resourceId: 'otp', |
| 323 | + className: 'android.widget.EditText', |
| 324 | + packageName: 'com.test', |
| 325 | + rect: { x: 0, y: 0, width: 100, height: 40 }, |
| 326 | +}; |
| 327 | + |
| 328 | +test.each([ |
| 329 | + ['an unrecorded', {}, '[REDACTED]', makeSession], |
| 330 | + ['a --record-as', { recordAs: 'CODE' }, '${CODE}', makeAuthoringSession], |
| 331 | +])( |
| 332 | + '%s --text-stdin fill that settles on a formatted value returns no read-back of it', |
| 333 | + async (_name, flags, placeholder, sessionFor) => { |
| 334 | + const { response } = fillWithStdinText( |
| 335 | + sessionFor('web', { device: WEB_DESKTOP_DEVICE }), |
| 336 | + flags, |
| 337 | + undefined, |
| 338 | + '000629177', |
| 339 | + { |
| 340 | + verification: 'unconfirmed', |
| 341 | + requested: '000629177', |
| 342 | + before: '000 62', |
| 343 | + after: '00 062 91 77', |
| 344 | + target: FILL_TARGET, |
| 345 | + }, |
| 346 | + ); |
| 347 | + |
| 348 | + const result = await response; |
| 349 | + expect(result.ok).toBe(true); |
| 350 | + if (!result.ok) return; |
| 351 | + expect(result.data).toMatchObject({ |
| 352 | + verification: 'unconfirmed', |
| 353 | + requested: placeholder, |
| 354 | + before: placeholder, |
| 355 | + after: placeholder, |
| 356 | + target: FILL_TARGET, |
| 357 | + }); |
| 358 | + expect(JSON.stringify(result)).not.toContain('062 91'); |
| 359 | + expect(JSON.stringify(result)).not.toContain('000 62'); |
| 360 | + }, |
| 361 | +); |
| 362 | + |
| 363 | +test('an unrecorded fill that settles on a formatted value still returns its read-back', async () => { |
| 364 | + const { response } = sendToWebSession( |
| 365 | + makeSession('web', { device: WEB_DESKTOP_DEVICE }), |
| 366 | + { command: 'fill', positionals: ['10', '20', '000629177'] }, |
| 367 | + undefined, |
| 368 | + { |
| 369 | + verification: 'unconfirmed', |
| 370 | + requested: '000629177', |
| 371 | + before: null, |
| 372 | + after: '00 062 91 77', |
| 373 | + target: FILL_TARGET, |
| 374 | + }, |
| 375 | + ); |
| 376 | + |
| 377 | + const result = await response; |
| 378 | + expect(result.ok).toBe(true); |
| 379 | + if (result.ok) expect(result.data).toMatchObject({ after: '00 062 91 77' }); |
| 380 | +}); |
| 381 | + |
| 382 | +const ALTERED_READ_BACK_FAILURE = (text: string) => |
| 383 | + new AppError('COMMAND_FAILED', 'Android fill verification failed', { |
| 384 | + failureReason: 'text_mismatch', |
| 385 | + expected: text, |
| 386 | + actual: '00 062 91 77', |
| 387 | + targetInput: { text: '000 62', className: 'android.widget.EditText' }, |
| 388 | + actualInput: { text: '00 062 91 77', className: 'android.widget.EditText' }, |
| 389 | + }); |
| 390 | + |
| 391 | +test.each([ |
| 392 | + ['an unrecorded', {}, '[REDACTED]', makeSession], |
| 393 | + ['a --record-as', { recordAs: 'CODE' }, '${CODE}', makeAuthoringSession], |
| 394 | +])( |
| 395 | + '%s --text-stdin fill failure that reports an altered read-back returns no part of it', |
| 396 | + async (_name, flags, placeholder, sessionFor) => { |
| 397 | + const { response } = fillWithStdinText( |
| 398 | + sessionFor('web', { device: WEB_DESKTOP_DEVICE }), |
| 399 | + flags, |
| 400 | + ALTERED_READ_BACK_FAILURE, |
| 401 | + '000629177', |
| 402 | + ); |
| 403 | + |
| 404 | + const result = await response; |
| 405 | + expect(result.ok).toBe(false); |
| 406 | + if (result.ok) return; |
| 407 | + expect(result.error.details).toMatchObject({ |
| 408 | + failureReason: 'text_mismatch', |
| 409 | + expected: placeholder, |
| 410 | + actual: placeholder, |
| 411 | + targetInput: { text: placeholder, className: 'android.widget.EditText' }, |
| 412 | + actualInput: { text: placeholder, className: 'android.widget.EditText' }, |
| 413 | + }); |
| 414 | + expect(JSON.stringify(result)).not.toContain('062 91'); |
| 415 | + }, |
| 416 | +); |
| 417 | + |
| 418 | +test('an empty --record-as fill that fails to clear returns no part of the residual field text', async () => { |
| 419 | + const { response } = sendToWebSession( |
| 420 | + makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE }), |
| 421 | + { command: 'fill', positionals: ['10', '20', ''], flags: { recordAs: 'CODE' } }, |
| 422 | + () => |
| 423 | + new AppError('COMMAND_FAILED', 'Android fill verification failed', { |
| 424 | + failureReason: 'text_mismatch', |
| 425 | + expected: '', |
| 426 | + actual: 'residual-4471', |
| 427 | + targetInput: { text: 'residual-4471', className: 'android.widget.EditText' }, |
| 428 | + actualInput: { text: 'residual-4471', className: 'android.widget.EditText' }, |
| 429 | + }), |
| 430 | + ); |
| 431 | + |
| 432 | + const result = await response; |
| 433 | + expect(result.ok).toBe(false); |
| 434 | + if (result.ok) return; |
| 435 | + expect(result.error.details).toMatchObject({ |
| 436 | + actual: '${CODE}', |
| 437 | + targetInput: { text: '${CODE}' }, |
| 438 | + actualInput: { text: '${CODE}' }, |
| 439 | + }); |
| 440 | + expect(JSON.stringify(result)).not.toContain('residual-4471'); |
| 441 | +}); |
| 442 | + |
| 443 | +test.each([ |
| 444 | + ['a one-letter value', 'a'], |
| 445 | + ['a value that is the verification discriminator', 'unconfirmed'], |
| 446 | + ['a value that is a response key', 'target'], |
| 447 | +])( |
| 448 | + 'a --record-as fill of %s keeps the unconfirmed structure and redacts its read-back', |
| 449 | + async (_name, text) => { |
| 450 | + const { response, sessionStore } = fillWithStdinText( |
| 451 | + makeAuthoringSession('web', { device: WEB_DESKTOP_DEVICE }), |
| 452 | + { recordAs: 'CODE' }, |
| 453 | + undefined, |
| 454 | + text, |
| 455 | + { |
| 456 | + verification: 'unconfirmed', |
| 457 | + requested: text, |
| 458 | + before: 'zq7', |
| 459 | + after: 'Q7z', |
| 460 | + target: FILL_TARGET, |
| 461 | + }, |
| 462 | + ); |
| 463 | + |
| 464 | + const result = await response; |
| 465 | + expect(result.ok).toBe(true); |
| 466 | + if (!result.ok) return; |
| 467 | + const unconfirmed = { |
| 468 | + verification: 'unconfirmed', |
| 469 | + requested: '${CODE}', |
| 470 | + before: '${CODE}', |
| 471 | + after: '${CODE}', |
| 472 | + target: FILL_TARGET, |
| 473 | + }; |
| 474 | + expect(result.data).toMatchObject(unconfirmed); |
| 475 | + expect(sessionStore.get('web')?.actions[0]?.result).toMatchObject(unconfirmed); |
| 476 | + expect(JSON.stringify(sessionStore.get('web')?.actions)).not.toMatch(/zq7|Q7z/); |
| 477 | + }, |
| 478 | +); |
0 commit comments