@@ -65,23 +65,31 @@ test('uses the request-scoped Android ADB executor rather than a host fallback',
6565 expect ( calls ) . toEqual ( [
6666 [ 'shell' , expect . stringContaining ( 'screenrecord --bit-rate 8000000' ) ] ,
6767 [ 'shell' , 'test' , '-d' , '/proc/42' ] ,
68- [ 'shell' , 'cat' , '/proc/42/stat' ] ,
6968 [ 'shell' , 'cat' , '/proc/42/cmdline' ] ,
69+ [ 'shell' , 'cat' , '/proc/42/stat' ] ,
7070 [ 'shell' , 'test' , '-e' , '/sdcard/capture.mp4' ] ,
7171 [ 'shell' , 'stat' , '-c' , '%s' , '/sdcard/capture.mp4' ] ,
7272 ] ) ;
7373} ) ;
7474
7575test ( 'finds only exact screenrecord processes for the canonical remote path' , async ( ) => {
7676 const remotePath = '/sdcard/agent-device-recording-123.mp4' ;
77+ let candidateExited = false ;
78+ const candidateCalls : string [ ] = [ ] ;
7779 await withAndroidAdbProvider (
7880 {
7981 exec : async ( args ) => {
8082 const command = args . slice ( 1 ) . join ( ' ' ) ;
81- if ( command === 'ps -A -o pid=' ) return result ( '41\n42\n43\n44\n' ) ;
83+ if ( command . includes ( '/proc/45' ) ) candidateCalls . push ( command ) ;
84+ if ( command === 'ps -A -o pid=' ) return result ( '41\n42\n43\n44\n45\n' ) ;
85+ if ( command === 'test -d /proc/45' ) return result ( '' , '' , candidateExited ? 1 : 0 ) ;
8286 if ( / ^ t e s t - d \/ p r o c \/ \d + $ / . test ( command ) ) return result ( ) ;
8387 const pid = / \/ p r o c \/ ( \d + ) \/ / . exec ( command ) ?. [ 1 ] ;
8488 if ( ! pid ) return result ( '' , 'unexpected command' , 1 ) ;
89+ if ( pid === '45' ) {
90+ candidateExited = true ;
91+ return result ( '' , 'process exited' , 1 ) ;
92+ }
8593 if ( command . endsWith ( '/stat' ) ) return result ( procStat ( Number ( pid ) , pid ) ) ;
8694 const commandLines : Record < string , string > = {
8795 '41' : [ '/system/bin/screenrecord' , '--bit-rate' , '8000000' , remotePath , '' ] . join ( '\0' ) ,
@@ -111,6 +119,9 @@ test('finds only exact screenrecord processes for the canonical remote path', as
111119 ] ,
112120 conclusive : true ,
113121 } ) ;
122+ expect ( candidateCalls ) . toContain ( 'cat /proc/45/cmdline' ) ;
123+ expect ( candidateCalls ) . toContain ( 'cat /proc/45/stat' ) ;
124+ expect ( candidateCalls . filter ( ( command ) => command === 'test -d /proc/45' ) ) . toHaveLength ( 2 ) ;
114125 } ,
115126 ) ;
116127} ) ;
@@ -247,6 +258,45 @@ test('distinguishes a missing proc directory from an unavailable identity probe'
247258 ) ;
248259} ) ;
249260
261+ for ( const failure of [ 'cmdline' , 'stat' , 'empty-stat' ] as const ) {
262+ const field = failure === 'cmdline' ? 'cmdline' : 'stat' ;
263+ test . each ( [
264+ [ 'absent' , 1 , 'already-missing' ] ,
265+ [ 'present' , 0 , 'uncertain' ] ,
266+ [ 'interrupted' , null , 'uncertain' ] ,
267+ ] as const ) (
268+ `rechecks process presence after ${ failure } : %s` ,
269+ async ( _presence , probeExit , expected ) => {
270+ const commands : string [ ] = [ ] ;
271+ let probes = 0 ;
272+ adbExecutor . override = async ( args ) => {
273+ const command = args . slice ( 1 ) . join ( ' ' ) ;
274+ commands . push ( command ) ;
275+ if ( command === 'test -d /proc/42' ) {
276+ return ++ probes === 1 ? result ( ) : { stdout : '' , stderr : '' , exitCode : probeExit } ;
277+ }
278+ if ( command === `cat /proc/42/${ field } ` ) {
279+ return failure === 'empty-stat' ? result ( '' ) : result ( '' , 'read failed' , 1 ) ;
280+ }
281+ if ( command . endsWith ( '/stat' ) ) return result ( procStat ( 42 , '42' ) ) ;
282+ return result ( [ '/system/bin/screenrecord' , '/sdcard/capture.mp4' , '' ] . join ( '\0' ) ) ;
283+ } ;
284+ try {
285+ const transport = await createAndroidScreenRecordingTransport (
286+ android ,
287+ createLiveTestHostEnvironment ( ) ,
288+ ) ;
289+ await expect (
290+ transport . stop ( { pid : '42' , remotePath : '/sdcard/capture.mp4' , startTime : '42' } ) ,
291+ ) . resolves . toBe ( expected ) ;
292+ expect ( commands . some ( ( command ) => command . startsWith ( 'kill ' ) ) ) . toBe ( false ) ;
293+ } finally {
294+ adbExecutor . override = undefined ;
295+ }
296+ } ,
297+ ) ;
298+ }
299+
250300test ( 'retains an interrupted empty-stderr presence probe as uncertain' , async ( ) => {
251301 adbExecutor . override = async ( args ) => {
252302 expect ( args ) . toEqual ( [ 'shell' , 'test' , '-d' , '/proc/44' ] ) ;
@@ -314,7 +364,7 @@ test('retains an interrupted empty-stderr manifest probe as unavailable', async
314364 }
315365} ) ;
316366
317- test ( 'proves termination from each ownership-lost producer without signalling ' , async ( ) => {
367+ test ( 'does not signal an exited or reassigned recorder ' , async ( ) => {
318368 const commands : string [ ] = [ ] ;
319369 const recorded = { pid : '4004' , remotePath : '/sdcard/capture.mp4' , startTime : '3766' } ;
320370 const reassignedPid = {
@@ -349,27 +399,32 @@ test('proves termination from each ownership-lost producer without signalling',
349399 }
350400 identity = exitedTask ;
351401 await expect ( transport . stop ( recorded ) ) . resolves . toBe ( 'ownership-lost' ) ;
402+ identity = { stat : procStat ( 4004 , '3766' , 'Z' ) , cmdline : '' } ;
403+ await expect ( transport . inspect ( recorded ) ) . resolves . toBe ( 'missing' ) ;
404+ await expect ( transport . stop ( recorded ) ) . resolves . toBe ( 'already-missing' ) ;
352405 expect ( commands . some ( ( command ) => command . startsWith ( 'kill ' ) ) ) . toBe ( false ) ;
353406 } ,
354407 ) ;
355408} ) ;
356409
357- test ( 'classifies a replacement recorder on the same path as a foreign writer and never signals it ' , async ( ) => {
410+ test ( 'never signals a recorder whose pid was reused during the command-line read ' , async ( ) => {
358411 const commands : string [ ] = [ ] ;
359412 const recorded = { pid : '4004' , remotePath : '/sdcard/capture.mp4' , startTime : '3766' } ;
360- const replacementRecorder = {
361- stat : procStat ( 4004 , '9911' ) ,
362- cmdline : [ '/system/bin/screenrecord' , '--bit-rate' , '8000000' , recorded . remotePath , '' ] . join (
363- '\0' ,
364- ) ,
365- } ;
413+ let currentStart = recorded . startTime ;
366414 await withAndroidAdbProvider (
367415 {
368416 exec : async ( args ) => {
369417 const command = args . slice ( 1 ) . join ( ' ' ) ;
370418 commands . push ( command ) ;
371- if ( command . endsWith ( '/stat' ) ) return result ( replacementRecorder . stat ) ;
372- if ( command . endsWith ( '/cmdline' ) ) return result ( replacementRecorder . cmdline ) ;
419+ if ( command . endsWith ( '/stat' ) ) return result ( procStat ( 4004 , currentStart ) ) ;
420+ if ( command . endsWith ( '/cmdline' ) ) {
421+ currentStart = '9911' ;
422+ return result (
423+ [ '/system/bin/screenrecord' , '--bit-rate' , '8000000' , recorded . remotePath , '' ] . join (
424+ '\0' ,
425+ ) ,
426+ ) ;
427+ }
373428 return result ( '' ) ;
374429 } ,
375430 } ,
@@ -379,15 +434,15 @@ test('classifies a replacement recorder on the same path as a foreign writer and
379434 android ,
380435 createLiveTestHostEnvironment ( ) ,
381436 ) ;
382- await expect ( transport . inspect ( recorded ) ) . resolves . toBe ( 'foreign-writer' ) ;
383437 await expect ( transport . stop ( recorded ) ) . resolves . toBe ( 'ownership-lost' ) ;
438+ await expect ( transport . inspect ( recorded ) ) . resolves . toBe ( 'foreign-writer' ) ;
384439 expect ( commands . some ( ( command ) => command . startsWith ( 'kill ' ) ) ) . toBe ( false ) ;
385440 } ,
386441 ) ;
387442} ) ;
388443
389- function procStat ( pid : number , startTime : string ) : string {
390- return `${ pid } (screenrecord) S ${ Array . from ( { length : 18 } , ( ) => '0' ) . join ( ' ' ) } ${ startTime } ` ;
444+ function procStat ( pid : number , startTime : string , state = 'S' ) : string {
445+ return `${ pid } (screenrecord) ${ state } ${ Array . from ( { length : 18 } , ( ) => '0' ) . join ( ' ' ) } ${ startTime } ` ;
391446}
392447
393448function result (
@@ -440,3 +495,37 @@ test('retains unavailable manifest reads and confirms manifest deletion', async
440495 ) ;
441496 expect ( commands ) . toHaveLength ( 2 ) ;
442497} ) ;
498+
499+ for ( const interruptedAt of [ 'initial' , 'recheck' ] as const ) {
500+ test ( `retains legal provider interruption on the ${ interruptedAt } presence probe` , async ( ) => {
501+ const commands : string [ ] = [ ] ;
502+ let probes = 0 ;
503+ await withAndroidAdbProvider (
504+ {
505+ exec : async ( args ) => {
506+ const command = args . slice ( 1 ) . join ( ' ' ) ;
507+ commands . push ( command ) ;
508+ if ( command === 'test -d /proc/42' ) {
509+ return ++ probes === 1 && interruptedAt === 'recheck'
510+ ? result ( )
511+ : { stdout : '' , stderr : '' , exitCode : 1 , signal : 'SIGTERM' } ;
512+ }
513+ if ( command . endsWith ( '/cmdline' ) ) return result ( '' , 'read failed' , 1 ) ;
514+ if ( command . endsWith ( '/stat' ) ) return result ( procStat ( 42 , '42' ) ) ;
515+ throw new Error ( 'unexpected command' ) ;
516+ } ,
517+ } ,
518+ { serial : android . id } ,
519+ async ( ) => {
520+ const transport = await createAndroidScreenRecordingTransport (
521+ android ,
522+ createLiveTestHostEnvironment ( ) ,
523+ ) ;
524+ await expect (
525+ transport . stop ( { pid : '42' , remotePath : '/sdcard/capture.mp4' , startTime : '42' } ) ,
526+ ) . resolves . toBe ( 'uncertain' ) ;
527+ expect ( commands . some ( ( command ) => command . startsWith ( 'kill ' ) ) ) . toBe ( false ) ;
528+ } ,
529+ ) ;
530+ } ) ;
531+ }
0 commit comments