|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 8 | +import { SkillCommandLoader } from './SkillCommandLoader.js'; |
| 9 | +import { CommandKind } from '../ui/commands/types.js'; |
| 10 | +import { ACTIVATE_SKILL_TOOL_NAME } from '@google/gemini-cli-core'; |
| 11 | + |
| 12 | +describe('SkillCommandLoader', () => { |
| 13 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 14 | + let mockConfig: any; |
| 15 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 16 | + let mockSkillManager: any; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + mockSkillManager = { |
| 20 | + getDisplayableSkills: vi.fn(), |
| 21 | + isAdminEnabled: vi.fn().mockReturnValue(true), |
| 22 | + }; |
| 23 | + |
| 24 | + mockConfig = { |
| 25 | + isSkillsSupportEnabled: vi.fn().mockReturnValue(true), |
| 26 | + getSkillManager: vi.fn().mockReturnValue(mockSkillManager), |
| 27 | + }; |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return an empty array if skills support is disabled', async () => { |
| 31 | + mockConfig.isSkillsSupportEnabled.mockReturnValue(false); |
| 32 | + const loader = new SkillCommandLoader(mockConfig); |
| 33 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 34 | + expect(commands).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return an empty array if SkillManager is missing', async () => { |
| 38 | + mockConfig.getSkillManager.mockReturnValue(null); |
| 39 | + const loader = new SkillCommandLoader(mockConfig); |
| 40 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 41 | + expect(commands).toEqual([]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return an empty array if skills are admin-disabled', async () => { |
| 45 | + mockSkillManager.isAdminEnabled.mockReturnValue(false); |
| 46 | + const loader = new SkillCommandLoader(mockConfig); |
| 47 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 48 | + expect(commands).toEqual([]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should load skills as slash commands', async () => { |
| 52 | + const mockSkills = [ |
| 53 | + { name: 'skill1', description: 'Description 1' }, |
| 54 | + { name: 'skill2', description: '' }, |
| 55 | + ]; |
| 56 | + mockSkillManager.getDisplayableSkills.mockReturnValue(mockSkills); |
| 57 | + |
| 58 | + const loader = new SkillCommandLoader(mockConfig); |
| 59 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 60 | + |
| 61 | + expect(commands).toHaveLength(2); |
| 62 | + |
| 63 | + expect(commands[0]).toMatchObject({ |
| 64 | + name: 'skill1', |
| 65 | + description: 'Description 1', |
| 66 | + kind: CommandKind.SKILL, |
| 67 | + autoExecute: true, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(commands[1]).toMatchObject({ |
| 71 | + name: 'skill2', |
| 72 | + description: 'Activate the skill2 skill', |
| 73 | + kind: CommandKind.SKILL, |
| 74 | + autoExecute: true, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return a tool action when a skill command is executed', async () => { |
| 79 | + const mockSkills = [{ name: 'test-skill', description: 'Test skill' }]; |
| 80 | + mockSkillManager.getDisplayableSkills.mockReturnValue(mockSkills); |
| 81 | + |
| 82 | + const loader = new SkillCommandLoader(mockConfig); |
| 83 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 84 | + |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 86 | + const actionResult = await commands[0].action!({} as any, ''); |
| 87 | + expect(actionResult).toEqual({ |
| 88 | + type: 'tool', |
| 89 | + toolName: ACTIVATE_SKILL_TOOL_NAME, |
| 90 | + toolArgs: { name: 'test-skill' }, |
| 91 | + postSubmitPrompt: undefined, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should return a tool action with postSubmitPrompt when args are provided', async () => { |
| 96 | + const mockSkills = [{ name: 'test-skill', description: 'Test skill' }]; |
| 97 | + mockSkillManager.getDisplayableSkills.mockReturnValue(mockSkills); |
| 98 | + |
| 99 | + const loader = new SkillCommandLoader(mockConfig); |
| 100 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 101 | + |
| 102 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 103 | + const actionResult = await commands[0].action!({} as any, 'hello world'); |
| 104 | + expect(actionResult).toEqual({ |
| 105 | + type: 'tool', |
| 106 | + toolName: ACTIVATE_SKILL_TOOL_NAME, |
| 107 | + toolArgs: { name: 'test-skill' }, |
| 108 | + postSubmitPrompt: 'hello world', |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should sanitize skill names with spaces', async () => { |
| 113 | + const mockSkills = [{ name: 'my awesome skill', description: 'Desc' }]; |
| 114 | + mockSkillManager.getDisplayableSkills.mockReturnValue(mockSkills); |
| 115 | + |
| 116 | + const loader = new SkillCommandLoader(mockConfig); |
| 117 | + const commands = await loader.loadCommands(new AbortController().signal); |
| 118 | + |
| 119 | + expect(commands[0].name).toBe('my-awesome-skill'); |
| 120 | + |
| 121 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 122 | + const actionResult = (await commands[0].action!({} as any, '')) as any; |
| 123 | + expect(actionResult.toolArgs).toEqual({ name: 'my awesome skill' }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments