# Testing Template

Mandatory test categories for implementation phases.

Baseline strategy: [testing-strategy.md](../development/testing-strategy.md).  
Enterprise mandate: [definition-of-done.md](definition-of-done.md) · [ADR-018](../architecture/adr/ADR-018.md).

Mark a category **N/A** only with written justification (e.g. docs-only phase).

## Unit

- Pure validators, mappers, permission helpers, specifications, policies, formatting, template rendering without I/O.
- Deterministic; no real network.

## Repository

- Query correctness, pagination, filtering, sorting, searching.
- Soft-delete defaults.
- **Tenant filter** present on tenant-owned entities.
- Cross-tenant get/update returns empty/denied.
- Optimistic lock conflict behavior when versioning is used.

## Service / Command / Query

- Business rules and invariants (commands).
- Read-side correctness without write side-effects (queries).
- Audit/event emission expectations (outbox rows or publisher fakes).
- Domain error codes for illegal transitions.
- Policy denials where policies are in scope.

## Integration / API

- HTTP contracts: status codes, auth, permission denials.
- Tenant header requirements.
- Pagination / filter / sort / search query params.
- OpenAPI route presence for new endpoints (smoke or schema assertion when pattern exists).
- Health / capabilities / metrics when the phase owns them.
- End-to-end flows for the phase’s primary use cases.

## Migration

- Upgrade applies on clean DB.
- Smoke query against new tables/columns.
- Seed data applies when required.
- Downgrade only if the service supports it — document if not.

## Permission

- Each new privileged route denies without permission/role.
- Permission names match `{service}.*` trees documented in the phase.

## Security

- Unauthenticated access denied where required.
- Token scope / role bypass attempts fail.
- No secret leakage in error bodies.

## Performance

- Required for hot paths (entitlement, routing, queue claim, posting, large lists) when the phase touches them.
- Otherwise document N/A.
- Avoid unbounded queries; assert basic index presence or acceptable timing in CI only when stable.

## Tenant Isolation

- At least one cross-tenant denial per new tenant-scoped API/aggregate.
- No list endpoint returns other tenants’ rows.

## Architecture / Boundary

- No forbidden imports across services.
- Layering rules (API thin; no business logic in repos).
- No future-phase modules accidentally introduced (when detectable).
- Follow sibling static checks used by CRM/Loyalty/Communication/platform services.

## Documentation Validation

- Phase doc exists and matches delivered surface.
- Registry/manifest entries updated.
- Critical links resolve (tests or scripted checks when the service already has `test_docs` / architecture tests — follow sibling pattern).
- DoD / completeness sections present in handover for implementation phases.

## Phase Rules

1. New behavior ships with tests in the same phase.
2. Failing or missing required tests → phase incomplete.
3. Do not use production data.
4. Prefer fakes for OTP, SMS providers, time.
5. CRUD-only test coverage without domain-rule tests is insufficient when business rules exist.

## Related Documents

- [Testing Strategy](../development/testing-strategy.md)
- [Quality Gates](quality-gates.md)
- [Development Loop](development-loop.md)
- [Definition of Done](definition-of-done.md)
- [Project Principles](../development/project-principles.md)
