|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import type { Interactor } from './interactor-types.ts'; |
| 3 | +import { |
| 4 | + bindLocalTypeTextInteractor, |
| 5 | + bindProviderTypeTextInteractor, |
| 6 | + typeTextRuntimeOperationFacts, |
| 7 | +} from './type-text-runtime.ts'; |
| 8 | + |
| 9 | +const device = { |
| 10 | + platform: 'android', |
| 11 | + id: 'emulator-5554', |
| 12 | + name: 'Pixel', |
| 13 | + kind: 'emulator', |
| 14 | + booted: true, |
| 15 | +} as const; |
| 16 | + |
| 17 | +test('builds the exact type operation fact catalog', () => { |
| 18 | + const type = { available: true } as const; |
| 19 | + expect(typeTextRuntimeOperationFacts({ type })).toEqual({ typeText: type }); |
| 20 | +}); |
| 21 | + |
| 22 | +test('a local binding drives the interactor with the text, delay, and resolver context', async () => { |
| 23 | + const type = vi.fn(async () => ({ textEntryRoute: 'synthesized-first-responder' }) as const); |
| 24 | + const resolveInteractor = vi.fn(async () => ({ type }) as unknown as Interactor); |
| 25 | + const signal = new AbortController().signal; |
| 26 | + |
| 27 | + const operations = bindLocalTypeTextInteractor({ device, signal, resolveInteractor }); |
| 28 | + const result = await operations.typeText({ |
| 29 | + text: 'hello world', |
| 30 | + delayMs: 25, |
| 31 | + options: { appBundleId: 'com.example.app' }, |
| 32 | + execution: { logPath: '/tmp/daemon.log', requestId: 'type-1' }, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(resolveInteractor).toHaveBeenCalledWith(device, { |
| 36 | + logPath: '/tmp/daemon.log', |
| 37 | + requestId: 'type-1', |
| 38 | + appBundleId: 'com.example.app', |
| 39 | + signal, |
| 40 | + }); |
| 41 | + // Positional (text, delayMs): the `Interactor` seam takes two arguments, and a swapped or |
| 42 | + // dropped delay is the drift a structural assertion would not catch. |
| 43 | + expect(type).toHaveBeenCalledWith('hello world', 25); |
| 44 | + // The owner's closed result passes through untouched — the route evidence is the payload. |
| 45 | + expect(result).toEqual({ textEntryRoute: 'synthesized-first-responder' }); |
| 46 | +}); |
| 47 | + |
| 48 | +test('a provider binding drives its own resolved interactor and passes a blind result through', async () => { |
| 49 | + const type = vi.fn(async () => undefined); |
| 50 | + const resolveInteractor = vi.fn(() => ({ type }) as unknown as Interactor); |
| 51 | + const signal = new AbortController().signal; |
| 52 | + |
| 53 | + const operations = bindProviderTypeTextInteractor({ device, signal, resolveInteractor }); |
| 54 | + const result = await operations.typeText({ text: 'abc', delayMs: 0 }); |
| 55 | + |
| 56 | + expect(resolveInteractor).toHaveBeenCalledWith({ |
| 57 | + appBundleId: undefined, |
| 58 | + signal, |
| 59 | + }); |
| 60 | + expect(type).toHaveBeenCalledWith('abc', 0); |
| 61 | + expect(result).toBeUndefined(); |
| 62 | +}); |
| 63 | + |
| 64 | +test('a provider binding fails closed when its exact owner exposes no interactor', async () => { |
| 65 | + const operations = bindProviderTypeTextInteractor({ |
| 66 | + device, |
| 67 | + signal: new AbortController().signal, |
| 68 | + resolveInteractor: () => undefined, |
| 69 | + }); |
| 70 | + |
| 71 | + await expect(operations.typeText({ text: 'abc', delayMs: 0 })).rejects.toMatchObject({ |
| 72 | + code: 'UNSUPPORTED_OPERATION', |
| 73 | + details: { reason: 'provider-runtime-interactor-missing', deviceId: device.id }, |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test('an already-cancelled request never resolves an interactor', async () => { |
| 78 | + const controller = new AbortController(); |
| 79 | + controller.abort(); |
| 80 | + const type = vi.fn(async () => undefined); |
| 81 | + const resolveInteractor = vi.fn(async () => ({ type }) as unknown as Interactor); |
| 82 | + |
| 83 | + const operations = bindLocalTypeTextInteractor({ |
| 84 | + device, |
| 85 | + signal: controller.signal, |
| 86 | + resolveInteractor, |
| 87 | + }); |
| 88 | + |
| 89 | + await expect(operations.typeText({ text: 'abc', delayMs: 0 })).rejects.toThrow(); |
| 90 | + expect(resolveInteractor).not.toHaveBeenCalled(); |
| 91 | + expect(type).not.toHaveBeenCalled(); |
| 92 | +}); |
0 commit comments