|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import { createLimrunIosInteractor, type LimrunIosSession } from './ios.ts'; |
| 3 | +import type { IosTreeNode } from './snapshot.ts'; |
| 4 | + |
| 5 | +const FIELD_RECT = Object.freeze({ x: 24, y: 142, width: 354, height: 56 }); |
| 6 | + |
| 7 | +function emailField(editing: boolean): IosTreeNode { |
| 8 | + return { |
| 9 | + type: 'TextField', |
| 10 | + AXLabel: 'Email', |
| 11 | + pid: 4242, |
| 12 | + frame: FIELD_RECT, |
| 13 | + traits: editing |
| 14 | + ? ['TextEntry', 'TextOperationsAvailable', 'IsEditing'] |
| 15 | + : ['TextEntry', 'TextOperationsAvailable'], |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function sessionWithTrees(trees: IosTreeNode[]) { |
| 20 | + const calls: string[] = []; |
| 21 | + let reads = 0; |
| 22 | + const client = { |
| 23 | + elementTree: vi.fn(async () => { |
| 24 | + calls.push('elementTree'); |
| 25 | + const tree = trees[Math.min(reads, trees.length - 1)]; |
| 26 | + reads += 1; |
| 27 | + return tree; |
| 28 | + }), |
| 29 | + tap: vi.fn(async () => { |
| 30 | + calls.push('tap'); |
| 31 | + }), |
| 32 | + pressKey: vi.fn(async (key: string, modifiers?: string[]) => { |
| 33 | + calls.push(`pressKey:${key}${modifiers?.length ? `+${modifiers.join('+')}` : ''}`); |
| 34 | + }), |
| 35 | + typeText: vi.fn(async () => { |
| 36 | + calls.push('typeText'); |
| 37 | + }), |
| 38 | + }; |
| 39 | + const session = { |
| 40 | + platform: 'ios', |
| 41 | + instanceId: 'limrun-text-entry-instance', |
| 42 | + client, |
| 43 | + } as unknown as LimrunIosSession; |
| 44 | + return { interactor: createLimrunIosInteractor(session), client, calls }; |
| 45 | +} |
| 46 | + |
| 47 | +test('type enters the focused field without the provider focus scan', async () => { |
| 48 | + const { interactor, client } = sessionWithTrees([]); |
| 49 | + |
| 50 | + await interactor.type('[email protected]'); |
| 51 | + |
| 52 | + expect(client.typeText).toHaveBeenCalledWith('[email protected]', false, { requireFocus: false }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('a delayed type sends one focused-blind key press per character', async () => { |
| 56 | + const { interactor, client } = sessionWithTrees([]); |
| 57 | + |
| 58 | + await interactor.type('abc', 1); |
| 59 | + |
| 60 | + expect(client.typeText.mock.calls).toEqual([ |
| 61 | + ['a', false, { requireFocus: false }], |
| 62 | + ['b', false, { requireFocus: false }], |
| 63 | + ['c', false, { requireFocus: false }], |
| 64 | + ]); |
| 65 | +}); |
| 66 | + |
| 67 | +test('fill waits for text-entry focus to arrive and only then sends keys', async () => { |
| 68 | + const { interactor, client, calls } = sessionWithTrees([ |
| 69 | + emailField(false), |
| 70 | + emailField(false), |
| 71 | + emailField(true), |
| 72 | + ]); |
| 73 | + |
| 74 | + const result = await interactor.fill(200, 170, '[email protected]'); |
| 75 | + |
| 76 | + expect(calls).toEqual([ |
| 77 | + 'elementTree', |
| 78 | + 'tap', |
| 79 | + 'elementTree', |
| 80 | + 'elementTree', |
| 81 | + 'pressKey:a+command', |
| 82 | + 'typeText', |
| 83 | + ]); |
| 84 | + expect(result).toEqual({ textEntryReadiness: 'focused-element' }); |
| 85 | + expect(client.typeText).toHaveBeenCalledWith('[email protected]', false, { requireFocus: false }); |
| 86 | +}); |
| 87 | + |
| 88 | +test('fill selects the field value first, so entry replaces it', async () => { |
| 89 | + const { interactor, calls } = sessionWithTrees([emailField(false), emailField(true)]); |
| 90 | + |
| 91 | + await interactor.fill(200, 170, 'replacement'); |
| 92 | + |
| 93 | + expect(calls.indexOf('pressKey:a+command')).toBeLessThan(calls.indexOf('typeText')); |
| 94 | +}); |
| 95 | + |
| 96 | +test('the clear request empties the focused field instead of typing nothing', async () => { |
| 97 | + const { interactor, client, calls } = sessionWithTrees([emailField(false), emailField(true)]); |
| 98 | + |
| 99 | + const result = await interactor.fill(200, 170, ''); |
| 100 | + |
| 101 | + expect(calls).toEqual([ |
| 102 | + 'elementTree', |
| 103 | + 'tap', |
| 104 | + 'elementTree', |
| 105 | + 'pressKey:a+command', |
| 106 | + 'pressKey:delete', |
| 107 | + ]); |
| 108 | + expect(client.typeText).not.toHaveBeenCalled(); |
| 109 | + expect(result).toEqual({ textEntryReadiness: 'focused-element' }); |
| 110 | +}); |
| 111 | + |
| 112 | +test('a session that cannot answer the focus read fails before the device is touched', async () => { |
| 113 | + const { interactor, client, calls } = sessionWithTrees([emailField(false)]); |
| 114 | + client.elementTree.mockImplementationOnce(async () => { |
| 115 | + calls.push('elementTree'); |
| 116 | + throw new Error('instance connection lost'); |
| 117 | + }); |
| 118 | + |
| 119 | + await expect(interactor.fill(200, 170, '[email protected]')).rejects.toThrow( |
| 120 | + 'instance connection lost', |
| 121 | + ); |
| 122 | + expect(calls).toEqual(['elementTree']); |
| 123 | + expect(client.tap).not.toHaveBeenCalled(); |
| 124 | + expect(client.typeText).not.toHaveBeenCalled(); |
| 125 | +}); |
0 commit comments