@@ -148,7 +148,9 @@ describe("downloadCommentImages", () => {
148148 mediaType : { format : "full+json" } ,
149149 } ) ;
150150
151- expect ( fetchSpy ) . toHaveBeenCalledWith ( signedUrl ) ;
151+ expect ( fetchSpy ) . toHaveBeenCalledWith ( signedUrl , {
152+ signal : expect . any ( AbortSignal ) ,
153+ } ) ;
152154 expect ( fsWriteFileSpy ) . toHaveBeenCalledWith (
153155 "/tmp/github-images/image-1704067200000-0.png" ,
154156 Buffer . from ( mockArrayBuffer ) ,
@@ -481,8 +483,12 @@ describe("downloadCommentImages", () => {
481483 ) ;
482484
483485 expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
484- expect ( fetchSpy ) . toHaveBeenNthCalledWith ( 1 , signedUrl1 ) ;
485- expect ( fetchSpy ) . toHaveBeenNthCalledWith ( 2 , signedUrl2 ) ;
486+ expect ( fetchSpy ) . toHaveBeenNthCalledWith ( 1 , signedUrl1 , {
487+ signal : expect . any ( AbortSignal ) ,
488+ } ) ;
489+ expect ( fetchSpy ) . toHaveBeenNthCalledWith ( 2 , signedUrl2 , {
490+ signal : expect . any ( AbortSignal ) ,
491+ } ) ;
486492 expect ( result . get ( imageUrl1 ) ) . toBe (
487493 "/tmp/github-images/image-1704067200000-0.png" ,
488494 ) ;
@@ -523,7 +529,9 @@ describe("downloadCommentImages", () => {
523529 comments ,
524530 ) ;
525531
526- expect ( fetchSpy ) . toHaveBeenCalledWith ( signedUrl ) ;
532+ expect ( fetchSpy ) . toHaveBeenCalledWith ( signedUrl , {
533+ signal : expect . any ( AbortSignal ) ,
534+ } ) ;
527535 expect ( result . get ( imageUrl ) ) . toBe (
528536 "/tmp/github-images/image-1704067200000-0.png" ,
529537 ) ;
@@ -766,6 +774,95 @@ describe("downloadCommentImages", () => {
766774 ) ;
767775 } ) ;
768776
777+ test ( "should skip an image when the fetch times out" , async ( ) => {
778+ const mockOctokit = createMockOctokit ( ) ;
779+ const imageUrl = assetUrl ( GUID_1 ) ;
780+ const signedUrl = signedUrlFor ( GUID_1 , ".png" ) ;
781+ let signal : AbortSignal | null | undefined ;
782+
783+ // @ts -expect-error Mock implementation doesn't match full type signature
784+ mockOctokit . rest . issues . getComment = jest . fn ( ) . mockResolvedValue ( {
785+ data : {
786+ body_html : `<img src="${ signedUrl } ">` ,
787+ } ,
788+ } ) ;
789+
790+ fetchSpy = spyOn ( global , "fetch" ) ;
791+ fetchSpy . mockImplementation ( ( _input : unknown , init ?: RequestInit ) => {
792+ signal = init ?. signal ;
793+ return new Promise < Response > ( ( ) => { } ) ;
794+ } ) ;
795+
796+ const result = await downloadCommentImages (
797+ mockOctokit ,
798+ "owner" ,
799+ "repo" ,
800+ [
801+ {
802+ type : "issue_comment" ,
803+ id : "445" ,
804+ body : `Stalled image: ` ,
805+ } ,
806+ ] ,
807+ { timeoutMs : 5 } ,
808+ ) ;
809+
810+ expect ( result . size ) . toBe ( 0 ) ;
811+ expect ( signal ?. aborted ) . toBe ( true ) ;
812+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
813+ expect . stringContaining ( "Failed to download" ) ,
814+ expect . objectContaining ( {
815+ message : "Image download timed out after 5ms" ,
816+ } ) ,
817+ ) ;
818+ } ) ;
819+
820+ test ( "should time out while reading a response body" , async ( ) => {
821+ const mockOctokit = createMockOctokit ( ) ;
822+ const imageUrl = assetUrl ( GUID_1 ) ;
823+ const signedUrl = signedUrlFor ( GUID_1 , ".png" ) ;
824+ let signal : AbortSignal | null | undefined ;
825+
826+ // @ts -expect-error Mock implementation doesn't match full type signature
827+ mockOctokit . rest . issues . getComment = jest . fn ( ) . mockResolvedValue ( {
828+ data : {
829+ body_html : `<img src="${ signedUrl } ">` ,
830+ } ,
831+ } ) ;
832+
833+ fetchSpy = spyOn ( global , "fetch" ) ;
834+ fetchSpy . mockImplementation ( ( _input : unknown , init ?: RequestInit ) => {
835+ signal = init ?. signal ;
836+ return Promise . resolve ( {
837+ ok : true ,
838+ arrayBuffer : ( ) => new Promise < ArrayBuffer > ( ( ) => { } ) ,
839+ } as Response ) ;
840+ } ) ;
841+
842+ const result = await downloadCommentImages (
843+ mockOctokit ,
844+ "owner" ,
845+ "repo" ,
846+ [
847+ {
848+ type : "issue_comment" ,
849+ id : "446" ,
850+ body : `Stalled body: ` ,
851+ } ,
852+ ] ,
853+ { timeoutMs : 5 } ,
854+ ) ;
855+
856+ expect ( result . size ) . toBe ( 0 ) ;
857+ expect ( signal ?. aborted ) . toBe ( true ) ;
858+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
859+ expect . stringContaining ( "Failed to download" ) ,
860+ expect . objectContaining ( {
861+ message : "Image download timed out after 5ms" ,
862+ } ) ,
863+ ) ;
864+ } ) ;
865+
769866 test ( "should handle API errors gracefully" , async ( ) => {
770867 const mockOctokit = createMockOctokit ( ) ;
771868 const imageUrl = assetUrl ( GUID_1 ) ;
@@ -936,7 +1033,9 @@ describe("downloadCommentImages", () => {
9361033 mediaType : { format : "full+json" } ,
9371034 } ) ;
9381035
939- expect ( fetchSpy ) . toHaveBeenCalledWith ( signedUrl ) ;
1036+ expect ( fetchSpy ) . toHaveBeenCalledWith ( signedUrl , {
1037+ signal : expect . any ( AbortSignal ) ,
1038+ } ) ;
9401039 expect ( fsWriteFileSpy ) . toHaveBeenCalledWith (
9411040 "/tmp/github-images/image-1704067200000-0.png" ,
9421041 Buffer . from ( mockArrayBuffer ) ,
0 commit comments