/** * AI Test Case Generation API Service * * Integrates with the AI backend microservice to generate test cases * for features (issues) based on their tasks. * * Response format matches the frontend Task interface for direct * use with tasksApi.create() */ import { Task } from './tasksApi'; // AI Backend Configuration const AI_BACKEND_URL = 'https://devarshia5-aipmtool.hf.space'; // ============================================================================ // TYPES // ============================================================================ export interface TaskInfo { id: number; taskCode?: string; title: string; description?: string; status?: string; assignedTo?: number | null; } export interface FrontendGenerateRequest { feature_id: number; feature_title?: string; feature_description?: string; project_id?: number; project_name?: string; deliverable_id?: number; deliverable_name?: string; tasks: TaskInfo[]; test_case_count?: number; environment?: 'Development' | 'Testing' | 'Staging' | 'Production'; // Existing test cases to avoid duplicates existing_test_cases?: ExistingTestCase[]; // Additional context from user (text or PDF content) additional_context?: string; } export interface ExistingTestCase { title: string; description?: string; } export interface FrontendTestCase { type: 'Test Case'; id?: number; title: string; description: string; issuesId: number; projectId?: number; status: string; priority: string; severity: string; environment: string; stepsToReproduce: string; inputs: string; expectedBehavior: string; actualBehavior: string; rootCause: string; resolution: string; version: string; url: string; assignedTo: number | null; stateDate: string; endDate: string | null; related_task_id?: number; related_task_code?: string; projectName?: string; issueName?: string; deliverableName?: string; } export interface FrontendGenerateResponse { success: boolean; message: string; feature_id: number; feature_name?: string; generated_at: string; test_cases: FrontendTestCase[]; metadata?: { total_tasks_processed: number; total_tasks_failed: number; test_cases_per_task: number; environment: string; failed_tasks?: Array<{ task_id: number; task_title: string; error: string; }>; }; } // ============================================================================ // AI TEST CASE API CLASS // ============================================================================ class AITestCaseApi { /** * Generate test cases for a feature (issue) * * @param request - Feature context and tasks * @returns Generated test cases in Task format */ async generateForFeature(request: FrontendGenerateRequest): Promise { try { const response = await fetch(`${AI_BACKEND_URL}/api/testcases/generate/frontend`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ ...request, test_case_count: request.test_case_count || 3, environment: request.environment || 'Staging', }), }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.detail || `HTTP ${response.status}: ${response.statusText}`); } const data: FrontendGenerateResponse = await response.json(); return data; } catch (error) { console.error('AI Test Case Generation Error:', error); throw error; } } /** * Check if AI backend is healthy */ async checkHealth(): Promise<{ status: string; groq_status: string; pm_tool_status: string; }> { try { const response = await fetch(`${AI_BACKEND_URL}/health`, { method: 'GET', }); if (!response.ok) { return { status: 'unhealthy', groq_status: 'unknown', pm_tool_status: 'unknown', }; } const data = await response.json(); return { status: data.status, groq_status: data.groq_status, pm_tool_status: data.pm_tool_status, }; } catch (error) { return { status: 'unreachable', groq_status: 'unknown', pm_tool_status: 'unknown', }; } } /** * Convert FrontendTestCase to Task format for tasksApi.create() */ toTaskFormat(testCase: FrontendTestCase): Partial { return { type: 'Test Case', title: testCase.title, description: testCase.description, issuesId: testCase.issuesId, projectId: testCase.projectId, status: testCase.status, priority: testCase.priority, severity: testCase.severity, environment: testCase.environment, stepsToReproduce: testCase.stepsToReproduce, inputs: testCase.inputs, expectedBehavior: testCase.expectedBehavior, actualBehavior: testCase.actualBehavior || '', rootCause: testCase.rootCause || '', resolution: testCase.resolution || '', version: testCase.version || '', url: testCase.url || '', assignedTo: testCase.assignedTo, stateDate: testCase.stateDate, endDate: testCase.endDate, }; } } // Export singleton instance export const aiTestCaseApi = new AITestCaseApi(); export default aiTestCaseApi;