|
1 | 1 | import { expect, test, vi } from 'vitest'; |
2 | 2 | import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 3 | +import { createRequestCanceledError } from '@agent-device/kernel/errors'; |
3 | 4 |
|
4 | 5 | // Keep the route tests hermetic: the default system-surface presence probe shells out to `ps`, which |
5 | 6 | // never resolves under the fake timers these tests drive. Tests that exercise the bypass inject |
@@ -783,3 +784,130 @@ test('a slow app discovery keeps observation on the bridge while no runner can a |
783 | 784 | vi.useRealTimers(); |
784 | 785 | } |
785 | 786 | }); |
| 787 | + |
| 788 | +test.for(['rejects', 'exits'] as const)( |
| 789 | + 'a deadline during the cached target re-check is not readiness work (ps %s)', |
| 790 | + async (psOnAbort) => { |
| 791 | + // A known target is re-checked with one `ps` per capture; no discovery runs. A deadline that |
| 792 | + // lands there must stay a plain cancellation, or a wait would report readiness exhaustion over |
| 793 | + // evidence its earlier polls already gathered (#2343 review). |
| 794 | + const run = vi.fn(async (args: readonly string[]) => ({ |
| 795 | + stdout: |
| 796 | + args[0] === 'spawn' |
| 797 | + ? `42\t0\tUIKitApplication:${input.options.appBundleId}[launch-a][rb-legacy]` |
| 798 | + : JSON.stringify({ |
| 799 | + devices: { 'com.apple.CoreSimulator.SimRuntime.iOS-26-0': [{ udid: ios.id }] }, |
| 800 | + }), |
| 801 | + stderr: '', |
| 802 | + exitCode: 0, |
| 803 | + })); |
| 804 | + let recheckStarted!: () => void; |
| 805 | + const recheck = new Promise<void>((resolve) => { |
| 806 | + recheckStarted = resolve; |
| 807 | + }); |
| 808 | + let psCalls = 0; |
| 809 | + const runCommand = vi.fn( |
| 810 | + async (cmd: string, args: readonly string[], options?: { signal?: AbortSignal }) => { |
| 811 | + if (cmd !== 'ps' || ++psCalls === 1) return { stdout: 'start-a', stderr: '', exitCode: 0 }; |
| 812 | + recheckStarted(); |
| 813 | + return await new Promise<{ stdout: string; stderr: string; exitCode: number }>( |
| 814 | + (resolve, reject) => { |
| 815 | + options?.signal?.addEventListener( |
| 816 | + 'abort', |
| 817 | + () => |
| 818 | + psOnAbort === 'rejects' |
| 819 | + ? reject(createRequestCanceledError({ cmd, args })) |
| 820 | + : resolve({ stdout: '', stderr: '', exitCode: 1 }), |
| 821 | + { once: true }, |
| 822 | + ); |
| 823 | + }, |
| 824 | + ); |
| 825 | + }, |
| 826 | + ); |
| 827 | + const fallback = vi.fn(async () => runnerResult()); |
| 828 | + const baseHost = platformRuntimeHostFixture(); |
| 829 | + const route = createAppleSnapshotRoute( |
| 830 | + { |
| 831 | + ...baseHost, |
| 832 | + appleApplications: { |
| 833 | + ...baseHost.appleApplications, |
| 834 | + hasLiveRunnerSession: async () => false, |
| 835 | + }, |
| 836 | + snapshot: { |
| 837 | + captureSurface: vi.fn(), |
| 838 | + presentIosAcquisition: vi.fn(async () => ({ |
| 839 | + backend: 'xctest' as const, |
| 840 | + producer: 'simulator-ax-bridge' as const, |
| 841 | + nodes: [{ index: 0, type: 'Application' }], |
| 842 | + })), |
| 843 | + }, |
| 844 | + }, |
| 845 | + { |
| 846 | + source: sourceReturning(bridgeAcquisition()), |
| 847 | + resolveTarget: createSimulatorSnapshotTargetResolver(), |
| 848 | + }, |
| 849 | + ); |
| 850 | + await withAppleToolProvider( |
| 851 | + createLocalAppleToolProvider({ simctl: { run }, runCommand }), |
| 852 | + async () => { |
| 853 | + await route.capture(ios, input, signal(), fallback); |
| 854 | + const deadline = new AbortController(); |
| 855 | + const capture = route.capture(ios, input, deadline.signal, fallback); |
| 856 | + await recheck; |
| 857 | + deadline.abort(new DOMException('Wait deadline exceeded', 'TimeoutError')); |
| 858 | + |
| 859 | + await expect(capture).rejects.not.toHaveProperty('details.readinessPhase'); |
| 860 | + expect(run.mock.calls.filter(([args]) => args[0] === 'spawn')).toHaveLength(1); |
| 861 | + expect(fallback).not.toHaveBeenCalled(); |
| 862 | + }, |
| 863 | + ); |
| 864 | + }, |
| 865 | +); |
| 866 | + |
| 867 | +test('a deadline during a slow app discovery names the discovery as the readiness phase', async () => { |
| 868 | + // The capture never reached the bridge or the runner: its whole cost was finding the target, so |
| 869 | + // the cancellation says so instead of reading as a capture that produced nothing (#2343). |
| 870 | + const run = vi.fn(async (args: readonly string[]) => { |
| 871 | + if (args[0] === 'spawn') await new Promise<never>(() => {}); |
| 872 | + return { |
| 873 | + stdout: JSON.stringify({ |
| 874 | + devices: { 'com.apple.CoreSimulator.SimRuntime.iOS-26-0': [{ udid: ios.id }] }, |
| 875 | + }), |
| 876 | + stderr: '', |
| 877 | + exitCode: 0, |
| 878 | + }; |
| 879 | + }); |
| 880 | + const runCommand = vi.fn(async () => ({ stdout: 'start-a', stderr: '', exitCode: 0 })); |
| 881 | + const fallback = vi.fn(async () => runnerResult()); |
| 882 | + const baseHost = platformRuntimeHostFixture(); |
| 883 | + const route = createAppleSnapshotRoute( |
| 884 | + { |
| 885 | + ...baseHost, |
| 886 | + appleApplications: { ...baseHost.appleApplications, hasLiveRunnerSession: async () => false }, |
| 887 | + }, |
| 888 | + { |
| 889 | + source: sourceReturning(bridgeAcquisition()), |
| 890 | + resolveTarget: createSimulatorSnapshotTargetResolver(), |
| 891 | + }, |
| 892 | + ); |
| 893 | + const deadline = new AbortController(); |
| 894 | + vi.useFakeTimers(); |
| 895 | + try { |
| 896 | + await withAppleToolProvider( |
| 897 | + createLocalAppleToolProvider({ simctl: { run }, runCommand }), |
| 898 | + async () => { |
| 899 | + const capture = route.capture(ios, input, deadline.signal, fallback); |
| 900 | + const settled = expect(capture).rejects.toMatchObject({ |
| 901 | + code: 'COMMAND_FAILED', |
| 902 | + details: { reason: 'request_canceled', readinessPhase: 'target-discovery' }, |
| 903 | + }); |
| 904 | + await vi.advanceTimersByTimeAsync(3_000); |
| 905 | + deadline.abort(new DOMException('Wait deadline exceeded', 'TimeoutError')); |
| 906 | + await settled; |
| 907 | + expect(fallback).not.toHaveBeenCalled(); |
| 908 | + }, |
| 909 | + ); |
| 910 | + } finally { |
| 911 | + vi.useRealTimers(); |
| 912 | + } |
| 913 | +}); |
0 commit comments