feat(autoupdate): Improve update check and refactor for testability (#5389)

This commit is contained in:
Gal Zahavi
2025-08-01 20:17:32 -07:00
committed by GitHub
parent 15a1f1af9d
commit 820169ba2e
5 changed files with 258 additions and 68 deletions

View File

@@ -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');
});
});
});