2025-08-25 17:40:15 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-13 21:28:01 -08:00
|
|
|
import {
|
|
|
|
|
vi,
|
|
|
|
|
describe,
|
|
|
|
|
it,
|
|
|
|
|
expect,
|
|
|
|
|
beforeEach,
|
|
|
|
|
afterEach,
|
|
|
|
|
type Mock,
|
|
|
|
|
} from 'vitest';
|
2025-11-20 10:44:02 -08:00
|
|
|
import { format } from 'node:util';
|
2025-12-12 17:43:43 -08:00
|
|
|
import { type Argv } from 'yargs';
|
2025-11-13 21:28:01 -08:00
|
|
|
import { handleUninstall, uninstallCommand } from './uninstall.js';
|
|
|
|
|
import { ExtensionManager } from '../../config/extension-manager.js';
|
|
|
|
|
import { loadSettings, type LoadedSettings } from '../../config/settings.js';
|
|
|
|
|
import { getErrorMessage } from '../../utils/errors.js';
|
|
|
|
|
|
2025-11-18 20:50:25 +05:30
|
|
|
// NOTE: This file uses vi.hoisted() mocks to enable testing of sequential
|
|
|
|
|
// mock behaviors (mockResolvedValueOnce/mockRejectedValueOnce chaining).
|
|
|
|
|
// The hoisted mocks persist across vi.clearAllMocks() calls, which is necessary
|
|
|
|
|
// for testing partial failure scenarios in the multiple extension uninstall feature.
|
|
|
|
|
|
|
|
|
|
// Hoisted mocks - these survive vi.clearAllMocks()
|
|
|
|
|
const mockUninstallExtension = vi.hoisted(() => vi.fn());
|
|
|
|
|
const mockLoadExtensions = vi.hoisted(() => vi.fn());
|
|
|
|
|
|
|
|
|
|
// Mock dependencies with hoisted functions
|
|
|
|
|
vi.mock('../../config/extension-manager.js', async (importOriginal) => {
|
|
|
|
|
const actual =
|
|
|
|
|
await importOriginal<typeof import('../../config/extension-manager.js')>();
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
ExtensionManager: vi.fn().mockImplementation(() => ({
|
|
|
|
|
uninstallExtension: mockUninstallExtension,
|
|
|
|
|
loadExtensions: mockLoadExtensions,
|
|
|
|
|
setRequestConsent: vi.fn(),
|
|
|
|
|
setRequestSetting: vi.fn(),
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-20 10:44:02 -08:00
|
|
|
// Mock dependencies
|
|
|
|
|
const emitConsoleLog = vi.hoisted(() => vi.fn());
|
|
|
|
|
const debugLogger = vi.hoisted(() => ({
|
|
|
|
|
log: vi.fn((message, ...args) => {
|
|
|
|
|
emitConsoleLog('log', format(message, ...args));
|
|
|
|
|
}),
|
|
|
|
|
error: vi.fn((message, ...args) => {
|
|
|
|
|
emitConsoleLog('error', format(message, ...args));
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
2025-11-13 21:28:01 -08:00
|
|
|
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
|
|
|
|
|
const actual =
|
|
|
|
|
await importOriginal<typeof import('@google/gemini-cli-core')>();
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
2025-11-20 10:44:02 -08:00
|
|
|
coreEvents: {
|
|
|
|
|
emitConsoleLog,
|
2025-11-13 21:28:01 -08:00
|
|
|
},
|
2025-11-20 10:44:02 -08:00
|
|
|
debugLogger,
|
2025-11-13 21:28:01 -08:00
|
|
|
};
|
|
|
|
|
});
|
2025-11-20 10:44:02 -08:00
|
|
|
|
|
|
|
|
vi.mock('../../config/settings.js');
|
|
|
|
|
vi.mock('../../utils/errors.js');
|
2025-11-13 21:28:01 -08:00
|
|
|
vi.mock('../../config/extensions/consent.js', () => ({
|
|
|
|
|
requestConsentNonInteractive: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
vi.mock('../../config/extensions/extensionSettings.js', () => ({
|
|
|
|
|
promptForSetting: vi.fn(),
|
|
|
|
|
}));
|
2025-11-21 21:08:06 -05:00
|
|
|
vi.mock('../utils.js', () => ({
|
|
|
|
|
exitCli: vi.fn(),
|
|
|
|
|
}));
|
2025-08-25 17:40:15 +00:00
|
|
|
|
|
|
|
|
describe('extensions uninstall command', () => {
|
2025-11-13 21:28:01 -08:00
|
|
|
const mockLoadSettings = vi.mocked(loadSettings);
|
|
|
|
|
const mockGetErrorMessage = vi.mocked(getErrorMessage);
|
|
|
|
|
const mockExtensionManager = vi.mocked(ExtensionManager);
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
mockLoadSettings.mockReturnValue({
|
|
|
|
|
merged: {},
|
|
|
|
|
} as unknown as LoadedSettings);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
2025-11-18 20:50:25 +05:30
|
|
|
mockLoadExtensions.mockClear();
|
|
|
|
|
mockUninstallExtension.mockClear();
|
|
|
|
|
vi.clearAllMocks();
|
2025-11-13 21:28:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleUninstall', () => {
|
2025-11-18 20:50:25 +05:30
|
|
|
it('should uninstall a single extension', async () => {
|
|
|
|
|
mockLoadExtensions.mockResolvedValue(undefined);
|
|
|
|
|
mockUninstallExtension.mockResolvedValue(undefined);
|
2025-11-13 21:28:01 -08:00
|
|
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
2025-11-18 20:50:25 +05:30
|
|
|
await handleUninstall({ names: ['my-extension'] });
|
2025-11-13 21:28:01 -08:00
|
|
|
|
|
|
|
|
expect(mockExtensionManager).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
workspaceDir: '/test/dir',
|
|
|
|
|
}),
|
|
|
|
|
);
|
2025-11-18 20:50:25 +05:30
|
|
|
expect(mockLoadExtensions).toHaveBeenCalled();
|
|
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledWith(
|
|
|
|
|
'my-extension',
|
|
|
|
|
false,
|
|
|
|
|
);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'log',
|
2025-11-13 21:28:01 -08:00
|
|
|
'Extension "my-extension" successfully uninstalled.',
|
|
|
|
|
);
|
|
|
|
|
mockCwd.mockRestore();
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-18 20:50:25 +05:30
|
|
|
it('should uninstall multiple extensions', async () => {
|
|
|
|
|
mockLoadExtensions.mockResolvedValue(undefined);
|
|
|
|
|
mockUninstallExtension.mockResolvedValue(undefined);
|
|
|
|
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
|
|
|
|
await handleUninstall({ names: ['ext1', 'ext2', 'ext3'] });
|
|
|
|
|
|
|
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledTimes(3);
|
|
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledWith('ext1', false);
|
|
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledWith('ext2', false);
|
|
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledWith('ext3', false);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'log',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Extension "ext1" successfully uninstalled.',
|
|
|
|
|
);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'log',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Extension "ext2" successfully uninstalled.',
|
|
|
|
|
);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'log',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Extension "ext3" successfully uninstalled.',
|
|
|
|
|
);
|
|
|
|
|
mockCwd.mockRestore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should report errors for failed uninstalls but continue with others', async () => {
|
|
|
|
|
mockLoadExtensions.mockResolvedValue(undefined);
|
|
|
|
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
|
|
|
|
const mockProcessExit = vi
|
|
|
|
|
.spyOn(process, 'exit')
|
|
|
|
|
.mockImplementation((() => {}) as (
|
|
|
|
|
code?: string | number | null | undefined,
|
|
|
|
|
) => never);
|
|
|
|
|
|
|
|
|
|
const error = new Error('Extension not found');
|
|
|
|
|
// Chain sequential mock behaviors - this works with hoisted mocks
|
|
|
|
|
mockUninstallExtension
|
|
|
|
|
.mockResolvedValueOnce(undefined)
|
|
|
|
|
.mockRejectedValueOnce(error)
|
|
|
|
|
.mockResolvedValueOnce(undefined);
|
|
|
|
|
mockGetErrorMessage.mockReturnValue('Extension not found');
|
|
|
|
|
|
|
|
|
|
await handleUninstall({ names: ['ext1', 'ext2', 'ext3'] });
|
|
|
|
|
|
|
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledTimes(3);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'log',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Extension "ext1" successfully uninstalled.',
|
|
|
|
|
);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'error',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Failed to uninstall "ext2": Extension not found',
|
|
|
|
|
);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'log',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Extension "ext3" successfully uninstalled.',
|
|
|
|
|
);
|
|
|
|
|
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
|
|
|
|
mockProcessExit.mockRestore();
|
|
|
|
|
mockCwd.mockRestore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should exit with error code if all uninstalls fail', async () => {
|
|
|
|
|
mockLoadExtensions.mockResolvedValue(undefined);
|
|
|
|
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
2025-11-13 21:28:01 -08:00
|
|
|
const mockProcessExit = vi
|
|
|
|
|
.spyOn(process, 'exit')
|
|
|
|
|
.mockImplementation((() => {}) as (
|
|
|
|
|
code?: string | number | null | undefined,
|
|
|
|
|
) => never);
|
2025-11-18 20:50:25 +05:30
|
|
|
const error = new Error('Extension not found');
|
|
|
|
|
mockUninstallExtension.mockRejectedValue(error);
|
|
|
|
|
mockGetErrorMessage.mockReturnValue('Extension not found');
|
2025-11-13 21:28:01 -08:00
|
|
|
|
2025-11-18 20:50:25 +05:30
|
|
|
await handleUninstall({ names: ['ext1', 'ext2'] });
|
2025-11-13 21:28:01 -08:00
|
|
|
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'error',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Failed to uninstall "ext1": Extension not found',
|
|
|
|
|
);
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'error',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Failed to uninstall "ext2": Extension not found',
|
2025-11-13 21:28:01 -08:00
|
|
|
);
|
|
|
|
|
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
|
|
|
|
mockProcessExit.mockRestore();
|
2025-11-18 20:50:25 +05:30
|
|
|
mockCwd.mockRestore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should log an error message and exit with code 1 when initialization fails', async () => {
|
|
|
|
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
|
|
|
|
const mockProcessExit = vi
|
|
|
|
|
.spyOn(process, 'exit')
|
|
|
|
|
.mockImplementation((() => {}) as (
|
|
|
|
|
code?: string | number | null | undefined,
|
|
|
|
|
) => never);
|
|
|
|
|
const error = new Error('Initialization failed');
|
|
|
|
|
mockLoadExtensions.mockRejectedValue(error);
|
|
|
|
|
mockGetErrorMessage.mockReturnValue('Initialization failed message');
|
|
|
|
|
|
|
|
|
|
await handleUninstall({ names: ['my-extension'] });
|
|
|
|
|
|
2025-11-20 10:44:02 -08:00
|
|
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
|
|
|
|
'error',
|
2025-11-18 20:50:25 +05:30
|
|
|
'Initialization failed message',
|
|
|
|
|
);
|
|
|
|
|
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
|
|
|
|
mockProcessExit.mockRestore();
|
|
|
|
|
mockCwd.mockRestore();
|
2025-11-13 21:28:01 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('uninstallCommand', () => {
|
2025-12-12 17:43:43 -08:00
|
|
|
const command = uninstallCommand;
|
2025-11-13 21:28:01 -08:00
|
|
|
|
|
|
|
|
it('should have correct command and describe', () => {
|
2025-11-18 20:50:25 +05:30
|
|
|
expect(command.command).toBe('uninstall <names..>');
|
|
|
|
|
expect(command.describe).toBe('Uninstalls one or more extensions.');
|
2025-11-13 21:28:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('builder', () => {
|
|
|
|
|
interface MockYargs {
|
|
|
|
|
positional: Mock;
|
|
|
|
|
check: Mock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let yargsMock: MockYargs;
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
yargsMock = {
|
|
|
|
|
positional: vi.fn().mockReturnThis(),
|
|
|
|
|
check: vi.fn().mockReturnThis(),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should configure positional argument', () => {
|
|
|
|
|
(command.builder as (yargs: Argv) => Argv)(
|
|
|
|
|
yargsMock as unknown as Argv,
|
|
|
|
|
);
|
2025-11-18 20:50:25 +05:30
|
|
|
expect(yargsMock.positional).toHaveBeenCalledWith('names', {
|
|
|
|
|
describe:
|
|
|
|
|
'The name(s) or source path(s) of the extension(s) to uninstall.',
|
2025-11-13 21:28:01 -08:00
|
|
|
type: 'string',
|
2025-11-18 20:50:25 +05:30
|
|
|
array: true,
|
2025-11-13 21:28:01 -08:00
|
|
|
});
|
|
|
|
|
expect(yargsMock.check).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-18 20:50:25 +05:30
|
|
|
it('check function should throw for missing names', () => {
|
2025-11-13 21:28:01 -08:00
|
|
|
(command.builder as (yargs: Argv) => Argv)(
|
|
|
|
|
yargsMock as unknown as Argv,
|
|
|
|
|
);
|
|
|
|
|
const checkCallback = yargsMock.check.mock.calls[0][0];
|
2025-11-18 20:50:25 +05:30
|
|
|
expect(() => checkCallback({ names: [] })).toThrow(
|
|
|
|
|
'Please include at least one extension name to uninstall as a positional argument.',
|
2025-11-13 21:28:01 -08:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('handler should call handleUninstall', async () => {
|
2025-11-18 20:50:25 +05:30
|
|
|
mockLoadExtensions.mockResolvedValue(undefined);
|
|
|
|
|
mockUninstallExtension.mockResolvedValue(undefined);
|
2025-11-13 21:28:01 -08:00
|
|
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
|
|
|
|
interface TestArgv {
|
2025-11-18 20:50:25 +05:30
|
|
|
names: string[];
|
2025-11-13 21:28:01 -08:00
|
|
|
[key: string]: unknown;
|
|
|
|
|
}
|
2025-11-18 20:50:25 +05:30
|
|
|
const argv: TestArgv = { names: ['my-extension'], _: [], $0: '' };
|
2025-11-13 21:28:01 -08:00
|
|
|
await (command.handler as unknown as (args: TestArgv) => void)(argv);
|
|
|
|
|
|
2025-11-18 20:50:25 +05:30
|
|
|
expect(mockUninstallExtension).toHaveBeenCalledWith(
|
|
|
|
|
'my-extension',
|
|
|
|
|
false,
|
|
|
|
|
);
|
2025-11-13 21:28:01 -08:00
|
|
|
mockCwd.mockRestore();
|
|
|
|
|
});
|
2025-08-25 17:40:15 +00:00
|
|
|
});
|
|
|
|
|
});
|