@@ -28,9 +28,44 @@ jest.mock('@genkit-ai/tools-common/utils', () => ({
28
28
setUserSettings : jest . fn ( ) ,
29
29
} ) ) ;
30
30
31
+ jest . mock ( '@genkit-ai/tools-common/manager' , ( ) => ( {
32
+ GenkitToolsError : class MockGenkitToolsError extends Error {
33
+ constructor ( message : string , options ?: { cause ?: any } ) {
34
+ super ( message ) ;
35
+ this . name = 'GenkitToolsError' ;
36
+ if ( options ?. cause ) {
37
+ this . cause = options . cause ;
38
+ }
39
+ }
40
+ }
41
+ } ) ) ;
42
+
43
+ jest . mock ( 'chokidar' , ( ) => ( {
44
+ watch : jest . fn ( ( ) => ( {
45
+ on : jest . fn ( ) ,
46
+ close : jest . fn ( ) ,
47
+ } ) ) ,
48
+ default : {
49
+ watch : jest . fn ( ( ) => ( {
50
+ on : jest . fn ( ) ,
51
+ close : jest . fn ( ) ,
52
+ } ) ) ,
53
+ } ,
54
+ } ) ) ;
55
+
56
+ const mockAxiosGet = jest . fn ( ) as jest . MockedFunction < any > ;
57
+
31
58
jest . mock ( 'axios' , ( ) => ( {
32
- get : jest . fn ( ) ,
33
- default : jest . fn ( ) ,
59
+ get : mockAxiosGet ,
60
+ create : jest . fn ( ( ) => ( {
61
+ get : mockAxiosGet ,
62
+ } ) ) ,
63
+ default : {
64
+ get : mockAxiosGet ,
65
+ create : jest . fn ( ( ) => ( {
66
+ get : mockAxiosGet ,
67
+ } ) ) ,
68
+ } ,
34
69
} ) ) ;
35
70
36
71
jest . mock ( 'child_process' , ( ) => ( {
@@ -103,6 +138,7 @@ import {
103
138
import { detectCLIRuntime } from '../../src/utils/runtime-detector' ;
104
139
105
140
const mockedAxios = axios as jest . Mocked < typeof axios > ;
141
+ const mockedAxiosGet = mockAxiosGet ;
106
142
const mockedDetectCLIRuntime = detectCLIRuntime as jest . MockedFunction <
107
143
typeof detectCLIRuntime
108
144
> ;
@@ -126,6 +162,7 @@ describe('update command', () => {
126
162
beforeEach ( ( ) => {
127
163
// Restore mocks
128
164
jest . clearAllMocks ( ) ;
165
+ mockedAxiosGet . mockReset ( ) ;
129
166
mockedDetectCLIRuntime . mockReturnValue ( mockCLIRuntime ) ;
130
167
( getUserSettings as jest . Mock ) . mockRestore ( ) ;
131
168
@@ -142,7 +179,7 @@ describe('update command', () => {
142
179
versions : { '1.1.0' : { } , '1.0.0' : { } } ,
143
180
} ,
144
181
} ;
145
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
182
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
146
183
147
184
const result = await checkForUpdates ( ) ;
148
185
@@ -161,7 +198,7 @@ describe('update command', () => {
161
198
versions : { '1.0.0' : { } } ,
162
199
} ,
163
200
} ;
164
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
201
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
165
202
166
203
const result = await checkForUpdates ( ) ;
167
204
@@ -183,26 +220,26 @@ describe('update command', () => {
183
220
platforms : { } ,
184
221
} ,
185
222
} ;
186
- mockedAxios . get . mockResolvedValueOnce ( mockGCSResponse ) ;
223
+ mockedAxiosGet . mockResolvedValueOnce ( mockGCSResponse ) ;
187
224
188
225
const result = await checkForUpdates ( ) ;
189
226
190
- expect ( mockedAxios . get ) . toHaveBeenCalledWith (
227
+ expect ( mockedAxiosGet ) . toHaveBeenCalledWith (
191
228
'https://storage.googleapis.com/genkit-assets-cli/latest.json'
192
229
) ;
193
230
expect ( result . latestVersion ) . toBe ( '1.1.0' ) ;
194
231
} ) ;
195
232
196
233
it ( 'should handle errors when fetching npm versions' , async ( ) => {
197
- mockedAxios . get . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
234
+ mockedAxiosGet . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
198
235
199
236
await expect ( getAvailableVersionsFromNpm ( ) ) . rejects . toThrow (
200
237
'Failed to fetch npm versions'
201
238
) ;
202
239
} ) ;
203
240
204
241
it ( 'should handle errors when fetching GCS versions' , async ( ) => {
205
- mockedAxios . get . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
242
+ mockedAxiosGet . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
206
243
207
244
await expect ( getLatestVersionFromGCS ( ) ) . rejects . toThrow (
208
245
'Failed to fetch GCS versions'
@@ -217,7 +254,7 @@ describe('update command', () => {
217
254
versions : { '2.0.0' : { } , '1.0.0' : { } } ,
218
255
} ,
219
256
} ;
220
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
257
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
221
258
222
259
const result = await checkForUpdates ( ) ;
223
260
@@ -244,7 +281,7 @@ describe('update command', () => {
244
281
} ,
245
282
} ,
246
283
} ;
247
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
284
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
248
285
249
286
const result = await getAvailableVersionsFromNpm ( ) ;
250
287
@@ -267,7 +304,7 @@ describe('update command', () => {
267
304
} ,
268
305
} ,
269
306
} ;
270
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
307
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
271
308
272
309
const result = await getAvailableVersionsFromNpm ( false ) ;
273
310
@@ -283,7 +320,7 @@ describe('update command', () => {
283
320
versions : { } ,
284
321
} ,
285
322
} ;
286
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
323
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
287
324
288
325
const result = await getAvailableVersionsFromNpm ( ) ;
289
326
@@ -298,7 +335,7 @@ describe('update command', () => {
298
335
// Missing versions property
299
336
} ,
300
337
} ;
301
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
338
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
302
339
303
340
await expect ( getAvailableVersionsFromNpm ( ) ) . rejects . toThrow (
304
341
'Failed to fetch npm versions'
@@ -323,12 +360,12 @@ describe('update command', () => {
323
360
} ,
324
361
} ,
325
362
} ;
326
- mockedAxios . get . mockResolvedValueOnce ( mockGCSResponse ) ;
363
+ mockedAxiosGet . mockResolvedValueOnce ( mockGCSResponse ) ;
327
364
328
365
const result = await getLatestVersionFromGCS ( ) ;
329
366
330
367
expect ( result ) . toEqual ( [ '1.5.0' ] ) ;
331
- expect ( mockedAxios . get ) . toHaveBeenCalledWith (
368
+ expect ( mockedAxiosGet ) . toHaveBeenCalledWith (
332
369
'https://storage.googleapis.com/genkit-assets-cli/latest.json'
333
370
) ;
334
371
} ) ;
@@ -342,7 +379,7 @@ describe('update command', () => {
342
379
lastUpdated : '2024-01-01T00:00:00Z' ,
343
380
} ,
344
381
} ;
345
- mockedAxios . get . mockResolvedValueOnce ( mockGCSResponse ) ;
382
+ mockedAxiosGet . mockResolvedValueOnce ( mockGCSResponse ) ;
346
383
347
384
await expect ( getLatestVersionFromGCS ( ) ) . rejects . toThrow (
348
385
'No latest version found'
@@ -386,7 +423,7 @@ describe('update command', () => {
386
423
versions : { '1.1.0' : { } , '1.0.0' : { } } ,
387
424
} ,
388
425
} ;
389
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
426
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
390
427
const consoleSpy = jest
391
428
. spyOn ( console , 'log' )
392
429
. mockImplementation ( ( ) => { } ) ;
@@ -406,7 +443,7 @@ describe('update command', () => {
406
443
versions : { '1.0.0' : { } } ,
407
444
} ,
408
445
} ;
409
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
446
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
410
447
const consoleSpy = jest
411
448
. spyOn ( console , 'log' )
412
449
. mockImplementation ( ( ) => { } ) ;
@@ -417,7 +454,7 @@ describe('update command', () => {
417
454
} ) ;
418
455
419
456
it ( 'should silently fail on network errors' , async ( ) => {
420
- mockedAxios . get . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
457
+ mockedAxiosGet . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
421
458
const consoleSpy = jest
422
459
. spyOn ( console , 'log' )
423
460
. mockImplementation ( ( ) => { } ) ;
@@ -429,7 +466,7 @@ describe('update command', () => {
429
466
it ( 'should show notification for binary runtime updates' , async ( ) => {
430
467
( getUserSettings as jest . Mock ) . mockReturnValue ( { } ) ;
431
468
mockedDetectCLIRuntime . mockReturnValue ( mockBinaryRuntime ) ;
432
- mockedAxios . get . mockReset ( ) ;
469
+ mockedAxiosGet . mockReset ( ) ;
433
470
const mockGCSResponse = {
434
471
status : 200 ,
435
472
data : {
@@ -439,7 +476,7 @@ describe('update command', () => {
439
476
platforms : { } ,
440
477
} ,
441
478
} ;
442
- mockedAxios . get . mockResolvedValueOnce ( mockGCSResponse ) ;
479
+ mockedAxiosGet . mockResolvedValueOnce ( mockGCSResponse ) ;
443
480
const consoleSpy = jest
444
481
. spyOn ( console , 'log' )
445
482
. mockImplementation ( ( ) => { } ) ;
@@ -455,7 +492,7 @@ describe('update command', () => {
455
492
describe ( 'error handling and edge cases' , ( ) => {
456
493
it ( 'should handle 404 errors from npm registry' , async ( ) => {
457
494
const error = new Error ( 'Request failed with status code 404' ) ;
458
- mockedAxios . get . mockRejectedValueOnce ( error ) ;
495
+ mockedAxiosGet . mockRejectedValueOnce ( error ) ;
459
496
460
497
await expect ( getAvailableVersionsFromNpm ( ) ) . rejects . toThrow (
461
498
'Failed to fetch npm versions'
@@ -464,8 +501,8 @@ describe('update command', () => {
464
501
465
502
it ( 'should handle 404 errors from GCS' , async ( ) => {
466
503
const notFoundError = new Error ( 'Request failed with status code 404' ) ;
467
- mockedAxios . get . mockReset ( ) ;
468
- mockedAxios . get . mockRejectedValueOnce ( notFoundError ) ;
504
+ mockedAxiosGet . mockReset ( ) ;
505
+ mockedAxiosGet . mockRejectedValueOnce ( notFoundError ) ;
469
506
470
507
await expect ( getLatestVersionFromGCS ( ) ) . rejects . toThrow (
471
508
'Failed to fetch GCS versions'
@@ -477,7 +514,7 @@ describe('update command', () => {
477
514
status : 200 ,
478
515
data : 'not a valid json object' ,
479
516
} ;
480
- mockedAxios . get . mockResolvedValueOnce ( malformedResponse ) ;
517
+ mockedAxiosGet . mockResolvedValueOnce ( malformedResponse ) ;
481
518
482
519
await expect ( getAvailableVersionsFromNpm ( ) ) . rejects . toThrow (
483
520
'Failed to fetch npm versions'
@@ -489,7 +526,7 @@ describe('update command', () => {
489
526
status : 200 ,
490
527
data : null ,
491
528
} ;
492
- mockedAxios . get . mockResolvedValueOnce ( emptyResponse ) ;
529
+ mockedAxiosGet . mockResolvedValueOnce ( emptyResponse ) ;
493
530
494
531
await expect ( getAvailableVersionsFromNpm ( ) ) . rejects . toThrow (
495
532
'Failed to fetch npm versions'
@@ -500,31 +537,31 @@ describe('update command', () => {
500
537
describe ( 'version comparison edge cases' , ( ) => {
501
538
it ( 'should handle version comparison with different formats' , async ( ) => {
502
539
// Test the basic functionality without version prefix issues
503
- mockedAxios . get . mockReset ( ) ;
540
+ mockedAxiosGet . mockReset ( ) ;
504
541
const mockNpmResponse = {
505
542
status : 200 ,
506
543
data : {
507
544
'dist-tags' : { latest : '1.1.0' } ,
508
545
versions : { '1.1.0' : { } , '1.0.0' : { } } ,
509
546
} ,
510
547
} ;
511
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
548
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
512
549
513
550
const result = await checkForUpdates ( ) ;
514
551
515
552
expect ( result . hasUpdate ) . toBe ( true ) ;
516
553
} ) ;
517
554
518
555
it ( 'should handle identical versions correctly' , async ( ) => {
519
- mockedAxios . get . mockReset ( ) ;
556
+ mockedAxiosGet . mockReset ( ) ;
520
557
const mockNpmResponse = {
521
558
status : 200 ,
522
559
data : {
523
560
'dist-tags' : { latest : '1.0.0' } ,
524
561
versions : { '1.0.0' : { } } ,
525
562
} ,
526
563
} ;
527
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
564
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
528
565
529
566
const result = await checkForUpdates ( ) ;
530
567
@@ -542,19 +579,19 @@ describe('update command', () => {
542
579
versions : { '1.1.0' : { } , '1.0.0' : { } } ,
543
580
} ,
544
581
} ;
545
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
582
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
546
583
547
584
const result = await checkForUpdates ( ) ;
548
585
549
- expect ( mockedAxios . get ) . toHaveBeenCalledWith (
586
+ expect ( mockedAxiosGet ) . toHaveBeenCalledWith (
550
587
'https://registry.npmjs.org/genkit-cli'
551
588
) ;
552
589
expect ( result . hasUpdate ) . toBe ( true ) ;
553
590
} ) ;
554
591
555
592
it ( 'should use GCS for compiled binary runtime' , async ( ) => {
556
593
mockedDetectCLIRuntime . mockReturnValue ( mockBinaryRuntime ) ;
557
- mockedAxios . get . mockReset ( ) ;
594
+ mockedAxiosGet . mockReset ( ) ;
558
595
const mockGCSResponse = {
559
596
status : 200 ,
560
597
data : {
@@ -564,11 +601,11 @@ describe('update command', () => {
564
601
platforms : { } ,
565
602
} ,
566
603
} ;
567
- mockedAxios . get . mockResolvedValueOnce ( mockGCSResponse ) ;
604
+ mockedAxiosGet . mockResolvedValueOnce ( mockGCSResponse ) ;
568
605
569
606
const result = await checkForUpdates ( ) ;
570
607
571
- expect ( mockedAxios . get ) . toHaveBeenCalledWith (
608
+ expect ( mockedAxiosGet ) . toHaveBeenCalledWith (
572
609
'https://storage.googleapis.com/genkit-assets-cli/latest.json'
573
610
) ;
574
611
expect ( result . hasUpdate ) . toBe ( true ) ;
@@ -590,11 +627,11 @@ describe('update command', () => {
590
627
versions : { '1.1.0' : { } , '1.0.0' : { } } ,
591
628
} ,
592
629
} ;
593
- mockedAxios . get . mockResolvedValueOnce ( mockNpmResponse ) ;
630
+ mockedAxiosGet . mockResolvedValueOnce ( mockNpmResponse ) ;
594
631
595
632
await checkForUpdates ( ) ;
596
633
597
- expect ( mockedAxios . get ) . toHaveBeenCalledWith (
634
+ expect ( mockedAxiosGet ) . toHaveBeenCalledWith (
598
635
'https://registry.npmjs.org/genkit-cli'
599
636
) ;
600
637
} ) ;
0 commit comments