Fix missing slash commands when Gemini CLI is in a project with a package.json that doesn't follow semantic versioning (#17561)

This commit is contained in:
Adib234
2026-01-27 13:18:16 -05:00
committed by GitHub
parent 50e4f9380b
commit 894bd66dc6
2 changed files with 75 additions and 4 deletions
+65
View File
@@ -0,0 +1,65 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { getPackageJson } from './package.js';
import { readPackageUp } from 'read-package-up';
vi.mock('read-package-up', () => ({
readPackageUp: vi.fn(),
}));
describe('getPackageJson', () => {
beforeEach(() => {
vi.resetAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should return packageJson when found', async () => {
const expectedPackageJsonResult = { name: 'test-pkg', version: '1.2.3' };
vi.mocked(readPackageUp).mockResolvedValue({
packageJson: expectedPackageJsonResult,
path: '/path/to/package.json',
});
const result = await getPackageJson('/some/path');
expect(result).toEqual(expectedPackageJsonResult);
expect(readPackageUp).toHaveBeenCalledWith({
cwd: '/some/path',
normalize: false,
});
});
it.each([
{
description: 'no package.json is found',
setup: () => vi.mocked(readPackageUp).mockResolvedValue(undefined),
expected: undefined,
},
{
description: 'non-semver versions (when normalize is false)',
setup: () =>
vi.mocked(readPackageUp).mockResolvedValue({
packageJson: { name: 'test-pkg', version: '2024.60' },
path: '/path/to/package.json',
}),
expected: { name: 'test-pkg', version: '2024.60' },
},
{
description: 'readPackageUp throws',
setup: () =>
vi.mocked(readPackageUp).mockRejectedValue(new Error('Read error')),
expected: undefined,
},
])('should handle $description', async ({ setup, expected }) => {
setup();
const result = await getPackageJson('/some/path');
expect(result).toEqual(expected);
});
});