ADR-0011: godog/Gherkin acceptance tests as executable specification
Status: Accepted
Context
By this point the service already had a deep test suite: table-driven unit
tests per aggregate and per invariant (internal/domain), use-case tests
against fakes (internal/application/usecases), httptest tests per handler,
Postgres/Kafka integration tests per adapter, a gremlins mutation pass over
the domain and an architecture fitness test. The CI workflow and the Makefile
even carried a bdd job and a make bdd target (go test ./... -run TestFeatures -v) — but the repository held no features and no godog
dependency, so the job ran go test against zero matching tests and passed
vacuously.
What was missing was a statement of behaviour in the domain's own
vocabulary. TestApproveEndpointConflictYields409 is precise, but it does
not say why reusing an idempotency key for a different request is refused,
and it cannot be read by anyone who does not read Go. That matters here
because this service's correctness is defined by rules that are easy to get
subtly wrong and are mostly about refusing: the planner fails closed on an
empty or stale read model (a 503 from the simulation is the correct answer),
an approval is validated against the current facts, a transfer moves only
along DRAFT → PROPOSED → APPROVED → ALLOCATING → ALLOCATED → PICKED → IN_TRANSIT → ARRIVED → RECEIVED (or to UNFULFILLABLE), and the analytics
reports are derived only from the analytics stream.
There was also a structural gap: the existing tests each verify one layer. Nothing drove the whole stack — router, DTOs, error mapping, use cases, aggregates, the CloudEvents encoders, the analytics consumer and the report handlers — as a black box the way a real client does.
Decision
We write executable specifications in Gherkin under features/, run them
with godog v0.16.0 (the official Cucumber
implementation for Go) in Strict mode, and gate them in CI as the existing
bdd job. This mirrors inventory-storage ADR 0007.
-
One feature file per concern, in the ubiquitous language of
.claude/rules/domain-model.md. Each file starts with a# Derived from:comment citing theapis/openapi.yamloperations and the domain rules it specifies, and is tagged@bdd:File Covers features/health.featureGET /healthzfeatures/proposal_generation.featurePOST /v1/transfer-proposal-runs— quantity bounds, lane rules, score ordering, determinism, advisory-only, 400/422features/simulation.featureGET /v1/transfer-simulations— headroom view, fail-closed 503 on empty/stale/disabled factsfeatures/transfer_approval.featurePOST /v1/transfer-approvals— saga start, idempotency (replay / 409 / required header), 400/422/503, CloudEvents on the wirefeatures/transfer_lifecycle.featurethe saga state machine driven by inventory-storage and fulfillment-execution facts: legal steps, illegal transitions refused, short picks, unknown transfers, audit trail features/transfer_queries.featureGET /v1/transfers,GET /v1/transfers/{id}— ordering, filters, paging, 404/400/503features/rebalance_runs.featureGET /v1/rebalance-runs— observe-only COMPLETED/FAILED runs, newest first, limitfeatures/analytics_reports.featureGET /reports/{transfer-funnel,state-dwell,stuck-transfers,rebalance-runs,freshness}— the publish → project → report path, range rules, idempotent projection -
True black-box acceptance tests. Step definitions live in
features_test.go(suite, world, HTTP and JSON steps) and its siblingsfeatures_facts_test.go,features_saga_test.go,features_reports_test.goat the repo root (package main_test). They wire the real HTTP routers —inboundhttp.Handler.Routes()for the OLTP API andReportsServer.Routes()fornip-reports— to in-memory outbound adapters, serve them overhttptest.NewServer, and drive them with plainnet/httprequests. Every outcome is asserted from the response (or from a read-back through the same API), never from a repository. -
In-memory outbound adapters.
internal/adapters/outbound/memoryimplements the saga store (TransferRepository,TransferQuery,StuckTransferReader), the planning read models, the rebalance run history, a unit of work and a settable clock with the same contracts as the Postgres adapters (unique idempotency key, optimistic version, per-transfer audit sequence, newest-first ordering). Nothing incmd/wires them. The existinganalyticsstore.Memoryserves the reports. -
Inbound Kafka is the one thing REST cannot reach, so it is simulated at the use-case seam. inventory-storage replies and fulfillment-execution facts arrive over Kafka;
Given/Whensteps hand them to the sameApplyTransfer*use cases the consumers call. Outbound events go through the productionTransferEncoder/AnalyticsEncoder, so scenarios assert the exact CloudEvents 1.0 messages the relay would publish, and the productionAnalyticsConsumerprojects them into the store the reports read. Planning facts are declared locally in the scenario — the suite never calls a sibling context, which this service must not do either (fleet rule). -
Fresh state per scenario, a frozen clock (
2026-10-06T15:00:00Z) moved only by steps such as11 minutes pass, and idempotency keys as the scenario's names for transfers ({T1}expands to the minted id). No sleeps, no ordering coupling. -
Scenarios assert the specified behaviour. Where the implementation diverges from
apis/openapi.yamlor from the documented rules, the divergence is reported (and fixed when trivial and safe), never encoded as a passing scenario. -
Blocking in CI as the
bddjob, locallymake bdd, and as part ofmake checkthroughgo test ./... -race.
Consequences
Easier
- The behaviour is readable by anyone who has not opened a Go file, in the vocabulary of the warehouse (fail closed, replayed, UNFULFILLABLE, stuck), and it fails the build when it stops being true.
- The full stack is covered end to end: status codes,
problem+jsonbodies, DTO field names, the CloudEvents envelope and the report handlers — the layer of bugs unit tests structurally cannot see. - Together with ADR 0009 the publish → project → report path is exercised as one flow without Kafka or Postgres.
Harder
- A second vocabulary to maintain. A change to a route or DTO now touches
the handler and the step glue. The composition in
features_test.gomirrorscmd/network-inventory-planning/main.go(packagemaincannot be imported); a new use case onHandlermust be added to both. - Step reuse needs discipline (the near-duplicate-step problem). New
scenarios should reuse the generic
the JSON field "path" is …steps and the transfer-fact steps before adding new ones. - Overlap with the
httptestand use-case suites. Some assertions now exist in two places. Accepted deliberately: those suites are exhaustive per unit, these are the readable specification of the important journeys. - The in-memory adapters are not the Postgres adapters. They specify behaviour, not persistence; the integration tests keep covering SQL.