|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import type { AndroidObservationHost } from '@agent-device/contracts/android-observation'; |
| 3 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 4 | +import { createAndroidObservationAdapter } from './observation.ts'; |
| 5 | + |
| 6 | +const device = { |
| 7 | + platform: 'android', |
| 8 | + target: 'mobile', |
| 9 | + id: 'emulator-5554', |
| 10 | + name: 'Pixel', |
| 11 | + kind: 'emulator', |
| 12 | + booted: true, |
| 13 | +} satisfies DeviceInfo; |
| 14 | + |
| 15 | +function hostFor(stdoutByCommand: ReadonlyMap<string, string>): AndroidObservationHost { |
| 16 | + return { |
| 17 | + runAdb: vi.fn(async (_device, args) => ({ |
| 18 | + exitCode: 0, |
| 19 | + stdout: stdoutByCommand.get(args.join(' ')) ?? '', |
| 20 | + stderr: '', |
| 21 | + })), |
| 22 | + readSnapshotNodes: vi.fn(async () => []), |
| 23 | + openApp: vi.fn(async () => {}), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +test('answers focus and blocking-dialog questions from one observation-bound dump', async () => { |
| 28 | + const host = hostFor( |
| 29 | + new Map([ |
| 30 | + ['shell dumpsys window windows', 'mCurrentFocus=Window{1 u0 com.example.app/.MainActivity}'], |
| 31 | + ]), |
| 32 | + ); |
| 33 | + const observer = createAndroidObservationAdapter(host); |
| 34 | + |
| 35 | + await expect( |
| 36 | + observer.readAppFocus(device, 'com.example.app', { requireNoBlockingDialog: true }), |
| 37 | + ).resolves.toBe(true); |
| 38 | + expect(host.runAdb).toHaveBeenCalledTimes(1); |
| 39 | +}); |
| 40 | + |
| 41 | +test('classifies an app-owned ANR without exposing dump mechanics to the daemon', async () => { |
| 42 | + const host = hostFor( |
| 43 | + new Map([ |
| 44 | + [ |
| 45 | + 'shell dumpsys window windows', |
| 46 | + 'mCurrentFocus=Window{1 u0 Application Not Responding: com.example.app}', |
| 47 | + ], |
| 48 | + ]), |
| 49 | + ); |
| 50 | + |
| 51 | + await expect(createAndroidObservationAdapter(host).readBlockingDialog(device)).resolves.toEqual({ |
| 52 | + status: 'dialog', |
| 53 | + focus: { |
| 54 | + package: 'com.example.app', |
| 55 | + focusedWindow: 'Application Not Responding: com.example.app', |
| 56 | + raw: 'mCurrentFocus=Window{1 u0 Application Not Responding: com.example.app}', |
| 57 | + }, |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +test('owns touch rounding and screen-size parsing behind raw host commands', async () => { |
| 62 | + const host = hostFor(new Map([['shell wm size', 'Physical size: 1080x2400']])); |
| 63 | + const observer = createAndroidObservationAdapter(host); |
| 64 | + |
| 65 | + await expect(observer.readScreenSize(device)).resolves.toEqual({ width: 1080, height: 2400 }); |
| 66 | + await observer.tap(device, 12.6, 42.2); |
| 67 | + expect(host.runAdb).toHaveBeenLastCalledWith(device, ['shell', 'input', 'tap', '13', '42'], { |
| 68 | + allowFailure: true, |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +test('a transient empty dump never demotes a variant that previously answered', async () => { |
| 73 | + const calls: string[] = []; |
| 74 | + let primaryReads = 0; |
| 75 | + const host: AndroidObservationHost = { |
| 76 | + ...hostFor(new Map()), |
| 77 | + runAdb: vi.fn(async (_device, args) => { |
| 78 | + const key = args.join(' '); |
| 79 | + calls.push(key); |
| 80 | + if (key === 'shell dumpsys window windows') primaryReads += 1; |
| 81 | + return { |
| 82 | + exitCode: 0, |
| 83 | + stdout: |
| 84 | + primaryReads === 2 && key === 'shell dumpsys window windows' |
| 85 | + ? '' |
| 86 | + : 'mCurrentFocus=Window{1 u0 com.example.app/.MainActivity}', |
| 87 | + stderr: '', |
| 88 | + }; |
| 89 | + }), |
| 90 | + }; |
| 91 | + const observer = createAndroidObservationAdapter(host); |
| 92 | + const transientDevice = { ...device, id: 'emulator-transient' }; |
| 93 | + |
| 94 | + await observer.readAppState(transientDevice); |
| 95 | + await observer.readAppState(transientDevice); |
| 96 | + const thirdReadStart = calls.length; |
| 97 | + await observer.readAppState(transientDevice); |
| 98 | + |
| 99 | + expect(calls[thirdReadStart]).toBe('shell dumpsys window windows'); |
| 100 | +}); |
0 commit comments