|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import type { Interactor, SnapshotOptions } from '@agent-device/contracts/interactor-types'; |
| 3 | +import type { PlatformRuntimeHost } from '@agent-device/contracts/platform-runtime-operations'; |
| 4 | +import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors'; |
| 5 | +import { |
| 6 | + ANDROID_LAUNCH_OBSERVATION_WINDOW_MS, |
| 7 | + createAndroidLaunchObservationProbe, |
| 8 | +} from './launch-observation.ts'; |
| 9 | +import { androidHelperContentUnavailableError } from './snapshot.ts'; |
| 10 | + |
| 11 | +type ProbeFixture = Readonly<{ |
| 12 | + snapshotOptions: SnapshotOptions[]; |
| 13 | + sleeps: number[]; |
| 14 | + /** Ends every pending clock sleep, as elapsed time would. */ |
| 15 | + elapse: () => void; |
| 16 | + observe: ( |
| 17 | + signal?: AbortSignal, |
| 18 | + ) => ReturnType<ReturnType<typeof createAndroidLaunchObservationProbe>['awaitObservable']>; |
| 19 | +}>; |
| 20 | + |
| 21 | +function createProbe(snapshot: (options: SnapshotOptions) => Promise<unknown>): ProbeFixture { |
| 22 | + const snapshotOptions: SnapshotOptions[] = []; |
| 23 | + const sleeps: number[] = []; |
| 24 | + const pendingSleeps: Array<() => void> = []; |
| 25 | + const clock = { |
| 26 | + now: () => Date.now(), |
| 27 | + sleep: async (ms: number, signal?: AbortSignal) => { |
| 28 | + sleeps.push(ms); |
| 29 | + await new Promise<void>((resolve, reject) => { |
| 30 | + pendingSleeps.push(resolve); |
| 31 | + signal?.addEventListener('abort', () => reject(signal.reason), { once: true }); |
| 32 | + }); |
| 33 | + }, |
| 34 | + } as unknown as PlatformRuntimeHost['clock']; |
| 35 | + const interactor = { |
| 36 | + snapshot: async (options: SnapshotOptions) => { |
| 37 | + snapshotOptions.push(options); |
| 38 | + return await snapshot(options); |
| 39 | + }, |
| 40 | + } as unknown as Pick<Interactor, 'snapshot'>; |
| 41 | + const probe = createAndroidLaunchObservationProbe({ clock }); |
| 42 | + return { |
| 43 | + snapshotOptions, |
| 44 | + sleeps, |
| 45 | + elapse: () => { |
| 46 | + for (const resolve of pendingSleeps.splice(0)) resolve(); |
| 47 | + }, |
| 48 | + observe: async (signal = new AbortController().signal) => |
| 49 | + await probe.awaitObservable(interactor, 'com.example.app', signal), |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** What the capture throws once its re-captures still see an unmounted app. */ |
| 54 | +function contentVerdict(): AppError { |
| 55 | + return androidHelperContentUnavailableError( |
| 56 | + { |
| 57 | + reason: 'content-poor-app-window', |
| 58 | + failureReason: 'Android snapshot helper returned insufficient foreground app content', |
| 59 | + diagnostics: { |
| 60 | + helperNodeCount: 3, |
| 61 | + helperSystemUiNodeCount: 0, |
| 62 | + helperWindowRootCount: 1, |
| 63 | + helperApplicationWindowRootCount: 1, |
| 64 | + helperMeaningfulNodeCount: 0, |
| 65 | + helperApplicationMeaningfulNodeCount: 0, |
| 66 | + helperNonSystemMeaningfulNodeCount: 0, |
| 67 | + helperInputMethodMeaningfulNodeCount: 0, |
| 68 | + helperWindowTypes: [1], |
| 69 | + }, |
| 70 | + }, |
| 71 | + 3, |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +/** A capture that ends only when its signal aborts, as a stuck helper call does. */ |
| 76 | +async function captureUntilAborted(options: SnapshotOptions): Promise<never> { |
| 77 | + return await new Promise<never>((_resolve, reject) => { |
| 78 | + if (options.signal?.aborted) reject(options.signal.reason); |
| 79 | + options.signal?.addEventListener('abort', () => reject(options.signal?.reason), { |
| 80 | + once: true, |
| 81 | + }); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +test('a readable launched app is observable through one transient capture', async () => { |
| 86 | + const probe = createProbe(async () => ({ nodes: [] })); |
| 87 | + |
| 88 | + await expect(probe.observe()).resolves.toEqual({ observation: 'observable' }); |
| 89 | + expect(probe.snapshotOptions).toHaveLength(1); |
| 90 | + expect(probe.snapshotOptions[0]).toMatchObject({ |
| 91 | + appBundleId: 'com.example.app', |
| 92 | + transient: true, |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +test('a content verdict after the capture re-captures is unobservable', async () => { |
| 97 | + const probe = createProbe(async () => { |
| 98 | + throw contentVerdict(); |
| 99 | + }); |
| 100 | + |
| 101 | + await expect(probe.observe()).resolves.toEqual({ observation: 'unobservable' }); |
| 102 | +}); |
| 103 | + |
| 104 | +test('a system surface covering the launched app is unobservable', async () => { |
| 105 | + const probe = createProbe(async () => ({ |
| 106 | + nodes: [], |
| 107 | + androidSnapshot: { backend: 'android-helper', systemSurfaceOnly: true }, |
| 108 | + })); |
| 109 | + |
| 110 | + await expect(probe.observe()).resolves.toEqual({ observation: 'unobservable' }); |
| 111 | +}); |
| 112 | + |
| 113 | +test('a capture mechanism failure is a failed probe with its typed reason', async () => { |
| 114 | + const probe = createProbe(async () => { |
| 115 | + throw new AppError('COMMAND_FAILED', 'Android snapshot helper failed: accessibility timeout', { |
| 116 | + androidSnapshotHelperFailureReason: 'Android snapshot helper failed: accessibility timeout', |
| 117 | + androidCaptureFailureReason: 'accessibility-timeout', |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + await expect(probe.observe()).resolves.toEqual({ |
| 122 | + observation: 'probe-failed', |
| 123 | + failure: { code: 'COMMAND_FAILED', reason: 'accessibility-timeout' }, |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +test('a helper that is not installed at the current version is a failed probe', async () => { |
| 128 | + const probe = createProbe(async () => { |
| 129 | + throw new AppError('COMMAND_FAILED', 'Android snapshot helper is not installed', { |
| 130 | + reason: 'android-snapshot-helper-not-current', |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + await expect(probe.observe()).resolves.toEqual({ |
| 135 | + observation: 'probe-failed', |
| 136 | + failure: { code: 'COMMAND_FAILED', reason: 'android-snapshot-helper-not-current' }, |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +test('a capture that outlasts the fixed window is abandoned as unobservable', async () => { |
| 141 | + const probe = createProbe(captureUntilAborted); |
| 142 | + |
| 143 | + const observing = probe.observe(); |
| 144 | + await vi.waitFor(() => expect(probe.sleeps).toEqual([ANDROID_LAUNCH_OBSERVATION_WINDOW_MS])); |
| 145 | + probe.elapse(); |
| 146 | + |
| 147 | + await expect(observing).resolves.toEqual({ observation: 'unobservable' }); |
| 148 | + expect(probe.snapshotOptions[0]?.signal?.aborted).toBe(true); |
| 149 | +}); |
| 150 | + |
| 151 | +test('a cancelled open rejects with its cancellation, not an observation', async () => { |
| 152 | + const controller = new AbortController(); |
| 153 | + const canceled = createRequestCanceledError(); |
| 154 | + const probe = createProbe(async (options) => { |
| 155 | + controller.abort(canceled); |
| 156 | + return await captureUntilAborted(options); |
| 157 | + }); |
| 158 | + |
| 159 | + await expect(probe.observe(controller.signal)).rejects.toBe(canceled); |
| 160 | +}); |
0 commit comments