cc-haha/src/utils/forkedAgent.test.ts
程序员阿江(Relakkes) 4589ef67f3 feat: surface agent selection without slash namespace collisions (#653)
Desktop users need a direct way to pick an installed Agent from slash completion while preserving the existing command and skill namespace. Add /agent <agent> <prompt> as the single execution surface and render active agents as namespaced completion rows.

Constraint: Existing slash command names and ordering must remain authoritative.
Rejected: Register each Agent as a top-level /<agent> command | would collide with built-in commands, skills, and future custom commands.
Confidence: high
Scope-risk: moderate
Tested: bun run verify
2026-06-01 21:55:38 +08:00

68 lines
1.9 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import type { PromptCommand } from '../commands.js'
import type { ToolUseContext } from '../Tool.js'
import type { AgentDefinition } from '../tools/AgentTool/loadAgentsDir.js'
import { prepareForkedCommandContext } from './forkedAgent.js'
const makeAgent = (agentType: string): AgentDefinition => ({
agentType,
whenToUse: `Use ${agentType}`,
source: 'built-in',
baseDir: 'built-in',
getSystemPrompt: () => `${agentType} prompt`,
})
function makeContext(activeAgents: AgentDefinition[]): ToolUseContext {
return {
getAppState: () => ({
toolPermissionContext: {
alwaysAllowRules: { command: [] },
},
}),
options: {
agentDefinitions: { activeAgents },
},
} as unknown as ToolUseContext
}
describe('prepareForkedCommandContext', () => {
const command: PromptCommand = {
type: 'prompt',
progressMessage: 'running',
contentLength: 0,
source: 'builtin',
getPromptForCommand: async (args) => [{ type: 'text', text: args }],
}
test('uses explicit agent and prompt overrides for dynamic forked commands', async () => {
const prepared = await prepareForkedCommandContext(
command,
'debugger fix tests',
makeContext([makeAgent('general-purpose'), makeAgent('debugger')]),
{
agentType: 'debugger',
promptArgs: 'fix tests',
requireAgentType: true,
},
)
expect(prepared.baseAgent.agentType).toBe('debugger')
expect(prepared.skillContent).toBe('fix tests')
})
test('throws when a required explicit agent is unavailable', async () => {
await expect(
prepareForkedCommandContext(
command,
'debugger fix tests',
makeContext([makeAgent('general-purpose')]),
{
agentType: 'debugger',
promptArgs: 'fix tests',
requireAgentType: true,
},
),
).rejects.toThrow('Agent not available: debugger')
})
})