@@ -507,6 +507,72 @@ for (const failure of ['sparse', 'canceled'] as const) {
507507 }
508508}
509509
510+ for ( const outcome of [ 'capture failure' , 'crop refusal' , 'cancellation' , 'published' ] as const ) {
511+ test ( `staging cleanup failure preserves ${ outcome } ` , async ( ) => {
512+ const controller = new AbortController ( ) ;
513+ const captureFailure = new Error ( 'capture transport failed' ) ;
514+ let stagedDirectory : string | undefined ;
515+ const fixture = screenshotRuntimeFixture ( {
516+ onCapture : ( input ) => {
517+ stagedDirectory = path . dirname ( input . outPath ) ;
518+ writeSolidPng ( input . outPath ) ;
519+ if ( outcome === 'capture failure' ) throw captureFailure ;
520+ } ,
521+ snapshotResult : ( ) => {
522+ if ( outcome === 'cancellation' ) controller . abort ( ) ;
523+ return {
524+ nodes : outcome === 'crop refusal' ? [ ] : CROP_TREE ,
525+ backend : 'android' ,
526+ producer : 'android-uiautomator' ,
527+ } ;
528+ } ,
529+ } ) ;
530+ const session = makeSession ( 'default' , { device : ANDROID_EMULATOR } ) ;
531+ const outPath = path . join ( mkdtempForTestSync ( 'crop-cleanup-outcome' ) , 'capture.png' ) ;
532+ const sentinel = Buffer . from ( 'old destination' ) ;
533+ fs . writeFileSync ( outPath , sentinel ) ;
534+ const req = screenshotRequest ( {
535+ positionals : [ outPath ] ,
536+ flags : { screenshotCropOn : 'label="Save"' } ,
537+ } ) ;
538+ const resolved = await resolveScreenshotGenericExecution ( {
539+ hostCwd : process . cwd ( ) ,
540+ createCommandSurface : testCreateCommandSurface ,
541+ platformServices : daemonPlatformServicesFixture ( ) ,
542+ req,
543+ session,
544+ inspectFacts : fixture . inspectFacts ,
545+ bindDevice : fixture . bindDevice ,
546+ } ) ;
547+ const remove = fs . promises . rm ;
548+ const cleanup = vi . spyOn ( fs . promises , 'rm' ) . mockImplementation ( async ( target , options ) => {
549+ if ( String ( target ) === stagedDirectory ) throw new Error ( 'staging removal refused' ) ;
550+ await remove ( target , options ) ;
551+ } ) ;
552+ try {
553+ const operation = executeResult ( resolved , session , req , { signal : controller . signal } ) ;
554+ if ( outcome === 'published' ) {
555+ await expect ( operation ) . resolves . toMatchObject ( { path : outPath , width : 40 , height : 20 } ) ;
556+ expect ( fs . readFileSync ( outPath ) ) . not . toEqual ( sentinel ) ;
557+ } else {
558+ if ( outcome === 'capture failure' ) await expect ( operation ) . rejects . toBe ( captureFailure ) ;
559+ if ( outcome === 'crop refusal' )
560+ await expect ( operation ) . rejects . toMatchObject ( {
561+ details : { reason : SCREENSHOT_CROP_REASONS . targetNotFound } ,
562+ } ) ;
563+ if ( outcome === 'cancellation' )
564+ await expect ( operation ) . rejects . toMatchObject ( { name : 'AbortError' } ) ;
565+ expect ( fs . readFileSync ( outPath ) ) . toEqual ( sentinel ) ;
566+ }
567+ expect ( stagedDirectory ) . toBeDefined ( ) ;
568+ if ( stagedDirectory ) expect ( fs . existsSync ( stagedDirectory ) ) . toBe ( true ) ;
569+ } finally {
570+ cleanup . mockRestore ( ) ;
571+ if ( stagedDirectory ) fs . rmSync ( stagedDirectory , { recursive : true , force : true } ) ;
572+ }
573+ } ) ;
574+ }
575+
510576test ( 'a partial crop surfaces its warning once in the result record and annotates nothing' , async ( ) => {
511577 const fixture = screenshotRuntimeFixture ( {
512578 snapshotResult : ( ) => ( {
0 commit comments