# libds spec-testing strategy

The discipline specific to `runtime/tests/libds/spec/` —
how spec tests are scoped, how their `<id>` slot is
filled, and how spec drift is handled when found.

The **structural conventions** (per-scenario functions,
naming with `__` separator, run-all-aggregate `main()`,
fixture isolation, sentinel checks) are SHARED with the
impl suite and live in [ITST.5]. This doc covers only
what's distinct about spec tests.

## What this is not

- Not the test-tree layout. See [ITST.2].
- Not the structural conventions. See [ITST.5].
- Not the impl-test discipline. Author-of-feature also
  writes the impl test; reaching internal headers is
  fine.
- Not the dispatcher contract. Spec tests cite
  [IDSP]; they don't redefine.

---

(ISTS-1)=
## Two-suite separation [ISTS.1]

(ISTS-1-1)=
[ISTS.1.1] libds ships two test suites with different jobs:

| Suite | Includes | Author | Catches |
|---|---|---|---|
| **impl** | internal headers + `libds.h` | Whoever wrote the feature | Refactor regressions, internal-invariant breaks |
| **spec** | only `libds.h` + `spec_common.h` | Whoever wrote / cited the spec point | Spec drift, public-API gaps, behavior-not-implementation |

(ISTS-1-2)=
[ISTS.1.2] **Decision rule.** A passing impl suite +
failing spec suite means the implementation drifted
from the spec. That's the signal the split exists for.
Either fix the impl (most common) or tighten the spec
(when the spec was ambiguous and the impl picked a
reasonable side).

(ISTS-1-3)=
[ISTS.1.3] A reimplementation of libds against the same
specs is required to pass the spec suite. The impl
suite couples to libds's specific data structures and
would not survive a reimplementation.

---

(ISTS-2)=
## Spec-test discipline [ISTS.2]

(ISTS-2-1)=
[ISTS.2.1] A spec test **must not** include any project header but `libds.h`
and `spec_common.h`; it may include standard C and POSIX headers.
`spec_common.h` itself **must** include only `libds.h` and standard C and POSIX
headers. [ISTS.2.2] lists what that excludes.

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

(ISTS-2-2)=
[ISTS.2.2] **What spec tests cannot do.** Each is an
explicit non-allowance, not a courtesy:

- **No engine internals.** Tests that need rules or schemas
  load a compiled artefact with `ds_engine_load_dsc`
  (public), through `spec_common.h`'s `spec_load_ds`
  ([ISTS.2.3]). They cannot call internal engine functions
  (Rete network, working memory, agenda) directly.
- **No internal headers** (`engine.h`, `wm.h`,
  `schema.h`, `dispatcher.h`, `recorder.h`,
  `clock.h`, `telemetry.h`).
- **No internal symbols** (`ds_schema_new`,
  `ds_rete_wm_assert`, `ds_engine_wire_rete_adapters`,
  `ds_engine_set_pending`, etc.).
- **No struct-field access on opaque handles**
  (`fact->schema`, `engine->wm`, `f->ttl_deadline_ns`).

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

(ISTS-2-3)=
[ISTS.2.3] **Public-API gap rule.** If a spec test needs
something that isn't in `libds.h`, that's a public-API gap to close in
the header, or a sign that the test arranges state no integrator can
reach — never a reason to escape the discipline ([ISTS.2.2]).

A spec test arranges an engine as an integrator does. A schema reaches an
engine only from a compiled artefact ([NFCT.1.1]), so a spec test
that needs one must declare it in DS source, compile it with the build's
compiler and load the artefact with `ds_engine_load_dsc`, which
`spec_common.h`'s `spec_load_ds` does. It must not register a schema
through an internal entry point. A spec test whose subject is a function
that leaves `libds.h` leaves with it.

Each gap found is a small public-API addition, and they accumulate into a
complete integrator surface.

---

