Testing: a first-class language concern#
The language-level testing contract — the primitives every DS runtime must provide, the affordances rule authors must have, and the conformance surface the spec itself defines.
Implementation choices (specific APIs, test-file formats, how the reference runtime wires it all up) live in [RTIM]; this doc is the contract they must honour.
What this is not#
Not a test-framework API. The language specifies what primitives must exist; the runtime documents the concrete surface.
Not the test-file syntax. That is [LTSL], built on the primitives this doc establishes.
Not a CI / build-integration story. How tests execute under
make/ctest/bazelis a runtime concern.
Design invariant [LTES.1]#
[LTES.1.6] Design invariant. Any behaviour the language specifies must be observable and assertable in a test. If a construct exists that cannot be tested, either (a) a testability primitive is added, or (b) the construct is reconsidered.
The four kinds of test [LTES.2]#
[LTES.2.1] DS-level testing stacks four distinct kinds of test, each with its own concerns and primitives. They share a common foundation (the clock and adapter-mock contracts below) but the test shape differs.
1. Compiler tests [LTES.2.2]#
[LTES.2.2.1] Pure source-to-output transformations: .ds source →
generated IR / lowered runtime artefacts / error messages.
[LTES.2.2.2]
Test shape |
Example |
|---|---|
Golden: source → expected lowered form |
feed |
Golden: source → expected error |
feed malformed |
Negative: reject |
confirm illegal source fails to compile |
Round-trip: parse-format-parse identity |
verify formatter output parses back to the same AST |
Fuzz: random-valid input does not crash |
generate valid grammar, compile, expect no crash |
[LTES.2.2.3] Compiler tests belong with the compiler, not with the rule files. The language spec defines what the compiler must do; the test suite verifies it did it.
2. Runtime tests [LTES.2.3]#
[LTES.2.3.1] The engine itself: Rete semantics, action execution, temporal predicates, logical-fact maintenance, cross-tier flow, adapter integration, back-pressure, recovery. Driven by synthetic fact streams over a controlled clock.
[LTES.2.3.2]
Test shape |
Example |
|---|---|
Semantic: given LHS, confirm activation |
assert fact set, expect rule fire with specific bindings |
Temporal: clock-driven re-evaluation |
step clock, expect |
Adapter: mock adapter, verify emit path |
feed mocked adapter responses, confirm rule reactions |
Cross-tier: publish correctness |
drive tier A, observe tier B working memory |
Back-pressure: overflow policies |
flood queue, verify |
Recovery: an unreachable adapter |
have the call callback return a |
see also: LCRS.10.6.1
[LTES.2.3.3] Runtime tests verify the engine honours the spec. They are the substrate for the conformance suite below.
3. User rule tests [LTES.2.4]#
[LTES.2.4.1] A rule author testing their own rule file. "Given these input facts at these times under these adapter conditions, does my rule fire when I expect, and not fire when it shouldn't?"
[LTES.2.4.2]
Test shape |
Example |
|---|---|
Positive: scenario → expected reaction |
feed facts + mocked adapter state, confirm |
Negative: scenario → no reaction |
confirm rule does not fire under wrong conditions |
Temporal: time-dependent behaviour |
"5 minutes after last motion, lights turn off" |
A further shape is [LTES.2.4.4]'s.
see also: LTES.2.4.4
[LTES.2.4.4] A further test shape, beside [LTES.2.4.2]'s:
Test shape |
Example |
|---|---|
Regression: captured production trace replays |
replay real event log, confirm same outcomes |
Deferred: not binding in this edition.
see also: LTES.2.4.2
4. Conformance tests [LTES.2.5]#
[LTES.2.5.1] A portable suite of runtime tests the spec itself owns. Any conforming DS runtime must pass every test in the suite. The tests live in this repo (under the language side), are portable by design, and bind other runtimes to the spec.
[LTES.2.5.2] Two consequences:
[LTES.2.5.3] A runtime that can't run the conformance suite isn't a conforming runtime. Not a judgement — a definition.
[LTES.2.5.4] The spec must define test inputs and expected outputs in a form that doesn't privilege any single runtime. See [LTES.4] and [LTES.5] below.
The deterministic-clock contract [LTES.3]#
[LTES.3.1] Every temporal construct in DS (within, dwell, after,
cooldown, retain, window, tick-driven logical)
depends on "now." Testing temporal behaviour deterministically
requires controlling "now." This is therefore a language-
level requirement, not a runtime convenience.
Requirements [LTES.3.2]#
[LTES.3.2.1] Single clock source. The runtime has exactly one clock source, which yields both engine clocks ([LTIM.2.8.1]). Every time-dependent operation —
now(),monotonic_ns(), tick plumbing, timer firings, TTL expiry, window bounds — reads from it.
see also: LTIM.2.8.1
[LTES.3.2.2] Settable under test. The test harness can set the wall clock to any instant, forward or backward, without moving the monotonic clock. It can set the monotonic clock to any value before the engine consumes its first input, and afterwards only forward ([LTIM.2.8.2]).
see also: LTIM.2.8.2
[LTES.3.2.3] Steppable under test. The test harness can advance the clocks by a specific interval (
Δt); a step moves both clocks byΔt.
[LTES.3.2.4] Side-effect determinism. Stepping the clock by
Δttriggers all time-dependent side effects that would have fired in that interval on a real clock: tick re-evaluation for time-boundlogicalLHS tests,after-timer fact retractions,cooldownexpiry,retainexpiry, series- window slides. Stepping is atomic with respect to rule fires — the engine finishes one step's cascade before reporting the step complete.
[LTES.3.2.5] Wall-clock independence under test. The engine must not consult wall time for any language-level decision when under test-mode clock. Runtime-internal timers that are not user-observable (e.g. socket timeouts) may still use wall time.
Spec consequences [LTES.3.3]#
[LTES.3.3.1]
now()at the language level reads "the engine's clock," never literally "wall time."
[LTES.3.3.2] A pump is under the deterministic clock: a test stepping by 5 s pumps at that instant ([RTIM.14.5]), whatever wall time passed.
see also: RTIM.14.5
[LTES.3.3.3] Re-evaluation of time-bound tests is observable: the engine reports which time-bound activations it re-examined on a step, so tests can verify that a
logicalfact was retracted because itswithinbound expired rather than for some other reason.
Deferred: not binding in this edition.
Non-test mode [LTES.3.4]#
[LTES.3.4.1] In non-test mode the clock is wall time; this contract does not require any production behaviour change. The contract is a hooks contract: under test, the harness takes over.
The adapter-mock contract [LTES.4]#
[LTES.4.1] External adapters are the primary source of untestability in a rule language — they involve real IO, real network, real external state. DS requires every adapter-facing interaction to be mockable.
Requirements [LTES.4.2]#
[LTES.4.2.1] All adapter dispatch is table-driven.
calldispatches via the engine-resolved(adapter, domain, method) → adapter_idtable. No adapter is reachable through a side channel the engine doesn't know about. [LCRS] already requires this; tests rely on it.
[LTES.4.2.2] Mock adapter registration. The test harness registers mock adapters that expose service manifests, response schemas, adapter contracts (per [LADP]) — same surface real adapters use. The engine can't tell the difference.
[LTES.4.2.3] Mock response injection. For any
(adapter, domain, method)plus optional correlation-id, the test harness declares what response the mock returns and on what clock delay (for testingon timeoutcontinuations). Mocks may be stateful (return different values on successive calls) or stateless.
[LTES.4.2.4] Mock emission injection. A mock adapter can emit facts at test-chosen times, driving the tier's working memory exactly like a real adapter would.
[LTES.4.2.5] Call observation. The test harness records every outgoing
callthe engine issues — adapter, domain, method, args, correlation id, fire time — available for assertions.
Spec consequences [LTES.4.3]#
[LTES.4.3.1] The spec owns the adapter contract (service manifest format, response-schema grammar, adapter modes) at a level of detail sufficient for a test harness to implement mocks. No runtime-specific bits leak into what mocks must honour.
[LTES.4.3.2] "Adapter said X" is an assertable fact; "adapter never said X" is too. Temporal assertions ("adapter said X within 500 ms of clock step") compose with the deterministic-clock contract.
[LTES.4.3.3] Adapter-specific conveniences (HA-style entity discovery, MQTT retained messages) are accessible through the mock contract: the test harness can replay the discovery burst, flip the
retainedflag, simulate adapter reconnect (per [LADP]) without running a real adapter.
The event-trace primitive [LTES.5]#
[LTES.5.1] An event trace is the canonical input a test drives the engine with. What a test authors is the inject-class part of one — a time-ordered sequence of fact assertions, retractions, clock advances and adapter events ([RFCS.17.1]). The compare-class events are what the engine emits in response ([LFST.1.3]), and what the test then checks.
Shape [LTES.5.2]#
[LTES.5.2.1] Abstractly, an event trace is a list of events:
<event-trace> ::= <event>*
<event> ::= "at" <instant> <op>
<op> ::= "assert" <schema> <slot=value>*
| "retract" <schema> <selector>
| "advance" <duration>
| "adapter-response" <corr> <response-or-error>
| "adapter-emit" <adapter-id> <schema> <slot=value>*
<instant> ::= <wall-time-ish string> | <Δ-from-start>
[LTES.5.2.2] What matters here is the shape: the primitives the runtime
must accept. The author-facing syntax over those primitives is
.dstest ([LTSL.2]). Any test harness — user-rule tests, conformance
tests, replay of captured production traces — is driven by a fact
stream.
see also: LTSL.2
Requirements [LTES.5.3]#
[LTES.5.3.1] Runtime-driveable. The runtime accepts an event trace as input in test mode: stream advances drive the clock; stream asserts/retracts drive working memory; stream adapter-response/emit events drive mocks.
[LTES.5.3.2] Deterministic. Given the same event trace and the same compiled rule set, the runtime must produce the same observable output (rule activations, emits, calls, cache operations, operator-log entries) on every run, byte-for-byte. No hidden non-determinism, within a platform or across platforms: every v1 result is cross-platform bit-identical ([LDET.3.4.1]), so a harness compares the same way in both cases. See [LDET] for the full guarantee, implementation commitments, and rule-firing-order specification.
see also: LDET.3.4.1
[LTES.5.3.3] Capturable. A running engine must be able to record its activity as an event trace suitable for later replay. This is the primitive for regression tests against real production activity.
Spec consequences [LTES.5.4]#
[LTES.5.4.1] Determinism at the event-trace level is a non-negotiable language guarantee. Implementations may not use map- iteration order, unordered hash-set traversal, or other hidden non-determinism sources when driving rule fire or producing outputs.
[LTES.5.4.2] Rule-firing order among activations that match at the same clock instant is spec-defined: the total order of [LDET.5.1] — the terminal ordinal, which is source order, then activation-creation order within one rule. The runtime's conflict resolver must honour it.
see also: LDET.5.1
[LTES.5.4.3] Recording is lossless: capturing a live session and replaying it must produce bit-for-bit identical observable output.
Introspection primitives [LTES.6]#
[LTES.6.1] Tests assert against what the engine did. The runtime must expose an introspection surface sufficient for every assertion kind the language can imply.
Required observables [LTES.6.2]#
[LTES.6.2.1] Activations. For each rule, which activations fired between clock T0 and T1, with what bindings.
[LTES.6.2.2] Emissions. Which facts were asserted / retracted by rule actions, with asserting-rule identity.
[LTES.6.2.3] Calls. Which
callactions fired, their args, correlation ids, and which responses returned.
[LTES.6.2.4] Cache operations.
cachewrites,cache_deleteremovals, andcached()reads, with keys, values, and caller identity.
[LTES.6.2.6] Logs. Every
logmessage with interpolated content.
[LTES.6.2.7] Logical-fact lifecycle. Assert, retract, flip-count per logical fact; the justifier-end vs hydration-ttl vs explicit-retract cause per [LLOG].
[LTES.6.2.8] Timer and continuation activity.
aftertimers asserted, fired, cancelled, and superseded (per [LRST]);on-continuation matches (per [LCCO]).
[LTES.6.2.10] Cross-tier flow. Facts that crossed tier boundaries in either direction, with TTL and (when logical) explicit- retract events.
[LTES.6.2.11] Compile warnings / errors. On compile, the set of messages produced with source location.
Query model [LTES.6.3]#
[LTES.6.3.1] Tests assert against observables via a query model that, abstractly:
[LTES.6.3.2] Selects by clock interval.
[LTES.6.3.3] Filters by rule / adapter / schema / pattern.
[LTES.6.3.4] Counts, enumerates, or summarises.
[LTES.6.3.5] The concrete query API is a runtime concern; the set of observables above is a spec commitment. Any runtime must expose every observable in this list; extensions are allowed.
The .dstest test surface [LTES.7]#
[LTES.7.5] The DS author-facing test surface is .dstest — a declarative
input→output relation over a program, specified in
[LTSL] and compiled to the Test* / Oracle*
test atoms ([RDSC.7.1]). A test is the top-level test block ([LGRA.21]), written
inline in a .ds beside the rules it exercises or collected in a .dstest file;
one grammar backs both. A test needs no capability directive — it is bound to its
project and adopts the program-under-test's dialect ([LTSL.2]).
see also: LGRA.21, LTSL.2, RDSC.7.1
[LTES.7.6] .dstest is the author-facing test surface. A
production runtime skips the Test* / Oracle* atoms it does not run
(skip-don't-reject, [RDSC.9.4]), so a bundle carrying them costs it only
file size.
see also: RDSC.9.4
[LTES.7.7] The .dstest interpreter runs over the substrate this doc specifies —
the single deterministic clock, the event-trace primitive, and the introspection
surface — driving the real engine through them, never a shadow evaluator. The
runtime interpreter contract is [RTIM.14].
see also: RTIM.14
Conformance suite [LTES.8]#
[LTES.8.1] The language spec owns a portable conformance test suite. It lives in this repo (language side, not runtime side) and binds every DS runtime.
Commitments [LTES.8.2]#
[LTES.8.2.1] Portable by construction. Tests are written against language primitives: event traces, introspection queries, mock adapter contracts. No runtime-specific bindings.
[LTES.8.2.2] Exhaustive for every observable behaviour. Every observable behaviour gets conformance coverage. A "specified behaviour with no test" is a gap, and is recorded as one ([ISTS.5.3]).
see also: ISTS.5.3
[LTES.8.2.3] Versioned with the spec. Conformance is always against a specific spec version. Major revisions retire tests; minor revisions add them.
[LTES.8.2.4] Reference-runtime-tested. The reference runtime in
../../runtime/passes the suite on every commit. A regression there is either a runtime bug (fix the runtime) or a spec clarification (update the spec; update the test).
Layout [LTES.8.3]#
[LTES.8.3.1] The conformance suite is the fixture corpus at
specs/conformance/, one directory per category, with the harness that runs
it beside them:
Directory |
Holds |
|---|---|
|
compile-time tests |
|
engine-semantic tests |
|
clock-driven behaviour |
|
adapter-contract conformance |
|
truth-maintenance |
|
fact-flow rules |
|
the driver the conformance tests run under |
[LTES.8.3.2] The fixtures are defined independently of any runtime, so any runtime can adopt them; the harness ([LTES.8.3.1]) drives each runtime under test through that runtime's own interface.
see also: LTES.8.3.1
What the reference runtime must provide [LTES.9]#
[LTES.9.1] Concretely for the reference runtime at ../../runtime/:
[LTES.9.2] Test-mode engine entry point that disables wall-clock, starts the clock at a specified instant, registers mock adapters, accepts event-trace input, and exposes introspection queries.
[LTES.9.3] Conformance-suite runner that loads the language-side
specs/conformance/*and runs every test against the runtime, with clean pass/fail output.
[LTES.9.4] Event-trace recorder that captures live engine activity in a replayable format.
[LTES.9.5] Compiler test harness that drives the compiler with fixture input and diffs against golden output.
[LTES.9.6] User-rule test runner that loads a user's
.dsruleset and the accompanying.dstestfiles ([LTSL]), runs each scenario, and reports results.
[LTES.9.7] Coverage tracker that reports which rules in a rule set fired during a test run and which didn't (dead-rule hint).
Deferred: not binding in this edition.
[LTES.9.8] See [RTIM] for the concrete API and test-file formats the reference runtime chooses.
Interaction with other language features [LTES.11]#
With activation-scoped facts and supersedure [LTES.11.1]#
[LTES.11.1.1] Activation-scoped facts (scope activation per
[LSCP]) introduce
activation-bound fact lifetimes. The
as <expr> clause on after per
[LRST] introduces per-fact supersedure
on author-named correlation keys. Tests must be able to:
[LTES.11.1.2] Observe
scope activationfact assertion and retraction events on every completion path of an activation.
[LTES.11.1.4] Assert that
after as <key>re-fires supersede prior timers with the same key.
[LTES.11.1.6] The introspection-primitive list above covers it; no additional testing mechanism is required beyond honouring the observability contract.
With series and aggregations [LTES.11.2]#
[LTES.11.2.1] Series windows (mean(s), p95(s)) respond to clock
progression. The deterministic-clock contract covers the time
axis; the event-trace covers the sample-input axis. Tests
assert aggregation values at specific instants after specific
advances.
With fact hydration [LTES.11.3]#
Deferred: not binding in this edition.
[LTES.11.3.1] Hydration (per [LHYD])
brings historical records into working memory via a call
response. Tests mock the hydration adapter the same way any
other adapter is mocked; the hydration-corr slot is introspectable
on hydrated facts.
With cross-tier flow [LTES.11.4]#
[LTES.11.4.1] A multi-tier test runs multiple tier engines under one shared clock and one shared test harness. Cross-tier emissions are observable as they cross boundaries; the event-trace format identifies the originating tier for each event.
Testing commitments [LTES.12]#
[LTES.12.1] Testing is first-class. Any behaviour the language specifies is observable and assertable. Untestable constructs are spec bugs.
[LTES.12.2] Four test kinds distinguished. Compiler tests, runtime tests, user rule tests, conformance tests. Each has its own shape but they share the clock + adapter-mock + event-trace
introspection foundation.
[LTES.12.3] Deterministic clock is a language-level contract. Every temporal construct reads from the single clock; under test the harness controls it; clock stepping is atomic with respect to rule fires; no wall-clock leakage in test mode.
ratified
[LTES.12.4] Adapter mocking is a language-level contract. All adapter dispatch is table-driven; mocks register at the same contract real adapters honour; responses and emissions are test-injectable; calls are observable.
ratified
[LTES.12.5] Event-trace primitive. Time-ordered events (assert/retract/advance/adapter-response/adapter-emit) drive every test. Determinism guaranteed. Recording is lossless so live sessions replay bit-for-bit.
ratified
[LTES.12.6] Introspection primitives enumerated. Activations, emissions, calls, cache ops, record-events, logs, logical lifecycle, timers, type-mismatch increments, cross-tier flow, compile diagnostics. Runtime must expose all; may expose more.
[LTES.12.7]
.dstestis the test surface. The author-facing test surface is.dstest([LTSL.2], [LTES.7]).
ratified — see also: LTES.7, LTSL.2
[LTES.12.8] Conformance suite lives on the language side. Portable, versioned with the spec, tested against the reference runtime on every commit. Other runtimes adopt it to claim conformance.
[LTES.12.9] Rule-firing order is spec-defined, not implementation- defined. When multiple activations match at the same clock instant, the order they fire in is the total order of [LDET.5.1] — the terminal ordinal, which is source order, then activation-creation order within one rule. No dependence on hash ordering, and none on the order matches happen to COMPLETE: which is why the ordinal is the first key and not the last. Restates [LTES.5.4.2].
ratified — checks: idris — see also: LDET.5.1, LTES.5.4.2