Skip to content

Commit 1e20f9b

Browse files
committed
fix(cli/update): failing tests
1 parent d348927 commit 1e20f9b

File tree

1 file changed

+74
-37
lines changed

1 file changed

+74
-37
lines changed

genkit-tools/cli/tests/commands/update_test.ts

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,44 @@ jest.mock('@genkit-ai/tools-common/utils', () => ({
2828
setUserSettings: jest.fn(),
2929
}));
3030

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+
3158
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+
},
3469
}));
3570

3671
jest.mock('child_process', () => ({
@@ -103,6 +138,7 @@ import {
103138
import { detectCLIRuntime } from '../../src/utils/runtime-detector';
104139

105140
const mockedAxios = axios as jest.Mocked<typeof axios>;
141+
const mockedAxiosGet = mockAxiosGet;
106142
const mockedDetectCLIRuntime = detectCLIRuntime as jest.MockedFunction<
107143
typeof detectCLIRuntime
108144
>;
@@ -126,6 +162,7 @@ describe('update command', () => {
126162
beforeEach(() => {
127163
// Restore mocks
128164
jest.clearAllMocks();
165+
mockedAxiosGet.mockReset();
129166
mockedDetectCLIRuntime.mockReturnValue(mockCLIRuntime);
130167
(getUserSettings as jest.Mock).mockRestore();
131168

@@ -142,7 +179,7 @@ describe('update command', () => {
142179
versions: { '1.1.0': {}, '1.0.0': {} },
143180
},
144181
};
145-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
182+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
146183

147184
const result = await checkForUpdates();
148185

@@ -161,7 +198,7 @@ describe('update command', () => {
161198
versions: { '1.0.0': {} },
162199
},
163200
};
164-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
201+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
165202

166203
const result = await checkForUpdates();
167204

@@ -183,26 +220,26 @@ describe('update command', () => {
183220
platforms: {},
184221
},
185222
};
186-
mockedAxios.get.mockResolvedValueOnce(mockGCSResponse);
223+
mockedAxiosGet.mockResolvedValueOnce(mockGCSResponse);
187224

188225
const result = await checkForUpdates();
189226

190-
expect(mockedAxios.get).toHaveBeenCalledWith(
227+
expect(mockedAxiosGet).toHaveBeenCalledWith(
191228
'https://storage.googleapis.com/genkit-assets-cli/latest.json'
192229
);
193230
expect(result.latestVersion).toBe('1.1.0');
194231
});
195232

196233
it('should handle errors when fetching npm versions', async () => {
197-
mockedAxios.get.mockRejectedValueOnce(new Error('Network error'));
234+
mockedAxiosGet.mockRejectedValueOnce(new Error('Network error'));
198235

199236
await expect(getAvailableVersionsFromNpm()).rejects.toThrow(
200237
'Failed to fetch npm versions'
201238
);
202239
});
203240

204241
it('should handle errors when fetching GCS versions', async () => {
205-
mockedAxios.get.mockRejectedValueOnce(new Error('Network error'));
242+
mockedAxiosGet.mockRejectedValueOnce(new Error('Network error'));
206243

207244
await expect(getLatestVersionFromGCS()).rejects.toThrow(
208245
'Failed to fetch GCS versions'
@@ -217,7 +254,7 @@ describe('update command', () => {
217254
versions: { '2.0.0': {}, '1.0.0': {} },
218255
},
219256
};
220-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
257+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
221258

222259
const result = await checkForUpdates();
223260

@@ -244,7 +281,7 @@ describe('update command', () => {
244281
},
245282
},
246283
};
247-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
284+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
248285

249286
const result = await getAvailableVersionsFromNpm();
250287

@@ -267,7 +304,7 @@ describe('update command', () => {
267304
},
268305
},
269306
};
270-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
307+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
271308

272309
const result = await getAvailableVersionsFromNpm(false);
273310

@@ -283,7 +320,7 @@ describe('update command', () => {
283320
versions: {},
284321
},
285322
};
286-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
323+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
287324

288325
const result = await getAvailableVersionsFromNpm();
289326

@@ -298,7 +335,7 @@ describe('update command', () => {
298335
// Missing versions property
299336
},
300337
};
301-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
338+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
302339

303340
await expect(getAvailableVersionsFromNpm()).rejects.toThrow(
304341
'Failed to fetch npm versions'
@@ -323,12 +360,12 @@ describe('update command', () => {
323360
},
324361
},
325362
};
326-
mockedAxios.get.mockResolvedValueOnce(mockGCSResponse);
363+
mockedAxiosGet.mockResolvedValueOnce(mockGCSResponse);
327364

328365
const result = await getLatestVersionFromGCS();
329366

330367
expect(result).toEqual(['1.5.0']);
331-
expect(mockedAxios.get).toHaveBeenCalledWith(
368+
expect(mockedAxiosGet).toHaveBeenCalledWith(
332369
'https://storage.googleapis.com/genkit-assets-cli/latest.json'
333370
);
334371
});
@@ -342,7 +379,7 @@ describe('update command', () => {
342379
lastUpdated: '2024-01-01T00:00:00Z',
343380
},
344381
};
345-
mockedAxios.get.mockResolvedValueOnce(mockGCSResponse);
382+
mockedAxiosGet.mockResolvedValueOnce(mockGCSResponse);
346383

347384
await expect(getLatestVersionFromGCS()).rejects.toThrow(
348385
'No latest version found'
@@ -386,7 +423,7 @@ describe('update command', () => {
386423
versions: { '1.1.0': {}, '1.0.0': {} },
387424
},
388425
};
389-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
426+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
390427
const consoleSpy = jest
391428
.spyOn(console, 'log')
392429
.mockImplementation(() => {});
@@ -406,7 +443,7 @@ describe('update command', () => {
406443
versions: { '1.0.0': {} },
407444
},
408445
};
409-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
446+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
410447
const consoleSpy = jest
411448
.spyOn(console, 'log')
412449
.mockImplementation(() => {});
@@ -417,7 +454,7 @@ describe('update command', () => {
417454
});
418455

419456
it('should silently fail on network errors', async () => {
420-
mockedAxios.get.mockRejectedValueOnce(new Error('Network error'));
457+
mockedAxiosGet.mockRejectedValueOnce(new Error('Network error'));
421458
const consoleSpy = jest
422459
.spyOn(console, 'log')
423460
.mockImplementation(() => {});
@@ -429,7 +466,7 @@ describe('update command', () => {
429466
it('should show notification for binary runtime updates', async () => {
430467
(getUserSettings as jest.Mock).mockReturnValue({});
431468
mockedDetectCLIRuntime.mockReturnValue(mockBinaryRuntime);
432-
mockedAxios.get.mockReset();
469+
mockedAxiosGet.mockReset();
433470
const mockGCSResponse = {
434471
status: 200,
435472
data: {
@@ -439,7 +476,7 @@ describe('update command', () => {
439476
platforms: {},
440477
},
441478
};
442-
mockedAxios.get.mockResolvedValueOnce(mockGCSResponse);
479+
mockedAxiosGet.mockResolvedValueOnce(mockGCSResponse);
443480
const consoleSpy = jest
444481
.spyOn(console, 'log')
445482
.mockImplementation(() => {});
@@ -455,7 +492,7 @@ describe('update command', () => {
455492
describe('error handling and edge cases', () => {
456493
it('should handle 404 errors from npm registry', async () => {
457494
const error = new Error('Request failed with status code 404');
458-
mockedAxios.get.mockRejectedValueOnce(error);
495+
mockedAxiosGet.mockRejectedValueOnce(error);
459496

460497
await expect(getAvailableVersionsFromNpm()).rejects.toThrow(
461498
'Failed to fetch npm versions'
@@ -464,8 +501,8 @@ describe('update command', () => {
464501

465502
it('should handle 404 errors from GCS', async () => {
466503
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);
469506

470507
await expect(getLatestVersionFromGCS()).rejects.toThrow(
471508
'Failed to fetch GCS versions'
@@ -477,7 +514,7 @@ describe('update command', () => {
477514
status: 200,
478515
data: 'not a valid json object',
479516
};
480-
mockedAxios.get.mockResolvedValueOnce(malformedResponse);
517+
mockedAxiosGet.mockResolvedValueOnce(malformedResponse);
481518

482519
await expect(getAvailableVersionsFromNpm()).rejects.toThrow(
483520
'Failed to fetch npm versions'
@@ -489,7 +526,7 @@ describe('update command', () => {
489526
status: 200,
490527
data: null,
491528
};
492-
mockedAxios.get.mockResolvedValueOnce(emptyResponse);
529+
mockedAxiosGet.mockResolvedValueOnce(emptyResponse);
493530

494531
await expect(getAvailableVersionsFromNpm()).rejects.toThrow(
495532
'Failed to fetch npm versions'
@@ -500,31 +537,31 @@ describe('update command', () => {
500537
describe('version comparison edge cases', () => {
501538
it('should handle version comparison with different formats', async () => {
502539
// Test the basic functionality without version prefix issues
503-
mockedAxios.get.mockReset();
540+
mockedAxiosGet.mockReset();
504541
const mockNpmResponse = {
505542
status: 200,
506543
data: {
507544
'dist-tags': { latest: '1.1.0' },
508545
versions: { '1.1.0': {}, '1.0.0': {} },
509546
},
510547
};
511-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
548+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
512549

513550
const result = await checkForUpdates();
514551

515552
expect(result.hasUpdate).toBe(true);
516553
});
517554

518555
it('should handle identical versions correctly', async () => {
519-
mockedAxios.get.mockReset();
556+
mockedAxiosGet.mockReset();
520557
const mockNpmResponse = {
521558
status: 200,
522559
data: {
523560
'dist-tags': { latest: '1.0.0' },
524561
versions: { '1.0.0': {} },
525562
},
526563
};
527-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
564+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
528565

529566
const result = await checkForUpdates();
530567

@@ -542,19 +579,19 @@ describe('update command', () => {
542579
versions: { '1.1.0': {}, '1.0.0': {} },
543580
},
544581
};
545-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
582+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
546583

547584
const result = await checkForUpdates();
548585

549-
expect(mockedAxios.get).toHaveBeenCalledWith(
586+
expect(mockedAxiosGet).toHaveBeenCalledWith(
550587
'https://registry.npmjs.org/genkit-cli'
551588
);
552589
expect(result.hasUpdate).toBe(true);
553590
});
554591

555592
it('should use GCS for compiled binary runtime', async () => {
556593
mockedDetectCLIRuntime.mockReturnValue(mockBinaryRuntime);
557-
mockedAxios.get.mockReset();
594+
mockedAxiosGet.mockReset();
558595
const mockGCSResponse = {
559596
status: 200,
560597
data: {
@@ -564,11 +601,11 @@ describe('update command', () => {
564601
platforms: {},
565602
},
566603
};
567-
mockedAxios.get.mockResolvedValueOnce(mockGCSResponse);
604+
mockedAxiosGet.mockResolvedValueOnce(mockGCSResponse);
568605

569606
const result = await checkForUpdates();
570607

571-
expect(mockedAxios.get).toHaveBeenCalledWith(
608+
expect(mockedAxiosGet).toHaveBeenCalledWith(
572609
'https://storage.googleapis.com/genkit-assets-cli/latest.json'
573610
);
574611
expect(result.hasUpdate).toBe(true);
@@ -590,11 +627,11 @@ describe('update command', () => {
590627
versions: { '1.1.0': {}, '1.0.0': {} },
591628
},
592629
};
593-
mockedAxios.get.mockResolvedValueOnce(mockNpmResponse);
630+
mockedAxiosGet.mockResolvedValueOnce(mockNpmResponse);
594631

595632
await checkForUpdates();
596633

597-
expect(mockedAxios.get).toHaveBeenCalledWith(
634+
expect(mockedAxiosGet).toHaveBeenCalledWith(
598635
'https://registry.npmjs.org/genkit-cli'
599636
);
600637
});

0 commit comments

Comments
 (0)