Care Plan Service — Testing Strategy
Status: populated Owner: TBD Last updated: 2026-04-18 Companion: Service Template · 03 platform-services · 02 DDD
Coverage Targets
| Layer | Minimum coverage |
|---|---|
| Unit (domain + use cases) | 90% |
| Integration | 80% |
| Contract | All API endpoints and published events |
| E2E | Critical paths only |
Test Types
Unit Tests (test/unit/)
- Care plan status transition logic — all valid and invalid transitions
- Optimistic concurrency: version increment, stale-version rejection
- Goal overdue detection algorithm
- Activity status transition rules
- FHIR mapper —
CarePlan,Goal,Task,CareTeamdomain → FHIR resource - Outbox event construction — payload shape per event type
- Error factory — correct HTTP codes for domain errors
Integration Tests (test/integration/)
Mandatory (must pass before any merge):
| Test file | What it proves |
|---|---|
tenant-isolation.integration.spec.ts | Tenant A cannot read or write Tenant B's care plans; zero cross-tenant leakage under adversarial queries |
outbox.integration.spec.ts | Domain events written to outbox in same transaction as entity; relay publishes to NATS; idempotent re-publish |
inbox.integration.spec.ts | Consumed events (patient.registered) are deduplicated; duplicate delivery has no side effect |
Additional:
| Test file | Scenario |
|---|---|
create-care-plan.integration.spec.ts | Full create → DB → outbox → event published |
version-conflict.integration.spec.ts | Concurrent updates return 409; no silent overwrite |
care-plan-closed.integration.spec.ts | Mutations on completed/revoked plan return 409 |
fhir-read-surface.integration.spec.ts | FHIR CarePlan/Goal/CareTeam read returns valid R4 resource |
licensing-gate.integration.spec.ts | MODULE_NOT_LICENSED returned when entitlement absent |
Contract Tests (test/contract/)
care-plans-api.pact.spec.ts— consumer-driven Pact contracts for patient-chart-service and patient-portal-servicecare-plan-events.schema.spec.ts— all produced events validated against JSON schemas registered in schema registryfhir-careplan.schema.spec.ts— FHIR CarePlan output validated against R4 StructureDefinition
E2E Tests (test/e2e/)
| Scenario | File |
|---|---|
| Create plan → add goal → add activity → complete activity → review | care-plan-full-lifecycle.e2e.spec.ts |
| Multidisciplinary edit: two clinicians updating same plan; conflict surfaced | concurrent-edit.e2e.spec.ts |
| Patient portal reads plan via FHIR surface | portal-fhir-read.e2e.spec.ts |
Test Infrastructure
- Vitest for unit and integration tests
- Testcontainers for Postgres (per-test schema isolation)
- NATS test server for outbox/inbox integration tests
- Pact broker for consumer-driven contract tests
- Golden FHIR fixtures stored in
test/fixtures/fhir/— validated in CI against R4 base profile
Key Scenarios to Never Remove
- Tenant isolation — adversarial cross-tenant query returns 0 results
- Version conflict — stale write returns 409, not 200
- Closed plan mutation — returns 409, not 200
- Outbox transactionality — crash after DB write replays event correctly on recovery