Split monolithic chat service into modular structure with Matrix client support, extract web server into per-resource route modules, introduce bootstrap/main-loop separation, and add comprehensive vitest coverage across services, repositories, and utilities. Adds project docs and CLAUDE.md agent instructions.
3.6 KiB
3.6 KiB
Agent Guidelines & Code Standards
Refactoring Goal
Refactor code to improve maintainability, testability, and clarity. Break down large components into smaller, single-purpose classes/modules.
Engineering Standards
1. Single Responsibility Principle (SRP)
- Avoid "God Classes" (e.g., massive
Serviceclasses that do everything). - Extract logic into dedicated handlers, managers, or utilities.
- Example: Instead of one
BotServicehandling login, monitoring, and messaging, split intoAuthService,MonitorService, andMessagingService.
2. Design Patterns
- Dependency Injection: Pass dependencies via constructors. This makes swapping implementations (e.g., for testing) easier.
- Factory Pattern: Use factories for creating complex objects (e.g., configuring different AI models).
- Strategy Pattern: Good for interchangeable algorithms (e.g., differentsubreddit selection strategies).
3. TypeScript Best Practices
- Strict Typing: No
any. Define interfaces/types for all data structures (especially API/Reddit responses). - Zod Validation: Use
zod(already in deps) to validate external data (config files, API responses). - Async/Await: properly handle promises. Use
p-queueorp-retryfor rate-limited operations.
4. Test-Driven Development (TDD) — REQUIRED
All new functionality and bug fixes MUST follow the red-green-refactor cycle. This is non-negotiable.
- Red: Write a failing test FIRST that describes the desired behavior. Run it and confirm it fails for the expected reason.
- Green: Write the minimum production code needed to make the test pass. No extra features, no speculative abstractions.
- Refactor: With tests green, clean up implementation and tests. Re-run the suite after each change.
Rules:
- Never commit production code without an accompanying test that fails without it.
- Tests live in
tests/(mirroringsrc/structure) and run viavitest(npm test). - Use dependency injection so collaborators (DB, HTTP, browser, clock) can be faked — do not mock what you don't own; wrap third-party APIs in a thin interface and fake the interface.
- Prefer behavior-focused tests (inputs → outputs / observable side effects) over tests that pin internal structure.
- Bug fixes start with a regression test that reproduces the bug.
- If a change is genuinely untestable (e.g. a one-line config tweak), state that explicitly in the PR/commit rather than skipping silently.
5. Project Specifics
- Tech Stack: Node.js, TypeScript, Playwright, Express, SQLite, Vitest.
- Directories:
src/services/for business logic.src/web/for Express server and UI.src/browser/for Playwright interactions.tests/for vitest specs, mirroringsrc/layout.
- Logging: Use
winstonfor structured logging. Do not useconsole.login production code.
Workflow for Agents
- Analyze: Before writing code, read the file and understand the context.
- Plan: Identify what needs to change. Check if the change violates SRP. Identify the test(s) that will drive the change.
- Write failing test (Red): Add a vitest spec that captures the new behavior or reproduces the bug. Run
npm testand confirm it fails for the right reason. - Implement (Green): Write the smallest change that makes the test pass. Create new small files when SRP calls for it.
- Refactor: Tidy the implementation and the test with the suite green. Re-run tests after each refactor step.
- Verify: Ensure the full suite passes (
npm test) and types check (npm run buildortsc --noEmit).