diff options
Diffstat (limited to 'packages/cli/src')
| -rw-r--r-- | packages/cli/src/ui/utils/updateCheck.test.ts | 82 | ||||
| -rw-r--r-- | packages/cli/src/ui/utils/updateCheck.ts | 6 |
2 files changed, 87 insertions, 1 deletions
diff --git a/packages/cli/src/ui/utils/updateCheck.test.ts b/packages/cli/src/ui/utils/updateCheck.test.ts new file mode 100644 index 00000000..6d3c8b77 --- /dev/null +++ b/packages/cli/src/ui/utils/updateCheck.test.ts @@ -0,0 +1,82 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { vi, describe, it, expect, beforeEach } from 'vitest'; +import { checkForUpdates } from './updateCheck.js'; + +const getPackageJson = vi.hoisted(() => vi.fn()); +vi.mock('../../utils/package.js', () => ({ + getPackageJson, +})); + +const updateNotifier = vi.hoisted(() => vi.fn()); +vi.mock('update-notifier', () => ({ + default: updateNotifier, +})); + +describe('checkForUpdates', () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + it('should return null if package.json is missing', async () => { + getPackageJson.mockResolvedValue(null); + const result = await checkForUpdates(); + expect(result).toBeNull(); + }); + + it('should return null if there is no update', async () => { + getPackageJson.mockResolvedValue({ + name: 'test-package', + version: '1.0.0', + }); + updateNotifier.mockReturnValue({ update: null }); + const result = await checkForUpdates(); + expect(result).toBeNull(); + }); + + it('should return a message if a newer version is available', async () => { + getPackageJson.mockResolvedValue({ + name: 'test-package', + version: '1.0.0', + }); + updateNotifier.mockReturnValue({ + update: { current: '1.0.0', latest: '1.1.0' }, + }); + const result = await checkForUpdates(); + expect(result).toContain('1.0.0 → 1.1.0'); + }); + + it('should return null if the latest version is the same as the current version', async () => { + getPackageJson.mockResolvedValue({ + name: 'test-package', + version: '1.0.0', + }); + updateNotifier.mockReturnValue({ + update: { current: '1.0.0', latest: '1.0.0' }, + }); + const result = await checkForUpdates(); + expect(result).toBeNull(); + }); + + it('should return null if the latest version is older than the current version', async () => { + getPackageJson.mockResolvedValue({ + name: 'test-package', + version: '1.1.0', + }); + updateNotifier.mockReturnValue({ + update: { current: '1.1.0', latest: '1.0.0' }, + }); + const result = await checkForUpdates(); + expect(result).toBeNull(); + }); + + it('should handle errors gracefully', async () => { + getPackageJson.mockRejectedValue(new Error('test error')); + const result = await checkForUpdates(); + expect(result).toBeNull(); + }); +}); diff --git a/packages/cli/src/ui/utils/updateCheck.ts b/packages/cli/src/ui/utils/updateCheck.ts index e6e6bd62..6be5effc 100644 --- a/packages/cli/src/ui/utils/updateCheck.ts +++ b/packages/cli/src/ui/utils/updateCheck.ts @@ -5,6 +5,7 @@ */ import updateNotifier from 'update-notifier'; +import semver from 'semver'; import { getPackageJson } from '../../utils/package.js'; export async function checkForUpdates(): Promise<string | null> { @@ -24,7 +25,10 @@ export async function checkForUpdates(): Promise<string | null> { shouldNotifyInNpmScript: true, }); - if (notifier.update) { + if ( + notifier.update && + semver.gt(notifier.update.latest, notifier.update.current) + ) { return `Gemini CLI update available! ${notifier.update.current} → ${notifier.update.latest}\nRun npm install -g ${packageJson.name} to update`; } |
