# Testing with scenarios

A scenario is a JSON file of steps that drives your game in a real browser and checks what happens. Time only moves forward through `wait` steps, so runs are deterministic.

```json
{
  "$schema": "../node_modules/@debuffjp/cli/schema/scenario.schema.json",
  "steps": [
    { "press": "ArrowRight" },
    { "wait": 30 },
    { "release": "ArrowRight" },
    { "expect": { "state.player.x": { "gt": 200 } } },
    { "set": { "player.speed": 400 } },
    { "capture": "after-move" }
  ]
}
```

Save it in `scenarios/`, then run:

```bash
npx debuff scenario            # all scenarios
npx debuff scenario --update   # accept new screenshots as baselines
```

## Steps

| Step | Effect |
| --- | --- |
| `{ "wait": n }` | Advance `n` fixed simulation steps |
| `{ "press": code }` / `{ "release": code }` | Hold or release a key |
| `{ "tap": [x, y] }` | Tap the pointer at logical coordinates |
| `{ "pointerDown" / "pointerMove": [x, y] }`, `{ "pointerUp": true }` | Pointer control |
| `{ "set": { "group.key": value } }` | Change params |
| `{ "expect": { path: comparison } }` | Assert on state: `eq`, `gt`, `gte`, `lt`, `lte`, `approx`. A bare value like `0` is an error (before 0.16 it threw a TypeError or passed silently) |
| `{ "until": { path: comparison }, "within": n }` | Play until the conditions hold, failing after `n` steps. `events.` counts `game.emit("")` calls, so you wait for what happened instead of guessing a time |
| `{ "command": name, "args": [...] }` | Run a dev command your game registered (warp to a level, set up a board) |
| `{ "capture": name }` | Screenshot compared with the baseline |
| `{ "flashes": n }` | Play `n` steps and fail if anything flashes more than three times a second over a large area (photosensitivity) |
| `{ "contrast": [x, y, w, h] }` | Fail if text in that region is below 4.5:1 against its background (`"min"` changes the bar) |
| `{ "checkpoint": { part: hash } }` | Written by recorded replays from 0.31, one a second: a hash of each part of the state at that tick. The first that differs adds a [warning](#warnings) |

`expect` and `until` paths read from the dev state: `scene`, `tick`, `stats.fps`, `state.*` (your scene's `inspect()`), `events.*`, and `plugins.*`.

## Screen size and motion

Scenarios run in a 960x540 browser window. If your game is a different shape, a portrait phone game say, set the size for the whole project in `debuff.config.ts`, so captures and contrast checks see the game at full size (`debuff fuzz` uses it too):

```ts
import { defineConfig } from "@debuffjp/cli";

export default defineConfig({
  scenarios: { viewport: [360, 640] },
});
```

A single scenario can override it, and can run with the browser asking for reduced motion:

```json
{ "viewport": [1280, 720], "reducedMotion": true, "steps": [] }
```

Baselines are stored per operating system, because fonts render differently on each.

## Renderer {#renderer}

By default the browser draws with this machine's graphics card, so captures can differ between machines. From 0.28 you can draw with Chrome's software renderer, SwiftShader, instead. Fuzz runs and headless agent sessions use it too:

```ts
export default defineConfig({
  scenarios: { renderer: "software" },
});
```

Switching can change captures, so run `npx debuff scenario` and check the result. Update baselines only if you approve the change.

## Warnings {#warnings}

From 0.28, a scenario's report has a `warnings` list: console errors and failed requests. A missing file shows up here even when the dev server answers it with its HTML page and a 200. Warnings don't fail the scenario.

From 0.31, a replay saved from the overlay or with `game_replay` holds a checkpoint every 60 ticks. When the game's state at one no longer matches the recording, the report warns once, with the tick and the parts that differ:

```text
! the game's state stopped matching the recording at tick 180: state.player differs (it matched at tick 120).
```

Parts are `scene`, `events`, each top-level key of your `inspect()` as `state.`, and `plugins.`, so a richer `inspect()` points closer to the cause. After you change the game, a drift is expected. Without a change, the replay isn't reproducing what you played: look for `Math.random()`, `Date.now()` or real-time timers in game logic, a load finishing on a different frame, or logic that depends on the window size. Rewinding in the overlay checks the same checkpoints and notes a drift in the activity log.

## When the game hangs or crashes {#hangs-and-crashes}

From 0.28, a step that takes over 60 s fails with "The game stopped responding" and ends that scenario, and a page crash does the same. If the step is just slow, raise the limit in seconds with `"stepTimeout": 120` at the top of the scenario. Fuzz runs and headless agent sessions fail with the error code `game_hung` after 60 s.

Before 0.28, a game stuck in a loop hung the run forever.

## When the game won't start {#when-the-game-wont-start}

Each scenario waits up to 15 s for the game's dev hooks. From 0.26.1:

- If the game throws while loading, that scenario fails and lists the error under runtime errors. The other scenarios still run.
- If the page loads without errors but never exposes `__DEBUFF_DEV__`, the run stops with `dev_hooks_missing` (exit code 2) and the line to add to `src/main.ts`.

Before 0.26.1, both cases ended the run with an `unexpected` error from a 15 s timeout.

From 0.32, `dev_hooks_missing` says which cause it found and gives the fix for that one: nothing in `src/` imports `@debuffjp/dev`, a request failed or the console logged an error while the page loaded (the first one is quoted), the page's URL lost `?debuff-scenario` before the dev tools started, or the game waits for something (a click, a sound unlock, a slow load) before installing them. Before 0.32 every case said the page loaded without errors and suggested adding `devTools` to `src/main.ts`, even when the game already had it.

## When no browser starts {#when-no-browser-starts}

`debuff scenario`, `debuff fuzz`, `debuff doctor` and the MCP server's browser tools drive an installed Chrome, then Edge, then Playwright's own Chromium, using the first that starts. To choose one, set `DEBUFF_BROWSER` to the path of a Chromium-based browser.

From 0.32, when none starts the error (`browser_not_found`) says what was tried: Chrome and Edge with the path Playwright looked in, and Playwright's Chromium with the browser's own error when it is installed but won't start (a missing shared library, say). It also says whether `DEBUFF_BROWSER` is set. If that variable points at a browser that won't start, the message names it and its path and says to correct or unset it. Before 0.32 the message said "No Chromium-based browser found" and repeated one fix, even when a browser was installed and failing.