@@ -19,8 +19,8 @@ const baseReq = (files) => ({
1919 githubToken : "ght" ,
2020 files,
2121} ) ;
22- const fileWith = ( content ) => async ( ) => ( { ok : true , text : async ( ) => content } ) ;
23- const status = ( code ) => async ( ) => ( { ok : code >= 200 && code < 300 , status : code , text : async ( ) => "" } ) ;
22+ const fileWith = ( content , init ) => async ( ) => new Response ( content , init ) ;
23+ const status = ( code ) => async ( ) => new Response ( "" , { status : code } ) ;
2424const oldParams = ( entries ) => new Map ( entries . map ( ( [ name , ids ] ) => [ name , new Set ( ids ) ] ) ) ;
2525
2626const DRIFTED = `/**\n * @param oldName the old one\n */\nexport function doThing(newName) {\n return newName;\n}\n` ;
@@ -227,6 +227,43 @@ test("scanDocCommentDrift: fetches the file at headSha and reports drift", async
227227 assert . deepEqual ( findings [ 0 ] . staleParams , [ "oldName" ] ) ;
228228} ) ;
229229
230+ test ( "scanDocCommentDrift: skips oversized file responses before reading the body" , async ( ) => {
231+ let bodyAccessed = false ;
232+ const out = await scanDocCommentDrift (
233+ baseReq ( [ { path : "src/a.ts" , patch : DRIFT_PATCH } ] ) ,
234+ async ( ) => ( {
235+ ok : true ,
236+ headers : new Headers ( { "content-length" : "1000001" } ) ,
237+ get body ( ) {
238+ bodyAccessed = true ;
239+ return new Response ( DRIFTED ) . body ;
240+ } ,
241+ } ) ,
242+ ) ;
243+ assert . deepEqual ( out , [ ] ) ;
244+ assert . equal ( bodyAccessed , false ) ;
245+ } ) ;
246+
247+ test ( "scanDocCommentDrift: cancels streamed file responses that exceed the byte cap" , async ( ) => {
248+ let canceled = false ;
249+ const chunk = new Uint8Array ( 500_001 ) ;
250+ const stream = new ReadableStream ( {
251+ start ( controller ) {
252+ controller . enqueue ( chunk ) ;
253+ controller . enqueue ( chunk ) ;
254+ } ,
255+ cancel ( ) {
256+ canceled = true ;
257+ } ,
258+ } ) ;
259+ const out = await scanDocCommentDrift (
260+ baseReq ( [ { path : "src/a.ts" , patch : DRIFT_PATCH } ] ) ,
261+ async ( ) => new Response ( stream ) ,
262+ ) ;
263+ assert . deepEqual ( out , [ ] ) ;
264+ assert . equal ( canceled , true ) ;
265+ } ) ;
266+
230267test ( "scanDocCommentDrift: requires a github token and a head sha" , async ( ) => {
231268 assert . deepEqual ( await scanDocCommentDrift ( { repoFullName : "o/r" , prNumber : 1 , headSha : "x" , files : [ { path : "src/a.ts" , patch : DRIFT_PATCH } ] } , fileWith ( DRIFTED ) ) , [ ] ) ;
232269 assert . deepEqual ( await scanDocCommentDrift ( { repoFullName : "o/r" , prNumber : 1 , githubToken : "t" , files : [ { path : "src/a.ts" , patch : DRIFT_PATCH } ] } , fileWith ( DRIFTED ) ) , [ ] ) ;
0 commit comments