1- import { describe , expect , test , vi } from 'vitest' ;
1+ import { afterEach , describe , expect , test , vi } from 'vitest' ;
22import { AppError } from '@agent-device/kernel/errors' ;
33import type { DeviceInfo } from '@agent-device/kernel/device' ;
44import { createAndroidApplicationTools } from '../platform-runtime-android-application-tools.ts' ;
5+ import * as androidMechanics from '../platform-runtime-android-mechanics.ts' ;
56
67const activateAndroidTestIme = vi . hoisted ( ( ) => vi . fn ( ) ) ;
78const restoreAndroidTestIme = vi . hoisted ( ( ) => vi . fn ( ) ) ;
89
910vi . mock ( '@agent-device/platform-android/mechanics' , ( ) => ( {
1011 activateAndroidTestIme,
1112 restoreAndroidTestIme,
13+ inferAndroidPackageAfterOpen : async ( ) => 'com.example.foreground' ,
1214 listAndroidAdbSerialsQuick : async ( ) => [ ] ,
1315 restoreOrphanedAndroidTestImeOnDaemonStartup : async ( ) => undefined ,
1416} ) ) ;
@@ -27,6 +29,42 @@ const settled = {
2729 helperPackageName : 'pkg' ,
2830} ;
2931
32+ describe ( 'android application tools: optional opened package inference' , ( ) => {
33+ afterEach ( ( ) => vi . restoreAllMocks ( ) ) ;
34+
35+ test . each ( [
36+ [ 'a targetless fresh open' , undefined , undefined ] ,
37+ [ 'a targetless open with an existing identity' , undefined , 'com.example.app' ] ,
38+ [ 'a deep link with an existing identity' , 'example://home' , 'com.example.app' ] ,
39+ ] ) ( '%s needs no Android mechanics' , async ( _name , target , currentAppBundleId ) => {
40+ const load = vi
41+ . spyOn ( androidMechanics , 'loadAndroidMechanics' )
42+ . mockRejectedValue ( new Error ( 'Android mechanics unavailable' ) ) ;
43+
44+ await expect (
45+ createAndroidApplicationTools ( ) . inferOpenedAppBundleId ( device , target , currentAppBundleId ) ,
46+ ) . resolves . toBe ( currentAppBundleId ) ;
47+ expect ( load ) . not . toHaveBeenCalled ( ) ;
48+ } ) ;
49+
50+ test ( 'a deep link leaves its package identity unset if Android mechanics cannot load' , async ( ) => {
51+ const load = vi
52+ . spyOn ( androidMechanics , 'loadAndroidMechanics' )
53+ . mockRejectedValue ( new Error ( 'Android mechanics unavailable' ) ) ;
54+
55+ await expect (
56+ createAndroidApplicationTools ( ) . inferOpenedAppBundleId ( device , 'example://home' , undefined ) ,
57+ ) . resolves . toBeUndefined ( ) ;
58+ expect ( load ) . toHaveBeenCalledOnce ( ) ;
59+ } ) ;
60+
61+ test ( 'a deep link adopts the inferred foreground package when mechanics are available' , async ( ) => {
62+ await expect (
63+ createAndroidApplicationTools ( ) . inferOpenedAppBundleId ( device , 'example://home' , undefined ) ,
64+ ) . resolves . toBe ( 'com.example.foreground' ) ;
65+ } ) ;
66+ } ) ;
67+
3068describe ( 'android application tools: test IME activation policy' , ( ) => {
3169 // Test IME is default-on for emulators, so an unobtainable helper must not fail the open.
3270 test ( 'an unobtainable helper warns and leaves the open successful' , async ( ) => {
0 commit comments