|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { afterEach, expect, test } from 'vitest'; |
| 4 | +import { ANDROID_EMULATOR } from '@agent-device/test-kit'; |
| 5 | +import { bindAndroidImeTestHost } from '@agent-device/platform-android/snapshot-capture-fixtures'; |
| 6 | +import { |
| 7 | + activateAndroidTestIme, |
| 8 | + inspectAndroidSessionTeardownResources, |
| 9 | + isAndroidTestImeActive, |
| 10 | + resetAndroidTestImeActivationCacheForTests, |
| 11 | + restoreAndroidTestIme, |
| 12 | + withAndroidAdbProvider, |
| 13 | +} from '@agent-device/platform-android/mechanics'; |
| 14 | +import { resourceAdmissionWorld } from './composite-resource-admission.fixtures.ts'; |
| 15 | + |
| 16 | +const originalIme = 'com.google.android.inputmethod.latin/.LatinIME'; |
| 17 | +const helperIme = 'com.callstack.agentdevice.imehelper/.TestInputMethodService'; |
| 18 | + |
| 19 | +function androidHelperWorld() { |
| 20 | + let currentIme = originalIme; |
| 21 | + let previousIme: string | undefined; |
| 22 | + const imeSelections: string[] = []; |
| 23 | + bindAndroidImeTestHost(); |
| 24 | + const settings = (command: readonly string[]): string => { |
| 25 | + switch (command[1]) { |
| 26 | + case 'get': |
| 27 | + if (command.at(-1) === 'default_input_method') return currentIme; |
| 28 | + return command.at(-1) === 'agent_device_ime_helper_previous_ime' |
| 29 | + ? (previousIme ?? 'null') |
| 30 | + : 'null'; |
| 31 | + case 'put': |
| 32 | + previousIme = command.at(-1); |
| 33 | + return ''; |
| 34 | + case 'delete': |
| 35 | + previousIme = undefined; |
| 36 | + return ''; |
| 37 | + default: |
| 38 | + throw new Error(`Unexpected settings operation: ${command.join(' ')}`); |
| 39 | + } |
| 40 | + }; |
| 41 | + const ime = (command: readonly string[]): string => { |
| 42 | + if (command[1] === 'set') { |
| 43 | + currentIme = command.at(-1)!; |
| 44 | + imeSelections.push(currentIme); |
| 45 | + } else if (command[1] !== 'enable') |
| 46 | + throw new Error(`Unexpected IME operation: ${command.join(' ')}`); |
| 47 | + return ''; |
| 48 | + }; |
| 49 | + const provider = { |
| 50 | + imeHelperArtifact: { |
| 51 | + apkPath: '/unused/helper.apk', |
| 52 | + manifest: { |
| 53 | + name: 'android-ime-helper' as const, |
| 54 | + version: '1.0.0', |
| 55 | + assetName: 'helper.apk', |
| 56 | + sha256: '0'.repeat(64), |
| 57 | + packageName: 'com.callstack.agentdevice.imehelper', |
| 58 | + versionCode: 1, |
| 59 | + serviceComponent: helperIme, |
| 60 | + broadcastProtocol: 'android-ime-helper-v1' as const, |
| 61 | + }, |
| 62 | + }, |
| 63 | + exec: async (args: readonly string[]) => { |
| 64 | + const shell = args.indexOf('shell'); |
| 65 | + const command = args.slice(shell + 1); |
| 66 | + let stdout: string; |
| 67 | + if (command[0] === 'settings') stdout = settings(command); |
| 68 | + else if (command[0] === 'ime') stdout = ime(command); |
| 69 | + else throw new Error(`Unexpected fake ADB operation: ${command.join(' ')}`); |
| 70 | + return { stdout, stderr: '', exitCode: 0 }; |
| 71 | + }, |
| 72 | + }; |
| 73 | + const inProvider = async <T>(fn: () => Promise<T>) => |
| 74 | + await withAndroidAdbProvider(provider, { serial: ANDROID_EMULATOR.id }, fn); |
| 75 | + return { |
| 76 | + currentIme: () => currentIme, |
| 77 | + previousIme: () => previousIme, |
| 78 | + imeSelections: () => [...imeSelections], |
| 79 | + ime: { |
| 80 | + activate: async (stateDir: string) => { |
| 81 | + const result = await inProvider( |
| 82 | + async () => await activateAndroidTestIme(ANDROID_EMULATOR, { stateDir }), |
| 83 | + ); |
| 84 | + expect(result).toMatchObject({ outcome: 'settled', activated: true }); |
| 85 | + }, |
| 86 | + restore: async (stateDir: string) => { |
| 87 | + const result = await inProvider( |
| 88 | + async () => await restoreAndroidTestIme(ANDROID_EMULATOR, { stateDir }), |
| 89 | + ); |
| 90 | + expect(result.reason).toBe('ok'); |
| 91 | + }, |
| 92 | + inspect: inspectAndroidSessionTeardownResources, |
| 93 | + }, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +afterEach(() => resetAndroidTestImeActivationCacheForTests()); |
| 98 | + |
| 99 | +test('nested Android test restores its activated helper after primary resource refusal', async () => { |
| 100 | + const helper = androidHelperWorld(); |
| 101 | + const world = await resourceAdmissionWorld('capture', { ime: helper.ime }); |
| 102 | + try { |
| 103 | + world.sessionStore.retire(world.ref); |
| 104 | + const script = path.join(world.daemon.stateDir, 'android-ime-resource.ad'); |
| 105 | + const source = 'open com.android.settings\nperf memory snapshot\n'; |
| 106 | + fs.writeFileSync(script, source); |
| 107 | + const flags = { |
| 108 | + replayScriptSources: [{ entry: script, files: { [script]: source } }], |
| 109 | + artifactsDir: path.join(world.daemon.stateDir, 'android-ime-artifacts'), |
| 110 | + }; |
| 111 | + const nested = await world.call('batch', [], { |
| 112 | + batchSteps: [{ command: 'test', positionals: [script], flags }], |
| 113 | + }); |
| 114 | + expect(JSON.stringify(nested.json)).toContain('bounded_composite_resource_unsupported'); |
| 115 | + expect(JSON.stringify(nested.json)).toContain('perf:memory-snapshot'); |
| 116 | + const details = nested.json.result.data.results[0].data.failures[0].error.details; |
| 117 | + expect(details.standaloneCommandName).toBe('test'); |
| 118 | + expect(details).not.toHaveProperty('standaloneCommand'); |
| 119 | + expect(details.divergence.cause.hint).toContain('original test'); |
| 120 | + expect(details.divergence.cause.hint).toContain('live outer session'); |
| 121 | + expect(details.divergence.cause.hint).not.toContain('attempt-1'); |
| 122 | + expect(details.divergence.screen.hint).toBe(details.divergence.cause.hint); |
| 123 | + expect(helper.imeSelections()).toEqual([helperIme, originalIme]); |
| 124 | + expect(helper.currentIme()).toBe(originalIme); |
| 125 | + expect(helper.previousIme()).toBeUndefined(); |
| 126 | + expect(isAndroidTestImeActive(ANDROID_EMULATOR)).toBe(false); |
| 127 | + expect(world.sessionStore.listRefs()).toHaveLength(0); |
| 128 | + expect(world.native).not.toHaveBeenCalled(); |
| 129 | + const direct = await world.call('test', [script], flags); |
| 130 | + expect(JSON.stringify(direct.json)).not.toContain('bounded_composite_resource_unsupported'); |
| 131 | + expect(world.native).toHaveBeenCalledOnce(); |
| 132 | + expect(helper.imeSelections()).toEqual([helperIme, originalIme, helperIme, originalIme]); |
| 133 | + expect(helper.currentIme()).toBe(originalIme); |
| 134 | + expect(world.sessionStore.listRefs()).toHaveLength(0); |
| 135 | + } finally { |
| 136 | + await world.daemon.close(); |
| 137 | + } |
| 138 | +}); |
0 commit comments