@@ -100,55 +100,103 @@ test('a transient empty dump never demotes a variant that previously answered',
100100 expect ( calls [ thirdReadStart ] ) . toBe ( 'shell dumpsys window windows' ) ;
101101} ) ;
102102
103- function timedOutAdb ( args : readonly string [ ] ) : AppError {
104- return new AppError ( 'COMMAND_FAILED' , 'adb timed out' , { cmd : 'adb' , args, timeoutMs : 1 } ) ;
103+ function timedOutAdb ( args : readonly string [ ] , timeoutMs : number ) : AppError {
104+ return new AppError ( 'COMMAND_FAILED' , 'adb timed out' , { cmd : 'adb' , args, timeoutMs } ) ;
105105}
106106
107- test ( 'bounds every dump read the foreground question makes' , async ( ) => {
108- const host = hostFor ( new Map ( ) ) ;
109- const observer = createAndroidObservationAdapter ( host ) ;
107+ type Observer = ReturnType < typeof createAndroidObservationAdapter > ;
110108
111- await observer . readAppState ( { ...device , id : 'emulator-bounded' } ) ;
109+ const QUESTIONS = [
110+ {
111+ question : 'foreground' ,
112+ ask : ( observer : Observer , target : DeviceInfo ) => observer . readAppState ( target ) ,
113+ unanswered : { } ,
114+ } ,
115+ {
116+ question : 'blocking dialog' ,
117+ ask : ( observer : Observer , target : DeviceInfo ) => observer . readBlockingDialog ( target ) ,
118+ unanswered : { status : 'unknown' } ,
119+ } ,
120+ ] as const ;
121+
122+ /**
123+ * A host whose every dump answers without a focus line after `spendMs(timeoutMs)` of a virtual
124+ * clock, optionally timing out instead, so an observation walks every variant it is allowed to.
125+ */
126+ function slowHost ( params : { spendMs : ( timeoutMs : number ) => number ; timeOut ?: boolean } ) : {
127+ host : AndroidObservationHost ;
128+ timeouts : number [ ] ;
129+ restore : ( ) => void ;
130+ } {
131+ const originMs = Date . now ( ) ;
132+ let elapsedMs = 0 ;
133+ const now = vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => originMs + elapsedMs ) ;
134+ const timeouts : number [ ] = [ ] ;
135+ const host : AndroidObservationHost = {
136+ ...hostFor ( new Map ( ) ) ,
137+ runAdb : vi . fn ( async ( _device , args , options ) => {
138+ const timeoutMs = options ?. timeoutMs ?? Number . POSITIVE_INFINITY ;
139+ timeouts . push ( timeoutMs ) ;
140+ elapsedMs += params . spendMs ( timeoutMs ) ;
141+ if ( params . timeOut ) throw timedOutAdb ( args , timeoutMs ) ;
142+ return { exitCode : 0 , stdout : '' , stderr : '' } ;
143+ } ) ,
144+ } ;
145+ return { host, timeouts, restore : ( ) => now . mockRestore ( ) } ;
146+ }
112147
113- const calls = vi . mocked ( host . runAdb ) . mock . calls ;
114- expect ( calls . map ( ( [ , args ] ) => args . join ( ' ' ) ) ) . toEqual ( [
148+ test ( 'bounds every dump read the foreground question makes' , async ( ) => {
149+ const { host, timeouts, restore } = slowHost ( { spendMs : ( ) => 0 } ) ;
150+ try {
151+ await createAndroidObservationAdapter ( host ) . readAppState ( { ...device , id : 'emulator-bounded' } ) ;
152+ } finally {
153+ restore ( ) ;
154+ }
155+
156+ expect ( vi . mocked ( host . runAdb ) . mock . calls . map ( ( [ , args ] ) => args . join ( ' ' ) ) ) . toEqual ( [
115157 'shell dumpsys window windows' ,
116158 'shell dumpsys window' ,
117159 'shell dumpsys activity activities' ,
118160 'shell dumpsys activity' ,
119161 ] ) ;
120- for ( const [ , , options ] of calls ) {
121- expect ( options ) . toEqual ( { allowFailure : true , timeoutMs : expect . any ( Number ) } ) ;
122- expect ( options ?. timeoutMs ) . toBeGreaterThan ( 0 ) ;
162+ for ( const timeoutMs of timeouts ) expect ( Number . isFinite ( timeoutMs ) && timeoutMs > 0 ) . toBe ( true ) ;
163+ } ) ;
164+
165+ test ( 'later variants receive only what the earlier reads left of one observation budget' , async ( ) => {
166+ const { host, timeouts, restore } = slowHost ( {
167+ spendMs : ( timeoutMs ) => Math . floor ( timeoutMs / 2 ) ,
168+ } ) ;
169+ try {
170+ await createAndroidObservationAdapter ( host ) . readAppState ( { ...device , id : 'emulator-slow' } ) ;
171+ } finally {
172+ restore ( ) ;
173+ }
174+
175+ const budgetMs = timeouts [ 0 ] ! ;
176+ expect ( timeouts . length ) . toBeGreaterThan ( 1 ) ;
177+ for ( let index = 1 ; index < timeouts . length ; index += 1 ) {
178+ expect ( timeouts [ index ] ) . toBe ( timeouts [ index - 1 ] ! - Math . floor ( timeouts [ index - 1 ] ! / 2 ) ) ;
123179 }
180+ const spentMs = timeouts . reduce ( ( sum , timeoutMs ) => sum + Math . floor ( timeoutMs / 2 ) , 0 ) ;
181+ expect ( spentMs ) . toBeLessThanOrEqual ( budgetMs ) ;
124182} ) ;
125183
126- test . each ( [
127- {
128- question : 'foreground' ,
129- ask : ( observer : ReturnType < typeof createAndroidObservationAdapter > , target : DeviceInfo ) =>
130- observer . readAppState ( target ) ,
131- unanswered : { } ,
132- } ,
133- {
134- question : 'blocking dialog' ,
135- ask : ( observer : ReturnType < typeof createAndroidObservationAdapter > , target : DeviceInfo ) =>
136- observer . readBlockingDialog ( target ) ,
137- unanswered : { status : 'unknown' } ,
138- } ,
139- ] ) (
140- 'a timed-out dump leaves the $question question unanswered and reads no further variant' ,
141- async ( { question, ask, unanswered } ) => {
142- const host : AndroidObservationHost = {
143- ...hostFor ( new Map ( ) ) ,
144- runAdb : vi . fn ( async ( _device , args ) => {
145- throw timedOutAdb ( args ) ;
146- } ) ,
147- } ;
148-
149- await expect (
150- ask ( createAndroidObservationAdapter ( host ) , { ...device , id : `emulator-hung-${ question } ` } ) ,
151- ) . resolves . toEqual ( unanswered ) ;
184+ test . each (
185+ QUESTIONS . flatMap ( ( entry ) => [
186+ { ...entry , ending : 'times out' , timeOut : true } ,
187+ { ...entry , ending : 'answers at its bound' , timeOut : false } ,
188+ ] ) ,
189+ ) (
190+ 'a $question read that $ending spends the observation budget and leaves it unanswered' ,
191+ async ( { question, ask, unanswered, timeOut } ) => {
192+ const { host, restore } = slowHost ( { spendMs : ( timeoutMs ) => timeoutMs , timeOut } ) ;
193+ try {
194+ await expect (
195+ ask ( createAndroidObservationAdapter ( host ) , { ...device , id : `emulator-${ question } ` } ) ,
196+ ) . resolves . toEqual ( unanswered ) ;
197+ } finally {
198+ restore ( ) ;
199+ }
152200 expect ( host . runAdb ) . toHaveBeenCalledTimes ( 1 ) ;
153201 } ,
154202) ;
0 commit comments