|
| 1 | +import type { AgentTask } from '@frontagent/shared'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import type { AgentEvent } from '../types.js'; |
| 4 | +import { prepareTaskExecutionSetup } from './task-execution-setup.js'; |
| 5 | + |
| 6 | +function makeTask(description = 'Use skill text'): AgentTask { |
| 7 | + return { |
| 8 | + id: 'task-1', |
| 9 | + type: 'code', |
| 10 | + description, |
| 11 | + context: { |
| 12 | + workingDirectory: '/repo', |
| 13 | + relevantFiles: ['src/app.ts'], |
| 14 | + browserUrl: 'http://localhost:5173', |
| 15 | + }, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function makeContext() { |
| 20 | + return { |
| 21 | + collectedContext: { |
| 22 | + files: new Map<string, string>(), |
| 23 | + pageStructure: undefined, |
| 24 | + ragResults: undefined, |
| 25 | + skillContext: undefined, |
| 26 | + matchedSkillNames: [] as string[], |
| 27 | + memoryContext: undefined, |
| 28 | + metadata: {} as Record<string, unknown>, |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function makeSetupDeps({ ragEnabled = true }: { ragEnabled?: boolean } = {}) { |
| 34 | + const events: AgentEvent[] = []; |
| 35 | + const statusUpdates: Array<{ label: string; operation?: string; detail?: string }> = []; |
| 36 | + const context = makeContext(); |
| 37 | + const createdTasks: AgentTask[] = []; |
| 38 | + const preloadMemory = vi.fn(); |
| 39 | + const prepareProjectPlanningContext = vi.fn( |
| 40 | + async ({ deps }: { deps: { emitStatus: typeof emitStatus } }) => { |
| 41 | + deps.emitStatus('扫描项目结构', 'list_directory 扫描项目结构'); |
| 42 | + deps.emitStatus('检测开发服务器端口', '检测开发服务器端口'); |
| 43 | + deps.emitStatus('检索知识库', 'RAG 检索'); |
| 44 | + return { |
| 45 | + projectStructure: 'project files', |
| 46 | + devServerPort: 5173, |
| 47 | + ragResults: ['rag result'], |
| 48 | + ragEvent: { |
| 49 | + searchMode: 'keyword_only' as const, |
| 50 | + matches: [ |
| 51 | + { |
| 52 | + type: 'doc', |
| 53 | + title: 'Guide', |
| 54 | + sourceUrl: 'https://example.test', |
| 55 | + snippet: 'Use it.', |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + }; |
| 60 | + }, |
| 61 | + ); |
| 62 | + const emit = vi.fn((event: AgentEvent) => { |
| 63 | + events.push(event); |
| 64 | + }); |
| 65 | + const emitStatus = vi.fn((label: string, operation = label, detail?: string) => { |
| 66 | + statusUpdates.push({ label, operation, detail }); |
| 67 | + emit({ type: 'status_update', label, operation, detail }); |
| 68 | + }); |
| 69 | + const addMessage = vi.fn(); |
| 70 | + const promptGenerator = { generate: vi.fn(() => 'sdd prompt') }; |
| 71 | + const workflowIntegration = { getConstitutionPrompt: vi.fn(() => 'constitution prompt') }; |
| 72 | + |
| 73 | + return { |
| 74 | + events, |
| 75 | + statusUpdates, |
| 76 | + context, |
| 77 | + createdTasks, |
| 78 | + preloadMemory, |
| 79 | + prepareProjectPlanningContext, |
| 80 | + addMessage, |
| 81 | + promptGenerator, |
| 82 | + workflowIntegration, |
| 83 | + deps: { |
| 84 | + config: { |
| 85 | + projectRoot: '/repo', |
| 86 | + llm: { provider: 'openai' as const, model: 'gpt-4', apiKey: 'test' }, |
| 87 | + rag: ragEnabled |
| 88 | + ? { repoUrl: 'https://example.test/rag.git' } |
| 89 | + : { enabled: false, repoUrl: 'https://example.test/rag.git' }, |
| 90 | + }, |
| 91 | + contextManager: { |
| 92 | + createContext: vi.fn((task: AgentTask) => { |
| 93 | + createdTasks.push(task); |
| 94 | + return context; |
| 95 | + }), |
| 96 | + addMessage, |
| 97 | + }, |
| 98 | + memoryStore: { resetSession: vi.fn() }, |
| 99 | + memoryDeps: {}, |
| 100 | + promptGenerator, |
| 101 | + workflowIntegration, |
| 102 | + planningDeps: { |
| 103 | + executor: { callTool: vi.fn() }, |
| 104 | + ragDeps: {}, |
| 105 | + emitStatus, |
| 106 | + debugLog: vi.fn(), |
| 107 | + debugWarn: vi.fn(), |
| 108 | + }, |
| 109 | + preloadMemory, |
| 110 | + prepareProjectPlanningContext, |
| 111 | + emit, |
| 112 | + emitStatus, |
| 113 | + debugLog: vi.fn(), |
| 114 | + }, |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +describe('prepareTaskExecutionSetup', () => { |
| 119 | + it('prepares task inputs, status, memory, SDD prompts, and planning context in execute order', async () => { |
| 120 | + const setup = makeSetupDeps(); |
| 121 | + const task = makeTask(); |
| 122 | + |
| 123 | + const result = await prepareTaskExecutionSetup({ |
| 124 | + task, |
| 125 | + originalTaskDescription: 'Use @skill-a for this', |
| 126 | + skillContext: 'skill prompt', |
| 127 | + matchedSkillNames: ['skill-a'], |
| 128 | + deps: setup.deps, |
| 129 | + }); |
| 130 | + |
| 131 | + expect(result.task).toBe(task); |
| 132 | + expect(result.context).toBe(setup.context); |
| 133 | + expect(result.skillContext).toBe('skill prompt'); |
| 134 | + expect(result.matchedSkillNames).toEqual(['skill-a']); |
| 135 | + expect(result.planningPreparation).toMatchObject({ |
| 136 | + projectStructure: 'project files', |
| 137 | + devServerPort: 5173, |
| 138 | + ragResults: ['rag result'], |
| 139 | + }); |
| 140 | + expect(setup.context.collectedContext.skillContext).toBe('skill prompt'); |
| 141 | + expect(setup.context.collectedContext.matchedSkillNames).toEqual(['skill-a']); |
| 142 | + expect(setup.context.collectedContext.metadata.originalTaskDescription).toBe( |
| 143 | + 'Use @skill-a for this', |
| 144 | + ); |
| 145 | + expect(setup.deps.memoryStore.resetSession).toHaveBeenCalledOnce(); |
| 146 | + expect(setup.preloadMemory).toHaveBeenCalledWith( |
| 147 | + setup.deps.memoryDeps, |
| 148 | + result.task.id, |
| 149 | + setup.context, |
| 150 | + ); |
| 151 | + expect(setup.addMessage).toHaveBeenCalledWith(result.task.id, { |
| 152 | + role: 'system', |
| 153 | + content: 'constitution prompt', |
| 154 | + }); |
| 155 | + expect(setup.addMessage).toHaveBeenCalledWith(result.task.id, { |
| 156 | + role: 'system', |
| 157 | + content: 'sdd prompt', |
| 158 | + }); |
| 159 | + expect(setup.events.map((event) => event.type)).toEqual([ |
| 160 | + 'status_update', |
| 161 | + 'status_update', |
| 162 | + 'status_update', |
| 163 | + 'status_update', |
| 164 | + 'rag_retrieved', |
| 165 | + ]); |
| 166 | + expect(setup.statusUpdates.map((status) => [status.label, status.operation])).toEqual([ |
| 167 | + ['加载跨会话记忆', '加载跨会话记忆'], |
| 168 | + ['扫描项目结构', 'list_directory 扫描项目结构'], |
| 169 | + ['检测开发服务器端口', '检测开发服务器端口'], |
| 170 | + ['检索知识库', 'RAG 检索'], |
| 171 | + ]); |
| 172 | + }); |
| 173 | + |
| 174 | + it('keeps sanitized empty task descriptions from replacing the original and suppresses rag events when disabled', async () => { |
| 175 | + const setup = makeSetupDeps({ ragEnabled: false }); |
| 176 | + const task = makeTask('Original task'); |
| 177 | + |
| 178 | + const result = await prepareTaskExecutionSetup({ |
| 179 | + task, |
| 180 | + originalTaskDescription: 'Original task', |
| 181 | + skillContext: 'skill prompt', |
| 182 | + matchedSkillNames: ['skill-a'], |
| 183 | + deps: setup.deps, |
| 184 | + }); |
| 185 | + |
| 186 | + expect(result.task.description).toBe('Original task'); |
| 187 | + expect(setup.prepareProjectPlanningContext).toHaveBeenCalledWith( |
| 188 | + expect.objectContaining({ |
| 189 | + taskId: result.task.id, |
| 190 | + taskDescription: 'Original task', |
| 191 | + projectRoot: '/repo', |
| 192 | + ragEnabled: false, |
| 193 | + preScanFailureLabel: '[Agent] Failed to pre-scan project structure:', |
| 194 | + logProjectStructure: true, |
| 195 | + }), |
| 196 | + ); |
| 197 | + expect(setup.events.some((event) => event.type === 'rag_retrieved')).toBe(false); |
| 198 | + }); |
| 199 | +}); |
0 commit comments