# Test conventions and coverage

Test infrastructure and conventions for libds's two test
suites — `impl/` (white-box, internal headers) and `spec/`
(black-box, public-API only) — and, in its last section, how
every DS product's test coverage is measured and held.

The structural conventions in [ITST.5] apply to both suites.
Spec-specific discipline (what's unique about how spec tests
are scoped) lives in [ISTS].

## What this is not

- Not the spec-suite-specific discipline. See [ISTS].
- Not a benchmark / perf-test doc.

---

(ITST-1)=
## Test-tree commitments [ITST.1]

(ITST-1-2)=
- [ITST.1.2] **Two test surfaces.** `runtime/tests/libds/impl/`
  holds white-box impl tests for libds's modules.
  `runtime/tests/libds/spec/` holds black-box spec
  tests against the libds public surface. Each suite
  is a table in `libds.build`: `ImplTest` for
  impl, `SpecTest` for spec, one row per test binary.

(ITST-1-3)=
- [ITST.1.3] **The two suites have different reach.** `impl/`
  reaches `runtime/src/libds/` (so tests can exercise
  internal modules directly); `spec/` reaches only
  `runtime/include/`. A passing impl + failing spec
  means impl-vs-spec drift — that's the signal the
  split exists for.

(ITST-1-4)=
- [ITST.1.4] **Tests run only inside a container.** A libds
  test is built and run only by its `libds.build`
  steps, which run in a container on a build host
  ([ITST.7.1]). Nothing in the test tree builds or runs
  on the local machine.

