2025-06-30 20:03:16 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
2025-08-01 20:17:32 -07:00
|
|
|
import { checkForUpdates } from './updateCheck.js';
|
2025-06-30 20:03:16 -07:00
|
|
|
|
|
|
|
|
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(() => {
|
2025-07-29 20:11:15 -04:00
|
|
|
vi.useFakeTimers();
|
2025-06-30 20:03:16 -07:00
|
|
|
vi.resetAllMocks();
|
2025-07-18 09:44:45 +09:00
|
|
|
// Clear DEV environment variable before each test
|
2025-08-17 12:43:21 -04:00
|
|
|
delete process.env['DEV'];
|
2025-07-18 09:44:45 +09:00
|
|
|
});
|
|
|
|
|
|
2025-07-29 20:11:15 -04:00
|
|
|
afterEach(() => {
|
|
|
|
|
vi.useRealTimers();
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-18 09:44:45 +09:00
|
|
|
it('should return null when running from source (DEV=true)', async () => {
|
2025-08-17 12:43:21 -04:00
|
|
|
process.env['DEV'] = 'true';
|
2025-07-18 09:44:45 +09:00
|
|
|
getPackageJson.mockResolvedValue({
|
|
|
|
|
name: 'test-package',
|
|
|
|
|
version: '1.0.0',
|
|
|
|
|
});
|
|
|
|
|
updateNotifier.mockReturnValue({
|
2025-07-29 20:11:15 -04:00
|
|
|
fetchInfo: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
|
2025-07-18 09:44:45 +09:00
|
|
|
});
|
|
|
|
|
const result = await checkForUpdates();
|
|
|
|
|
expect(result).toBeNull();
|
|
|
|
|
expect(getPackageJson).not.toHaveBeenCalled();
|
|
|
|
|
expect(updateNotifier).not.toHaveBeenCalled();
|
2025-06-30 20:03:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
});
|
2025-07-28 17:56:52 -07:00
|
|
|
updateNotifier.mockReturnValue({
|
2025-07-29 20:11:15 -04:00
|
|
|
fetchInfo: vi.fn().mockResolvedValue(null),
|
2025-07-28 17:56:52 -07:00
|
|
|
});
|
2025-06-30 20:03:16 -07:00
|
|
|
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({
|
2025-07-29 20:11:15 -04:00
|
|
|
fetchInfo: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
|
2025-06-30 20:03:16 -07:00
|
|
|
});
|
2025-07-28 17:56:52 -07:00
|
|
|
|
2025-06-30 20:03:16 -07:00
|
|
|
const result = await checkForUpdates();
|
2025-07-28 17:56:52 -07:00
|
|
|
expect(result?.message).toContain('1.0.0 → 1.1.0');
|
|
|
|
|
expect(result?.update).toEqual({ current: '1.0.0', latest: '1.1.0' });
|
2025-06-30 20:03:16 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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({
|
2025-07-29 20:11:15 -04:00
|
|
|
fetchInfo: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValue({ current: '1.0.0', latest: '1.0.0' }),
|
2025-06-30 20:03:16 -07:00
|
|
|
});
|
|
|
|
|
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({
|
2025-07-29 20:11:15 -04:00
|
|
|
fetchInfo: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValue({ current: '1.1.0', latest: '1.0.0' }),
|
2025-06-30 20:03:16 -07:00
|
|
|
});
|
|
|
|
|
const result = await checkForUpdates();
|
|
|
|
|
expect(result).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-01 20:17:32 -07:00
|
|
|
it('should return null if fetchInfo rejects', async () => {
|
2025-07-29 20:11:15 -04:00
|
|
|
getPackageJson.mockResolvedValue({
|
|
|
|
|
name: 'test-package',
|
|
|
|
|
version: '1.0.0',
|
|
|
|
|
});
|
|
|
|
|
updateNotifier.mockReturnValue({
|
2025-08-01 20:17:32 -07:00
|
|
|
fetchInfo: vi.fn().mockRejectedValue(new Error('Timeout')),
|
2025-07-29 20:11:15 -04:00
|
|
|
});
|
2025-08-01 20:17:32 -07:00
|
|
|
|
|
|
|
|
const result = await checkForUpdates();
|
2025-07-29 20:11:15 -04:00
|
|
|
expect(result).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-30 20:03:16 -07:00
|
|
|
it('should handle errors gracefully', async () => {
|
|
|
|
|
getPackageJson.mockRejectedValue(new Error('test error'));
|
|
|
|
|
const result = await checkForUpdates();
|
|
|
|
|
expect(result).toBeNull();
|
|
|
|
|
});
|
2025-08-01 20:17:32 -07:00
|
|
|
|
|
|
|
|
describe('nightly updates', () => {
|
|
|
|
|
it('should notify for a newer nightly version when current is nightly', async () => {
|
|
|
|
|
getPackageJson.mockResolvedValue({
|
|
|
|
|
name: 'test-package',
|
|
|
|
|
version: '1.2.3-nightly.1',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fetchInfoMock = vi.fn().mockImplementation(({ distTag }) => {
|
|
|
|
|
if (distTag === 'nightly') {
|
|
|
|
|
return Promise.resolve({
|
|
|
|
|
latest: '1.2.3-nightly.2',
|
|
|
|
|
current: '1.2.3-nightly.1',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (distTag === 'latest') {
|
|
|
|
|
return Promise.resolve({
|
|
|
|
|
latest: '1.2.3',
|
|
|
|
|
current: '1.2.3-nightly.1',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
updateNotifier.mockImplementation(({ pkg, distTag }) => ({
|
|
|
|
|
fetchInfo: () => fetchInfoMock({ pkg, distTag }),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const result = await checkForUpdates();
|
|
|
|
|
expect(result?.message).toContain('1.2.3-nightly.1 → 1.2.3-nightly.2');
|
|
|
|
|
expect(result?.update.latest).toBe('1.2.3-nightly.2');
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-06-30 20:03:16 -07:00
|
|
|
});
|