11import { beforeEach , expect , test , vi } from 'vitest' ;
22
3- const { runCmd, runCmdDetached } = vi . hoisted ( ( ) => ( {
3+ const { runCmd, runCmdDetached, runCmdDetachedMonitored , emitDiagnostic } = vi . hoisted ( ( ) => ( {
44 runCmd : vi . fn ( async ( _cmd : string , args : readonly string [ ] ) => ( {
55 stdout : args [ 0 ] === 'front' ? 'ASN:0x0-0xa26a26:\n' : '"pid"=17044\n' ,
66 stderr : '' ,
77 exitCode : 0 ,
88 } ) ) ,
99 runCmdDetached : vi . fn ( ( ) => 4242 ) ,
10+ runCmdDetachedMonitored : vi . fn ( ( _cmd : string , _args : readonly string [ ] ) => ( {
11+ pid : 5151 ,
12+ exited : Promise . resolve < { pid : number ; exitCode ?: number ; error ?: string } > ( {
13+ pid : 5151 ,
14+ exitCode : 0 ,
15+ } ) ,
16+ } ) ) ,
17+ emitDiagnostic : vi . fn ( ) ,
18+ } ) ) ;
19+ vi . mock ( '@agent-device/host-kit/command' , ( ) => ( {
20+ runCmd,
21+ runCmdDetached,
22+ runCmdDetachedMonitored,
1023} ) ) ;
11- vi . mock ( '@agent-device/host-kit/command ' , ( ) => ( { runCmd , runCmdDetached } ) ) ;
24+ vi . mock ( '@agent-device/host-kit/diagnostics ' , ( ) => ( { emitDiagnostic } ) ) ;
1225const { readProcessIdentityFacts, stopProcessAndConfirmExit } = vi . hoisted ( ( ) => ( {
1326 readProcessIdentityFacts : vi . fn ( async ( ) => ( {
1427 startTime : 'Mon Nov 9 10:00:00 2026' ,
@@ -27,6 +40,8 @@ import { createAndroidEmulatorHost } from './platform-runtime-android-emulator-h
2740beforeEach ( ( ) => {
2841 runCmd . mockClear ( ) ;
2942 runCmdDetached . mockClear ( ) ;
43+ runCmdDetachedMonitored . mockClear ( ) ;
44+ emitDiagnostic . mockClear ( ) ;
3045 stopProcessAndConfirmExit . mockClear ( ) ;
3146} ) ;
3247
@@ -40,9 +55,9 @@ test('a windowed macOS launch hands focus back to the app frontmost before launc
4055 [ 'info' , '-only' , 'pid' , 'ASN:0x0-0xa26a26:' ] ,
4156 expect . anything ( ) ,
4257 ) ;
43- expect ( runCmdDetached ) . toHaveBeenCalledTimes ( 2 ) ;
44- expect ( runCmdDetached ) . toHaveBeenNthCalledWith ( 1 , 'emulator' , [ '-avd' , 'Pixel_9' ] ) ;
45- expect ( runCmdDetached ) . toHaveBeenNthCalledWith ( 2 , 'osascript' , [
58+ expect ( runCmdDetached ) . toHaveBeenCalledOnce ( ) ;
59+ expect ( runCmdDetached ) . toHaveBeenCalledWith ( 'emulator' , [ '-avd' , 'Pixel_9' ] ) ;
60+ expect ( runCmdDetachedMonitored ) . toHaveBeenCalledWith ( 'osascript' , [
4661 '-l' ,
4762 'JavaScript' ,
4863 '-e' ,
@@ -59,6 +74,7 @@ test('a macOS launch with no readable frontmost app starts no focus watcher', as
5974 await createAndroidEmulatorHost ( 'darwin' ) . launch ( 'Pixel_9' , false , 90_000 ) ;
6075
6176 expect ( runCmdDetached ) . toHaveBeenCalledOnce ( ) ;
77+ expect ( runCmdDetachedMonitored ) . not . toHaveBeenCalled ( ) ;
6278} ) ;
6379
6480test ( 'a failed frontmost-app read still launches the emulator, without a focus watcher' , async ( ) => {
@@ -70,6 +86,7 @@ test('a failed frontmost-app read still launches the emulator, without a focus w
7086
7187 expect ( runCmdDetached ) . toHaveBeenCalledOnce ( ) ;
7288 expect ( runCmdDetached ) . toHaveBeenCalledWith ( 'emulator' , [ '-avd' , 'Pixel_9' ] ) ;
89+ expect ( runCmdDetachedMonitored ) . not . toHaveBeenCalled ( ) ;
7390} ) ;
7491
7592test ( 'a headless launch has no window to keep in the background' , async ( ) => {
@@ -90,6 +107,7 @@ test('a launch on another host leaves window focus alone', async () => {
90107
91108 expect ( runCmd ) . not . toHaveBeenCalled ( ) ;
92109 expect ( runCmdDetached ) . toHaveBeenCalledOnce ( ) ;
110+ expect ( runCmdDetachedMonitored ) . not . toHaveBeenCalled ( ) ;
93111} ) ;
94112
95113test ( 'terminate stops only the emulator this host launched, by its recorded identity' , async ( ) => {
@@ -139,19 +157,32 @@ test('a terminate that could not prove the exit keeps the launch for a retry', a
139157 expect ( stopProcessAndConfirmExit ) . toHaveBeenCalledTimes ( 2 ) ;
140158} ) ;
141159
142- test ( 'a launch that fails after spawning stops its own emulator before it rethrows' , async ( ) => {
143- runCmdDetached
144- . mockImplementationOnce ( ( ) => 4242 )
145- . mockImplementationOnce ( ( ) => {
160+ // The focus helper only hands focus back to the previous app, so it is best effort: a helper that
161+ // cannot start never fails the launch or stops the booting emulator.
162+ test . each ( [
163+ [
164+ 'throws on spawn' ,
165+ ( ) => {
146166 throw new Error ( 'osascript missing' ) ;
147- } ) ;
167+ } ,
168+ ] ,
169+ [
170+ 'reports a spawn error after it starts' ,
171+ ( ) => ( {
172+ pid : 0 ,
173+ exited : Promise . resolve ( { pid : 0 , error : 'spawn osascript ENOENT' } ) ,
174+ } ) ,
175+ ] ,
176+ ] as const ) ( 'a focus helper that %s still returns the emulator pid' , async ( _name , spawn ) => {
177+ runCmdDetachedMonitored . mockImplementationOnce ( spawn ) ;
148178 const host = createAndroidEmulatorHost ( 'darwin' ) ;
149179
150- await expect ( host . launch ( 'Pixel_9' , false , 90_000 ) ) . rejects . toThrow ( 'osascript missing' ) ;
180+ await expect ( host . launch ( 'Pixel_9' , false , 90_000 ) ) . resolves . toBe ( 4242 ) ;
151181
152- expect ( stopProcessAndConfirmExit ) . toHaveBeenCalledOnce ( ) ;
153- expect ( stopProcessAndConfirmExit ) . toHaveBeenCalledWith (
154- expect . objectContaining ( { pid : 4242 } ) ,
155- expect . anything ( ) ,
182+ await vi . waitFor ( ( ) =>
183+ expect ( emitDiagnostic ) . toHaveBeenCalledWith (
184+ expect . objectContaining ( { phase : 'android_emulator_focus_return_failed' } ) ,
185+ ) ,
156186 ) ;
187+ expect ( stopProcessAndConfirmExit ) . not . toHaveBeenCalled ( ) ;
157188} ) ;
0 commit comments