Orders Service — Testing Strategy
Status: populated Owner: TBD Last updated: 2026-04-18 Companion: Service Template
1. Coverage Targets
| Scope | Target |
|---|---|
| Line coverage | ≥ 80% |
| Branch coverage | ≥ 75% |
| Domain layer (use cases, domain model, CDS guard) | ≥ 90% |
2. Test Types
| Type | Framework | Scope |
|---|---|---|
| Unit | Jest | Use cases, CDS guard logic, state machine, value objects |
| Integration | Jest + Testcontainers (PostgreSQL) | Repository adapters, outbox relay, RLS policies, allergy cache |
| Contract (consumer) | Pact | Events produced: order.activated, referral.created; events consumed: allergy.recorded, patient.vital-status-changed |
| API (E2E) | Supertest | REST endpoints against in-process NestJS app |
| Performance | k6 | Order creation with CDS; order list query |
3. Mandatory Integration Tests
tenant-isolation.integration.spec.ts— Verify orders from tenant A are never accessible to tenant B under RLS.outbox.integration.spec.ts— Verify that order activation writes to outbox in same transaction; relay publishes to NATS at-least-once.cds-guard.integration.spec.ts— Verify CDS allergy check hard-stop blocks activation; warning requires acknowledgement; after acknowledgement, activation succeeds.
4. Scenario Coverage Matrix
| Scenario | Unit | Integration | Contract |
|---|---|---|---|
| Create medication order — happy path | Yes | Yes | Yes |
| Create lab order — happy path | Yes | Yes | No |
| Create referral — happy path | Yes | Yes | Yes |
| CDS allergy hard-stop blocks activation | Yes | Yes | No |
| CDS warning acknowledged, order activated | Yes | Yes | No |
| CDS duplicate order warning | Yes | No | No |
| CDS dosing range hard-stop | Yes | No | No |
| Order activated → event emitted | Yes | Yes | Yes |
| Order cancelled — active | Yes | Yes | No |
| Order cancelled — draft | Yes | No | No |
| Order terminal state (cancel completed order) | Yes | No | No |
| Optimistic lock conflict | Yes | Yes | No |
| Idempotent create (duplicate clientMutationId) | Yes | Yes | No |
| Order set instantiation (2 orders) | Yes | Yes | No |
| Referral activated → referral.created event | Yes | Yes | Yes |
| Patient deceased blocks order create | Yes | Yes | No |
| Allergy cache updated on event | No | Yes | No |
| Batch order — partial failure | Yes | Yes | No |
| Tenant isolation (RLS) | No | Yes | No |
| Module not entitled returns 403 | Yes | No | No |
| FHIR ServiceRequest read | No | Yes | No |
5. Contract Tests
| Event | Role | Consumer |
|---|---|---|
clinical.orders.order.activated.v1 | Producer | pharmacy-service, laboratory-service, radiology-service |
clinical.orders.referral.created.v1 | Producer | scheduling-service, communication-service |
REGISTRATION.allergy.recorded.v1 | Consumer | orders-service |
REGISTRATION.patient.vital-status-changed.v1 | Consumer | orders-service |
6. CDS-Specific Tests
The CDS guard must have comprehensive unit test coverage for:
- Allergy check: confirmed allergy → hard-stop; intolerance → warning
- Drug-drug interaction: contraindicated pair → hard-stop; monitored pair → warning
- Duplicate order: same code within 24h → warning; different code → no alert
- Dosing: above max dose → hard-stop; above recommended → warning; within range → no alert
- Multiple simultaneous alerts: all captured; any hard-stop blocks activation
7. Performance Tests
| Scenario | Tool | Target |
|---|---|---|
POST /v1/orders with CDS — 50 concurrent | k6 | p95 < 800 ms |
GET /v1/orders?patientId= — 100 concurrent | k6 | p95 < 600 ms |
POST /v1/order-sets/:id/instantiate (5-order set) | k6 | p95 < 2 000 ms |