mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-15 16:41:11 -07:00
feat(autoupdate): Improve update check and refactor for testability (#5389)
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||
import { checkForUpdates, FETCH_TIMEOUT_MS } from './updateCheck.js';
|
||||
import { checkForUpdates } from './updateCheck.js';
|
||||
|
||||
const getPackageJson = vi.hoisted(() => vi.fn());
|
||||
vi.mock('../../utils/package.js', () => ({
|
||||
@@ -109,24 +109,16 @@ describe('checkForUpdates', () => {
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if fetchInfo times out', async () => {
|
||||
it('should return null if fetchInfo rejects', async () => {
|
||||
getPackageJson.mockResolvedValue({
|
||||
name: 'test-package',
|
||||
version: '1.0.0',
|
||||
});
|
||||
updateNotifier.mockReturnValue({
|
||||
fetchInfo: vi.fn(
|
||||
async () =>
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve({ current: '1.0.0', latest: '1.1.0' });
|
||||
}, FETCH_TIMEOUT_MS + 1);
|
||||
}),
|
||||
),
|
||||
fetchInfo: vi.fn().mockRejectedValue(new Error('Timeout')),
|
||||
});
|
||||
const promise = checkForUpdates();
|
||||
await vi.advanceTimersByTimeAsync(FETCH_TIMEOUT_MS);
|
||||
const result = await promise;
|
||||
|
||||
const result = await checkForUpdates();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
@@ -135,4 +127,37 @@ describe('checkForUpdates', () => {
|
||||
const result = await checkForUpdates();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
describe('nightly updates', () => {
|
||||
it('should notify for a newer nightly version when current is nightly', async () => {
|
||||
getPackageJson.mockResolvedValue({
|
||||
name: 'test-package',
|
||||
version: '1.2.3-nightly.1',
|
||||
});
|
||||
|
||||
const fetchInfoMock = vi.fn().mockImplementation(({ distTag }) => {
|
||||
if (distTag === 'nightly') {
|
||||
return Promise.resolve({
|
||||
latest: '1.2.3-nightly.2',
|
||||
current: '1.2.3-nightly.1',
|
||||
});
|
||||
}
|
||||
if (distTag === 'latest') {
|
||||
return Promise.resolve({
|
||||
latest: '1.2.3',
|
||||
current: '1.2.3-nightly.1',
|
||||
});
|
||||
}
|
||||
return Promise.resolve(null);
|
||||
});
|
||||
|
||||
updateNotifier.mockImplementation(({ pkg, distTag }) => ({
|
||||
fetchInfo: () => fetchInfoMock({ pkg, distTag }),
|
||||
}));
|
||||
|
||||
const result = await checkForUpdates();
|
||||
expect(result?.message).toContain('1.2.3-nightly.1 → 1.2.3-nightly.2');
|
||||
expect(result?.update.latest).toBe('1.2.3-nightly.2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user