|
| 1 | +import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' |
| 2 | + |
| 3 | +import { useChatStore } from '../../state/chat-store' |
| 4 | +import { |
| 5 | + __resetSkillRegistryForTests, |
| 6 | + __setSkillsForTests, |
| 7 | +} from '../../utils/skill-registry' |
| 8 | +import { findCommand } from '../command-registry' |
| 9 | +import { buildSkillPrompt } from '../prompt-builders' |
| 10 | +import { routeUserPrompt } from '../router' |
| 11 | + |
| 12 | +import type { RouterParams } from '../command-registry' |
| 13 | +import type { SkillDefinition } from '@codebuff/common/types/skill' |
| 14 | + |
| 15 | +const TEST_SKILL: SkillDefinition = { |
| 16 | + name: 'release-notes', |
| 17 | + description: 'Draft release notes from recent commits', |
| 18 | + content: |
| 19 | + '---\nname: release-notes\ndescription: Draft release notes\n---\n\nDo the thing.', |
| 20 | + filePath: '/tmp/skills/release-notes/SKILL.md', |
| 21 | +} |
| 22 | + |
| 23 | +const createMockParams = (overrides: Partial<RouterParams> = {}): RouterParams => |
| 24 | + ({ |
| 25 | + agentMode: 'DEFAULT', |
| 26 | + inputRef: { current: null }, |
| 27 | + inputValue: '', |
| 28 | + isChainInProgressRef: { current: false }, |
| 29 | + isStreaming: false, |
| 30 | + logoutMutation: {} as RouterParams['logoutMutation'], |
| 31 | + streamMessageIdRef: { current: null }, |
| 32 | + addToQueue: mock(() => {}), |
| 33 | + clearMessages: mock(() => {}), |
| 34 | + saveToHistory: mock(() => {}), |
| 35 | + scrollToLatest: mock(() => {}), |
| 36 | + sendMessage: mock(async () => {}), |
| 37 | + setCanProcessQueue: mock(() => {}), |
| 38 | + setInputFocused: mock(() => {}), |
| 39 | + setInputValue: mock(() => {}), |
| 40 | + setIsAuthenticated: mock(() => {}), |
| 41 | + setMessages: mock(() => {}), |
| 42 | + setUser: mock(() => {}), |
| 43 | + ...overrides, |
| 44 | + }) as RouterParams |
| 45 | + |
| 46 | +const resetChatStore = () => { |
| 47 | + useChatStore.getState().setInputMode('default') |
| 48 | + useChatStore.getState().setPendingSkillName(null) |
| 49 | +} |
| 50 | + |
| 51 | +beforeEach(() => { |
| 52 | + __setSkillsForTests({ [TEST_SKILL.name]: TEST_SKILL }) |
| 53 | + resetChatStore() |
| 54 | +}) |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + __resetSkillRegistryForTests() |
| 58 | + resetChatStore() |
| 59 | +}) |
| 60 | + |
| 61 | +describe('/skill:<name> command', () => { |
| 62 | + test('bare invocation enters skill input mode instead of sending', async () => { |
| 63 | + const command = findCommand('skill:release-notes') |
| 64 | + expect(command).toBeDefined() |
| 65 | + |
| 66 | + const params = createMockParams({ inputValue: '/skill:release-notes' }) |
| 67 | + await command!.handler(params, '') |
| 68 | + |
| 69 | + expect(useChatStore.getState().inputMode).toBe('skill') |
| 70 | + expect(useChatStore.getState().pendingSkillName).toBe('release-notes') |
| 71 | + expect(params.sendMessage).not.toHaveBeenCalled() |
| 72 | + expect(params.addToQueue).not.toHaveBeenCalled() |
| 73 | + }) |
| 74 | + |
| 75 | + test('invocation with trailing text sends immediately', async () => { |
| 76 | + const command = findCommand('skill:release-notes') |
| 77 | + const params = createMockParams({ |
| 78 | + inputValue: '/skill:release-notes for v2.1 only', |
| 79 | + }) |
| 80 | + await command!.handler(params, 'for v2.1 only') |
| 81 | + |
| 82 | + expect(useChatStore.getState().inputMode).toBe('default') |
| 83 | + expect(params.sendMessage).toHaveBeenCalledTimes(1) |
| 84 | + const [{ content }] = (params.sendMessage as ReturnType<typeof mock>).mock |
| 85 | + .calls[0] as [{ content: string }] |
| 86 | + expect(content).toBe(buildSkillPrompt(TEST_SKILL, 'for v2.1 only')) |
| 87 | + expect(content).toContain('<skill name="release-notes">') |
| 88 | + expect(content).toContain('User request: for v2.1 only') |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe('skill input mode submit', () => { |
| 93 | + const enterSkillMode = () => { |
| 94 | + useChatStore.getState().setInputMode('skill') |
| 95 | + useChatStore.getState().setPendingSkillName(TEST_SKILL.name) |
| 96 | + } |
| 97 | + |
| 98 | + test('submit with text sends the skill plus the user request', async () => { |
| 99 | + enterSkillMode() |
| 100 | + const params = createMockParams({ inputValue: 'focus on the API changes' }) |
| 101 | + await routeUserPrompt(params) |
| 102 | + |
| 103 | + expect(useChatStore.getState().inputMode).toBe('default') |
| 104 | + expect(useChatStore.getState().pendingSkillName).toBeNull() |
| 105 | + expect(params.sendMessage).toHaveBeenCalledTimes(1) |
| 106 | + const [{ content }] = (params.sendMessage as ReturnType<typeof mock>).mock |
| 107 | + .calls[0] as [{ content: string }] |
| 108 | + expect(content).toBe( |
| 109 | + buildSkillPrompt(TEST_SKILL, 'focus on the API changes'), |
| 110 | + ) |
| 111 | + }) |
| 112 | + |
| 113 | + test('empty submit runs the skill without a user request', async () => { |
| 114 | + enterSkillMode() |
| 115 | + const params = createMockParams({ inputValue: '' }) |
| 116 | + await routeUserPrompt(params) |
| 117 | + |
| 118 | + expect(params.sendMessage).toHaveBeenCalledTimes(1) |
| 119 | + const [{ content }] = (params.sendMessage as ReturnType<typeof mock>).mock |
| 120 | + .calls[0] as [{ content: string }] |
| 121 | + expect(content).toBe(buildSkillPrompt(TEST_SKILL, '')) |
| 122 | + expect(content).not.toContain('User request:') |
| 123 | + }) |
| 124 | + |
| 125 | + test('submit while a turn is running queues instead of sending', async () => { |
| 126 | + enterSkillMode() |
| 127 | + const params = createMockParams({ |
| 128 | + inputValue: 'and be brief', |
| 129 | + isStreaming: true, |
| 130 | + }) |
| 131 | + await routeUserPrompt(params) |
| 132 | + |
| 133 | + expect(params.sendMessage).not.toHaveBeenCalled() |
| 134 | + expect(params.addToQueue).toHaveBeenCalledTimes(1) |
| 135 | + const [queued] = (params.addToQueue as ReturnType<typeof mock>).mock |
| 136 | + .calls[0] as [string] |
| 137 | + expect(queued).toBe(buildSkillPrompt(TEST_SKILL, 'and be brief')) |
| 138 | + }) |
| 139 | + |
| 140 | + test('a skill deleted mid-session reports instead of sending nothing', async () => { |
| 141 | + enterSkillMode() |
| 142 | + __resetSkillRegistryForTests() |
| 143 | + const params = createMockParams({ inputValue: 'anything' }) |
| 144 | + await routeUserPrompt(params) |
| 145 | + |
| 146 | + expect(params.sendMessage).not.toHaveBeenCalled() |
| 147 | + expect(params.setMessages).toHaveBeenCalled() |
| 148 | + expect(useChatStore.getState().inputMode).toBe('default') |
| 149 | + }) |
| 150 | + |
| 151 | + test('leaving skill mode clears the pending skill', () => { |
| 152 | + enterSkillMode() |
| 153 | + useChatStore.getState().setInputMode('default') |
| 154 | + expect(useChatStore.getState().pendingSkillName).toBeNull() |
| 155 | + }) |
| 156 | +}) |
0 commit comments