---
name: ferret-ferretic
description: Use to check how a Ferret game plays by playing it with a script in Ferretic, the engine's BASIC-like test language: the game's actions pressed in the fixed step, waits on time, state and events, EXPECT, LOG and SAMPLE tables, RUNS; test.run and tests kept in the project.
---

# Playing a game by script (Ferretic)

## When to use
Behaviour checked by playing, where timing or state matters: a race, a jump from a run-up, a lever then a door, an AI reaching a place, a balance change. Arithmetic on paper is no check, nor are keys pressed one tool call at a time (real time passes between). A Ferretic program runs inside the fixed step: the same program on the same game gives the same run.

## The model
- `test.run {source | file}` plays a program in the running game and returns its report: `id`, `status`, `expectations`, the failures' number and the first one (line, code, message with the compared values), `log` (last values), with RUNS a `summary`, and the sizes of the tables; read the tables (`samples`, `log`, `prints`, `failures`) with `test.rows {id, table, offset?, count?}`. A run has no limit of its own; any number may run at once. The game is paused at the end; held actions are let go.
- The program goes on until it WAITs; then game steps pass. Names: an entity's id or name, or a node's name. Values only the game's code knows: `world.expose('rpm', () => car.rpm)` in the game, `GAME("rpm")` in the test. Events: `WAIT EVENT`, `ON EVENT`.
- Tests belong to the project: `tests/<what>.ferretic`, run with `viewer_command test.run {file}`; in a checkout, `ferret scene <example> --test <file>`.
- Probes answer facts without a picture: `INVIEW VIEWSHARE VISIBLE OCCLUDER COUNT ONGROUND HEIGHT ERRORS` (`probe.*`).

## Steps
1. `input.actions` (what the game binds) and `scene.entities` (names). If the check needs a value only the game's code knows, add `world.expose(...)` for it in the game's code (it costs nothing when no test reads it).
2. Write the test as a file in `tests/`, one behaviour a file, with EXPECTs that say in words what must hold.
3. `viewer_restart`, then `viewer_command test.run {file}`. Read the failures and the values; for motion, `SAMPLE EVERY` and read the table (`test.rows`); for looks, `SHOT` and `test.shot`.
4. Change the game, run the same file again, compare. For anything random, `RUNS 5` and read `summary`.
5. Keep the tests that guard what was asked; after a change run the test of what changed.

## The language
`reference/ferretic.md` has all of it; `test.help` gives one page. The shape of a test:
```
SAMPLE EVERY 0.25 s z = POS("car").z, rpm = GAME("rpm")
PRESS throttle
PARALLEL ANY
  WAIT EVENT "race:finish"
AND
  DO
    WAIT UNTIL GAME("rpm") >= 7000
    TAP shiftUp
  LOOP
END PARALLEL
LOG time = TIME
EXPECT TIME < 14 s, "the quarter mile in under 14 s"
```

## Check afterwards
The report's `status` is `passed` and each EXPECT you rely on is counted in `expectations.passed`; the numbers in `log` and `samples` are the ones the request is about (a time, a distance, a speed), not only "no error". A failing test says what failed in its message; a test that cannot fail checks nothing.

## What usually goes wrong
- `no action "x"`: the names are the game's actions (`input.actions`), not keys.
- A value only the game's code knows is not exposed: `GAME("rpm")` says which names are; add `world.expose` in the game.
- A run that does not end: a WAIT UNTIL that never comes true, or a loop that never WAITs (it holds the game). `test.state` shows where it is; `test.stop` ends it; a WAIT with TIMEOUT and an EXPECT on TIMEDOUT makes such a case a failure instead.
- Comparing two runs of a game that uses its own random numbers: use `RUNS n` and the summary, not one run.
- Leaving the game paused: the report says `paused`; `world.pause {paused: false}` or `viewer_restart` before playing by hand.

## Files

- `reference/ferretic.md` — Ferretic, the engine's BASIC-like language for playing a game by script: statements, waits, the game's actions and state, checks, tables, the test.* commands and tests kept in a project.
- `reference/commands.md` — The debug command channel: how a tool, the inspector or an AI agent drives a running game, and the commands each package registers.
- `reference/api-ferretic.md` — every export of `@ferret/ferretic` with signature and doc comment
- `reference/api-inspector.md` — every export of `@ferret/inspector` with signature and doc comment
- `reference/api-core.md` — every export of `@ferret/core` with signature and doc comment
- `reference/api-core-2.md` — every export of `@ferret/core` with signature and doc comment
- `reference/commands.md` — 64 commands of these packages with their arguments
