File size: 8,537 Bytes
d810ed8 |
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';
import { getInstallationInfo, PackageManager } from './installationInfo.js';
import { updateEventEmitter } from './updateEventEmitter.js';
import { UpdateObject } from '../ui/utils/updateCheck.js';
import { LoadedSettings } from '../config/settings.js';
import EventEmitter from 'node:events';
import { handleAutoUpdate } from './handleAutoUpdate.js';
vi.mock('./installationInfo.js', async () => {
const actual = await vi.importActual('./installationInfo.js');
return {
...actual,
getInstallationInfo: vi.fn(),
};
});
vi.mock('./updateEventEmitter.js', async () => {
const actual = await vi.importActual('./updateEventEmitter.js');
return {
...actual,
updateEventEmitter: {
...actual.updateEventEmitter,
emit: vi.fn(),
},
};
});
interface MockChildProcess extends EventEmitter {
stdin: EventEmitter & {
write: Mock;
end: Mock;
};
stderr: EventEmitter;
}
const mockGetInstallationInfo = vi.mocked(getInstallationInfo);
const mockUpdateEventEmitter = vi.mocked(updateEventEmitter);
describe('handleAutoUpdate', () => {
let mockSpawn: Mock;
let mockUpdateInfo: UpdateObject;
let mockSettings: LoadedSettings;
let mockChildProcess: MockChildProcess;
beforeEach(() => {
mockSpawn = vi.fn();
vi.clearAllMocks();
mockUpdateInfo = {
update: {
latest: '2.0.0',
current: '1.0.0',
type: 'major',
name: '@qwen-code/qwen-code',
},
message: 'An update is available!',
};
mockSettings = {
merged: {
disableAutoUpdate: false,
},
} as LoadedSettings;
mockChildProcess = Object.assign(new EventEmitter(), {
stdin: Object.assign(new EventEmitter(), {
write: vi.fn(),
end: vi.fn(),
}),
stderr: new EventEmitter(),
}) as MockChildProcess;
mockSpawn.mockReturnValue(
mockChildProcess as unknown as ReturnType<typeof mockSpawn>,
);
});
afterEach(() => {
vi.clearAllMocks();
});
it('should do nothing if update info is null', () => {
handleAutoUpdate(null, mockSettings, '/root', mockSpawn);
expect(mockGetInstallationInfo).not.toHaveBeenCalled();
expect(mockUpdateEventEmitter.emit).not.toHaveBeenCalled();
expect(mockSpawn).not.toHaveBeenCalled();
});
it('should do nothing if update nag is disabled', () => {
mockSettings.merged.disableUpdateNag = true;
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
expect(mockGetInstallationInfo).not.toHaveBeenCalled();
expect(mockUpdateEventEmitter.emit).not.toHaveBeenCalled();
expect(mockSpawn).not.toHaveBeenCalled();
});
it('should emit "update-received" but not update if auto-updates are disabled', () => {
mockSettings.merged.disableAutoUpdate = true;
mockGetInstallationInfo.mockReturnValue({
updateCommand: 'npm i -g @qwen-code/qwen-code@latest',
updateMessage: 'Please update manually.',
isGlobal: true,
packageManager: PackageManager.NPM,
});
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledTimes(1);
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledWith(
'update-received',
{
message: 'An update is available!\nPlease update manually.',
},
);
expect(mockSpawn).not.toHaveBeenCalled();
});
it('should emit "update-received" but not update if no update command is found', () => {
mockGetInstallationInfo.mockReturnValue({
updateCommand: undefined,
updateMessage: 'Cannot determine update command.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledTimes(1);
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledWith(
'update-received',
{
message: 'An update is available!\nCannot determine update command.',
},
);
expect(mockSpawn).not.toHaveBeenCalled();
});
it('should combine update messages correctly', () => {
mockGetInstallationInfo.mockReturnValue({
updateCommand: undefined, // No command to prevent spawn
updateMessage: 'This is an additional message.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledTimes(1);
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledWith(
'update-received',
{
message: 'An update is available!\nThis is an additional message.',
},
);
});
it('should attempt to perform an update when conditions are met', async () => {
mockGetInstallationInfo.mockReturnValue({
updateCommand: 'npm i -g @qwen-code/qwen-code@latest',
updateMessage: 'This is an additional message.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
// Simulate successful execution
setTimeout(() => {
mockChildProcess.emit('close', 0);
}, 0);
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
expect(mockSpawn).toHaveBeenCalledOnce();
});
it('should emit "update-failed" when the update process fails', async () => {
await new Promise<void>((resolve) => {
mockGetInstallationInfo.mockReturnValue({
updateCommand: 'npm i -g @qwen-code/qwen-code@latest',
updateMessage: 'This is an additional message.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
// Simulate failed execution
setTimeout(() => {
mockChildProcess.stderr.emit('data', 'An error occurred');
mockChildProcess.emit('close', 1);
resolve();
}, 0);
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
});
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledWith('update-failed', {
message:
'Automatic update failed. Please try updating manually. (command: npm i -g @qwen-code/qwen-code@2.0.0, stderr: An error occurred)',
});
});
it('should emit "update-failed" when the spawn function throws an error', async () => {
await new Promise<void>((resolve) => {
mockGetInstallationInfo.mockReturnValue({
updateCommand: 'npm i -g @qwen-code/qwen-code@latest',
updateMessage: 'This is an additional message.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
// Simulate an error event
setTimeout(() => {
mockChildProcess.emit('error', new Error('Spawn error'));
resolve();
}, 0);
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
});
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledWith('update-failed', {
message:
'Automatic update failed. Please try updating manually. (error: Spawn error)',
});
});
it('should use the "@nightly" tag for nightly updates', async () => {
mockUpdateInfo.update.latest = '2.0.0-nightly';
mockGetInstallationInfo.mockReturnValue({
updateCommand: 'npm i -g @qwen-code/qwen-code@latest',
updateMessage: 'This is an additional message.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
expect(mockSpawn).toHaveBeenCalledWith(
'npm i -g @qwen-code/qwen-code@nightly',
{
shell: true,
stdio: 'pipe',
},
);
});
it('should emit "update-success" when the update process succeeds', async () => {
await new Promise<void>((resolve) => {
mockGetInstallationInfo.mockReturnValue({
updateCommand: 'npm i -g @qwen-code/qwen-code@latest',
updateMessage: 'This is an additional message.',
isGlobal: false,
packageManager: PackageManager.NPM,
});
// Simulate successful execution
setTimeout(() => {
mockChildProcess.emit('close', 0);
resolve();
}, 0);
handleAutoUpdate(mockUpdateInfo, mockSettings, '/root', mockSpawn);
});
expect(mockUpdateEventEmitter.emit).toHaveBeenCalledWith('update-success', {
message:
'Update successful! The new version will be used on your next run.',
});
});
});
|