11import { expect , test , vi } from 'vitest' ;
22import type { DeviceInfo } from '@agent-device/kernel/device' ;
33import { createLocalAppleToolProvider , withAppleToolProvider } from './core/tool-provider.ts' ;
4- import { resolveSimulatorSnapshotTarget } from './snapshot-target.ts' ;
4+ import { createSimulatorSnapshotTargetResolver } from './snapshot-target.ts' ;
55
66const ios = {
77 platform : 'apple' ,
@@ -12,42 +12,128 @@ const ios = {
1212 target : 'mobile' ,
1313 booted : true ,
1414} as const satisfies DeviceInfo ;
15+ const app = 'com.example.app' ;
16+ const signal = ( ) => new AbortController ( ) . signal ;
1517
16- test ( 'resolves exact app pid, runtime, and launch generation from simctl' , async ( ) => {
17- const run = vi . fn ( async ( args : string [ ] ) => {
18- if ( args [ 0 ] === 'spawn' ) {
19- return {
20- stdout : [
21- '90\t0\tUIKitApplication:com.example.app.beta[wrong][rb-legacy]' ,
22- '42\t0\tUIKitApplication:com.example.app[launch-a][rb-legacy]' ,
23- ] . join ( '\n' ) ,
24- stderr : '' ,
25- exitCode : 0 ,
26- } ;
27- }
28- return {
29- stdout : JSON . stringify ( {
30- devices : {
31- 'com.apple.CoreSimulator.SimRuntime.iOS-26-0' : [ { udid : ios . id } ] ,
32- } ,
33- } ) ,
34- stderr : '' ,
35- exitCode : 0 ,
36- } ;
18+ function targetFixture ( ) {
19+ const state = { pid : 42 , launch : 'launch-a' , start : 'start-a' as string | null } ;
20+ const run = vi . fn ( async ( args : string [ ] ) => ( {
21+ stdout :
22+ args [ 0 ] === 'spawn'
23+ ? `90\t0\tUIKitApplication:com.example.app.beta[wrong][rb-legacy]\n${ state . pid } \t0\tUIKitApplication:${ app } [${ state . launch } ][rb-legacy]`
24+ : JSON . stringify ( {
25+ devices : { 'com.apple.CoreSimulator.SimRuntime.iOS-26-0' : [ { udid : ios . id } ] } ,
26+ } ) ,
27+ stderr : '' ,
28+ exitCode : 0 ,
29+ } ) ) ;
30+ const runCommand = vi . fn ( async ( ) => ( {
31+ stdout : state . start ?? '' ,
32+ stderr : '' ,
33+ exitCode : state . start ? 0 : 1 ,
34+ } ) ) ;
35+ const provider = createLocalAppleToolProvider ( { simctl : { run } , runCommand } ) ;
36+ const resolve = createSimulatorSnapshotTargetResolver ( ) ;
37+ return {
38+ state,
39+ run,
40+ runCommand,
41+ provider,
42+ resolve,
43+ discoveryCount : ( ) => run . mock . calls . filter ( ( [ args ] ) => args [ 0 ] === 'spawn' ) . length ,
44+ } ;
45+ }
46+
47+ test ( 'an unchanged OS process reuses its exact app target without another simctl spawn' , async ( ) => {
48+ const fixture = targetFixture ( ) ;
49+ await withAppleToolProvider ( fixture . provider , async ( ) => {
50+ const first = await fixture . resolve ( ios , app , signal ( ) ) ;
51+ const second = await fixture . resolve ( ios , app , signal ( ) ) ;
52+ expect ( second ) . toBe ( first ) ;
53+ expect ( first ) . toEqual ( {
54+ udid : ios . id ,
55+ runtime : 'com.apple.CoreSimulator.SimRuntime.iOS-26-0' ,
56+ pid : 42 ,
57+ generation : `42:UIKitApplication:${ app } [launch-a][rb-legacy]:start-a` ,
58+ targetId : `${ ios . id } :${ app } ` ,
59+ processStartTime : 'start-a' ,
60+ } ) ;
61+ expect ( fixture . discoveryCount ( ) ) . toBe ( 1 ) ;
62+ expect ( fixture . runCommand ) . toHaveBeenCalledTimes ( 2 ) ;
3763 } ) ;
38- const provider = createLocalAppleToolProvider ( { simctl : { run } } ) ;
64+ } ) ;
3965
40- const result = await withAppleToolProvider (
41- provider ,
42- async ( ) =>
43- await resolveSimulatorSnapshotTarget ( ios , 'com.example.app' , new AbortController ( ) . signal ) ,
44- ) ;
45-
46- expect ( result ) . toEqual ( {
47- udid : 'ios-1' ,
48- runtime : 'com.apple.CoreSimulator.SimRuntime.iOS-26-0' ,
49- pid : 42 ,
50- generation : '42:UIKitApplication:com.example.app[launch-a][rb-legacy]' ,
51- targetId : 'ios-1:com.example.app' ,
66+ test ( 'PID reuse cannot reuse a target from a different OS process start' , async ( ) => {
67+ const fixture = targetFixture ( ) ;
68+ await withAppleToolProvider ( fixture . provider , async ( ) => {
69+ const first = await fixture . resolve ( ios , app , signal ( ) ) ;
70+ fixture . state . start = 'start-b' ;
71+ const second = await fixture . resolve ( ios , app , signal ( ) ) ;
72+ expect ( second . pid ) . toBe ( first . pid ) ;
73+ expect ( second . generation ) . not . toBe ( first . generation ) ;
74+ expect ( second . processStartTime ) . toBe ( 'start-b' ) ;
75+ expect ( fixture . discoveryCount ( ) ) . toBe ( 2 ) ;
76+ } ) ;
77+ } ) ;
78+
79+ test ( 'a relaunched app resolves the replacement PID after the prior process disappears' , async ( ) => {
80+ const fixture = targetFixture ( ) ;
81+ await withAppleToolProvider ( fixture . provider , async ( ) => {
82+ await fixture . resolve ( ios , app , signal ( ) ) ;
83+ fixture . runCommand . mockResolvedValueOnce ( { stdout : '' , stderr : '' , exitCode : 1 } ) ;
84+ fixture . state . pid = 84 ;
85+ fixture . state . launch = 'launch-b' ;
86+ fixture . state . start = 'start-b' ;
87+ expect ( await fixture . resolve ( ios , app , signal ( ) ) ) . toMatchObject ( {
88+ pid : 84 ,
89+ processStartTime : 'start-b' ,
90+ } ) ;
91+ expect ( fixture . discoveryCount ( ) ) . toBe ( 2 ) ;
92+ } ) ;
93+ } ) ;
94+
95+ test ( 'unavailable OS identity never publishes or retains an unverified target' , async ( ) => {
96+ const fixture = targetFixture ( ) ;
97+ await withAppleToolProvider ( fixture . provider , async ( ) => {
98+ await fixture . resolve ( ios , app , signal ( ) ) ;
99+ fixture . state . start = null ;
100+ await expect ( fixture . resolve ( ios , app , signal ( ) ) ) . rejects . toMatchObject ( {
101+ details : { reason : 'simulator-target-identity-unavailable' } ,
102+ } ) ;
103+ fixture . state . start = 'start-a' ;
104+ await fixture . resolve ( ios , app , signal ( ) ) ;
105+ expect ( fixture . discoveryCount ( ) ) . toBe ( 3 ) ;
106+ } ) ;
107+ } ) ;
108+
109+ test ( 'stale acquisition refresh explicitly bypasses an otherwise live cached target' , async ( ) => {
110+ const fixture = targetFixture ( ) ;
111+ await withAppleToolProvider ( fixture . provider , async ( ) => {
112+ await fixture . resolve ( ios , app , signal ( ) ) ;
113+ fixture . state . pid = 84 ;
114+ fixture . state . launch = 'launch-b' ;
115+ const next = await fixture . resolve ( ios , app , signal ( ) , 'refresh' ) ;
116+ expect ( next . pid ) . toBe ( 84 ) ;
117+ expect ( fixture . discoveryCount ( ) ) . toBe ( 2 ) ;
118+ } ) ;
119+ } ) ;
120+
121+ test ( 'target facts stay within their runtime owner' , async ( ) => {
122+ const fixture = targetFixture ( ) ;
123+ await withAppleToolProvider ( fixture . provider , async ( ) => {
124+ await fixture . resolve ( ios , app , signal ( ) ) ;
125+ await createSimulatorSnapshotTargetResolver ( ) ( ios , app , signal ( ) ) ;
126+ expect ( fixture . discoveryCount ( ) ) . toBe ( 2 ) ;
127+ } ) ;
128+ } ) ;
129+
130+ test ( 'an aborted request cannot reuse a cached target' , async ( ) => {
131+ const fixture = targetFixture ( ) ;
132+ await withAppleToolProvider ( fixture . provider , async ( ) => {
133+ await fixture . resolve ( ios , app , signal ( ) ) ;
134+ const controller = new AbortController ( ) ;
135+ controller . abort ( new Error ( 'request-ended' ) ) ;
136+ await expect ( fixture . resolve ( ios , app , controller . signal ) ) . rejects . toThrow ( 'request-ended' ) ;
137+ expect ( fixture . runCommand ) . toHaveBeenCalledTimes ( 1 ) ;
52138 } ) ;
53139} ) ;
0 commit comments