Skip to content

TDD Rules

Every change is a slice: one vertical use case through one surface (or Front Views → API). A slice is designed, tested, implemented, and shipped as a unit. Nothing unrelated rides along in the same commit.

1. The loop

flowchart LR
    R[Red] --> G[Green]
    G --> RF[Refactor]
    RF --> D[Document]
    D --> S[Ship]
  1. Red — Write the failing test that names the behavior and the contract (endpoint, screen flow, invariant). It must fail for the right reason.
  2. Green — Implement the minimum down the stack until the test passes. No speculative code.
  3. Refactor — Remove duplication, sharpen names, move logic to the right layer, keep tests green.
  4. Document — Update docs/domain / docs/api-conventions if a term, field, or contract changed; update .opencode/context/ if an entry point moved.
  5. Ship — Tag the surface (vX.Y.Z) if it is deployed; never ship untagged.

A slice is not done because it runs. It is done when its tests define its behavior, the contract test passes, and the docs match.

2. Core rules

  • Failing test first. Do not write production behavior before a test names it.
  • One vertical slice per PR. Keep the change scoped to one use case.
  • Red/Green/Refactor/Document/Ship. Follow the loop in order; do not skip the contract or documentation step.
  • No speculative code. Green means the minimum needed to satisfy the failing test.

3. Test layers

Surface Unit Integration / feature End-to-end
API Domain invariants (seat limit, detour rejection, reservation deadline, membership state machine) HTTP feature tests: FormRequest validation, JsonResource shape, error-shape contract, auth + CompanyScope isolation, broadcast/channel auth —
Front Views RHF + Zod schema tests API mocked to docs/api-conventions.md Playwright per flow (onboarding, OTP, success)
Dashboard Query hooks / pure utils MSW handlers mirroring the API contract; realtime event fixtures Playwright per page flow (login → overview → enrollment)
Passenger / Driver apps Jest + React Native Testing Library — Detox/E2E later; CI lives in the app repo

4. Mandatory test rules

  • The error-shape contract test ({ message, errors? }) is mandatory for every new endpoint group.
  • Tenant isolation is tested explicitly: a manager of company A must not read or mutate company B.
  • Time-dependent rules (deadlines, ETA) use a frozen clock.
  • External services (Directions, SMS) are faked at the boundary; no live network in tests.

5. Example slice — approve enrollment

This worked example shows the expected test-first shape for an enrollment approval slice.

  1. Red (API feature test): POST /company/enrollments/{id}/approve as a manager of the same company returns 200 and flips status to approved; as another company's manager returns 404; when the tier seat limit is reached returns 422 with an upgrade message.
  2. Red (domain test): Membership::approve() throws when the company has no seat left.
  3. Green: FormRequest → DTO → ApproveEnrollment service → Company seat check + Membership transition → MembershipResource; broadcast on private-company.{id}.
  4. Green (Dashboard): MSW handler + query hook; Playwright flow login → enrollment queue → approve → seat count updates.
  5. Docs: Glossary entry for Seat/Membership; context map notes the new endpoint.