From f21d9cd6d457dec2ebc7a1a47cc63ebc48daa426 Mon Sep 17 00:00:00 2001 From: Yaojia Wang Date: Tue, 4 Nov 2025 10:28:10 +0100 Subject: [PATCH] feat(agents): Enforce mandatory testing in backend agent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update backend agent to enforce testing requirements: - Extended workflow from 8 to 9 steps with explicit test phases - Added CRITICAL Testing Rule: Must run dotnet test after every change - Never commit with failing tests or compilation errors - Updated Best Practices to emphasize testing (item 8) - Removed outdated TypeScript/NestJS examples - Updated Tech Stack to reflect actual .NET 9 stack - Simplified configuration for better clarity Changes: - Workflow step 6: "Run Tests: MUST run dotnet test - fix any failures" - Workflow step 7: "Git Commit: Auto-commit ONLY when all tests pass" - Added "CRITICAL Testing Rule" section after workflow - Removed Project Structure, Naming Conventions, Code Standards sections - Updated tech stack: C# + .NET 9 + ASP.NET Core + EF Core + PostgreSQL + MediatR + FluentValidation - Removed Example Flow section for brevity 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .claude/agents/backend.md | 152 +++++--------------------------------- 1 file changed, 17 insertions(+), 135 deletions(-) diff --git a/.claude/agents/backend.md b/.claude/agents/backend.md index 94502b8..b538303 100644 --- a/.claude/agents/backend.md +++ b/.claude/agents/backend.md @@ -42,12 +42,18 @@ Write high-quality, maintainable, testable backend code following best practices 2. Read: Existing code + architecture docs 3. Plan: Design approach (services, models, APIs) 4. Implement: Write/Edit code following standards -5. Test: Write tests, run test suite -6. Git Commit: Auto-commit changes with descriptive message -7. TodoWrite: Mark completed -8. Deliver: Working code + tests +5. Write Tests: Create/update unit and integration tests +6. Run Tests: MUST run dotnet test - fix any failures +7. Git Commit: Auto-commit ONLY when all tests pass +8. TodoWrite: Mark completed +9. Deliver: Working code + passing tests ``` +**CRITICAL Testing Rule:** +- After EVERY code change, run: `dotnet test` +- If tests fail or don't compile: Fix code OR tests, then re-run +- NEVER commit with failing tests + ## IMPORTANT: Git Commit Policy **After EVERY code change (service, API, model, test, or fix), you MUST automatically commit:** @@ -87,148 +93,24 @@ EOF - `perf(backend): Performance improvement` - Performance optimization - `db(backend): Database migration/change` - Database changes -**Example:** -```bash -git add src/services/issue.service.ts src/services/issue.service.spec.ts -git commit -m "$(cat <<'EOF' -feat(backend): Implement Issue CRUD service - -Add complete CRUD operations for Issue entity with validation. - -Changes: -- Created IssueService with create/read/update/delete methods -- Added Zod validation schemas -- Implemented unit tests with 90% coverage - -🤖 Generated with [Claude Code](https://claude.com/claude-code) - -Co-Authored-By: Claude -EOF -)" -``` - -## Project Structure (NestJS/TypeScript) - -``` -src/ -├── controllers/ # HTTP request handlers -├── services/ # Business logic layer -├── repositories/ # Data access layer -├── models/ # Data models/entities -├── dto/ # Data transfer objects -├── validators/ # Input validation -├── config/ # Configuration -└── mcp/ # MCP Server/Client -``` - -## Naming Conventions - -- Files: `kebab-case.ts` (e.g., `user-service.ts`) -- Classes: `PascalCase` (e.g., `UserService`) -- Functions/variables: `camelCase` (e.g., `getUserById`) -- Constants: `UPPER_SNAKE_CASE` (e.g., `MAX_RETRIES`) -- Interfaces: `IPascalCase` (e.g., `IUserRepository`) - -## Code Standards - -### Service Layer Example - -```typescript -@Injectable() -export class IssueService { - constructor( - @InjectRepository(Issue) - private readonly issueRepository: Repository, - private readonly auditService: AuditService, - ) {} - - async create(dto: CreateIssueDto, userId: string): Promise { - // 1. Validate - const validated = CreateIssueSchema.parse(dto); - - // 2. Create entity - const issue = this.issueRepository.create({ - ...validated, - createdBy: userId, - }); - - // 3. Save - const saved = await this.issueRepository.save(issue); - - // 4. Audit log - await this.auditService.log({ - entityType: 'Issue', - entityId: saved.id, - action: 'CREATE', - userId, - changes: dto, - }); - - return saved; - } -} -``` - -### Data Validation (Zod) - -```typescript -export const CreateIssueSchema = z.object({ - title: z.string().min(1).max(200), - description: z.string().optional(), - priority: z.enum(['low', 'medium', 'high', 'urgent']), - assigneeId: z.string().uuid().optional(), -}); - -export type CreateIssueDto = z.infer; -``` - -### Testing Example - -```typescript -describe('IssueService', () => { - let service: IssueService; - - it('should create an issue', async () => { - const dto = { title: 'Test', priority: 'high' }; - const result = await service.create(dto, 'user-1'); - - expect(result.id).toBeDefined(); - expect(result.title).toBe('Test'); - }); -}); -``` ## IMPORTANT: Best Practices 1. **Dependency Injection**: Use DI for testability 2. **Single Responsibility**: Each class/function does one thing -3. **Input Validation**: Validate at boundary (DTO) -4. **Error Handling**: Use custom error classes + global handler +3. **Input Validation**: Use FluentValidation at boundary +4. **Error Handling**: Use custom exceptions + global handler 5. **Logging**: Log important operations and errors 6. **Security**: Parameterized queries, input sanitization, permission checks 7. **Performance**: Use indexes, avoid N+1 queries, cache when appropriate -8. **Use TodoWrite**: Track ALL coding tasks -9. **Read before Edit**: Always read existing code before modifying +8. **Testing**: Write tests BEFORE committing - Run `dotnet test` - Fix ALL failures - NEVER commit broken tests +9. **Use TodoWrite**: Track ALL coding tasks including test runs +10. **Read before Edit**: Always read existing code before modifying ## Tech Stack -- TypeScript + NestJS + TypeORM + PostgreSQL + Redis - -## Example Flow - -``` -Coordinator: "Implement Issue CRUD APIs" - -Your Response: -1. TodoWrite: Create tasks (model, service, controller, tests) -2. Read: Existing project structure -3. Implement: Issue entity, service, controller -4. Test: Write unit + integration tests -5. Run: npm test -6. TodoWrite: Mark completed -7. Deliver: Working APIs with 80%+ test coverage -``` +- C# + .NET 9 + ASP.NET Core + EF Core + PostgreSQL + MediatR + FluentValidation --- -**Remember**: Code quality matters. Write clean, testable, maintainable code. Test everything. Document complex logic. +**Remember**: Code quality matters. Write clean, testable, maintainable code. Test everything. NEVER commit failing tests.