feat(cli): align hooks enable/disable with skills and improve completion (#16822)

This commit is contained in:
Sehoon Shon
2026-01-27 20:56:06 -05:00
committed by GitHub
parent 130b596c80
commit 2c09785748
6 changed files with 695 additions and 150 deletions
@@ -11,6 +11,7 @@ import { MessageType } from '../types.js';
import type { HookRegistryEntry } from '@google/gemini-cli-core';
import { HookType, HookEventName, ConfigSource } from '@google/gemini-cli-core';
import type { CommandContext } from './types.js';
import { SettingScope } from '../../config/settings.js';
describe('hooksCommand', () => {
let mockContext: CommandContext;
@@ -34,6 +35,11 @@ describe('hooksCommand', () => {
};
};
setValue: ReturnType<typeof vi.fn>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
workspace: { path: string; settings: any };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
user: { path: string; settings: any };
forScope: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
@@ -56,6 +62,17 @@ describe('hooksCommand', () => {
};
// Create mock settings
const mockUser = {
path: '/mock/user.json',
settings: { hooksConfig: { disabled: [] } },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
const mockWorkspace = {
path: '/mock/workspace.json',
settings: { hooksConfig: { disabled: [] } },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
mockSettings = {
merged: {
hooksConfig: {
@@ -63,7 +80,15 @@ describe('hooksCommand', () => {
},
},
setValue: vi.fn(),
};
workspace: mockWorkspace,
user: mockUser,
forScope: vi.fn((scope) => {
if (scope === SettingScope.User) return mockUser;
if (scope === SettingScope.Workspace) return mockWorkspace;
return mockUser;
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
// Create mock context with config and settings
mockContext = createMockCommandContext({
@@ -272,11 +297,12 @@ describe('hooksCommand', () => {
});
it('should enable a hook and update settings', async () => {
// Update the context's settings with disabled hooks
mockContext.services.settings.merged.hooksConfig.disabled = [
// Update the user settings with disabled hooks
mockSettings.user.settings.hooksConfig.disabled = [
'test-hook',
'other-hook',
];
mockSettings.workspace.settings.hooksConfig.disabled = [];
const enableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'enable',
@@ -288,7 +314,7 @@ describe('hooksCommand', () => {
const result = await enableCmd.action(mockContext, 'test-hook');
expect(mockContext.services.settings.setValue).toHaveBeenCalledWith(
expect.any(String),
SettingScope.User,
'hooksConfig.disabled',
['other-hook'],
);
@@ -299,28 +325,8 @@ describe('hooksCommand', () => {
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'Hook "test-hook" enabled successfully.',
});
});
it('should handle error when enabling hook fails', async () => {
mockSettings.setValue.mockImplementationOnce(() => {
throw new Error('Failed to save settings');
});
const enableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'enable',
);
if (!enableCmd?.action) {
throw new Error('enable command must have an action');
}
const result = await enableCmd.action(mockContext, 'test-hook');
expect(result).toEqual({
type: 'message',
messageType: 'error',
content: 'Failed to enable hook: Failed to save settings',
content:
'Hook "test-hook" enabled by removing it from the disabled list in user (/mock/user.json) and workspace (/mock/workspace.json) settings.',
});
});
@@ -332,7 +338,7 @@ describe('hooksCommand', () => {
const hookEntry = createMockHook(
'./hooks/test.sh',
HookEventName.BeforeTool,
true,
false, // Must be disabled for enable completion
);
hookEntry.config.name = 'friendly-name';
@@ -404,7 +410,9 @@ describe('hooksCommand', () => {
});
it('should disable a hook and update settings', async () => {
mockContext.services.settings.merged.hooksConfig.disabled = [];
// Ensure not disabled anywhere
mockSettings.workspace.settings.hooksConfig.disabled = [];
mockSettings.user.settings.hooksConfig.disabled = [];
const disableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'disable',
@@ -415,8 +423,9 @@ describe('hooksCommand', () => {
const result = await disableCmd.action(mockContext, 'test-hook');
// Should default to workspace if present
expect(mockContext.services.settings.setValue).toHaveBeenCalledWith(
expect.any(String),
SettingScope.Workspace,
'hooksConfig.disabled',
['test-hook'],
);
@@ -427,13 +436,14 @@ describe('hooksCommand', () => {
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'Hook "test-hook" disabled successfully.',
content:
'Hook "test-hook" disabled by adding it to the disabled list in workspace (/mock/workspace.json) settings.',
});
});
it('should synchronize with hook system even if hook is already in disabled list', async () => {
// Update the context's settings with the hook already disabled
mockContext.services.settings.merged.hooksConfig.disabled = ['test-hook'];
it('should return info when hook is already disabled', async () => {
// Update the context's settings with the hook already disabled in Workspace
mockSettings.workspace.settings.hooksConfig.disabled = ['test-hook'];
const disableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'disable',
@@ -445,38 +455,29 @@ describe('hooksCommand', () => {
const result = await disableCmd.action(mockContext, 'test-hook');
expect(mockContext.services.settings.setValue).not.toHaveBeenCalled();
expect(mockHookSystem.setHookEnabled).toHaveBeenCalledWith(
'test-hook',
false,
);
expect(mockConfig.updateDisabledHooks).toHaveBeenCalled();
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'Hook "test-hook" disabled successfully.',
content: 'Hook "test-hook" is already disabled.',
});
});
it('should handle error when disabling hook fails', async () => {
mockContext.services.settings.merged.hooksConfig.disabled = [];
mockSettings.setValue.mockImplementationOnce(() => {
throw new Error('Failed to save settings');
});
it('should complete hook names using friendly names', () => {
const disableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'disable',
)!;
const hookEntry = createMockHook(
'./hooks/test.sh',
HookEventName.BeforeTool,
true, // Must be enabled for disable completion
);
if (!disableCmd?.action) {
throw new Error('disable command must have an action');
}
hookEntry.config.name = 'friendly-name';
const result = await disableCmd.action(mockContext, 'test-hook');
mockHookSystem.getAllHooks.mockReturnValue([hookEntry]);
expect(result).toEqual({
type: 'message',
messageType: 'error',
content: 'Failed to disable hook: Failed to save settings',
});
const completions = disableCmd.completion!(mockContext, 'frie');
expect(completions).toContain('friendly-name');
});
});
@@ -513,50 +514,52 @@ describe('hooksCommand', () => {
expect(result).toEqual([]);
});
it('should return matching hook names', () => {
it('should return matching hook names based on status', () => {
const mockHooks: HookRegistryEntry[] = [
createMockHook('test-hook-1', HookEventName.BeforeTool, true),
createMockHook('test-hook-2', HookEventName.AfterTool, true),
createMockHook('other-hook', HookEventName.AfterAgent, false),
createMockHook('test-hook-enabled', HookEventName.BeforeTool, true),
createMockHook('test-hook-disabled', HookEventName.AfterTool, false),
];
mockHookSystem.getAllHooks.mockReturnValue(mockHooks);
const enableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'enable',
);
if (!enableCmd?.completion) {
throw new Error('enable command must have completion');
}
)!;
const disableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'disable',
)!;
const result = enableCmd.completion(mockContext, 'test');
expect(result).toEqual(['test-hook-1', 'test-hook-2']);
const enableResult = enableCmd.completion!(mockContext, 'test');
expect(enableResult).toEqual(['test-hook-disabled']);
const disableResult = disableCmd.completion!(mockContext, 'test');
expect(disableResult).toEqual(['test-hook-enabled']);
});
it('should return all hook names when partial is empty', () => {
it('should return all relevant hook names when partial is empty', () => {
const mockHooks: HookRegistryEntry[] = [
createMockHook('hook-1', HookEventName.BeforeTool, true),
createMockHook('hook-2', HookEventName.AfterTool, true),
createMockHook('hook-enabled', HookEventName.BeforeTool, true),
createMockHook('hook-disabled', HookEventName.AfterTool, false),
];
mockHookSystem.getAllHooks.mockReturnValue(mockHooks);
const enableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'enable',
);
if (!enableCmd?.completion) {
throw new Error('enable command must have completion');
}
)!;
const disableCmd = hooksCommand.subCommands!.find(
(cmd) => cmd.name === 'disable',
)!;
const result = enableCmd.completion(mockContext, '');
expect(result).toEqual(['hook-1', 'hook-2']);
expect(enableCmd.completion!(mockContext, '')).toEqual(['hook-disabled']);
expect(disableCmd.completion!(mockContext, '')).toEqual(['hook-enabled']);
});
it('should handle hooks without command name gracefully', () => {
const mockHooks: HookRegistryEntry[] = [
createMockHook('test-hook', HookEventName.BeforeTool, true),
createMockHook('test-hook', HookEventName.BeforeTool, false),
{
...createMockHook('', HookEventName.AfterTool, true),
...createMockHook('', HookEventName.AfterTool, false),
config: { command: '', type: HookType.Command, timeout: 30 },
},
];
@@ -636,7 +639,7 @@ describe('hooksCommand', () => {
const result = await enableAllCmd.action(mockContext, '');
expect(mockContext.services.settings.setValue).toHaveBeenCalledWith(
expect.any(String),
expect.any(String), // enableAll uses legacy logic so it might return 'Workspace' or 'User' depending on ternary
'hooksConfig.disabled',
[],
);