test: increase coverage for ClaudeAPI and koha services (9% → 86%)
Major test coverage improvements for Week 1 Task 3 (PHASE-4-PREPARATION-CHECKLIST).
ClaudeAPI.service.js Coverage:
- Before: 9.41% (CRITICAL - lowest coverage in codebase)
- After: 85.88% ✅ (exceeds 80% target)
- Tests: 34 passing
- File: tests/unit/ClaudeAPI.test.js (NEW)
Test Coverage:
- Constructor and configuration
- sendMessage() with various options
- extractTextContent() edge cases
- extractJSON() with markdown code blocks
- classifyInstruction() AI classification
- generateBlogTopics() content generation
- classifyMediaInquiry() triage system
- draftMediaResponse() AI drafting
- analyzeCaseRelevance() case study scoring
- curateResource() resource evaluation
- Error handling (network, parsing, empty responses)
- Private _makeRequest() method validation
Mocking Strategy:
- Mocked _makeRequest() to avoid real API calls
- Tested all public methods with mock responses
- Validated error paths and edge cases
koha.service.js Coverage:
- Before: 13.76% (improved from 5.79% after integration tests)
- After: 86.23% ✅ (exceeds 80% target)
- Tests: 34 passing
- File: tests/unit/koha.service.test.js (NEW)
Test Coverage:
- createCheckoutSession() validation and Stripe calls
- handleWebhook() event routing (7 event types)
- handleCheckoutComplete() donation creation/update
- handlePaymentSuccess/Failure() status updates
- handleInvoicePaid() recurring payments
- verifyWebhookSignature() security
- getTransparencyMetrics() public data
- sendReceiptEmail() receipt generation
- cancelRecurringDonation() subscription management
- getStatistics() admin reporting
Mocking Strategy:
- Mocked Stripe SDK (customers, checkout, subscriptions, webhooks)
- Mocked Donation model (all database operations)
- Mocked currency utilities (exchange rates)
- Suppressed console output in tests
Impact:
- 2 of 4 critical services now have >80% coverage
- Added 68 comprehensive test cases
- Improved codebase reliability and maintainability
- Reduced risk for Phase 4 deployment
Remaining Coverage Targets (Task 3):
- governance.routes.js: 31.81% → 80%+ (pending)
- markdown.util.js: 17.39% → 80%+ (pending)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>