@@ -84,3 +84,63 @@ function cropPngBuffer(
8484 }
8585 return PNG . sync . write ( cropped ) ;
8686}
87+
88+ for ( const staged of [ false , true ] ) {
89+ test ( `late cancellation ${ staged ? 'refuses staged publication' : 'retains direct screenshot success' } ` , async ( ) => {
90+ const root = mkdtempForTestSync ( 'screenshot-late-cancellation' ) ;
91+ const destination = path . join ( root , 'output.png' ) ;
92+ const staging = path . join ( root , 'work.png' ) ;
93+ const sentinel = Buffer . from ( 'old screenshot' ) ;
94+ fs . writeFileSync ( destination , sentinel ) ;
95+ const controller = new AbortController ( ) ;
96+ const cancellation = new Error ( 'caller cancelled after capture' ) ;
97+ const adapter = createLocalArtifactAdapter ( ) ;
98+ let publications = 0 ;
99+ let cleanups = 0 ;
100+ if ( staged ) {
101+ adapter . reserveOutput = async ( ) => ( {
102+ path : staging ,
103+ publishedPath : destination ,
104+ visibility : 'client-visible' ,
105+ publish : async ( ) => {
106+ publications += 1 ;
107+ fs . renameSync ( staging , destination ) ;
108+ return undefined ;
109+ } ,
110+ cleanup : async ( ) => {
111+ cleanups += 1 ;
112+ fs . rmSync ( staging , { force : true } ) ;
113+ } ,
114+ } ) ;
115+ }
116+ const device = createAgentDevice ( {
117+ backend : {
118+ platform : 'ios' ,
119+ captureScreenshot : async ( _context , requestedPath ) => {
120+ fs . writeFileSync ( requestedPath , solidPngBuffer ( 8 , 8 ) ) ;
121+ controller . abort ( cancellation ) ;
122+ } ,
123+ } ,
124+ artifacts : adapter ,
125+ sessions : { get : ( ) => undefined , set : ( ) => { } } ,
126+ policy : localCommandPolicy ( ) ,
127+ } ) ;
128+ const operation = device . capture . screenshot ( {
129+ out : { kind : 'path' , path : destination } ,
130+ scale : 0.5 ,
131+ signal : controller . signal ,
132+ } ) ;
133+ if ( staged ) {
134+ await assert . rejects ( operation , ( error ) => error === cancellation ) ;
135+ assert . deepEqual ( fs . readFileSync ( destination ) , sentinel ) ;
136+ assert . equal ( fs . existsSync ( staging ) , false ) ;
137+ assert . equal ( publications , 0 ) ;
138+ assert . equal ( cleanups , 1 ) ;
139+ } else {
140+ const result = await operation ;
141+ assert . equal ( result . path , destination ) ;
142+ const image = PNG . sync . read ( fs . readFileSync ( destination ) ) ;
143+ assert . deepEqual ( { width : image . width , height : image . height } , { width : 4 , height : 4 } ) ;
144+ }
145+ } ) ;
146+ }
0 commit comments