@@ -8,7 +8,16 @@ import type { ClientRequest, IncomingMessage } from "node:http";
88import { afterEach , describe , expect , it , vi } from "vitest" ;
99import { downloadFile } from "./download.js" ;
1010
11+ const { unlinkSyncMock } = vi . hoisted ( ( ) => ( {
12+ unlinkSyncMock : vi . fn ( ) ,
13+ } ) ) ;
14+
1115vi . mock ( "node:https" , ( ) => ( { get : vi . fn ( ) } ) ) ;
16+ vi . mock ( "node:fs" , async ( importOriginal ) => {
17+ const actual = await importOriginal < typeof import ( "node:fs" ) > ( ) ;
18+ unlinkSyncMock . mockImplementation ( actual . unlinkSync ) ;
19+ return { ...actual , unlinkSync : unlinkSyncMock } ;
20+ } ) ;
1221
1322const mockGet = vi . mocked ( httpsGet ) ;
1423const tempDirs : string [ ] = [ ] ;
@@ -20,13 +29,27 @@ afterEach(() => {
2029
2130describe ( "downloadFile" , ( ) => {
2231 it ( "rejects an idle response and removes the partial file" , async ( ) => {
32+ const actualFs = await vi . importActual < typeof import ( "node:fs" ) > ( "node:fs" ) ;
33+ let responseClosed = false ;
34+ unlinkSyncMock . mockImplementation ( ( path ) => {
35+ if ( ! responseClosed ) {
36+ const error = new Error ( "file is locked" ) ;
37+ Object . assign ( error , { code : "EBUSY" } ) ;
38+ throw error ;
39+ }
40+ actualFs . unlinkSync ( path ) ;
41+ } ) ;
42+
2343 mockGet . mockImplementation ( ( ( _url : string , callback : ( response : IncomingMessage ) => void ) => {
2444 const response = new PassThrough ( ) as PassThrough & {
2545 statusCode : number ;
2646 headers : Record < string , string > ;
2747 } ;
2848 response . statusCode = 200 ;
2949 response . headers = { } ;
50+ response . once ( "close" , ( ) => {
51+ responseClosed = true ;
52+ } ) ;
3053
3154 class FakeRequest extends EventEmitter {
3255 private timeout : ReturnType < typeof setTimeout > | undefined ;
0 commit comments