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:
parent
091804c750
commit
d5a1b717c2
|
@ -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();
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue