|
| 1 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +vi.mock('../adb.ts', async (importOriginal) => { |
| 5 | + const actual = await importOriginal<typeof import('../adb.ts')>(); |
| 6 | + return { ...actual, sleep: vi.fn(async () => {}) }; |
| 7 | +}); |
| 8 | + |
| 9 | +import { isUnreadableCaptureContentError } from '@agent-device/contracts/android-snapshot-quality'; |
| 10 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 11 | +import type { AppError } from '@agent-device/kernel/errors'; |
| 12 | +import { snapshotAndroid } from '../snapshot.ts'; |
| 13 | +import { resetAndroidSnapshotHelperInstallCache } from '../snapshot-helper-install.ts'; |
| 14 | +import { resetAndroidSnapshotHelperSessions } from '../snapshot-helper-session-lifecycle.ts'; |
| 15 | +import type { AndroidAdbExecutor } from '../snapshot-helper.ts'; |
| 16 | +import { ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT } from './test-utils/android-snapshot-helper.ts'; |
| 17 | +import { |
| 18 | + androidSystemWindowOnlyXml, |
| 19 | + createPersistentSnapshotHelperProvider, |
| 20 | + isAndroidHelperRuntimeForceStop as isHelperRuntimeReset, |
| 21 | + type FakeAndroidProcess, |
| 22 | +} from './snapshot-helper-session.fixtures.ts'; |
| 23 | + |
| 24 | +const device: DeviceInfo = { |
| 25 | + platform: 'android', |
| 26 | + id: 'emulator-5554', |
| 27 | + name: 'Pixel', |
| 28 | + kind: 'emulator', |
| 29 | + booted: true, |
| 30 | +}; |
| 31 | + |
| 32 | +const helperArtifact = ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT; |
| 33 | + |
| 34 | +beforeEach(async () => { |
| 35 | + await resetAndroidSnapshotHelperSessions(); |
| 36 | + resetAndroidSnapshotHelperInstallCache(); |
| 37 | +}); |
| 38 | + |
| 39 | +afterEach(async () => { |
| 40 | + await resetAndroidSnapshotHelperSessions(); |
| 41 | +}); |
| 42 | + |
| 43 | +function openSettleWindow(): { settleBy: number } { |
| 44 | + return { settleBy: Date.now() + 60_000 }; |
| 45 | +} |
| 46 | + |
| 47 | +function isHelperVersionProbe(args: readonly string[]): boolean { |
| 48 | + return args.includes('--show-versioncode'); |
| 49 | +} |
| 50 | + |
| 51 | +test('a transient capture releases a helper session it had to start', async () => { |
| 52 | + const adbCalls: (readonly string[])[] = []; |
| 53 | + const spawnArgs: (readonly string[])[] = []; |
| 54 | + const processes: FakeAndroidProcess[] = []; |
| 55 | + const provider = createPersistentSnapshotHelperProvider({ |
| 56 | + calls: adbCalls, |
| 57 | + spawnArgs, |
| 58 | + processes, |
| 59 | + }); |
| 60 | + |
| 61 | + await snapshotAndroid(device, { |
| 62 | + helperAdb: provider, |
| 63 | + helperArtifact, |
| 64 | + transient: openSettleWindow(), |
| 65 | + }); |
| 66 | + |
| 67 | + assert.equal(spawnArgs.length, 1); |
| 68 | + assert.equal(processes[0]?.exitCode, 0); |
| 69 | + assert.equal( |
| 70 | + adbCalls.some((args) => args[0] === 'forward' && args[1] === '--remove'), |
| 71 | + true, |
| 72 | + ); |
| 73 | +}); |
| 74 | + |
| 75 | +test('a transient capture leaves a warm daemon-session helper running', async () => { |
| 76 | + const adbCalls: (readonly string[])[] = []; |
| 77 | + const spawnArgs: (readonly string[])[] = []; |
| 78 | + const processes: FakeAndroidProcess[] = []; |
| 79 | + const provider = createPersistentSnapshotHelperProvider({ |
| 80 | + calls: adbCalls, |
| 81 | + spawnArgs, |
| 82 | + processes, |
| 83 | + }); |
| 84 | + |
| 85 | + await snapshotAndroid(device, { |
| 86 | + helperAdb: provider, |
| 87 | + helperArtifact, |
| 88 | + helperSessionScope: 'daemon-session', |
| 89 | + }); |
| 90 | + const borrowed = await snapshotAndroid(device, { |
| 91 | + helperAdb: provider, |
| 92 | + helperArtifact, |
| 93 | + transient: openSettleWindow(), |
| 94 | + }); |
| 95 | + |
| 96 | + assert.equal(borrowed.androidSnapshot.helperSessionReused, true); |
| 97 | + assert.equal(spawnArgs.length, 1); |
| 98 | + assert.equal(processes[0]?.exitCode, null); |
| 99 | + assert.equal( |
| 100 | + adbCalls.some((args) => args[0] === 'forward' && args[1] === '--remove'), |
| 101 | + false, |
| 102 | + ); |
| 103 | +}); |
| 104 | + |
| 105 | +test('a transient capture reports its content verdict without retiring the warm helper', async () => { |
| 106 | + const adbCalls: (readonly string[])[] = []; |
| 107 | + const spawnArgs: (readonly string[])[] = []; |
| 108 | + const processes: FakeAndroidProcess[] = []; |
| 109 | + const provider = createPersistentSnapshotHelperProvider({ |
| 110 | + calls: adbCalls, |
| 111 | + spawnArgs, |
| 112 | + processes, |
| 113 | + sessionXml: (_sessionIndex, snapshotCount) => |
| 114 | + snapshotCount === 1 |
| 115 | + ? '<hierarchy><node text="warm helper" bounds="[0,0][10,10]" /></hierarchy>' |
| 116 | + : androidSystemWindowOnlyXml(), |
| 117 | + }); |
| 118 | + await snapshotAndroid(device, { |
| 119 | + helperAdb: provider, |
| 120 | + helperArtifact, |
| 121 | + helperSessionScope: 'daemon-session', |
| 122 | + }); |
| 123 | + |
| 124 | + await assert.rejects( |
| 125 | + snapshotAndroid(device, { helperAdb: provider, helperArtifact, transient: openSettleWindow() }), |
| 126 | + (error: unknown) => isUnreadableCaptureContentError(error), |
| 127 | + ); |
| 128 | + |
| 129 | + assert.equal(processes[0]?.exitCode, null, 'the session helper is still running'); |
| 130 | + assert.equal(adbCalls.some(isHelperRuntimeReset), false); |
| 131 | + assert.equal(spawnArgs.length, 1); |
| 132 | +}); |
| 133 | + |
| 134 | +test('a settle window that passes during the helper start never cancels the start', async () => { |
| 135 | + const adbCalls: (readonly string[])[] = []; |
| 136 | + const spawnArgs: (readonly string[])[] = []; |
| 137 | + const processes: FakeAndroidProcess[] = []; |
| 138 | + const provider = createPersistentSnapshotHelperProvider({ |
| 139 | + calls: adbCalls, |
| 140 | + spawnArgs, |
| 141 | + processes, |
| 142 | + sessionXml: () => androidSystemWindowOnlyXml(), |
| 143 | + sessionReadyDelayMs: 150, |
| 144 | + }); |
| 145 | + |
| 146 | + await assert.rejects( |
| 147 | + snapshotAndroid(device, { |
| 148 | + helperAdb: provider, |
| 149 | + helperArtifact, |
| 150 | + transient: { settleBy: Date.now() + 30 }, |
| 151 | + }), |
| 152 | + (error: unknown) => |
| 153 | + isUnreadableCaptureContentError(error) && (error as AppError).details?.attempts === 1, |
| 154 | + ); |
| 155 | + |
| 156 | + assert.equal(spawnArgs.length, 1); |
| 157 | + assert.equal(processes[0]?.killed, false, 'the started helper was not signalled'); |
| 158 | + assert.equal(processes[0]?.exitCode, 0, 'the started helper quit on its own'); |
| 159 | + assert.equal(adbCalls.some(isHelperRuntimeReset), false); |
| 160 | +}); |
| 161 | + |
| 162 | +test('a settle window that passes during a warm session capture leaves the session running', async () => { |
| 163 | + const adbCalls: (readonly string[])[] = []; |
| 164 | + const spawnArgs: (readonly string[])[] = []; |
| 165 | + const processes: FakeAndroidProcess[] = []; |
| 166 | + const provider = createPersistentSnapshotHelperProvider({ |
| 167 | + calls: adbCalls, |
| 168 | + spawnArgs, |
| 169 | + processes, |
| 170 | + sessionXml: (_sessionIndex, snapshotCount) => |
| 171 | + snapshotCount === 1 |
| 172 | + ? '<hierarchy><node text="warm helper" bounds="[0,0][10,10]" /></hierarchy>' |
| 173 | + : androidSystemWindowOnlyXml(), |
| 174 | + captureResponseDelayMs: (snapshotCount) => (snapshotCount === 1 ? 0 : 150), |
| 175 | + }); |
| 176 | + await snapshotAndroid(device, { |
| 177 | + helperAdb: provider, |
| 178 | + helperArtifact, |
| 179 | + helperSessionScope: 'daemon-session', |
| 180 | + }); |
| 181 | + |
| 182 | + await assert.rejects( |
| 183 | + snapshotAndroid(device, { |
| 184 | + helperAdb: provider, |
| 185 | + helperArtifact, |
| 186 | + transient: { settleBy: Date.now() + 30 }, |
| 187 | + }), |
| 188 | + (error: unknown) => |
| 189 | + isUnreadableCaptureContentError(error) && (error as AppError).details?.attempts === 1, |
| 190 | + ); |
| 191 | + |
| 192 | + assert.equal(spawnArgs.length, 1); |
| 193 | + assert.equal(processes[0]?.killed, false); |
| 194 | + assert.equal(processes[0]?.exitCode, null, 'the session helper is still running'); |
| 195 | + assert.equal(adbCalls.some(isHelperRuntimeReset), false); |
| 196 | +}); |
| 197 | + |
| 198 | +test('a transient capture on a device without the current helper installs and resets nothing', async () => { |
| 199 | + const adbCalls: (readonly string[])[] = []; |
| 200 | + const helperAdb: AndroidAdbExecutor = async (args) => { |
| 201 | + adbCalls.push(args); |
| 202 | + if (isHelperVersionProbe(args)) return { exitCode: 1, stdout: '', stderr: 'not found' }; |
| 203 | + return { exitCode: 0, stdout: '', stderr: '' }; |
| 204 | + }; |
| 205 | + |
| 206 | + await assert.rejects( |
| 207 | + snapshotAndroid(device, { helperAdb, helperArtifact, transient: openSettleWindow() }), |
| 208 | + (error: unknown) => |
| 209 | + (error as AppError).details?.reason === 'android-snapshot-helper-not-current', |
| 210 | + ); |
| 211 | + |
| 212 | + assert.equal( |
| 213 | + adbCalls.some((args) => args.includes('install') || args.includes('instrument')), |
| 214 | + false, |
| 215 | + ); |
| 216 | + assert.equal(adbCalls.some(isHelperRuntimeReset), false, 'a refusal is not a helper failure'); |
| 217 | +}); |
0 commit comments