1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/**
* @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.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({
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
});
const result = await checkForUpdates();
expect(result).toBeNull();
expect(getPackageJson).not.toHaveBeenCalled();
expect(updateNotifier).not.toHaveBeenCalled();
});
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({
fetchInfo: vi.fn().mockResolvedValue(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({
fetchInfo: vi
.fn()
.mockResolvedValue({ current: '1.0.0', latest: '1.1.0' }),
});
const result = await checkForUpdates();
expect(result?.message).toContain('1.0.0 → 1.1.0');
expect(result?.update).toEqual({ current: '1.0.0', latest: '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({
fetchInfo: vi
.fn()
.mockResolvedValue({ 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({
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 rejects', async () => {
getPackageJson.mockResolvedValue({
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({
fetchInfo: vi.fn().mockRejectedValue(new Error('Timeout')),
});
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();
});
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');
});
});
});
|