|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Common Development Commands |
| 6 | + |
| 7 | +### Testing |
| 8 | +- **Run all tests with coverage**: `npm test` |
| 9 | +- **Run a specific test file**: `NODE_ENV=testing mocha './test/limit.test.js'` |
| 10 | +- **Run tests matching a pattern**: `NODE_ENV=testing mocha './test/**/*.test.js' --grep "MaxLimit"` |
| 11 | +- **Run tests with coverage report**: `NODE_ENV=testing c8 --all --reporter text --reporter cobertura mocha './test/**/*.test.js'` |
| 12 | + |
| 13 | +### Linting |
| 14 | +- **Run ESLint**: `npm run lint` |
| 15 | +- **Fix linting issues**: `npm run lint -- --fix` |
| 16 | + |
| 17 | +### Development |
| 18 | +- **Note**: There is no dev script currently implemented (placeholder exists) |
| 19 | + |
| 20 | +## High-Level Architecture |
| 21 | + |
| 22 | +The limit-service is a centralized limit enforcement system for Ghost that follows clean architecture principles: |
| 23 | + |
| 24 | +### Core Components |
| 25 | + |
| 26 | +1. **LimitService** (`lib/LimitService.js`): Main service class that acts as a facade for all limit operations. It creates and manages different limit types based on configuration. |
| 27 | + |
| 28 | +2. **Limit Types** (`lib/limit.js`): |
| 29 | + - **MaxLimit**: Enforces maximum counts (e.g., max 5 staff users) |
| 30 | + - **MaxPeriodicLimit**: Enforces limits over time periods (e.g., max emails per month) |
| 31 | + - **FlagLimit**: On/off feature toggles |
| 32 | + - **AllowlistLimit**: Restricts values to an allowed list |
| 33 | + |
| 34 | +3. **Configuration** (`lib/config.js`): Defines supported limits and their properties. Each limit can specify: |
| 35 | + - `type`: The limit type to use |
| 36 | + - `fallbackErrorMessage`: Default error message |
| 37 | + - `currentCountQuery`: Database query function for count-based limits |
| 38 | + |
| 39 | +### Key Architectural Patterns |
| 40 | + |
| 41 | +- **Strategy Pattern**: Different limit types implement a common interface (`checkIsOverLimit`, `checkWouldGoOverLimit`) |
| 42 | +- **Dependency Injection**: Database connection, errors handler, and configuration are injected at initialization |
| 43 | +- **Transaction Support**: All database operations can be wrapped in transactions via `options.transacting` |
| 44 | + |
| 45 | +### Adding New Limits |
| 46 | + |
| 47 | +1. Add the limit configuration in `lib/config.js`: |
| 48 | + ```javascript |
| 49 | + newFeature: { |
| 50 | + type: 'max', // or 'flag', 'allowlist' |
| 51 | + fallbackErrorMessage: 'Default error for {{name}} limit', |
| 52 | + currentCountQuery: async (db, options) => { |
| 53 | + // Return current count from database |
| 54 | + } |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | +2. Test the new limit following existing patterns in `test/` |
| 59 | + |
| 60 | +### Testing Approach |
| 61 | + |
| 62 | +- Uses Mocha with Should.js for assertions |
| 63 | +- Sinon for mocking database queries and date/time |
| 64 | +- Tests focus on behavior, not implementation |
| 65 | +- Mock database responses to test limit logic in isolation |
| 66 | + |
| 67 | +### Database Integration |
| 68 | + |
| 69 | +- Expects a Knex instance for database queries |
| 70 | +- All queries support transactions |
| 71 | +- Count queries should return a number or be convertible to a number |
| 72 | +- For periodic limits, queries receive `startDate` and `endDate` parameters |
| 73 | + |
| 74 | +### Error Handling |
| 75 | + |
| 76 | +- Uses `@tryghost/errors` for consistent error formatting |
| 77 | +- Supports template variables in error messages: `{{max}}`, `{{count}}`, `{{name}}` |
| 78 | +- All limits have fallback error messages |
| 79 | +- Numbers in error messages are formatted with toLocaleString() |
| 80 | + |
| 81 | +### Key Methods Flow |
| 82 | + |
| 83 | +1. `loadLimits()`: Initializes the service with configuration |
| 84 | +2. `isLimited()`: Checks if a limit is configured |
| 85 | +3. `errorIfWouldGoOverLimit()`: Throws if action would exceed limit |
| 86 | +4. `errorIfIsOverLimit()`: Throws if already over limit |
| 87 | +5. `checkIsOverLimit()`: Returns boolean for limit status |
| 88 | +6. `checkWouldGoOverLimit()`: Returns boolean for potential limit breach |
| 89 | + |
| 90 | +### Environment Variable |
| 91 | + |
| 92 | +- Set `NODE_ENV=testing` when running tests |
0 commit comments