|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { generateTestsTool } from '../generateTestsTool'; |
| 3 | +import type { Task } from '../../task/Task'; // Using type for Task |
| 4 | +import type { ToolUse, Anthropic } from '@roo-code/types'; // Using type for ToolUse |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +// Using vi.hoisted for variables that need to be accessed in vi.mock factory |
| 8 | +const { extractSymbolCodeMock, pathExtnameMock } = vi.hoisted(() => { |
| 9 | + return { |
| 10 | + extractSymbolCodeMock: vi.fn(), |
| 11 | + pathExtnameMock: vi.fn(), |
| 12 | + }; |
| 13 | +}); |
| 14 | + |
| 15 | +vi.mock('../../../services/tree-sitter', () => ({ |
| 16 | + extractSymbolCode: extractSymbolCodeMock, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('path', async () => { |
| 20 | + const actualPath = await vi.importActual<typeof import('path')>('path'); |
| 21 | + return { |
| 22 | + ...actualPath, |
| 23 | + extname: pathExtnameMock, |
| 24 | + resolve: vi.fn((...paths) => actualPath.join(...paths)), // Use join for testing consistency |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +// Helper to create an async iterable stream |
| 29 | +async function* createMockStream(chunks: Array<{ type: string; text?: string; error?: any }>) { |
| 30 | + for (const chunk of chunks) { |
| 31 | + yield chunk; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +describe('generateTestsTool', () => { |
| 36 | + let mockCline: Task; |
| 37 | + let mockBlock: ToolUse; |
| 38 | + let mockAskApproval: ReturnType<typeof vi.fn>; |
| 39 | + let mockHandleError: ReturnType<typeof vi.fn>; |
| 40 | + let mockPushToolResult: ReturnType<typeof vi.fn>; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + mockAskApproval = vi.fn(); |
| 44 | + mockHandleError = vi.fn(); |
| 45 | + mockPushToolResult = vi.fn(); |
| 46 | + |
| 47 | + mockCline = { |
| 48 | + api: { |
| 49 | + createMessage: vi.fn(), |
| 50 | + }, |
| 51 | + cwd: '/test/workspace', |
| 52 | + taskId: 'test-task-id', |
| 53 | + // Add other Task properties/methods if generateTestsTool starts using them |
| 54 | + } as unknown as Task; // Cast to Task, acknowledging it's a partial mock |
| 55 | + |
| 56 | + mockBlock = { |
| 57 | + tool_name: 'generateTestsTool', |
| 58 | + tool_id: 'test-tool-id', |
| 59 | + params: {}, |
| 60 | + raw_content: '<tool_use tool_name="generateTestsTool"></tool_use>', // Example raw_content |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + vi.clearAllMocks(); |
| 66 | + }); |
| 67 | + |
| 68 | + // Test Cases will go here |
| 69 | + |
| 70 | + it('should call handleError if filePath is missing', async () => { |
| 71 | + mockBlock.params = { symbolName: 'testSymbol' }; |
| 72 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 73 | + expect(mockHandleError).toHaveBeenCalledWith(new Error('Missing required parameter: filePath')); |
| 74 | + expect(mockPushToolResult).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should call handleError if symbolName is missing', async () => { |
| 78 | + mockBlock.params = { filePath: 'src/test.ts' }; |
| 79 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 80 | + expect(mockHandleError).toHaveBeenCalledWith(new Error('Missing required parameter: symbolName')); |
| 81 | + expect(mockPushToolResult).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call handleError if extractSymbolCode returns null (symbol not found)', async () => { |
| 85 | + mockBlock.params = { filePath: 'src/test.ts', symbolName: 'testSymbol' }; |
| 86 | + extractSymbolCodeMock.mockResolvedValue(null); |
| 87 | + pathExtnameMock.mockReturnValue('.ts'); // Needed for prompt construction path |
| 88 | + |
| 89 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 90 | + |
| 91 | + expect(extractSymbolCodeMock).toHaveBeenCalledWith('/test/workspace/src/test.ts', 'testSymbol', undefined); |
| 92 | + expect(mockHandleError).toHaveBeenCalledWith( |
| 93 | + new Error('Could not extract code for symbol "testSymbol" from src/test.ts. The symbol may not exist, the file type might be unsupported for symbol extraction, or the file itself may not be found.') |
| 94 | + ); |
| 95 | + expect(mockPushToolResult).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('happy path: should call pushToolResult with generated tests', async () => { |
| 99 | + const filePath = 'src/component.jsx'; |
| 100 | + const symbolName = 'MyComponent'; |
| 101 | + const symbolCode = 'const MyComponent = () => <div>Hello</div>;'; |
| 102 | + const generatedTestCode = 'describe("MyComponent", () => { it("should render", () => {}); });'; |
| 103 | + |
| 104 | + mockBlock.params = { filePath, symbolName }; |
| 105 | + extractSymbolCodeMock.mockResolvedValue(symbolCode); |
| 106 | + pathExtnameMock.mockReturnValue('.jsx'); |
| 107 | + (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mockReturnValue( |
| 108 | + createMockStream([{ type: 'text', text: generatedTestCode }]) |
| 109 | + ); |
| 110 | + |
| 111 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 112 | + |
| 113 | + expect(extractSymbolCodeMock).toHaveBeenCalledWith(`/test/workspace/${filePath}`, symbolName, undefined); |
| 114 | + expect(pathExtnameMock).toHaveBeenCalledWith(filePath); |
| 115 | + expect(mockCline.api.createMessage).toHaveBeenCalled(); |
| 116 | + expect(mockPushToolResult).toHaveBeenCalledWith(generatedTestCode); |
| 117 | + expect(mockHandleError).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should call handleError if LLM stream returns an error chunk', async () => { |
| 121 | + const filePath = 'src/error.py'; |
| 122 | + const symbolName = 'errorFunc'; |
| 123 | + const symbolCode = 'def errorFunc(): pass'; |
| 124 | + |
| 125 | + mockBlock.params = { filePath, symbolName }; |
| 126 | + extractSymbolCodeMock.mockResolvedValue(symbolCode); |
| 127 | + pathExtnameMock.mockReturnValue('.py'); |
| 128 | + (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mockReturnValue( |
| 129 | + createMockStream([{ type: 'error', error: { message: 'LLM API error' } }]) |
| 130 | + ); |
| 131 | + |
| 132 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 133 | + |
| 134 | + expect(mockHandleError).toHaveBeenCalledWith(new Error('LLM call failed during test generation: LLM API error')); |
| 135 | + expect(mockPushToolResult).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should call handleError if LLM stream throws an error', async () => { |
| 139 | + const filePath = 'src/streamError.js'; |
| 140 | + const symbolName = 'streamErrorFunc'; |
| 141 | + const symbolCode = 'function streamErrorFunc() {}'; |
| 142 | + |
| 143 | + mockBlock.params = { filePath, symbolName }; |
| 144 | + extractSymbolCodeMock.mockResolvedValue(symbolCode); |
| 145 | + pathExtnameMock.mockReturnValue('.js'); |
| 146 | + (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mockImplementation(() => { |
| 147 | + // Simulate a stream that throws an error |
| 148 | + return (async function*() { |
| 149 | + yield { type: 'text', text: 'some partial text...' }; |
| 150 | + throw new Error('Network connection lost'); |
| 151 | + })(); |
| 152 | + }); |
| 153 | + |
| 154 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 155 | + |
| 156 | + expect(mockHandleError).toHaveBeenCalledWith(new Error('LLM call failed during test generation: Network connection lost')); |
| 157 | + expect(mockPushToolResult).not.toHaveBeenCalled(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should call handleError if LLM returns empty response', async () => { |
| 161 | + const filePath = 'src/empty.ts'; |
| 162 | + const symbolName = 'EmptySym'; |
| 163 | + const symbolCode = 'class EmptySym {}'; |
| 164 | + |
| 165 | + mockBlock.params = { filePath, symbolName }; |
| 166 | + extractSymbolCodeMock.mockResolvedValue(symbolCode); |
| 167 | + pathExtnameMock.mockReturnValue('.ts'); |
| 168 | + (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mockReturnValue( |
| 169 | + createMockStream([{ type: 'text', text: ' ' }]) // Empty or whitespace only |
| 170 | + ); |
| 171 | + |
| 172 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 173 | + |
| 174 | + expect(mockHandleError).toHaveBeenCalledWith(new Error('LLM returned empty response for test generation.')); |
| 175 | + expect(mockPushToolResult).not.toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('Prompt Construction', () => { |
| 179 | + const symbolCode = 'function example() {}'; |
| 180 | + beforeEach(() => { |
| 181 | + extractSymbolCodeMock.mockResolvedValue(symbolCode); |
| 182 | + (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mockReturnValue(createMockStream([])); // Prevent actual call |
| 183 | + }); |
| 184 | + |
| 185 | + it('should use correct language hint for TypeScript', async () => { |
| 186 | + mockBlock.params = { filePath: 'myFile.ts', symbolName: 'tsFunc' }; |
| 187 | + pathExtnameMock.mockReturnValue('.ts'); |
| 188 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 189 | + |
| 190 | + const createMessageArgs = (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 191 | + const systemPrompt = createMessageArgs[0]; |
| 192 | + const userMessage = createMessageArgs[1][0].content; |
| 193 | + |
| 194 | + expect(systemPrompt).toContain('TypeScript/JavaScript code'); |
| 195 | + expect(userMessage).toContain('TypeScript/JavaScript code'); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should use correct language hint for Python', async () => { |
| 199 | + mockBlock.params = { filePath: 'myScript.py', symbolName: 'pyFunc' }; |
| 200 | + pathExtnameMock.mockReturnValue('.py'); |
| 201 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 202 | + |
| 203 | + const createMessageArgs = (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 204 | + const systemPrompt = createMessageArgs[0]; |
| 205 | + const userMessage = createMessageArgs[1][0].content; |
| 206 | + |
| 207 | + expect(systemPrompt).toContain('PyTest style unit tests'); |
| 208 | + expect(userMessage).toContain('PyTest style unit tests'); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should use generic language hint for unknown extension', async () => { |
| 212 | + mockBlock.params = { filePath: 'myCode.unknown', symbolName: 'unknownFunc' }; |
| 213 | + pathExtnameMock.mockReturnValue('.unknown'); |
| 214 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 215 | + |
| 216 | + const createMessageArgs = (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 217 | + const systemPrompt = createMessageArgs[0]; |
| 218 | + const userMessage = createMessageArgs[1][0].content; |
| 219 | + |
| 220 | + expect(systemPrompt).toContain('Generate unit tests for this code.'); |
| 221 | + expect(userMessage).toContain('Generate unit tests for this code.'); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should include filePath and symbolName in user message', async () => { |
| 225 | + const filePath = 'src/app.js'; |
| 226 | + const symbolName = 'initialize'; |
| 227 | + mockBlock.params = { filePath, symbolName }; |
| 228 | + pathExtnameMock.mockReturnValue('.js'); |
| 229 | + await generateTestsTool(mockCline, mockBlock, mockAskApproval, mockHandleError, mockPushToolResult); |
| 230 | + |
| 231 | + const createMessageArgs = (mockCline.api.createMessage as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 232 | + const userMessage = createMessageArgs[1][0].content; |
| 233 | + |
| 234 | + expect(userMessage).toContain(`from the file "${filePath}"`); |
| 235 | + expect(userMessage).toContain(`symbol "${symbolName}"`); |
| 236 | + expect(userMessage).toContain(symbolCode); |
| 237 | + }); |
| 238 | + }); |
| 239 | +}); |
0 commit comments