@@ -37,7 +37,6 @@ describe('constants', () => {
3737
3838describe ( 'collectBody' , ( ) => {
3939 it ( 'resolves with the concatenated buffer from all data events' , async ( ) => {
40- const { EventEmitter } = require ( 'node:events' ) ;
4140 const mockRes = new EventEmitter ( ) ;
4241
4342 const promise = collectBody ( mockRes ) ;
@@ -51,7 +50,6 @@ describe('collectBody', () => {
5150 } ) ;
5251
5352 it ( 'rejects when the stream emits an error' , async ( ) => {
54- const { EventEmitter } = require ( 'node:events' ) ;
5553 const mockRes = new EventEmitter ( ) ;
5654
5755 const promise = collectBody ( mockRes ) ;
@@ -132,11 +130,36 @@ describe('saveFile', () => {
132130 } ) ;
133131} ) ;
134132
133+ // Shared test helpers
134+
135+ const { EventEmitter } = require ( 'node:events' ) ;
136+
137+ /**
138+ * Helper: creates a minimal IncomingMessage-like EventEmitter.
139+ */
140+ function makeResponse ( { statusCode = 200 , headers = { } , body = '' } = { } ) {
141+ const res = new EventEmitter ( ) ;
142+ res . statusCode = statusCode ;
143+ res . headers = headers ;
144+ // Schedule body/end asynchronously so the promise chain runs first
145+ setImmediate ( ( ) => {
146+ res . emit ( 'data' , typeof body === 'string' ? Buffer . from ( body ) : body ) ;
147+ res . emit ( 'end' ) ;
148+ } ) ;
149+ return res ;
150+ }
151+
152+ /**
153+ * Helper: creates a minimal https.get return stub with an .on() method.
154+ */
155+ function makeReq ( ) {
156+ return { on : jest . fn ( ) . mockReturnThis ( ) } ;
157+ }
158+
135159// fetchUrl
136160
137161describe ( 'fetchUrl' , ( ) => {
138162 const https = require ( 'node:https' ) ;
139- const { EventEmitter } = require ( 'node:events' ) ;
140163
141164 let httpsGetSpy ;
142165
@@ -148,28 +171,6 @@ describe('fetchUrl', () => {
148171 jest . restoreAllMocks ( ) ;
149172 } ) ;
150173
151- /**
152- * Helper: creates a minimal IncomingMessage-like EventEmitter.
153- */
154- function makeResponse ( { statusCode = 200 , headers = { } , body = 'ok' } ) {
155- const res = new EventEmitter ( ) ;
156- res . statusCode = statusCode ;
157- res . headers = headers ;
158- // Schedule body/end asynchronously so the promise chain runs first
159- setImmediate ( ( ) => {
160- res . emit ( 'data' , Buffer . from ( body ) ) ;
161- res . emit ( 'end' ) ;
162- } ) ;
163- return res ;
164- }
165-
166- /**
167- * Helper: creates a minimal https.get return stub with an .on() method.
168- */
169- function makeReq ( ) {
170- return { on : jest . fn ( ) . mockReturnThis ( ) } ;
171- }
172-
173174 it ( 'resolves with the body buffer for a 200 response' , async ( ) => {
174175 const res = makeResponse ( { body : 'hello world' } ) ;
175176 httpsGetSpy . mockImplementation ( ( _url , cb ) => {
@@ -280,7 +281,6 @@ describe('syncDistribution', () => {
280281 const fs = require ( 'node:fs' ) ;
281282 const path = require ( 'node:path' ) ;
282283 const os = require ( 'node:os' ) ;
283- const { EventEmitter } = require ( 'node:events' ) ;
284284
285285 let tmpDir ;
286286 let httpsGetSpy ;
@@ -295,21 +295,6 @@ describe('syncDistribution', () => {
295295 fs . rmSync ( tmpDir , { recursive : true , force : true } ) ;
296296 } ) ;
297297
298- function makeResponse ( { statusCode = 200 , headers = { } , body = '' } ) {
299- const res = new EventEmitter ( ) ;
300- res . statusCode = statusCode ;
301- res . headers = headers ;
302- setImmediate ( ( ) => {
303- res . emit ( 'data' , typeof body === 'string' ? Buffer . from ( body ) : body ) ;
304- res . emit ( 'end' ) ;
305- } ) ;
306- return res ;
307- }
308-
309- function makeReq ( ) {
310- return { on : jest . fn ( ) . mockReturnThis ( ) } ;
311- }
312-
313298 it ( 'returns true when all files are fetched successfully' , async ( ) => {
314299 const manifest = {
315300 timestamp : 12345 ,
@@ -449,7 +434,6 @@ describe('main', () => {
449434 const fs = require ( 'node:fs' ) ;
450435 const path = require ( 'node:path' ) ;
451436 const os = require ( 'node:os' ) ;
452- const { EventEmitter } = require ( 'node:events' ) ;
453437
454438 let tmpDir ;
455439 let httpsGetSpy ;
@@ -480,20 +464,6 @@ describe('main', () => {
480464 }
481465 } ) ;
482466
483- function makeResponse ( { statusCode = 200 , headers = { } , body = '' } ) {
484- const res = new EventEmitter ( ) ;
485- res . statusCode = statusCode ;
486- res . headers = headers ;
487- setImmediate ( ( ) => {
488- res . emit ( 'data' , Buffer . from ( body ) ) ;
489- res . emit ( 'end' ) ;
490- } ) ;
491- return res ;
492- }
493-
494- function makeReq ( ) {
495- return { on : jest . fn ( ) . mockReturnThis ( ) } ;
496- }
497467
498468 it ( 'completes successfully when all distributions sync without error' , async ( ) => {
499469 // main() reads DISTRIBUTIONS at module load time, so we call syncDistribution
0 commit comments