*see also:* [ITST.7.1](#ITST-7-1)

(ITST-1-5)=
- [ITST.1.5] **Plain-C assertions.** Tests return 0/1 from
  scenario functions and `main()`. No cmocka dependency.

---

(ITST-2)=
## Tree layout [ITST.2]

```
runtime/
  libds.build                  Builds libds.a and both suites:
                               an ImplTest and a SpecTest table,
                               one row per test binary.
  tests/
    libds/
      impl/
        test_<area>_<scenario>.c   May include internal headers.
      spec/
        spec_common.h          Public-API-only convenience macros.
        test_<area>_spec.c         libds.h and spec_common.h only.
      source/
        test_*.py              Scans over libds's sources and the
                               suites, run before the impl suite.
```

(ITST-2-1)=
[ITST.2.1] **Suites have different jobs.**

- **`impl/`** — author-of-feature also writes the test.
  Reaches into internal struct fields and functions
  (`engine.h`, `wm.h`, `dispatcher.h`). Catches
  refactor regressions and internal invariant breaks.
  Couples to implementation; survives only this
  particular libds.
- **`spec/`** — black-box conformance. Includes only
  `runtime/include/libds.h`. Each scenario cites the
  spec ID it covers (`IDSP.5.4`, `IDAT.4.1`, etc.).
  Doesn't depend on internals; a re-implementation
  of libds against the same specs must pass these.
  Spec-suite-specific rules are in [ISTS.1].

*see also:* [IDAT.4.1](#IDAT-4-1), [ISTS.1](#ISTS-1)

(ITST-2-2)=
[ITST.2.2] Filename convention (binaries):
- impl: `test_<area>_<scenario>.c` —
  `test_engine_basic.c`, `test_dispatcher_log.c`, etc.
  `_basic` is the smoke-level scenario; `_<specific>`
  variants land as cases grow enough bulk to split.
- spec: one binary per spec area —
  `test_log_spec.c`, `test_ttl_spec.c`, etc.

Inside both, every scenario function follows the
naming scheme in [ITST.5.3].

---

*see also:* [ITST.5.3](#ITST-5-3)

(ITST-3)=
## Build contract [ITST.3]

(ITST-3-1)=
### libds/impl include paths [ITST.3.1]

An impl test's compile reaches `runtime/include`,
`runtime/src/libds` and `runtime/src/libds/dsc`, for white-box access
to internal module headers.

(ITST-3-2)=
### libds/spec include paths [ITST.3.2]

A spec test's compile reaches only `runtime/include` — no internal
sources. A spec test that needs an additional symbol exposes a
public-API gap to close in `libds.h`.

(ITST-3-3)=
### Link line [ITST.3.3]

Every libds test (impl or spec) links against `libds.a` plus
`protobuf-c`, `re2`, the C++ standard library, `pthread` and `libm`.

---

(ITST-4)=
## Assertion style [ITST.4]

(ITST-4-1)=
[ITST.4.1] **libds impl tests** use plain-C return codes and per-test
helpers: a `fail` that prints the status and detail and returns 1, an
`expect_ok` asserting `DS_OK`, and an `expect_status` asserting a specific
status. Each impl test file defines its own copy.

(ITST-4-2)=
[ITST.4.2] **libds spec tests** include
`spec/spec_common.h` which provides the same
helpers as macros (`EXPECT_OK`, `EXPECT_STATUS`)
plus `spec_load_ds(engine, source)` ([ISTS.8.1]). Spec
tests do not copy the per-test helpers; they share via
`spec_common.h`.

*see also:* [ISTS.8.1](#ISTS-8-1)

(ITST-4-3)=
[ITST.4.3] **cmocka is allowed in the future** if a
test file's case count grows large enough that
per-case setup / teardown / scoped assertions become
worth the dependency. Not before.

---

(ITST-5)=
## Test structure conventions [ITST.5]

These conventions apply to **both** libds suites
(impl/ and spec/), except where a clause says it binds
one: the naming form of [ITST.5.3] binds the spec suite.

*see also:* [ITST.5.3](#ITST-5-3)

(ITST-5-1)=
### One binary per area [ITST.5.1]

(ITST-5-1-1)=
[ITST.5.1.1] Each test binary corresponds to a single
"area" — a module (impl tests) or a spec section (spec
tests). The binary's filename reflects the area:
`test_dispatcher_log.c`, `test_log_spec.c`,
`test_ttl_basic.c`, `test_ttl_spec.c`.

(ITST-5-1-2)=
[ITST.5.1.2] Cross-cutting tests (e.g. multi-engine
isolation that spans the whole engine) get their own
binary named for what they test:
`test_multi_engine_spec.c` — not pinned to one verb.

(ITST-5-2)=
### Per-scenario static functions [ITST.5.2]

(ITST-5-2-1)=
[ITST.5.2.1] Inside one binary, each test scenario lives
in its own `static int` function returning 0 (pass) or
1 (fail). `main()` invokes them all and aggregates.

(ITST-5-2-2)=
[ITST.5.2.2] Why per-scenario functions instead of one
monolithic main():
- **Failure isolation.** A failure in scenario A
  doesn't mask scenario B.
- **Naming as documentation.** The function name cites
  what's tested ([ITST.5.3]).
- **Easy to add / remove.** New scenario = new function
  + new line in main(). No threading through giant
  control flow.

*see also:* [ITST.5.3](#ITST-5-3)

(ITST-5-3)=
### Naming: `test_<id>__<tag>` [ITST.5.3]

(ITST-5-3-1)=
[ITST.5.3.1] **Every scenario function in the spec suite**
must follow this exact form:

```
test_<lowercased_id>__<short_tag>
```

An impl test's scenario function may take a descriptive
name instead. One that tests a clause takes this form with
the clause's id, or carries an `@spec [ID]` comment above
it; either is what links it to the clause ([STYL.5.3.6.3]).

- `<lowercased_id>` — the clause id with dots
  replaced by underscores.
- `__` — **two underscores**, required separator. The
  double form distinguishes the id from the tag on
  either side, since both sides use single
  underscores internally.
- `<short_tag>` — lowercase, underscore-separated,
  describes the facet being verified.

(ITST-5-3-2)=
[ITST.5.3.2] **The two suites name differently:**

| Suite | Name | Example |
|---|---|---|
| spec/ | `test_<id>__<tag>`, the spec section ID lowercased with dots → underscores | `test_idsp_2_3__null_cb_silent_drop` |
| impl/ | that form for a scenario that tests a clause; otherwise a descriptive name | `test_iret_11_2__same_rule_fires_by_timetag_not_by_hash`, `test_all_levels` |

Spec tests use spec IDs because the function name IS
the spec citation per [ISTS.3].
Most impl tests exercise internal behaviour that has no
clause, so only those that test one carry its id.

*see also:* [ISTS.3](#ISTS-3)

(ITST-5-3-3)=
[ITST.5.3.3] **One claim per function.** A scenario function **must** test one
claim of a clause, or one impl behaviour. A clause or behaviour with several
claims takes one function per claim, each with the same `<id>` and its own
`<short_tag>` ([ITST.5.3.1]).

*see also:* [ITST.5.3.1](#ITST-5-3-1)

(ITST-5-3-5)=
[ITST.5.3.5] **No bare descriptive names in the spec
suite.** In a spec test, `scenario_levels`, `test_levels`
and `do_thing` are rejected: there is no citation, so a
reviewer can't tell whether the test covers the spec point
the area promises or just what the author thought to test.

(ITST-5-4)=
### Run-all-aggregate `main()` [ITST.5.4]

(ITST-5-4-1)=
[ITST.5.4.1] `main()` aggregates failures with OR-and-store, never
short-circuits. Each scenario returns 0 or 1, the results are OR-ed into one
accumulator, and `main()` returns that accumulator.

(ITST-5-4-2)=
[ITST.5.4.2] **Why.** A failing scenario reports its
issue and returns 1; subsequent scenarios still
run. The reviewer sees every failure in one
invocation, not just the first. As scenarios
proliferate (15+ per area, dozens of areas total),
short-circuiting hides patterns of failure that
distinguish "one regression" from "everything
broke."

(ITST-5-4-3)=
[ITST.5.4.3] **Required for spec tests; required for
impl tests.** Earlier impl tests used short-circuit
mains; new and revised impl tests follow this
pattern.

(ITST-5-5)=
### Per-scenario fixture isolation [ITST.5.5]

(ITST-5-5-1)=
[ITST.5.5.1] Each scenario function builds a fresh
engine. Cost is small (~ms); isolation is the
point. A scenario must not depend on or interfere
with another scenario's state.

(ITST-5-5-2)=
[ITST.5.5.2] A scenario **must** take six steps, in this order: stand up the
probe, build the engine, load the rules, drive it, check the observable, tear
it down. The probe's state **must** live on the scenario function's stack, and
step 5 **must** assert on what the probe recorded, not on what the engine
currently contains.

That order is what makes the isolation claim structural rather than a
discipline ([ITST.5.5.1]). Probe state lives on the **stack** of the scenario
function, so it cannot survive into the next scenario whether or not anyone
remembers to reset it, and the engine is built after it and freed before the
function returns. Nothing about a scenario outlives its own frame.

Step 5 is the one that decides whether the test is worth anything: the
assertion reads what the probe **recorded**, not what the engine currently
contains. A scenario that reaches past its probe has started testing the
implementation instead of the claim.

The function's name is its citation ([ITST.5.3]).

*see also:* [ITST.5.3](#ITST-5-3), [ITST.5.5.1](#ITST-5-5-1)

(ITST-5-5-3)=
[ITST.5.5.3] **Per-area `build_engine` helper.** Each
test file has a private `build_engine()` (or
equivalent) that handles boilerplate. After ≥2 areas
need the same helper, lift to `spec_common.h` (for
spec tests) or a shared impl-test helper header
(for impl tests). Don't lift before — premature
abstraction across areas with diverging needs is
worse than one copy per file.

(ITST-5-6)=
### Sentinel checks [ITST.5.6]

(ITST-5-6-1)=
[ITST.5.6.1] When a scenario uses a stream / metric /
log probe, the probe should count "events that should
NOT have happened" and assert zero at end-of-scenario.
Catches regressions where an area starts emitting
events outside its spec or design without anyone
noticing.

(ITST-5-6-2)=
[ITST.5.6.2] The pattern — a probe counting unexpected action events,
asserted zero at the end of each relevant scenario — is
`stream_probe_t` / `assert_no_other_actions` in
`runtime/tests/libds/spec/test_log_spec.c`.

(ITST-5-6-3)=
[ITST.5.6.3] Sentinel checks are not required for every
scenario — only those whose probe naturally observes
multiple kinds of events. A scenario probing a
single-purpose callback (e.g. just the log cb)
doesn't need one.

(ITST-5-7)=
### Success line [ITST.5.7]

(ITST-5-7-1)=
[ITST.5.7.1] A test's verdict is its exit status: its run
step succeeds only when the binary exits 0, and `main()`
must return non-zero when any scenario failed ([ITST.5.4]).
On success `main()` may also print a line saying so
(`test_log_spec: OK`). Nothing reads that line, so its
absence is not a failure, and a test must not rely on it
as one.

---

*see also:* [ITST.5.4](#ITST-5-4)

(ITST-6)=
## How to add a libds test [ITST.6]

(ITST-6-1)=
[ITST.6.1] Decide which suite first.

- **impl/** if the test needs to reach internal
  module state, exercises a private helper, or
  proves an internal invariant. White-box.
- **spec/** if the test verifies an externally-
  documented behavior (a specific IDSP / IDAT /
  IERR / IARC point). Black-box; only `libds.h`
  available. See [ISTS.1] for the full spec-suite
  discipline.

*see also:* [ISTS.1](#ISTS-1)

(ITST-6-2)=
[ITST.6.2] Checklist for a new impl test:

1. Create `runtime/tests/libds/impl/test_<area>_<scenario>.c`.
2. Include the public API (`libds.h`) and any internal headers the
   scenario needs (e.g. `engine.h` for engine state, `wm.h` for wm
   state).
3. Copy the three helper functions from [ITST.4.1].
4. Write per-scenario static `int` functions per
   [ITST.5.2 / 5.3 / 5.5].
5. Write `main()` per [ITST.5.4 / 5.7].
6. Add a row for the test to the `ImplTest` table in
   `libds.build`. A `test_*.c` with no row is neither built nor
   run.
7. Run the suite on a build host per [ITST.7.1].

*see also:* [ITST.4.1](#ITST-4-1), [ITST.5.2](#ITST-5-2), [ITST.5.4](#ITST-5-4), [ITST.7.1](#ITST-7-1)

(ITST-6-3)=
[ITST.6.3] Checklist for a new spec test:

1. Create `runtime/tests/libds/spec/test_<area>_spec.c`, one binary
   per spec area ([ISTS.8.3]).
2. Include `spec_common.h`, and no project header but it and
   `libds.h` ([ISTS.2.1]).
3. Name each scenario function for the clause it covers,
   `test_<lowercased_id>__<short_tag>` ([ITST.5.3.1]); where the clause
   admits more than one valid implementation, follow [ISTS.4].
4. Write `main()` per [ITST.5.4] and [ITST.5.7].
5. Add a row for the test to the `SpecTest` table in
   `libds.build`. A `test_*.c` with no row is neither built
   nor run.

*see also:* [ISTS.2.1](#ISTS-2-1), [ISTS.4](#ISTS-4), [ISTS.8.3](#ISTS-8-3), [ITST.5.3.1](#ITST-5-3-1), [ITST.5.4](#ITST-5-4), [ITST.5.7](#ITST-5-7)

(ITST-6-4)=
[ITST.6.4] Reviews reject tests that:

- Depend on real-time sleeps or wall-clock timing.
  Use the mock clock for anything time-sensitive.
- Probe or assert on private struct layouts beyond
  the minimum the test requires (impl tests).
- Reach internal headers from a spec test
  (spec/ -only rule; see [ISTS.1]).
- Give a spec test's scenario a bare descriptive name
  without an `<id>` ([ITST.5.3]).
- Use short-circuit `main()` per [ITST.5.4].
- Leak allocations. `ds_engine_free` at end of each
  scenario catches most.

---

*see also:* [ISTS.1](#ISTS-1), [ITST.5.3](#ITST-5-3), [ITST.5.4](#ITST-5-4)

(ITST-7)=
## Docker execution [ITST.7]

(ITST-7-1)=
[ITST.7.1] The libds suite runs in a container on a build host. One run
builds `libds.a` and runs both `impl/` and `spec/`.

(ITST-10)=
## Coverage [ITST.10]

(ITST-10-1)=
[ITST.10.1] **A product has two coverage numbers.** Its line and branch
coverage is the share of its own source lines, and of its own source
branches, that its tests execute. Its clause coverage is the number of
clauses, per prefix, for which a linked test counts ([STYL.5.3.11]) and the
product is one of that test's class. Neither stands in for the other: a line
a test executes is not a claim the test checks.

(ITST-10-2)=
[ITST.10.2] **What a line and branch figure measures.** A C source must be
measured for lines and for branches, and a source in another language for
lines. A header the product includes and does not own must not be counted.
libds must report two figures, each over one instrumented build of the
commit under test:

| Figure | The tests it runs |
|---|---|
| unit | libds's own suites, `impl/` and `spec/` |
| all tests | those suites, the conformance corpus through pyds, libds's side of the runtime differential, and the binding suites |

Every other product's figure must be measured over its own suite. The wasm
build of libds compiles the same C source as native libds: native libds's
figures stand for it, and the report must say so.

(ITST-10-3)=
[ITST.10.3] **The Mercury products report clause coverage only.** CI must
not make a debug-grade Mercury build, and a line count of
Mercury code needs one, so the Mercury runtime, the compiler and
`mercury_ds` have no line or branch figure. Each must report its clause
coverage.

(ITST-10-4)=
[ITST.10.4] **Every line and branch figure is held by a two-sided floor.**
Each figure has a floor, a percentage, and every floor has a slack of one
percentage point. A measurement must fail when a figure is below its floor,
and must fail when a figure is at or above its floor plus the slack; it then
names the floor the figure gives, which is the figure less half the slack. A
figure the measurement does not produce must fail it as a breach does.

A floor must be set only to a value a measurement named. A figure's first
floor is the one its first measurement names. A change
that lowers a floor must say why.

(ITST-10-5)=
[ITST.10.5] **Coverage is measured after a merge, never in a pull
request's run.** An instrumented build replaces the product the per-PR checks
share, so a coverage measurement must run in a build of its own, after each
merge to `main` that changes an input it reads, and no pull request may wait
on it. A failing measurement is a broken `main`.

(ITST-10-6)=
[ITST.10.6] **A measurement reports what is missed, largest first.** For
each figure, a measurement must print the figure, its floor and its verdict,
then the product's source files ranked by missed lines, or missed branches for
a branch figure, most first. A ranking by percentage must not be used: it puts
a small file ahead of a large one. The spec gate must report clause coverage
as a table of prefix by product.

(ITST-10-7)=
[ITST.10.7] **No coverage figure is kept in the tree.** A measurement's
report is the only current figure. A file in the repository must not record a
measured figure, except a floor and, beside it, the measurement it was set
from.

(ITST-10-8)=
[ITST.10.8] **A test checks a claim, not a figure.** A test added to raise
coverage must check a stated behaviour of the product. A source no test
reaches, where it is dead code, should be deleted rather than tested.

(ITST-10-9)=
[ITST.10.9] **The shipped products' line coverage, and the harness's.**
pyds's Python layer, `ds_ast`, `@ds-lang/ds`'s JavaScript layer, `dsdb` and
the conformance harness must each report a line figure over its own suite,
held by a floor as [ITST.10.4] states. The measurement
must count a source of the product that no test loads as wholly missed. The harness's figure
counts its modules and not its test files, and it measures the tests, not a
product: a harness line no vector reaches is an expectation nothing
exercises.

*see also:* [ITST.10.4](#ITST-10-4)

(ITST-10-10)=
[ITST.10.10] **The C, C++ and Lua bindings' line coverage.** The C, C++ and
Lua bindings must each report a line figure over its own suite, and the C
binding a branch figure, each held by a floor as [ITST.10.4] states.

*Deferred: not binding in this edition.*

*see also:* [ITST.10.4](#ITST-10-4)

(ITST-10-11)=
[ITST.10.11] **Each libds fuzz target has figures of its own.** Beside the
two of [ITST.10.2], libds must report a line and a branch figure for each fuzz
target in `runtime/tests/fuzz/`. A measurement takes each over one
instrumented build of the commit under test, counting that target's replay of
its seeds alone, each seed once. Each figure is held by a floor as [ITST.10.4]
states. A replay that fails, or that runs fewer inputs than its seeds, must
fail the measurement and give no figure.

*see also:* [ITST.10.2](#ITST-10-2), [ITST.10.4](#ITST-10-4)

