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.
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,
invokeonDone/onErrorand singleaftertimers (as external events, once per visit),always, compound-stateonDone,raise,enqueueActions(viaassignAction,raiseAction,ifAction,enqueueAction), and context-writingentry/exitfolded into XState's macrostep. - Refusals are explicit. What a chart cannot run the same way, and any key
fromXStatedoes not read, is refused when built with the reason. - CLI comments on XState files. Prefer
fromXState+prove+conforms, or prove a nativechart()/rule()directly. In the CLI,npx gdp-wired-cli checkon an XState file checks// gdp: lawcomments on the extracted model only and reports them asunknown/unchecked(exit2), because XState extraction checks conformance viaconformsrather 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.