summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSambhav Khanna <[email protected]>2025-07-29 20:11:15 -0400
committerGitHub <[email protected]>2025-07-30 00:11:15 +0000
commitd5a1b717c258b3913dc41800f5a976ed9d60792a (patch)
treead5d02a4883a176c5a7a738076797f09e25348e2
parent091804c7507be25d8063a236210de0c31671783b (diff)
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 <[email protected]>
-rw-r--r--packages/cli/src/ui/utils/updateCheck.test.ts47
-rw-r--r--packages/cli/src/ui/utils/updateCheck.ts9
2 files changed, 48 insertions, 8 deletions
diff --git a/packages/cli/src/ui/utils/updateCheck.test.ts b/packages/cli/src/ui/utils/updateCheck.test.ts
index 4985afe8..fa6f342e 100644
--- a/packages/cli/src/ui/utils/updateCheck.test.ts
+++ b/packages/cli/src/ui/utils/updateCheck.test.ts
@@ -5,7 +5,7 @@
*/
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());
vi.mock('../../utils/package.js', () => ({
@@ -19,11 +19,17 @@ vi.mock('update-notifier', () => ({
describe('checkForUpdates', () => {
beforeEach(() => {
+ vi.useFakeTimers();
vi.resetAllMocks();
// Clear DEV environment variable before each test
delete process.env.DEV;
});
+ afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ });
+
it('should return null when running from source (DEV=true)', async () => {
process.env.DEV = 'true';
getPackageJson.mockResolvedValue({
@@ -31,7 +37,9 @@ describe('checkForUpdates', () => {
version: '1.0.0',
});
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();
expect(result).toBeNull();
@@ -51,7 +59,7 @@ describe('checkForUpdates', () => {
version: '1.0.0',
});
updateNotifier.mockReturnValue({
- fetchInfo: vi.fn(async () => null),
+ fetchInfo: vi.fn().mockResolvedValue(null),
});
const result = await checkForUpdates();
expect(result).toBeNull();
@@ -63,7 +71,9 @@ describe('checkForUpdates', () => {
version: '1.0.0',
});
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();
@@ -77,7 +87,9 @@ describe('checkForUpdates', () => {
version: '1.0.0',
});
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();
expect(result).toBeNull();
@@ -89,12 +101,35 @@ describe('checkForUpdates', () => {
version: '1.1.0',
});
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();
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 () => {
getPackageJson.mockRejectedValue(new Error('test error'));
const result = await checkForUpdates();
diff --git a/packages/cli/src/ui/utils/updateCheck.ts b/packages/cli/src/ui/utils/updateCheck.ts
index b0a0de1b..2fe5df39 100644
--- a/packages/cli/src/ui/utils/updateCheck.ts
+++ b/packages/cli/src/ui/utils/updateCheck.ts
@@ -8,6 +8,8 @@ import updateNotifier, { UpdateInfo } from 'update-notifier';
import semver from 'semver';
import { getPackageJson } from '../../utils/package.js';
+export const FETCH_TIMEOUT_MS = 2000;
+
export interface UpdateObject {
message: string;
update: UpdateInfo;
@@ -34,8 +36,11 @@ export async function checkForUpdates(): Promise<UpdateObject | null> {
// allow notifier to run in scripts
shouldNotifyInNpmScript: true,
});
-
- const updateInfo = await notifier.fetchInfo();
+ // avoid blocking by waiting at most FETCH_TIMEOUT_MS for fetchInfo to resolve
+ 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)) {
return {