|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { createLocalArtifactAdapter } from '@agent-device/host-kit/local-artifact-adapter'; |
| 4 | +import { createAgentDevice } from '../../../runtime.ts'; |
| 5 | +import { localCommandPolicy } from '@agent-device/contracts/command-policy'; |
| 6 | +import type { CommandSessionStore } from '@agent-device/contracts/runtime-contract'; |
| 7 | +import { makeSnapshotState } from '@agent-device/selectors/snapshot-geometry-fixtures'; |
| 8 | + |
| 9 | +async function snapshotWithForeground( |
| 10 | + foregroundWindowPackage: string | undefined, |
| 11 | + sessionApp: { appName: string; appBundleId: string } | undefined = { |
| 12 | + appName: 'Settings', |
| 13 | + appBundleId: 'com.android.settings', |
| 14 | + }, |
| 15 | +) { |
| 16 | + let stored: Parameters<CommandSessionStore['set']>[0] | undefined; |
| 17 | + const device = createAgentDevice({ |
| 18 | + backend: { |
| 19 | + platform: 'android', |
| 20 | + captureSnapshot: async () => ({ |
| 21 | + snapshot: makeSnapshotState([{ index: 0, depth: 0, type: 'TextView', label: 'Gmail' }], { |
| 22 | + backend: 'android', |
| 23 | + }), |
| 24 | + androidSnapshot: { |
| 25 | + backend: 'android-helper', |
| 26 | + ...(foregroundWindowPackage ? { foregroundWindowPackage } : {}), |
| 27 | + }, |
| 28 | + ...sessionApp, |
| 29 | + }), |
| 30 | + }, |
| 31 | + artifacts: createLocalArtifactAdapter(), |
| 32 | + sessions: { |
| 33 | + get: () => undefined, |
| 34 | + set: (record) => { |
| 35 | + stored = record; |
| 36 | + }, |
| 37 | + }, |
| 38 | + policy: localCommandPolicy(), |
| 39 | + }); |
| 40 | + const result = await device.capture.snapshot({ session: 'default', interactiveOnly: true }); |
| 41 | + return { result, stored }; |
| 42 | +} |
| 43 | + |
| 44 | +test('an Android snapshot names the captured foreground app when it is not the session app', async () => { |
| 45 | + const { result, stored } = await snapshotWithForeground('com.google.android.apps.nexuslauncher'); |
| 46 | + |
| 47 | + // A foreign app has no label of its own here, so the header shows its package. |
| 48 | + assert.equal(result.appName, 'com.google.android.apps.nexuslauncher'); |
| 49 | + assert.equal(result.appBundleId, 'com.google.android.apps.nexuslauncher'); |
| 50 | + assert.equal(stored?.appBundleId, 'com.android.settings'); |
| 51 | +}); |
| 52 | + |
| 53 | +test('an Android snapshot keeps the session app label when the session app is in front', async () => { |
| 54 | + const { result } = await snapshotWithForeground('com.android.settings'); |
| 55 | + |
| 56 | + assert.equal(result.appName, 'Settings'); |
| 57 | + assert.equal(result.appBundleId, 'com.android.settings'); |
| 58 | +}); |
| 59 | + |
| 60 | +test('a snapshot without a foreground window fact names the session app', async () => { |
| 61 | + const { result } = await snapshotWithForeground(undefined); |
| 62 | + |
| 63 | + assert.equal(result.appName, 'Settings'); |
| 64 | + assert.equal(result.appBundleId, 'com.android.settings'); |
| 65 | +}); |
| 66 | + |
| 67 | +test('an Android snapshot in a session with no app names the captured foreground app', async () => { |
| 68 | + const { result } = await snapshotWithForeground( |
| 69 | + 'com.google.android.apps.nexuslauncher', |
| 70 | + undefined, |
| 71 | + ); |
| 72 | + |
| 73 | + assert.equal(result.appName, 'com.google.android.apps.nexuslauncher'); |
| 74 | + assert.equal(result.appBundleId, 'com.google.android.apps.nexuslauncher'); |
| 75 | +}); |
| 76 | + |
| 77 | +test('a repeated unchanged snapshot of a foreign foreground app is acknowledged under its header', async () => { |
| 78 | + const snapshot = makeSnapshotState([{ index: 0, depth: 0, type: 'TextView', label: 'Gmail' }], { |
| 79 | + backend: 'android', |
| 80 | + }); |
| 81 | + let stored: Parameters<CommandSessionStore['set']>[0] | undefined = { |
| 82 | + name: 'default', |
| 83 | + appBundleId: 'com.android.settings', |
| 84 | + }; |
| 85 | + const device = createAgentDevice({ |
| 86 | + backend: { |
| 87 | + platform: 'android', |
| 88 | + captureSnapshot: async () => ({ |
| 89 | + snapshot, |
| 90 | + androidSnapshot: { |
| 91 | + backend: 'android-helper', |
| 92 | + foregroundWindowPackage: 'com.google.android.apps.nexuslauncher', |
| 93 | + }, |
| 94 | + appBundleId: 'com.android.settings', |
| 95 | + }), |
| 96 | + }, |
| 97 | + artifacts: createLocalArtifactAdapter(), |
| 98 | + sessions: { |
| 99 | + get: () => stored, |
| 100 | + set: (record) => { |
| 101 | + stored = record; |
| 102 | + }, |
| 103 | + }, |
| 104 | + policy: localCommandPolicy(), |
| 105 | + }); |
| 106 | + |
| 107 | + await device.capture.snapshot({ session: 'default' }); |
| 108 | + const result = await device.capture.snapshot({ session: 'default' }); |
| 109 | + |
| 110 | + // The acknowledgement compares against the last stored tree, the launcher's, which the header names. |
| 111 | + assert.equal(result.appBundleId, 'com.google.android.apps.nexuslauncher'); |
| 112 | + assert.ok(result.unchanged); |
| 113 | +}); |
0 commit comments