*see also:* [ISTS.2.2](#ISTS-2-2), [NFCT.1.1](#NFCT-1-1)

(ISTS-3)=
## Spec-id slot in the naming scheme [ISTS.3]

(ISTS-3-2)=
[ISTS.3.2] **For spec tests, `<lowercased_id>` is the spec
section ID** with dots replaced by underscores:

| Spec ID | Identifier slot |
|---|---|
| `IDSP.2.3`  | `idsp_2_3` |
| `IDSP.5.5`  | `idsp_5_5` |
| `IDAT.4.6.2`| `idat_4_6_2` |
| `IARC.1.11` | `iarc_1_11` |
| `IERR.4.1`  | `ierr_4_1` |

Cross-cutting rules (e.g. `IDSP.1.3`, `IDSP.1.4`) take
the same form. Spec areas covered today: IDSP, IDAT,
IERR, IARC.

*see also:* [IARC.1.11](#IARC-1-11), [IDAT.4.6.2](#IDAT-4-6-2), [IDSP.1.3](#IDSP-1-3), [IDSP.1.4](#IDSP-1-4), [IDSP.2.3](#IDSP-2-3), [IERR.4.1](#IERR-4-1)

(ISTS-4)=
## Either-conforming pattern [ISTS.4]

(ISTS-4-1)=
[ISTS.4.1] Some spec rules permit more than one valid
implementation. The most common form: "this shape is
invalid" can be enforced at multiple pipeline points,
and rejection at any one of them satisfies the spec.

(ISTS-4-2)=
[ISTS.4.2] **Concrete example.** An artefact whose `log`
action carries a level outside the five [IDSP.2.2] names is
invalid. Two valid impls:

- **Reject at load.** The loader refuses the artefact;
  `ds_engine_load_dsc` returns a non-OK status.
- **Reject at pump.** The artefact loads; the
  dispatcher's level check at fire time surfaces
  `DS_ERR_INVAL`.

Both are conforming, and the spec test must accept either.

*see also:* [IDSP.2.2](#IDSP-2-2)

(ISTS-4-3)=
[ISTS.4.3] **Always document the pattern inline.** A
reviewer skimming such a scenario should not be left
wondering whether the `if (r.status != DS_OK) return 0`
is a partial check. The function header comment names
the pattern: "Either-conforming: rejection at
load OR at pump satisfies the spec." Compact
inline reference is fine; this section is the long-
form explanation.

(ISTS-4-4)=
[ISTS.4.4] **Beware degenerate compliance.** If
`r.status == DS_OK` at load (`spec_load_ds`) AND
`r.status == DS_OK` at pump, neither point rejected — that's a bug. The
test catches it: the fall-through `EXPECT_STATUS` at
pump fires when nothing was rejected anywhere.

(ISTS-4-5)=
[ISTS.4.5] Either-conforming is spec-suite-specific.
Impl tests don't need it — they test specific impl
behaviors and can assert exact outcomes without
worrying about "another valid impl might do this
differently."

---

(ISTS-5)=
## Recording coverage [ISTS.5]

(ISTS-5-3)=
[ISTS.5.3] **A clause records its coverage in `checks`,
not in prose.** Every tie between a clause and a test that
covers it must be an entry in that clause's `checks` block
([STYL.5.3.6]) — impl-suite and spec-suite alike, naming the
file and the scenario function. A clause with no coverage
must record that as `verification: "gap"` with a
`gap_reason` ([STYL.5.3.7]).

A prose paragraph asserting that some test covers some
clause must not be written. `checks[*].path` is verified in
both directions ([STYL.5.5.2], [STYL.5.5.4]); a sentence is
verified by nobody.

A section may state what a conforming implementation **must**
exercise — an obligation on the implementer, carrying no
file path, no track ID and no completion state ([RBND.8],
[IRET.22]).

---

*see also:* [IRET.22](#IRET-22), [RBND.8](#RBND-8)

(ISTS-7)=
## What is not testable from spec tests [ISTS.7]

(ISTS-7-1)=
[ISTS.7.1] Some spec claims are real but not
unit-testable. They go in the impl-suite (where
applicable), in static-analysis tooling, or in
review rules — never in `spec/`.

| Claim | Why not spec-testable | Where it lives |
|---|---|---|
| `IARC.1.10` "no threads, never blocks" | Structural — can grep, can't unit-test absence | Code review; CI grep |
| `IARC.1.11` "no globals" | Structural; can grep for non-`static` mutable file-scope decls | Code review |
| `IARC.1.13` "per-object malloc" | Structural — same | Code review |
| `IERR.4.1.1` "detail valid until next libds call" | Lifetime invariant — provable in specific scenarios but not in general | Impl tests for specific scenarios; doc |
| `LDET.3.4.1` cross-platform byte-identical replay | Needs two platform signatures at once, so it is a calibration run rather than a suite case | A calibration run: one corpus replayed on two platforms, and the two replays compared byte for byte |
| `LLOG.5` test CEs under logical | The test CE is already lowered correctly, and behavioural coverage rides on the `LLOG.1` obligation, whose fixture uses a `where` clause | [LLOG.14.2] |
| `LLOG.8` explicit retract of a logical fact | Compile time: DS has no `retract` verb, so the case is rejected there. Runtime: `ds_engine_retract` works on any schema | [LLOG.14.2] |

*see also:* [IARC.1.10](#IARC-1-10), [IARC.1.11](#IARC-1-11), [IARC.1.13](#IARC-1-13), [IERR.4.1.1](#IERR-4-1-1), [LDET.3.4.1](#LDET-3-4-1), [LLOG.1](#LLOG-1), [LLOG.14.2](#LLOG-14-2), [LLOG.5](#LLOG-5), [LLOG.8](#LLOG-8)

(ISTS-7-2)=
[ISTS.7.2] The table above is the register of what is
not spec-testable and why. A claim that fits it is
added to it.

---

(ISTS-8)=
## Layout notes specific to spec/ [ISTS.8]

(ISTS-8-1)=
[ISTS.8.1] **`spec_common.h` is the shared header.** It
contains:

- `#include "libds.h"` and standard C and POSIX
  headers ([ISTS.2.1]).
- `spec_fail()` helper.
- `EXPECT_OK` / `EXPECT_STATUS` macros.
- `spec_load_ds(engine, source)`: compiles DS source with
  the build's compiler and loads the artefact with
  `ds_engine_load_dsc` ([ISTS.2.3]). A source the compiler
  refuses returns `DS_ERR_PARSE`.

It must NOT contain:

- Any per-area helper (`build_engine`, fixture probes).
  These live in each test file. After ≥2 spec test
  files need the same helper, lift it — but not
  before, to avoid premature abstraction across
  diverging needs.

*see also:* [ISTS.2.1](#ISTS-2-1), [ISTS.2.3](#ISTS-2-3)

(ISTS-8-2)=
[ISTS.8.2] Per-scenario fixture and run-all-aggregate
patterns are shared with impl tests ([ITST.5.5], [ITST.5.4]).

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

(ISTS-8-3)=
[ISTS.8.3] **One binary per spec area** in
`runtime/tests/libds/spec/`. Naming: `test_<area>_spec.c`
where `<area>` is the verb name (`log`, `emit`, etc.)
or the topic name (`ttl`, `wm_lifecycle`, `errors`).

---

(ISTS-9)=
## Cross-references [ISTS.9]

- ITST — overall test-tree layout, build contract,
  structural conventions (shared with impl suite).
- IDSP — spec source for verb-area drills.
- IDAT — spec source for data-model drills.
- IERR — spec source for error-code drills.
- IARC — spec source for architecture-invariant drills.
- `runtime/tests/libds/spec/test_log_spec.c` —
  reference example of a fully-drilled verb area
  following this strategy.

