11import assert from 'node:assert/strict' ;
22import path from 'node:path' ;
33import { expect , test , vi } from 'vitest' ;
4+ import { getRequestSignal } from '@agent-device/host-kit/request' ;
45import { createProviderScenarioHarness } from './harness.ts' ;
56import { PROVIDER_SCENARIO_ANDROID } from './fixtures.ts' ;
67import { assertRpcOk } from './assertions.ts' ;
@@ -9,7 +10,6 @@ import { persistentInputWorld } from './persistent-input-world.ts';
910test . each ( [
1011 'fill 20 40 proof' ,
1112 'press 20 40' ,
12- 'back' ,
1313 'home' ,
1414 'keyboard enter' ,
1515 'app-switcher' ,
@@ -124,3 +124,107 @@ test.each([
124124 }
125125 } ,
126126) ;
127+
128+ test ( 'replay aborts signaled Back in ADB, preserves progress, and keeps the warm helper' , async ( ) => {
129+ const requestId = 'persistent-back-adb' ;
130+ let entered ! : ( ) => void ;
131+ const backEntered = new Promise < void > ( ( resolve ) => {
132+ entered = resolve ;
133+ } ) ;
134+ let backSignal : AbortSignal | undefined ;
135+ let parentSignal : AbortSignal | undefined ;
136+ let helperBack = false ;
137+ const afterAbort : string [ ] = [ ] ;
138+ const world = persistentInputWorld ( ( ) => {
139+ helperBack = true ;
140+ entered ( ) ;
141+ } ) ;
142+ const exec = world . provider . exec ;
143+ world . provider . exec = async ( args , options ) => {
144+ if ( getRequestSignal ( requestId ) ?. aborted ) afterAbort . push ( args . join ( ' ' ) ) ;
145+ if ( args . join ( ' ' ) === 'shell input keyevent 4' ) {
146+ backSignal = options ?. signal ;
147+ parentSignal = getRequestSignal ( requestId ) ;
148+ entered ( ) ;
149+ return await new Promise < never > ( ( _resolve , reject ) => {
150+ backSignal ?. addEventListener ( 'abort' , ( ) => reject ( backSignal ! . reason ) , { once : true } ) ;
151+ } ) ;
152+ }
153+ return await exec ( args , options ) ;
154+ } ;
155+ const daemon = await createProviderScenarioHarness ( {
156+ deviceInventoryProvider : async ( ) => [ PROVIDER_SCENARIO_ANDROID ] ,
157+ androidAdbProvider : ( ) => world . provider ,
158+ } ) ;
159+ let pending : ReturnType < typeof daemon . callCommand > | undefined ;
160+ try {
161+ await daemon . client ( ) . apps . open ( {
162+ app : 'com.android.settings' ,
163+ platform : 'android' ,
164+ serial : PROVIDER_SCENARIO_ANDROID . id ,
165+ testIme : false ,
166+ } ) ;
167+ assertRpcOk ( await daemon . callCommand ( 'snapshot' , [ ] , { snapshotInteractiveOnly : true } ) ) ;
168+ const warmCount = world . processes . length ;
169+ const warmProcess = world . processes . at ( - 1 ) ;
170+ assert . ok ( warmProcess && ! warmProcess . killed ) ;
171+ const script = path . join ( daemon . stateDir , 'persistent-back.ad' ) ;
172+ const timeoutMs = 1_000 ;
173+ vi . useFakeTimers ( { toFake : [ 'setTimeout' , 'clearTimeout' , 'Date' ] } ) ;
174+ const commandOffset = world . commands . length ;
175+ let settled = false ;
176+ pending = daemon . callCommand (
177+ 'replay' ,
178+ [ script ] ,
179+ {
180+ platform : 'android' ,
181+ serial : PROVIDER_SCENARIO_ANDROID . id ,
182+ timeoutMs,
183+ replayScriptSource : {
184+ entry : script ,
185+ files : { [ script ] : '# Keep the completed press\npress 20 30\nback\npress 40 50\n' } ,
186+ } ,
187+ } ,
188+ { meta : { requestId } } ,
189+ ) ;
190+ void pending . then ( ( ) => {
191+ settled = true ;
192+ } ) ;
193+ await Promise . race ( [
194+ backEntered ,
195+ pending . then ( ( result ) => assert . fail ( JSON . stringify ( result . json ) ) ) ,
196+ ] ) ;
197+ assert . equal ( helperBack , false , 'signaled Back must not enter the persistent helper' ) ;
198+ assert . equal ( backSignal , parentSignal ) ;
199+ assert . ok ( backSignal ) ;
200+ await vi . advanceTimersByTimeAsync ( timeoutMs + 500 ) ;
201+ await vi . waitFor ( ( ) => assert . equal ( settled , true ) ) ;
202+ const response = await pending ;
203+ assert . equal ( backSignal . aborted , true ) ;
204+ expect ( response . json . error ?. data ?. details ) . toMatchObject ( {
205+ reason : 'daemon_request_timeout' ,
206+ timeoutMs,
207+ divergence : { step : { source : { path : script , line : 3 } } } ,
208+ } ) ;
209+ assert . deepEqual ( afterAbort , [ ] , 'no native work may start after abort' ) ;
210+ assert . equal ( world . wasRetired ( ) , false ) ;
211+ assert . equal ( warmProcess . killed , false ) ;
212+ assert . equal (
213+ world . commands . slice ( commandOffset ) . filter ( ( entry ) => entry . startsWith ( 'input ' ) ) . length ,
214+ 1 ,
215+ 'the press after Back must not dispatch' ,
216+ ) ;
217+ vi . useRealTimers ( ) ;
218+ assert . equal ( await daemon . hasSession ( ) , true ) ;
219+ assertRpcOk ( await daemon . callCommand ( 'snapshot' , [ ] , { snapshotInteractiveOnly : true } ) ) ;
220+ assert . equal ( world . processes . length , warmCount , 'the helper remains reusable' ) ;
221+ } finally {
222+ if ( vi . isFakeTimers ( ) ) {
223+ await vi . advanceTimersByTimeAsync ( 45_000 ) ;
224+ await pending ;
225+ }
226+ vi . useRealTimers ( ) ;
227+ await daemon . close ( ) ;
228+ world . close ( ) ;
229+ }
230+ } ) ;
0 commit comments