From e5b1208bd8842d6517b5033a1fa20dd900bc2134 Mon Sep 17 00:00:00 2001 From: Eddie Santos <9561596+eddie-santos@users.noreply.github.com> Date: Mon, 30 Jun 2025 20:03:16 -0700 Subject: [PATCH] update check + tests (#2772) --- package-lock.json | 8 ++ packages/cli/package.json | 7 +- packages/cli/src/ui/utils/updateCheck.test.ts | 82 +++++++++++++++++++ packages/cli/src/ui/utils/updateCheck.ts | 6 +- 4 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 packages/cli/src/ui/utils/updateCheck.test.ts diff --git a/package-lock.json b/package-lock.json index 483b42cb..09eb6d96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2369,6 +2369,13 @@ "@types/react": "^19.0.0" } }, + "node_modules/@types/semver": { + "version": "7.7.0", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.7.0.tgz", + "integrity": "sha512-k107IF4+Xr7UHjwDc7Cfd6PRQfbdkiRabXGRjo07b4WyPahFBZCZ1sE+BNxYIJPPg73UkfOsVOLwqVc/6ETrIA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/shell-quote": { "version": "1.7.5", "resolved": "https://registry.npmjs.org/@types/shell-quote/-/shell-quote-1.7.5.tgz", @@ -11234,6 +11241,7 @@ "@types/node": "^20.11.24", "@types/react": "^19.1.8", "@types/react-dom": "^19.1.6", + "@types/semver": "^7.7.0", "@types/shell-quote": "^1.7.5", "@types/yargs": "^17.0.32", "ink-testing-library": "^4.0.0", diff --git a/packages/cli/package.json b/packages/cli/package.json index 0f4aa06d..ade14e16 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -41,9 +41,9 @@ "ink": "^6.0.1", "ink-big-text": "^2.0.0", "ink-gradient": "^3.0.0", + "ink-link": "^4.1.0", "ink-select-input": "^6.2.0", "ink-spinner": "^5.0.0", - "ink-link": "^4.1.0", "ink-text-input": "^6.0.0", "lowlight": "^3.3.0", "mime-types": "^2.1.4", @@ -58,20 +58,21 @@ "yargs": "^17.7.2" }, "devDependencies": { - "@testing-library/react": "^16.3.0", "@babel/runtime": "^7.27.6", + "@testing-library/react": "^16.3.0", "@types/command-exists": "^1.2.3", "@types/diff": "^7.0.2", "@types/dotenv": "^6.1.1", "@types/node": "^20.11.24", "@types/react": "^19.1.8", "@types/react-dom": "^19.1.6", + "@types/semver": "^7.7.0", "@types/shell-quote": "^1.7.5", "@types/yargs": "^17.0.32", "ink-testing-library": "^4.0.0", "jsdom": "^26.1.0", - "react-dom": "^19.1.0", "pretty-format": "^30.0.2", + "react-dom": "^19.1.0", "typescript": "^5.3.3", "vitest": "^3.1.1" }, 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 { @@ -24,7 +25,10 @@ export async function checkForUpdates(): Promise { 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`; }