UNPKG

cursorai-errorprompter

Version:

AI-powered runtime error fixing for developers using Cursor

101 lines (100 loc) 4.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const gptService_1 = require("../gptService"); const openai_1 = __importDefault(require("openai")); vitest_1.vi.mock('openai', () => ({ default: vitest_1.vi.fn().mockImplementation(() => ({ chat: { completions: { create: vitest_1.vi.fn() } } })) })); (0, vitest_1.describe)('GPTService', () => { let gptService; const mockConfig = { apiKey: 'test-key', model: 'gpt-4-turbo-preview', temperature: 0.3, maxTokens: 1000 }; const mockError = { type: 'TypeError', message: 'Cannot read property of undefined', filePath: 'test.ts', lineNumber: 10, stackTrace: 'TypeError: Cannot read property of undefined\n at test.ts:10:15', codeSnippet: { before: ['const data = {', ' value: 42'], error: 'console.log(data.nonexistent.prop);', after: ['};', 'processData();'] } }; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); gptService = new gptService_1.GPTService(mockConfig); }); (0, vitest_1.it)('should validate configuration on initialization', () => { (0, vitest_1.expect)(() => new gptService_1.GPTService({ apiKey: '', model: 'gpt-4-turbo-preview' })) .toThrow('OpenAI API key is required'); (0, vitest_1.expect)(() => new gptService_1.GPTService({ apiKey: 'test', model: 'invalid-model' })) .toThrow('Unsupported model'); }); (0, vitest_1.it)('should send error to GPT and parse response', async () => { const mockResponse = { choices: [{ message: { content: JSON.stringify({ suggestion: 'const data = { value: 42, nonexistent: { prop: null } };', explanation: 'Add default value for nonexistent property', confidence: 0.9 }) } }] }; openai_1.default.mock.results[0].value.chat.completions.create .mockResolvedValueOnce(mockResponse); const result = await gptService.getErrorFix(mockError); (0, vitest_1.expect)(result).toEqual({ suggestion: 'const data = { value: 42, nonexistent: { prop: null } };', explanation: 'Add default value for nonexistent property', confidence: 0.9 }); }); (0, vitest_1.it)('should handle API errors gracefully', async () => { openai_1.default.mock.results[0].value.chat.completions.create .mockRejectedValueOnce(new Error('401 Unauthorized')); await (0, vitest_1.expect)(gptService.getErrorFix(mockError)) .rejects .toThrow('Invalid OpenAI API key'); }); (0, vitest_1.it)('should handle rate limiting', async () => { openai_1.default.mock.results[0].value.chat.completions.create .mockRejectedValueOnce(new Error('429 Too Many Requests')); await (0, vitest_1.expect)(gptService.getErrorFix(mockError)) .rejects .toThrow('OpenAI API rate limit exceeded'); }); (0, vitest_1.it)('should handle invalid JSON responses', async () => { const mockResponse = { choices: [{ message: { content: 'Invalid JSON response' } }] }; openai_1.default.mock.results[0].value.chat.completions.create .mockResolvedValueOnce(mockResponse); const result = await gptService.getErrorFix(mockError); (0, vitest_1.expect)(result).toEqual({ suggestion: 'Invalid JSON response', explanation: 'GPT provided a direct suggestion without structured format', confidence: 0.8 }); }); });