|
| 1 | +import path from 'node:path'; |
| 2 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { makeIosSession } from '../../../__tests__/test-utils/session-factories.ts'; |
| 5 | +import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts'; |
| 6 | +import { dispatchCommand } from '../../../core/dispatch.ts'; |
| 7 | +import { SessionStore } from '../../session-store.ts'; |
| 8 | +import { runReplayScriptFile } from '../session-replay-runtime.ts'; |
| 9 | +import { baseReplayRequest, writeReplayFile } from './session-replay-runtime.fixtures.ts'; |
| 10 | + |
| 11 | +vi.mock('../../../core/dispatch.ts', async (importOriginal) => { |
| 12 | + const actual = await importOriginal<typeof import('../../../core/dispatch.ts')>(); |
| 13 | + return { ...actual, dispatchCommand: vi.fn(), resolveTargetDevice: vi.fn() }; |
| 14 | +}); |
| 15 | + |
| 16 | +const mockDispatchCommand = vi.mocked(dispatchCommand); |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + mockDispatchCommand.mockReset(); |
| 20 | + mockDispatchCommand.mockRejectedValue(new Error('no device runner available')); |
| 21 | +}); |
| 22 | + |
| 23 | +test('replay executes selector reads before reporting a covered-target divergence', async () => { |
| 24 | + const root = mkdtempForTestSync('agent-device-replay-selector-routes-'); |
| 25 | + const sessionName = 'default'; |
| 26 | + const sessionStore = new SessionStore(path.join(root, 'sessions')); |
| 27 | + sessionStore.set(sessionName, makeIosSession(sessionName)); |
| 28 | + const filePath = writeReplayFile(root, [ |
| 29 | + 'open "Demo"', |
| 30 | + 'get text "id=\\"field-name\\""', |
| 31 | + 'get attrs "id=\\"field-name\\""', |
| 32 | + 'is visible "id=\\"field-name\\""', |
| 33 | + 'find id "field-name" get attrs', |
| 34 | + 'click "id=\\"field-name\\""', |
| 35 | + ]); |
| 36 | + const invoked: string[] = []; |
| 37 | + |
| 38 | + const response = await runReplayScriptFile({ |
| 39 | + req: baseReplayRequest({ positionals: [filePath] }), |
| 40 | + sessionName, |
| 41 | + logPath: path.join(root, 'daemon.log'), |
| 42 | + sessionStore, |
| 43 | + invoke: async (request) => { |
| 44 | + invoked.push(request.command); |
| 45 | + if (request.command !== 'click') return { ok: true, data: {} }; |
| 46 | + return { |
| 47 | + ok: false, |
| 48 | + error: { |
| 49 | + code: 'COMMAND_FAILED', |
| 50 | + message: 'Target is covered by another visible element', |
| 51 | + }, |
| 52 | + }; |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + expect(invoked).toEqual(['open', 'get', 'get', 'is', 'find', 'click']); |
| 57 | + expect(response.ok).toBe(false); |
| 58 | + if (response.ok) return; |
| 59 | + expect(response.error.code).toBe('REPLAY_DIVERGENCE'); |
| 60 | + expect(response.error.details?.divergence).toMatchObject({ |
| 61 | + kind: 'action-failure', |
| 62 | + step: { index: 6 }, |
| 63 | + cause: { |
| 64 | + code: 'COMMAND_FAILED', |
| 65 | + message: 'Target is covered by another visible element', |
| 66 | + }, |
| 67 | + }); |
| 68 | +}); |
0 commit comments