|
| 1 | +# Generate Tests Workflow |
| 2 | + |
| 3 | +**Workflow File**: [`.github/workflows/generate-tests.yml`](../../.github/workflows/generate-tests.yml) |
| 4 | + |
| 5 | +This workflow analyzes commits for missing unit test coverage and automatically creates issues for Copilot to write the tests. |
| 6 | + |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +The Generate Tests workflow uses GitHub Copilot CLI to examine code changes and identify when new or modified code lacks corresponding unit tests. By automating test coverage analysis, teams can maintain high code quality without manual review overhead. |
| 11 | + |
| 12 | + |
| 13 | +## How It Works |
| 14 | + |
| 15 | +```mermaid |
| 16 | +flowchart TD |
| 17 | + A[Push to Repository] --> B{Source files changed?} |
| 18 | + B -->|No| C[Skip workflow] |
| 19 | + B -->|Yes| D[Install Copilot CLI] |
| 20 | + D --> E[Load analyze-tests prompt] |
| 21 | + E --> F[Copilot analyzes code changes] |
| 22 | + F --> G{Tests missing?} |
| 23 | + G -->|No| H[Exit - Coverage adequate] |
| 24 | + G -->|Yes| I[Create GitHub Issue] |
| 25 | + I --> J[Assign Copilot Coding Agent] |
| 26 | + J --> K[Agent writes missing tests] |
| 27 | + K --> L[PR created with tests] |
| 28 | +``` |
| 29 | + |
| 30 | +### Step-by-Step Process |
| 31 | + |
| 32 | +1. **Triggers on every push** (excluding non-source files) |
| 33 | +2. **Installs Copilot CLI** in the GitHub Actions runner |
| 34 | +3. **Loads the analyze-for-tests prompt** from [`.github/prompts/analyze-for-tests.prompt.md`](../../.github/prompts/analyze-for-tests.prompt.md) |
| 35 | +4. **Copilot checks if new code has corresponding tests** |
| 36 | +5. **If tests are missing** → Creates a GitHub issue and assigns Copilot |
| 37 | +6. **Copilot Coding Agent** then writes the missing tests |
| 38 | + |
| 39 | + |
| 40 | +## Criteria for Tests |
| 41 | + |
| 42 | +### ✅ Tests ARE Needed |
| 43 | + |
| 44 | +| Change Type | Example | |
| 45 | +|-------------|---------| |
| 46 | +| New Functions/Methods | `calculateDiscount()`, `validateOrder()` | |
| 47 | +| New Classes | `WarehouseService`, `OrderProcessor` | |
| 48 | +| Modified Business Logic | Changed calculation formulas, updated validation | |
| 49 | +| New API Endpoints | REST routes, GraphQL resolvers | |
| 50 | +| Error Handling Paths | Try/catch blocks, error conditions | |
| 51 | + |
| 52 | +### ❌ Tests NOT Needed |
| 53 | + |
| 54 | +| Change Type | Example | |
| 55 | +|-------------|---------| |
| 56 | +| Configuration Files | `tsconfig.json`, `package.json` | |
| 57 | +| Type Definitions Only | Interface declarations, type aliases | |
| 58 | +| Test Files Themselves | `*.test.ts`, `*.spec.ts` | |
| 59 | +| Documentation | README, comments, JSDoc | |
| 60 | +| Static Assets | Images, fonts, CSS | |
| 61 | + |
| 62 | + |
| 63 | +## Configuration |
| 64 | + |
| 65 | +### Trigger Configuration |
| 66 | + |
| 67 | +The workflow focuses on source code changes: |
| 68 | + |
| 69 | +```yaml |
| 70 | +on: |
| 71 | + push: |
| 72 | + paths: |
| 73 | + - 'api/src/**/*.ts' |
| 74 | + - 'frontend/src/**/*.ts' |
| 75 | + - 'frontend/src/**/*.tsx' |
| 76 | + paths-ignore: |
| 77 | + - '**/*.test.ts' |
| 78 | + - '**/*.spec.ts' |
| 79 | + - '**/*.d.ts' |
| 80 | +``` |
| 81 | +
|
| 82 | +### Required Secrets |
| 83 | +
|
| 84 | +| Secret | Description | |
| 85 | +|--------|-------------| |
| 86 | +| `COPILOT_CLI_TOKEN` | Personal Access Token with Copilot permissions | |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +## Prompt File |
| 91 | + |
| 92 | +The workflow uses a specialized prompt to guide Copilot's analysis: |
| 93 | + |
| 94 | +**Location**: [`.github/prompts/analyze-for-tests.prompt.md`](../../.github/prompts/analyze-for-tests.prompt.md) |
| 95 | + |
| 96 | +This prompt instructs Copilot to: |
| 97 | +- Analyze the git diff for testable code changes |
| 98 | +- Identify functions, classes, and methods without test coverage |
| 99 | +- Evaluate if existing tests cover the modified code paths |
| 100 | +- Create a detailed issue listing specific tests to write |
| 101 | + |
| 102 | + |
| 103 | +## Example Issue Created |
| 104 | + |
| 105 | +When the workflow detects missing tests, it creates an issue like: |
| 106 | + |
| 107 | +```markdown |
| 108 | +## 🧪 Unit Tests Needed |
| 109 | +
|
| 110 | +**Commit**: def5678 |
| 111 | +**Author**: @developer |
| 112 | +
|
| 113 | +### Code Requiring Tests |
| 114 | +
|
| 115 | +#### `api/src/routes/warehouse.ts` |
| 116 | +- [ ] `POST /` - Create warehouse endpoint |
| 117 | +- [ ] `GET /` - Get all warehouses |
| 118 | +- [ ] `GET /:id` - Get warehouse by ID |
| 119 | +- [ ] `GET /branch/:branchId` - Get warehouses by branch |
| 120 | +- [ ] `PUT /:id` - Update warehouse |
| 121 | +- [ ] `DELETE /:id` - Delete warehouse |
| 122 | + |
| 123 | +### Test File Location |
| 124 | +`api/src/routes/warehouse.test.ts` |
| 125 | + |
| 126 | +### Testing Framework |
| 127 | +Vitest with supertest for API testing |
| 128 | + |
| 129 | +/assign @copilot |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +## Test Patterns Used |
| 134 | + |
| 135 | +The Copilot Coding Agent follows established testing patterns in the codebase |
| 136 | + |
| 137 | +### API Route Tests |
| 138 | + |
| 139 | +```typescript |
| 140 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 141 | +import request from 'supertest'; |
| 142 | +import app from '../index'; |
| 143 | + |
| 144 | +describe('Warehouse Routes', () => { |
| 145 | + describe('GET /api/warehouses', () => { |
| 146 | + it('should return all warehouses', async () => { |
| 147 | + const response = await request(app).get('/api/warehouses'); |
| 148 | + expect(response.status).toBe(200); |
| 149 | + expect(Array.isArray(response.body)).toBe(true); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('GET /api/warehouses/:id', () => { |
| 154 | + it('should return 404 for non-existent warehouse', async () => { |
| 155 | + const response = await request(app).get('/api/warehouses/99999'); |
| 156 | + expect(response.status).toBe(404); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | + |
| 163 | +## Troubleshooting |
| 164 | + |
| 165 | +### Workflow Not Triggering |
| 166 | + |
| 167 | +- Verify the push includes files matching the `paths` patterns |
| 168 | +- Ensure changes are not exclusively in `paths-ignore` patterns |
| 169 | +- Check that the workflow file exists in the default branch |
| 170 | + |
| 171 | +### Copilot Not Detecting Missing Tests |
| 172 | + |
| 173 | +- Review the generate-tests prompt for coverage criteria |
| 174 | +- Ensure the code changes are substantial enough to warrant tests |
| 175 | +- Check workflow logs for the analysis output |
| 176 | + |
| 177 | +### Agent Writing Incorrect Tests |
| 178 | + |
| 179 | +- Verify existing test patterns in the codebase are consistent |
| 180 | +- Check that the testing framework is correctly configured |
| 181 | +- Review the prompt for framework-specific instructions |
| 182 | + |
| 183 | +### Tests Failing After Generation |
| 184 | + |
| 185 | +- Run tests locally to identify issues |
| 186 | +- Check for missing imports or dependencies |
| 187 | +- Verify mock data matches the expected schema |
0 commit comments