This PR implements automated contract tests that verify important listener API responses maintain their expected structure. This addresses approximately 2% of the overall API contract testing requirement as requested.
File: listener/src/api/__tests__/api-contract.test.ts
A comprehensive test suite with 14 passing tests covering:
- ✅ GET /health - Returns expected structure with all required fields
- ✅ GET /health - Includes tracking headers (X-Request-Id, X-Correlation-Id)
- ✅ GET /api/status - Returns expected structure with timestamp and contracts
- ✅ GET /api/status - Contract status includes address and paused fields
- ✅ GET /api/indexing/health - Returns indexing status with all required fields
- ✅ GET /api/events - Returns expected structure (success, data wrapper)
- ✅ GET /api/events - Event objects have all required fields with correct types
- ✅ GET /api/events - Respects limit query parameter
- ✅ POST /api/notifications/validate-batch - Valid batch returns success structure
- ✅ POST /api/notifications/validate-batch - Invalid batch returns error details
- ✅ API versioning - Accepts /api/v1/* routes
- ✅ API versioning - Includes X-API-Version header on all responses
- ✅ CORS - Handles OPTIONS requests correctly
- ✅ Error handling - Returns 404 for unknown routes
File: listener/src/utils/request-id.ts
Fixed a syntax error where a closing brace was missing in the generateCorrelationId() function, which was preventing the code from compiling.
File: listener/src/api/__tests__/README.md
Created comprehensive documentation including:
- Overview of contract testing purpose
- Current test coverage details
- Instructions for running tests
- Response format documentation
- Roadmap for future expansion (remaining 98%)
- Contributing guidelines
Test Suites: 1 passed, 1 total
Tests: 14 passed, 14 total
Time: ~8s
All tests verify:
- ✅ Response structure matches documentation
- ✅ Required fields are present
- ✅ Field types are correct
- ✅ Error responses follow expected formats
All API responses use a standardized envelope:
// Success
{ success: true, data: {...}, meta?: {...} }
// Error
{ success: false, error: { code, message, details? } }expect(res.body).toHaveProperty('success');
expect(typeof body.data.timestamp).toBe('string');
expect(Array.isArray(body.data.events)).toBe(true);expect(new Date(body.timestamp).toISOString()).toBe(body.timestamp);- Mocked Stellar SDK to avoid external dependencies
- Mocked database connections
- Mocked event registry for predictable test data
- Branch:
api-contract-tests - Repository: https://github.com/coderolisa/Notify-Chain.git
- Commits: 2 commits
- Initial API contract tests implementation + bug fix
- Documentation
The README documents a comprehensive roadmap for expanding test coverage to include:
- Scheduled notification endpoints (5 endpoints)
- User preferences (2 endpoints)
- Notification history (1 endpoint with pagination)
- Notification search (2 endpoints)
- Template management (7 endpoints)
- Analytics (2 endpoints)
- Webhooks (1 endpoint with signature verification)
- Rate limiting (1 endpoint)
Each endpoint should have tests for:
- Success cases with valid data
- Error cases with invalid data
- Edge cases (empty arrays, null values, etc.)
- Query parameter handling
- Pagination where applicable
- Breaking Change Detection - Tests fail when documented response structures change unexpectedly
- Documentation Validation - Ensures API documentation matches actual behavior
- Regression Prevention - Catches unintended changes during refactoring
- Development Confidence - Developers can safely modify code knowing tests will catch issues
- API Stability - Encourages thoughtful API design changes
- Client Protection - External API consumers are protected from breaking changes
- Faster Debugging - Clear test failures pinpoint exact contract violations
- Living Documentation - Tests serve as executable documentation
# Navigate to listener directory
cd listener
# Install dependencies (if not already done)
npm install
# Run the contract tests
npm test -- api-contract.test.ts
# Run with coverage
npm test -- api-contract.test.ts --coverage✅ Core API responses have contract tests - Implemented for 5 critical endpoints
✅ Required fields are verified - All tests check for presence and types of required fields
✅ Error responses are covered - Tests verify error structures for invalid inputs
✅ Tests fail when documented response structures change unexpectedly - All assertions validate exact structure
- Tests are isolated and don't require external services (Stellar RPC, database, Discord)
- All dependencies are properly mocked for fast, reliable test execution
- Tests follow Jest best practices with clear describe/it blocks
- TypeScript typing ensures type safety throughout tests
- Tests can run in CI/CD pipelines without environment dependencies
- Review test coverage and assertions
- Verify tests align with API documentation
- Confirm mock setup is appropriate
- Validate README documentation
- Merge to main branch after approval
- Consider adding contract tests to CI/CD pipeline
- Plan next iteration to expand coverage beyond 2%