Docs /features/test-generation

Test Generation

AI-powered test generation (Planned Feature)

📋 Overview

Test Generation uses AI models (OpenAI GPT-4, Anthropic Claude) to automatically generate high-quality tests for your codebase. It leverages repository analysis results to understand context and generate appropriate unit, integration, and E2E tests.

🎯 Current Status

Status: 📋 Planned (Phase 2)

This feature is in the planning stage. The infrastructure is in place (AI service, analysis pipeline), but test generation logic is not yet implemented.

🏗️ Planned Architecture

Test Generation Flow

Repository Analysis
      ↓
Test Candidate Selection
      ↓
Context Gathering
      ↓
AI Prompt Generation
      ↓
Test Generation (AI)
      ↓
Test Validation & Formatting
      ↓
Save Generated Tests

Database Schema (Planned)

generatedTests table:

{
  id: string
  projectId: string
  sourceFile: string
  testFile: string
  testType: 'unit' | 'integration' | 'e2e'
  testFramework: string
  content: string
  status: 'draft' | 'approved' | 'rejected'
  aiModel: string
  generatedAt: Date
  approvedBy: string?
  approvedAt: Date?
}

🔌 API Reference (Planned)

Generate Tests

// Planned endpoint
const result = await trpc.tests.generateTests.mutate({
  projectId: 'proj_123',
  sourceFiles: ['src/utils/validation.ts'],
  testTypes: ['unit', 'integration'],
  options: {
    framework: 'vitest',
    coverage: 'comprehensive',
    includeEdgeCases: true
  }
})

// Expected response
{
  success: true,
  tests: [
    {
      id: 'test_456',
      sourceFile: 'src/utils/validation.ts',
      testFile: 'src/utils/__tests__/validation.test.ts',
      testType: 'unit',
      content: '// Generated test code...',
      stats: {
        testCases: 12,
        edgeCases: 3,
        mockCount: 2
      }
    }
  ]
}

Generate for Entire Project

await trpc.tests.generateProjectTests.mutate({
  projectId: 'proj_123',
  options: {
    priorityOnly: true,  // Only high-priority candidates
    maxFiles: 50,
    batchSize: 5
  }
})

🤖 AI Integration

Prompt Engineering

Unit Test Prompt Template:

const unitTestPrompt = `
Generate comprehensive unit tests for the following ${language} code.

File: ${filePath}
Framework: ${testFramework}
Detected patterns: ${patterns.join(', ')}

Code:
\`\`\`${language}
${sourceCode}
\`\`\`

Requirements:
- Use ${testFramework} syntax
- Test happy paths and edge cases
- Include proper mocking for dependencies
- Use descriptive test names
- Add comments explaining complex tests

Dependencies available:
${dependencies.join(', ')}

Generate tests that follow ${framework} best practices.
`

AI Provider Configuration

// In project settings
{
  aiProvider: 'openai' | 'anthropic' | 'cohere',
  aiModel: 'gpt-4' | 'claude-3-opus-20240229',
  temperature: 0.7,
  maxTokens: 4000
}

🎨 Frontend Implementation (Planned)

Test Generation UI

export function GenerateTestsButton({ projectId, sourceFile }: Props) {
  const generate = trpc.tests.generateTests.useMutation()

  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button>
          <SparklesIcon /> Generate Tests
        </Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Generate Tests</DialogTitle>
          <DialogDescription>
            Configure test generation for {sourceFile}
          </DialogDescription>
        </DialogHeader>
        
        <div className="space-y-4">
          <Select name="testType">
            <SelectTrigger>
              <SelectValue placeholder="Test type" />
            </SelectTrigger>
            <SelectContent>
              <SelectItem value="unit">Unit Tests</SelectItem>
              <SelectItem value="integration">Integration Tests</SelectItem>
              <SelectItem value="e2e">E2E Tests</SelectItem>
            </SelectContent>
          </Select>
          
          <Select name="framework">
            <SelectTrigger>
              <SelectValue placeholder="Test framework" />
            </SelectTrigger>
            <SelectContent>
              <SelectItem value="vitest">Vitest</SelectItem>
              <SelectItem value="jest">Jest</SelectItem>
              <SelectItem value="playwright">Playwright</SelectItem>
            </SelectContent>
          </Select>
          
          <div className="flex items-center space-x-2">
            <Checkbox id="edgeCases" />
            <label htmlFor="edgeCases">Include edge cases</label>
          </div>
        </div>
        
        <DialogFooter>
          <Button onClick={handleGenerate}>
            Generate
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

📊 Test Quality Metrics (Planned)

Generated Test Scoring

interface TestQualityMetrics {
  coverage: number       // % of code paths covered
  assertions: number     // Number of assertions
  edgeCases: number      // Edge cases tested
  mockQuality: number    // Quality of mocks (1-10)
  readability: number    // Code readability score (1-10)
  maintainability: number // Maintainability score (1-10)
}

🔗 Related Documentation


Next: Test Execution

Reading this with an agent? /docs/features/test-generation.md serves the raw markdown.

All docs