> ## Documentation Index
> Fetch the complete documentation index at: https://docs.expectedparrot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing and serialization

> Test a machine without a survey and preserve its definition as versioned JSON.

This chapter continues the [shared-state manual](https://docs.expectedparrot.com/en/latest/shared_state.md).
The poll examples build on the `activity_poll` definition in [Machines, commands, and views](https://docs.expectedparrot.com/en/latest/shared-state/machines.md). Alternative fragments are labeled separately.

## Testing without a survey

A machine is testable as an ordinary transition system:

```python theme={null}
from edsl.sharedstate import Runtime

runtime = Runtime()
state = runtime.initial_state(activity_poll)

state = runtime.execute(
    activity_poll,
    state,
    "vote",
    {"voter": "Amina", "activity": "hike"},
).state

state = runtime.execute(
    activity_poll,
    state,
    "vote",
    {"voter": "Boris", "activity": "sailing"},
).state
```

The state is now:

```python theme={null}
{
    "votes": {
        "Amina": "hike",
        "Boris": "sailing",
    }
}
```

Trying to change Amina's vote leaves it unchanged because `once=True`.

Render the visible data separately:

```python theme={null}
runtime.render_view(activity_poll, state)
```

```python theme={null}
{
    "votes": {
        "Amina": "hike",
        "Boris": "sailing",
    },
    "vote_count": 2,
}
```

These calls are useful for unit tests. Production execution performs the same
transition through a transactional store.

## Serialization

Machines and state maps use versioned JSON representations:

```python theme={null}
payload = activity_poll.to_dict()
restored = Machine.from_dict(payload)
assert restored.to_dict() == payload
```

An input reference becomes data resembling:

```json theme={null}
{
  "op": "ref",
  "namespace": "input",
  "name": "activity"
}
```

The payload contains no executable Python. Serialization is why the DSL uses
`field("votes")` rather than a closure and `arg("activity")` rather than a
lambda.

Persisted values follow a deliberately narrow JSON contract:

* mapping keys are strings;
* numbers are finite, so `NaN` and infinity are rejected;
* expression and effect operators have validated names and arities; and
* symbolic conditions use `&`, `|`, and `~`, not Python `and`, `or`, and `not`.

Python Boolean operators attempt to ask an expression for its truth value while
the definition is being built. EDSL raises a `TypeError` instead of accidentally
collapsing a symbolic condition to `True` or `False`.

`Machine` and `SharedStateMap` currently serialize as version 1. Unsupported
versions fail explicitly rather than being interpreted as the current language.

## Using the `ep` CLI

Write a serialized machine to a JSON file, then validate and inspect it without
loading the authoring code:

```python theme={null}
from pathlib import Path

Path("machine.json").write_text(activity_poll.to_json())
```

```bash theme={null}
ep state validate --file machine.json
ep state inspect --file machine.json
```

To exercise a sequence locally, put transitions in a JSON file. Each transition
names a command and supplies its inputs:

```json theme={null}
[
  {"command": "vote", "inputs": {"voter": "Amina", "activity": "hike"}},
  {"command": "vote", "inputs": {"voter": "Boris", "activity": "sailing"}}
]
```

```bash theme={null}
ep state simulate --file machine.json --transitions transitions.json
```

The output includes each event, resulting state, rendered view, and completion
status. This is a local dry run of the definition; it does not write to a
shared-state backend. For a completed job, inspect the persisted bindings and
snapshots with:

```bash theme={null}
ep results state --file results.ep
ep results state --file results.ep --binding 0
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.