Skip to main content

Document Service — Testing Strategy

Status: populated Owner: TBD Last updated: 2026-04-18 Companion: Service Template · TESTING_STANDARDS.md


1. Coverage Targets

Test typeCoverage targetTool
Unit tests≥ 80 % statement + branchVitest
Integration testsAll use cases coveredVitest + Testcontainers (PostgreSQL + MinIO + ClamAV)
Contract tests (consumer)All API endpoints + event schemasPact + JSON Schema
E2E testsCritical generation + upload flowsSuperTest against running service
Visual regression (PDF)Platform reference formsPDF hash contract tests

2. Mandatory Test Scenarios

2.1 Tenant Isolation (test/integration/tenant-isolation.spec.ts)

ScenarioExpected
GET /v1/documents?patientId for Tenant A documents by Tenant B caller0 results (RLS)
Download document owned by Tenant B as Tenant A actor404 Not Found
RLS policy: SELECT on document_templates with wrong tenant setting0 rows
Cross-tenant template fork attempt403 Forbidden

2.2 Outbox (test/integration/outbox.spec.ts)

ScenarioExpected
Template version published → event in outbox before tx commitEvent in outbox table
Artifact registered → document.artifact.registered.v1 in outboxEvent present
Relay publishes to NATS → marks delivered_atdelivered_at not null

2.3 Inbox (test/integration/inbox.spec.ts)

ScenarioExpected
Same document.artifact.registered.v1 delivered twiceSecond delivery acknowledged, not processed

3. Unit Test Scenarios

ModuleKey scenarios
PublishTemplateVersionUseCaseTransitions draft to published; rejects if already published
GenerateDocumentSyncUseCaseResolves FHIR bindings; rejects retired version; idempotency via clientMutationId
FinalizeUploadUseCaseClean file → DocumentReference created; virus detected → quarantine event
ForkPlatformTemplateUseCaseCreates tenant copy; sets forkedFromPlatformFormKey; preserves lineage
PDFRendererRTL output correct for ps-AF locale; binding placeholders substituted
PresignedUrlGeneratorTTL enforced; tenantId validated
InputSnapshotHasherDeterministic SHA-256 for same input; different for different FHIR data

4. Contract Tests

Pact Consumer Contracts

ProviderInteractions tested
interop-service (FHIR gateway)GET /fhir/R4/Patient/{id}, GET /fhir/R4/Encounter/{id}, POST /fhir/R4/DocumentReference, POST /fhir/R4/Binary
config-serviceGET /internal/config/tokens (tenant branding for PDF)
audit-serviceEvent schema: document.artifact.registered.v1

Event Schema Tests

All 9 event subjects validated against registered JSON schema on publish.

PDF Contract Tests (Platform Reference Forms)

For each platformFormKey in the reference forms catalog:

  • Generate PDF with fixed input snapshot
  • Compare SHA-256 hash or visual diff against golden fixture
  • Ensures platform form upgrades do not silently break output

5. Performance Tests

ScenarioTarget
Sync generation, typical prescription templatep95 < 5 s
Sync generation, complex 4-page report templatep95 < 10 s
Concurrent generation: 20 rpsNo deadlocks; p99 < 15 s
Template list query (1000 templates)p95 < 200 ms

6. Security Tests

TestExpected
Upload file with EICAR virus signature422 VIRUS_DETECTED; quarantine event
Generate without ehr.documents license403 MODULE_NOT_LICENSED
Edit platform reference template403 PLATFORM_TEMPLATE_IMMUTABLE
Access another tenant's document (RLS bypass attempt)0 results via RLS; 404 via controller
Presigned URL with expired signature403 from object storage
Generate document with expired effectiveTo template version422 TEMPLATE_VERSION_NOT_PUBLISHED