|
| 1 | +--- |
| 2 | +name: aegis-tdd-example |
| 3 | +description: Aegis AI project-specific TDD examples and patterns. Use when working on this project to follow established patterns for handlers, services, and core modules with 99%+ coverage standards. |
| 4 | +allowed-tools: Read, Write, Edit, Bash, Grep, Glob |
| 5 | +--- |
| 6 | + |
| 7 | +# 🛡️ Aegis TDD Example - Project Patterns |
| 8 | + |
| 9 | +## Purpose |
| 10 | + |
| 11 | +Provide Aegis AI project-specific TDD patterns and examples. |
| 12 | + |
| 13 | +**Project Context**: 99.33% coverage, 105 tests, LEGENDARY level 👑 |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🎯 Project-Specific Standards |
| 18 | + |
| 19 | +### Coverage Requirements |
| 20 | + |
| 21 | +- **Core modules**: 95%+ (currently 98.81%) |
| 22 | +- **Services**: 95%+ (currently 100%) |
| 23 | +- **MCP**: 100% (currently 100%) |
| 24 | + |
| 25 | +### Test Count |
| 26 | + |
| 27 | +- Minimum 10 tests per module |
| 28 | +- Edge cases mandatory |
| 29 | +- Error handling 100% covered |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 📋 Established Patterns |
| 34 | + |
| 35 | +### Pattern 1: Core Module (handlerLoader example) |
| 36 | + |
| 37 | +**Test Structure**: |
| 38 | +```javascript |
| 39 | +describe('HandlerLoader', () => { |
| 40 | + let instance; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + instance = require('../../src/core/handlerLoader'); |
| 44 | + instance.clearHandlers(); // Isolation |
| 45 | + }); |
| 46 | + |
| 47 | + // Use real fixtures instead of mocks |
| 48 | + test('loads handlers from directory', () => { |
| 49 | + const fixturesPath = path.join(__dirname, '../fixtures/handlers-valid'); |
| 50 | + instance.loadHandlers(fixturesPath); |
| 51 | + expect(instance.listHandlers()).toContain('handlerA'); |
| 52 | + }); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +**Key Points**: |
| 57 | +- ✅ Use `clearHandlers()` for isolation |
| 58 | +- ✅ Real fixtures over complex mocks |
| 59 | +- ✅ Test actual file system operations |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +### Pattern 2: Service with Mocking (geminiService example) |
| 64 | + |
| 65 | +**Mocking Strategy**: |
| 66 | +```javascript |
| 67 | +const { spawn } = require('child_process'); |
| 68 | +jest.mock('child_process'); |
| 69 | + |
| 70 | +describe('GeminiService', () => { |
| 71 | + let mockProcess; |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + mockProcess = { |
| 75 | + stdout: { on: jest.fn(), setEncoding: jest.fn() }, |
| 76 | + stderr: { on: jest.fn() }, |
| 77 | + on: jest.fn(), |
| 78 | + kill: jest.fn(), // Important for timeout tests |
| 79 | + }; |
| 80 | + spawn.mockReturnValue(mockProcess); |
| 81 | + }); |
| 82 | + |
| 83 | + test('handles timeout correctly', async () => { |
| 84 | + mockProcess.on.mockImplementation(() => {}); // No close event |
| 85 | + |
| 86 | + await expect(service.execute('test', { timeout: 100 })) |
| 87 | + .rejects.toThrow('timeout'); |
| 88 | + expect(mockProcess.kill).toHaveBeenCalled(); |
| 89 | + }); |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +**Key Points**: |
| 94 | +- ✅ Mock `child_process.spawn` |
| 95 | +- ✅ Include `kill` for timeout tests |
| 96 | +- ✅ Test error paths thoroughly |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### Pattern 3: MCP Protocol (index.js example) |
| 101 | + |
| 102 | +**JSON-RPC Testing**: |
| 103 | +```javascript |
| 104 | +describe('MCP Server', () => { |
| 105 | + test('handles tools/list request', async () => { |
| 106 | + const request = { |
| 107 | + jsonrpc: '2.0', |
| 108 | + method: 'tools/list', |
| 109 | + id: 1, |
| 110 | + }; |
| 111 | + |
| 112 | + const response = await server.handleRequest(request); |
| 113 | + |
| 114 | + expect(response.jsonrpc).toBe('2.0'); |
| 115 | + expect(response.id).toBe(1); |
| 116 | + expect(response.result).toHaveProperty('tools'); |
| 117 | + }); |
| 118 | + |
| 119 | + test('returns error for invalid jsonrpc version', async () => { |
| 120 | + const response = await server.handleRequest({ |
| 121 | + jsonrpc: '1.0', |
| 122 | + method: 'tools/list', |
| 123 | + id: 1, |
| 124 | + }); |
| 125 | + |
| 126 | + expect(response.error.code).toBe(-32600); |
| 127 | + }); |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +**Key Points**: |
| 132 | +- ✅ Test all error codes (-32600, -32601, -32602, -32603, -32700) |
| 133 | +- ✅ Verify JSON-RPC 2.0 compliance |
| 134 | +- ✅ 100% coverage mandatory |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## 🧪 Project Test Fixtures |
| 139 | + |
| 140 | +**Location**: `tests/fixtures/` |
| 141 | + |
| 142 | +**Example**: |
| 143 | +``` |
| 144 | +tests/fixtures/ |
| 145 | +├── handlers-valid/ |
| 146 | +│ ├── handlerA.js |
| 147 | +│ └── handlerB.js |
| 148 | +└── handlers-mixed/ |
| 149 | + ├── handlerC.js |
| 150 | + ├── config.json # Should be ignored |
| 151 | + └── README.md # Should be ignored |
| 152 | +``` |
| 153 | + |
| 154 | +**Usage**: |
| 155 | +```javascript |
| 156 | +const fixturesPath = path.join(__dirname, '../fixtures/handlers-valid'); |
| 157 | +handlerLoader.loadHandlers(fixturesPath); |
| 158 | +``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 🎮 Project Tools |
| 163 | + |
| 164 | +### Dashboard |
| 165 | + |
| 166 | +```bash |
| 167 | +npm run dashboard |
| 168 | +``` |
| 169 | + |
| 170 | +Shows: |
| 171 | +- Current coverage by module |
| 172 | +- TDD level (LEGENDARY 👑) |
| 173 | +- Progress bars |
| 174 | +- Achievement percentage |
| 175 | + |
| 176 | +### Test Generator |
| 177 | + |
| 178 | +```bash |
| 179 | +npm run generate-test <moduleName> <category> |
| 180 | + |
| 181 | +# Example |
| 182 | +npm run generate-test authHandler handlers |
| 183 | +``` |
| 184 | + |
| 185 | +Generates: |
| 186 | +- Test file with template |
| 187 | +- Source file template |
| 188 | +- TDD workflow guidance |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## 📊 Achieved Results |
| 193 | + |
| 194 | +**Coverage by Layer**: |
| 195 | +- Core: 98.81% (5 modules) |
| 196 | +- Services: 100% (3 services) 🏆 |
| 197 | +- MCP: 100% (index.js) 🏆 |
| 198 | + |
| 199 | +**100% Coverage Modules** (7): |
| 200 | +1. index.js |
| 201 | +2. stageOrchestrator.js |
| 202 | +3. stateManager.js |
| 203 | +4. workflowEngine.js |
| 204 | +5. geminiService.js |
| 205 | +6. qwenService.js |
| 206 | +7. codexService.js |
| 207 | + |
| 208 | +--- |
| 209 | + |
| 210 | +## 💡 When to Use This Skill |
| 211 | + |
| 212 | +**Auto-activate when user**: |
| 213 | +- Asks to "create tests" |
| 214 | +- Mentions "TDD" or "test-driven" |
| 215 | +- Wants to add a new module/service |
| 216 | +- Asks about test coverage |
| 217 | +- Mentions "follow Aegis AI pattern" |
| 218 | + |
| 219 | +**Steps**: |
| 220 | +1. Identify module category (core/services/handlers) |
| 221 | +2. Use appropriate pattern from this skill |
| 222 | +3. Follow TDD cycle |
| 223 | +4. Aim for 95%+ coverage |
| 224 | +5. Use project tools (dashboard, generate-test) |
| 225 | + |
| 226 | +--- |
| 227 | + |
| 228 | +## 🏆 Success Criteria |
| 229 | + |
| 230 | +- [ ] Tests written before implementation |
| 231 | +- [ ] Coverage 95%+ |
| 232 | +- [ ] All tests passing |
| 233 | +- [ ] Edge cases covered |
| 234 | +- [ ] Mocking strategy appropriate |
| 235 | +- [ ] JSDoc complete |
| 236 | +- [ ] CI/CD passing |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +## 📚 Reference Files |
| 241 | + |
| 242 | +**Within this project**: |
| 243 | +- Examples: All files in `tests/` |
| 244 | +- Patterns: `src/core/`, `src/services/` |
| 245 | +- Documentation: `docs/TDD_POLICY.md` |
| 246 | +- Benefits: `docs/PRACTICAL_BENEFITS.md` |
| 247 | + |
| 248 | +**Commands**: |
| 249 | +```bash |
| 250 | +npm test # Run tests |
| 251 | +npm run test:coverage # Coverage report |
| 252 | +npm run dashboard # Visual dashboard |
| 253 | +npm run generate-test # Generate new test |
| 254 | +``` |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +**Level**: 👑 LEGENDARY (99.33% coverage) |
| 259 | +**Tests**: 105 (100% pass) |
| 260 | +**Status**: Production Ready ✅ |
0 commit comments