fix(update): correctly report new updates (#4821)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Sambhav Khanna 2025-07-29 20:11:15 -04:00 committed by GitHub
parent 091804c750
commit d5a1b717c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 8 deletions

View File

@ -5,7 +5,7 @@
*/ */
import { vi, describe, it, expect, beforeEach } from 'vitest'; import { vi, describe, it, expect, beforeEach } from 'vitest';
import { checkForUpdates } from './updateCheck.js'; import { checkForUpdates, FETCH_TIMEOUT_MS } from './updateCheck.js';
const getPackageJson = vi.hoisted(() => vi.fn()); const getPackageJson = vi.hoisted(() => vi.fn());
vi.mock('../../utils/package.js', () => ({ vi.mock('../../utils/package.js', () => ({
@ -19,11 +19,17 @@ vi.mock('update-notifier', () => ({
describe('checkForUpdates', () => { describe('checkForUpdates', () => {
beforeEach(() => { beforeEach(() => {
vi.useFakeTimers();
vi.resetAllMocks(); vi.resetAllMocks();
// Clear DEV environment variable before each test // Clear DEV environment variable before each test
delete process.env.DEV; delete process.env.DEV;
}); });
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it('should return null when running from source (DEV=true)', async () => { it('should return null when running from source (DEV=true)', async () => {
process.env.DEV = 'true'; process.env.DEV = 'true';
getPackageJson.mockResolvedValue({ getPackageJson.mockResolvedValue({
@ -31,7 +37,9 @@ describe('checkForUpdates', () => {
version: '1.0.0', version: '1.0.0',
}); });
updateNotifier.mockReturnValue({ updateNotifier.mockReturnValue({
update: { current: '1.0.0', latest: '1.1.0' }, fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
}); });
const result = await checkForUpdates(); const result = await checkForUpdates();
expect(result).toBeNull(); expect(result).toBeNull();
@ -51,7 +59,7 @@ describe('checkForUpdates', () => {
version: '1.0.0', version: '1.0.0',
}); });
updateNotifier.mockReturnValue({ updateNotifier.mockReturnValue({
fetchInfo: vi.fn(async () => null), fetchInfo: vi.fn().mockResolvedValue(null),
}); });
const result = await checkForUpdates(); const result = await checkForUpdates();
expect(result).toBeNull(); expect(result).toBeNull();
@ -63,7 +71,9 @@ describe('checkForUpdates', () => {
version: '1.0.0', version: '1.0.0',
}); });
updateNotifier.mockReturnValue({ updateNotifier.mockReturnValue({
fetchInfo: vi.fn(async () => ({ current: '1.0.0', latest: '1.1.0' })), fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
}); });
const result = await checkForUpdates(); const result = await checkForUpdates();
@ -77,7 +87,9 @@ describe('checkForUpdates', () => {
version: '1.0.0', version: '1.0.0',
}); });
updateNotifier.mockReturnValue({ updateNotifier.mockReturnValue({
fetchInfo: vi.fn(async () => ({ current: '1.0.0', latest: '1.0.0' })), fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.0.0' }),
}); });
const result = await checkForUpdates(); const result = await checkForUpdates();
expect(result).toBeNull(); expect(result).toBeNull();
@ -89,12 +101,35 @@ describe('checkForUpdates', () => {
version: '1.1.0', version: '1.1.0',
}); });
updateNotifier.mockReturnValue({ updateNotifier.mockReturnValue({
fetchInfo: vi.fn(async () => ({ current: '1.0.0', latest: '0.09' })), fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.1.0', latest: '1.0.0' }),
}); });
const result = await checkForUpdates(); const result = await checkForUpdates();
expect(result).toBeNull(); expect(result).toBeNull();
}); });
it('should return null if fetchInfo times out', 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);
}),
),
});
const promise = checkForUpdates();
await vi.advanceTimersByTimeAsync(FETCH_TIMEOUT_MS);
const result = await promise;
expect(result).toBeNull();
});
it('should handle errors gracefully', async () => { it('should handle errors gracefully', async () => {
getPackageJson.mockRejectedValue(new Error('test error')); getPackageJson.mockRejectedValue(new Error('test error'));
const result = await checkForUpdates(); const result = await checkForUpdates();

View File

@ -8,6 +8,8 @@ import updateNotifier, { UpdateInfo } from 'update-notifier';
import semver from 'semver'; import semver from 'semver';
import { getPackageJson } from '../../utils/package.js'; import { getPackageJson } from '../../utils/package.js';
export const FETCH_TIMEOUT_MS = 2000;
export interface UpdateObject { export interface UpdateObject {
message: string; message: string;
update: UpdateInfo; update: UpdateInfo;
@ -34,8 +36,11 @@ export async function checkForUpdates(): Promise<UpdateObject | null> {
// allow notifier to run in scripts // allow notifier to run in scripts
shouldNotifyInNpmScript: true, shouldNotifyInNpmScript: true,
}); });
// avoid blocking by waiting at most FETCH_TIMEOUT_MS for fetchInfo to resolve
const updateInfo = await notifier.fetchInfo(); const timeout = new Promise<null>((resolve) =>
setTimeout(resolve, FETCH_TIMEOUT_MS, null),
);
const updateInfo = await Promise.race([notifier.fetchInfo(), timeout]);
if (updateInfo && semver.gt(updateInfo.latest, updateInfo.current)) { if (updateInfo && semver.gt(updateInfo.latest, updateInfo.current)) {
return { return {