gdp-wired
Recipes · XState & model-based tests

GuideRecipes

With XState, and model-based tests.

The proof covers the logic; a model-based test checks that the app around it follows the proven rule. gdp-wired/xstate does both without adding xstate or @xstate/graph to gdp-wired's runtime dependencies.

On this page
  1. End-to-end example
  2. Existing XState machines
  3. Model-based tests (@xstate/graph)
  4. Counterexamples as regressions
  5. Offline trace conformance
  6. The division of labour

End-to-end example#

An XState machine you already ship becomes a chart with fromXState, its laws proven and the chart held to XState's own actor with conforms. Any rule or chart (your app needn't use XState) becomes XState logic with toLogic, so @xstate/graph generates test paths from the proven rule and drives your real app along them; runToEvents turns a broken variant's run into a regression test, and conformTrace from gdp-wired/trace (or npx gdp-wired-cli conform <file> --trace <run.jsonl>) checks recorded JSONL execution traces against any rule or chart offline. More in docs/xstate.md.

ts
import {createActor} from 'xstate'
import {TestModel} from '@xstate/graph'
import {conforms, fromXState, runToEvents, toLogic} from 'gdp-wired/xstate'

const c = fromXState(orderMachine, {guards: {...}, actions: {...}})   // the machine you ship
c.prove([], [neverAfterShipping])                                     // {proven: true, checked: true}
conforms(c, orderMachine, createActor)                                // the chart against XState's own actor

const logic = toLogic(order)                                          // the proven rule as XState logic
const model = new TestModel(logic, logic.options)
for (const path of model.getShortestPaths())                          // every state the events reach
  await model.testPath(path, {states: {'*': snap => expectAppMatches(snap)},
                              events: {pay: () => app.pay(), ship: () => app.ship()}})

const v = brokenOrder.prove([], [neverAfterShipping])                 // the bug you keep, refuted with a run
const [regression] = model.getPathsFromEvents(runToEvents(brokenOrder, v.run!))
await model.testPath(regression!, harness)                            // fails the buggy app, passes the fixed one

An existing XState machine: fromXState and conforms#

When you already ship an XState v5 (or v4) machine, fromXState(machine, {guards, actions}) translates it into a chart() whose laws the kernel proves; conforms(chart, machine, createActor) runs XState's own actor beside the compiled rule on random events so any mismatch in a guard or action shows up as a concrete run (examples/xstate.ts).

  • What it reads. Nested states, parallel roots and nested parallel states (including disjoint parallel context writes on the same event), deep history, final states, targetless (internal) transitions, named guards and actions with params, invoke onDone/onError and single after timers (as external events, once per visit), always, compound-state onDone, raise, enqueueActions (via assignAction, raiseAction, ifAction, enqueueAction), and context-writing entry/exit folded into XState's macrostep.
  • Refusals are explicit. What a chart cannot run the same way, and any key fromXState does not read, is refused when built with the reason.
  • CLI comments on XState files. Prefer fromXState + prove + conforms, or prove a native chart() / rule() directly. In the CLI, npx gdp-wired-cli check on an XState file checks // gdp: law comments on the extracted model only and reports them as unknown / unchecked (exit 2), because XState extraction checks conformance via conforms rather than native parameter-capture source-law replay.

Model-based testing with @xstate/graph#

You do not need XState in your application to use @xstate/graph for model-based testing. toLogic(ruleOrChart, {params?, events?, maxValue?}) wraps any proven rule() or chart() as XState v5 ActorLogic. Each step runs the proven rule via rule.interpreter() (copied states, never the in-place rule.compiled() state), refused requests leave the snapshot unchanged, and serializeState serializes scalars, maps and logs canonically.

Pass logic and logic.options to new TestModel(logic, logic.options) to generate shortest paths and drive your real UI, database or service along them (examples/xstate-mbt.ts). getShortestPaths() covers every state reachable with the enumerated events; the proof covers the rest. To also exercise rejected or ignored self-loops within that graph, extend each shortest path with its outgoing transitions from model.getAdjacencyMap().

Counterexamples as regression paths#

runToEvents(ruleOrChart, run) turns a refuting run from proveRule, proveSteps, chart.prove, minimizeRun, or search into XState event objects {type, ...payload} ready for model.getPathsFromEvents(...). Keep your broken variant next to the proven rule, and its counterexample automatically becomes a regression path against your real application.

Offline trace conformance#

conformTrace(chartOrRule, jsonl, {laws?, strict?}) from gdp-wired/trace and npx gdp-wired-cli conform <file> --trace <run.jsonl> check recorded JSONL execution traces (including XState {value, context} snapshots) against any rule or chart offline, and report the exact diverging step, line, and field.

The division of labour#

The proof covers the logic

proveRule, proveSteps and chart.prove cover every reachable (or arbitrary {from: 'any'}) state, every unbounded natural, every map key, and every interleaving within the rule — with a kernel-checked certificate that Lean checks again.

Model-based testing covers the app

toLogic, runToEvents and @xstate/graph check the UI wiring, database transactions, serialization, HTTP handlers and integration boundaries that sit outside the rule, along shortest paths and proven counterexample regressions.