sync gemini-cli 0.1.17

Co-Authored-By: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
Yiheng Xu
2025-08-05 16:44:06 +08:00
235 changed files with 16997 additions and 3736 deletions

View File

@@ -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();
@@ -50,7 +58,9 @@ describe('checkForUpdates', () => {
name: 'test-package',
version: '1.0.0',
});
updateNotifier.mockReturnValue({ update: null });
updateNotifier.mockReturnValue({
fetchInfo: vi.fn().mockResolvedValue(null),
});
const result = await checkForUpdates();
expect(result).toBeNull();
});
@@ -61,10 +71,14 @@ 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).toContain('1.0.0 → 1.1.0');
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 () => {
@@ -73,7 +87,9 @@ describe('checkForUpdates', () => {
version: '1.0.0',
});
updateNotifier.mockReturnValue({
update: { 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();
@@ -85,15 +101,63 @@ describe('checkForUpdates', () => {
version: '1.1.0',
});
updateNotifier.mockReturnValue({
update: { current: '1.1.0', latest: '1.0.0' },
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');
});
});
});

View File

@@ -4,37 +4,92 @@
* SPDX-License-Identifier: Apache-2.0
*/
import updateNotifier from 'update-notifier';
import updateNotifier, { UpdateInfo } from 'update-notifier';
import semver from 'semver';
import { getPackageJson } from '../../utils/package.js';
export async function checkForUpdates(): Promise<string | null> {
export const FETCH_TIMEOUT_MS = 2000;
export interface UpdateObject {
message: string;
update: UpdateInfo;
}
/**
* From a nightly and stable update, determines which is the "best" one to offer.
* The rule is to always prefer nightly if the base versions are the same.
*/
function getBestAvailableUpdate(
nightly?: UpdateInfo,
stable?: UpdateInfo,
): UpdateInfo | null {
if (!nightly) return stable || null;
if (!stable) return nightly || null;
const nightlyVer = nightly.latest;
const stableVer = stable.latest;
if (
semver.coerce(stableVer)?.version === semver.coerce(nightlyVer)?.version
) {
return nightly;
}
return semver.gt(stableVer, nightlyVer) ? stable : nightly;
}
export async function checkForUpdates(): Promise<UpdateObject | null> {
try {
// Skip update check when running from source (development mode)
if (process.env.DEV === 'true') {
return null;
}
const packageJson = await getPackageJson();
if (!packageJson || !packageJson.name || !packageJson.version) {
return null;
}
const notifier = updateNotifier({
pkg: {
name: packageJson.name,
version: packageJson.version,
},
// check every time
updateCheckInterval: 0,
// allow notifier to run in scripts
shouldNotifyInNpmScript: true,
});
if (
notifier.update &&
semver.gt(notifier.update.latest, notifier.update.current)
) {
return `Qwen Code update available! ${notifier.update.current}${notifier.update.latest}\nRun npm install -g ${packageJson.name} to update`;
const { name, version: currentVersion } = packageJson;
const isNightly = currentVersion.includes('nightly');
const createNotifier = (distTag: 'latest' | 'nightly') =>
updateNotifier({
pkg: {
name,
version: currentVersion,
},
updateCheckInterval: 0,
shouldNotifyInNpmScript: true,
distTag,
});
if (isNightly) {
const [nightlyUpdateInfo, latestUpdateInfo] = await Promise.all([
createNotifier('nightly').fetchInfo(),
createNotifier('latest').fetchInfo(),
]);
const bestUpdate = getBestAvailableUpdate(
nightlyUpdateInfo,
latestUpdateInfo,
);
if (bestUpdate && semver.gt(bestUpdate.latest, currentVersion)) {
const message = `A new version of Qwen Code is available! ${currentVersion}${bestUpdate.latest}`;
return {
message,
update: { ...bestUpdate, current: currentVersion },
};
}
} else {
const updateInfo = await createNotifier('latest').fetchInfo();
if (updateInfo && semver.gt(updateInfo.latest, currentVersion)) {
const message = `Qwen Code update available! ${currentVersion}${updateInfo.latest}`;
return {
message,
update: { ...updateInfo, current: currentVersion },
};
}
}
return null;