|
| 1 | +# AROCAPI Project Guidelines |
| 2 | + |
| 3 | +This document provides guidance for working with the AROCAPI project using Claude Code. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +AROCAPI is a TypeScript/Fastify API for RO-Crate data management with: |
| 8 | + |
| 9 | +- **Framework**: Fastify with TypeScript |
| 10 | +- **Database**: MySQL with Prisma ORM |
| 11 | +- **Search**: OpenSearch integration |
| 12 | +- **Testing**: Vitest with 95% coverage requirement |
| 13 | + |
| 14 | +## Code Style |
| 15 | + |
| 16 | +### General |
| 17 | + |
| 18 | +- Use 2 spaces for indentation |
| 19 | +- Prefer single quotes for strings, double quotes for HTML attributes |
| 20 | +- Prefer US spelling (e.g., color, organize) |
| 21 | +- Use ES modules (import/export) |
| 22 | + |
| 23 | +### TypeScript |
| 24 | + |
| 25 | +- Use strict TypeScript configuration |
| 26 | +- Prefer arrow functions for component definitions |
| 27 | +- Use Fastify's type providers for route typing |
| 28 | +- Prefer forEach/map/filter/reduce over for loops |
| 29 | +- Add newlines before return and process.exit |
| 30 | + |
| 31 | +### Dependencies |
| 32 | + |
| 33 | +- Use `pnpm` for package management |
| 34 | +- Never edit package.json scripts directly, use pnpm commands |
| 35 | +- Use zod v4 for validation |
| 36 | +- Follow Fastify conventions for plugins and routes |
| 37 | + |
| 38 | +## Testing Guidelines |
| 39 | + |
| 40 | +### Testing Strategy |
| 41 | + |
| 42 | +The project uses a comprehensive testing approach with: |
| 43 | + |
| 44 | +- **Unit tests**: Fast, isolated tests with mocked dependencies |
| 45 | +- **Integration tests**: Full API tests with real database/OpenSearch |
| 46 | +- **OpenAPI validation**: Ensures API contract compliance |
| 47 | +- **95% coverage requirement**: Enforced in CI pipeline |
| 48 | + |
| 49 | +### Test Commands |
| 50 | + |
| 51 | +```bash |
| 52 | +pnpm run test # Run all tests |
| 53 | +pnpm run test:watch # Run in watch mode |
| 54 | +pnpm run test:ui # Interactive UI |
| 55 | +pnpm run test:coverage # With coverage report |
| 56 | +``` |
| 57 | + |
| 58 | +### Test Environment |
| 59 | + |
| 60 | +- **Database**: localhost:3307 (catalog_test) |
| 61 | +- **OpenSearch**: localhost:9201 |
| 62 | +- **Services**: Use docker-compose.test.yml |
| 63 | +- **Setup**: Automated via src/test/integration.setup.ts |
| 64 | + |
| 65 | +### Writing Tests |
| 66 | + |
| 67 | +1. **Unit tests**: Place in `src/**/*.test.ts` alongside source |
| 68 | +2. **Integration tests**: Use `src/test/integration.test.ts` |
| 69 | +3. **Mock external dependencies** in unit tests |
| 70 | +4. **Use real services** in integration tests with cleanup |
| 71 | +5. **Test both success and error paths** |
| 72 | +6. **Use snapshots** for complex response validation |
| 73 | + |
| 74 | +### Coverage Requirements |
| 75 | + |
| 76 | +- Minimum 95% for lines, functions, branches, statements |
| 77 | +- Excluded: generated code, dist, example, config files |
| 78 | +- CI pipeline fails if coverage drops below threshold |
| 79 | +- View reports: `open coverage/index.html` |
| 80 | + |
| 81 | +## Development Workflow |
| 82 | + |
| 83 | +### Local Development |
| 84 | + |
| 85 | +1. Install dependencies: `pnpm install` |
| 86 | +2. Start services: `docker compose up -d` |
| 87 | +3. Generate Prisma client: `pnpm run generate` |
| 88 | +4. Start development: `pnpm run dev` |
| 89 | + |
| 90 | +### Before Committing |
| 91 | + |
| 92 | +1. Run linting: `pnpm run lint:biome && pnpm run lint:types` |
| 93 | +2. Run tests: `pnpm run test` |
| 94 | +3. Check coverage: `pnpm run test:coverage` |
| 95 | +4. Ensure all checks pass |
| 96 | + |
| 97 | +### CI/CD Pipeline |
| 98 | + |
| 99 | +- **Triggers**: Pull requests to main branch |
| 100 | +- **Services**: MySQL and OpenSearch via GitHub Actions |
| 101 | +- **Checks**: Linting, type checking, tests, coverage |
| 102 | +- **Requirements**: All checks must pass, 95% coverage minimum |
| 103 | + |
| 104 | +## Project Structure |
| 105 | + |
| 106 | +``` |
| 107 | +src/ |
| 108 | +├── app.ts # Main Fastify application |
| 109 | +├── routes/ # API route handlers |
| 110 | +│ ├── entity.ts # Single entity operations |
| 111 | +│ ├── entities.ts # Entity listing/filtering |
| 112 | +│ └── search.ts # OpenSearch operations |
| 113 | +├── utils/ # Utility functions |
| 114 | +│ └── errors.ts # Error handling helpers |
| 115 | +├── generated/ # Prisma generated files (excluded from tests) |
| 116 | +└── test/ # Integration test setup |
| 117 | + ├── integration.setup.ts |
| 118 | + ├── integration.test.ts |
| 119 | + └── openapi.test.ts |
| 120 | +``` |
| 121 | + |
| 122 | +## Key Dependencies |
| 123 | + |
| 124 | +### Runtime |
| 125 | + |
| 126 | +- **fastify**: Web framework |
| 127 | +- **@prisma/client**: Database ORM |
| 128 | +- **@opensearch-project/opensearch**: Search client |
| 129 | +- **zod**: Schema validation (v4) |
| 130 | +- **fastify-type-provider-zod**: Fastify Zod integration |
| 131 | + |
| 132 | +### Development |
| 133 | + |
| 134 | +- **vitest**: Testing framework |
| 135 | +- **@vitest/coverage-v8**: Coverage reporting |
| 136 | +- **typescript**: Type checking |
| 137 | +- **@biomejs/biome**: Linting and formatting |
| 138 | +- **openapi-backend**: OpenAPI validation |
| 139 | +- **prisma**: Database toolkit |
| 140 | + |
| 141 | +## API Design |
| 142 | + |
| 143 | +### Route Structure |
| 144 | + |
| 145 | +- `GET /entity/:id` - Retrieve single entity |
| 146 | +- `GET /entities` - List/filter entities with pagination |
| 147 | +- `POST /search` - OpenSearch queries with faceting |
| 148 | + |
| 149 | +### Error Handling |
| 150 | + |
| 151 | +- Use consistent error response format |
| 152 | +- Implement proper HTTP status codes |
| 153 | +- Log errors appropriately |
| 154 | +- Use utility functions from `src/utils/errors.ts` |
| 155 | + |
| 156 | +### Validation |
| 157 | + |
| 158 | +- Use Zod schemas for request/response validation |
| 159 | +- Leverage Fastify's type providers |
| 160 | +- Validate against OpenAPI specification |
| 161 | +- Handle validation errors gracefully |
| 162 | + |
| 163 | +## Database Management |
| 164 | + |
| 165 | +### Prisma Operations |
| 166 | + |
| 167 | +```bash |
| 168 | +pnpm run generate # Generate client |
| 169 | +pnpm run db:migrate # Run migrations |
| 170 | +pnpm run dbconsole # Access database console |
| 171 | +``` |
| 172 | + |
| 173 | +### Test Database |
| 174 | + |
| 175 | +- Separate instance on port 3307 |
| 176 | +- Automated setup/cleanup in tests |
| 177 | +- Uses test-specific environment variables |
| 178 | + |
| 179 | +## OpenSearch Integration |
| 180 | + |
| 181 | +### Development |
| 182 | + |
| 183 | +- Service runs on localhost:9200 |
| 184 | +- Test instance on localhost:9201 |
| 185 | +- Index management handled in application |
| 186 | + |
| 187 | +### Search Features |
| 188 | + |
| 189 | +- Basic and advanced query modes |
| 190 | +- Faceted search with aggregations |
| 191 | +- Geospatial search capabilities |
| 192 | +- Highlighting and scoring |
| 193 | + |
| 194 | +## Troubleshooting |
| 195 | + |
| 196 | +### Common Issues |
| 197 | + |
| 198 | +1. **Test timeouts**: Check service health, increase timeouts |
| 199 | +2. **Database errors**: Verify connections, run migrations |
| 200 | +3. **Coverage failures**: Check excluded files, add tests |
| 201 | +4. **OpenSearch errors**: Verify service status, check mappings |
| 202 | + |
| 203 | +## Best Practices |
| 204 | + |
| 205 | +### Code Quality |
| 206 | + |
| 207 | +1. Follow TypeScript strict mode |
| 208 | +2. Use proper error handling |
| 209 | +3. Implement comprehensive logging |
| 210 | +4. Write self-documenting code |
| 211 | +5. Follow established patterns |
| 212 | + |
| 213 | +### Testing |
| 214 | + |
| 215 | +1. Maintain high test coverage (95%+) |
| 216 | +2. Test both happy and error paths |
| 217 | +3. Use appropriate test types (unit vs integration) |
| 218 | +4. Keep tests fast and reliable |
| 219 | +5. Clean up test data properly |
| 220 | + |
| 221 | +### Performance |
| 222 | + |
| 223 | +1. Use appropriate database queries |
| 224 | +2. Implement proper caching strategies |
| 225 | +3. Monitor OpenSearch performance |
| 226 | +4. Optimize for common use cases |
| 227 | +5. Profile and measure improvements |
| 228 | + |
| 229 | +### Security |
| 230 | + |
| 231 | +1. Validate all inputs |
| 232 | +2. Use parameterized queries |
| 233 | +3. Implement proper authentication/authorization |
| 234 | +4. Log security events |
| 235 | +5. Keep dependencies updated |
| 236 | + |
| 237 | +--- |
| 238 | + |
| 239 | +This document should be updated as the project evolves and new patterns emerge. |
| 240 | + |
0 commit comments