# Verification

The harness plays the real game in a real Chrome and checks what it observes. Run it after every change to gameplay, rendering or UI.

## Run

```
scripts/verify.sh <outdir> [seed]
```

Needs `go`, `node`, `ffmpeg` (for the recording), `python3`, and `agent-browser`. The default seed is 7. The script:

1. Builds and starts the server on a free port with `--seed`, through `scripts/serve.sh`, which both launchers share.
2. Opens Chrome with software WebGL (SwiftShader), so frames are the same on every machine and slow on purpose. By default the window is headed on `DISPLAY` (default `:0`) and appears on the desktop. Do not touch it while it runs. `AGENT_BROWSER_HEADED=0` runs it headless, off the desktop.
3. For each of 1280x720, 1920x1080, 2560x1440, 1366x768, 800x600 and 390x844, resets the campaign to the seed and plays through input only: clicks Begin, presses T at the wagon and clicks a potion, clicks a ground tile, presses I, J and Escape, then clicks the first main POI, clicks Prepare and back, and clicks Fight. Then the careful bot in `scripts/bot.mjs` plays a few steps with real clicks. Setup through the API walks until dusk, the page reloads, and the harness clicks Make camp and checks the new day and full health. The bot then wins the campaign through the API and the page reloads to the victory screen. Finally a level 1 party walks into the hardest place through the API and the page must show the defeat screen.
4. Exits non-zero if any assertion failed.

`VERIFY_ONLY=1280x720,390x844` limits the resolutions. `VERIFY_STEPS=n` caps the bot steps played through the UI at 1280x720 (default 10, 2 elsewhere).

## Playthrough

```
scripts/playthrough.sh <outdir> <seed> [width height name]
```

Plays one full campaign on `<seed>` through input only, with the careful bot from `scripts/bot.mjs`: it fights a threat at 80% odds or better and backs off otherwise, also backs off from a fight that would cost over half its health while hurt, saves the boss until nothing else is left or the party is at full health, levels up on easier places and bands, drinks or buys potions by day, and walks until dusk to make camp when hurt. On the first threat card it clicks Prepare and clicks pack items that beat the equipped ones. It records `<name>.webm`, saves screenshots and `<name>.md`/`<name>.json`, and exits non-zero on any failed step, a client and server mismatch, a layout problem, a console error, or an ending other than victory. `PLAY_MAX_STEPS` caps bot steps (default 160).

## Output

- `report.md` and `report.json`: per resolution, a table of every interaction with the server state after it (from `/api/state`), the layout checks and console errors.
- `screens/<WxH>/NN-*.png`: title, explore, trader, after-move, inventory, journal, threat, prepare, combat, night, camp, dawn, victory, new-game, defeat.
- `recording.webm`: the whole 1280x720 session.
- `server.log`.

## Assertions

- After each input, `/api/state` must show the expected change (party moved, battle counted, mode changed) and the client party position from `window.__game.state()` must equal the server's.
- Layout, read from `window.__game.rects()` only: every `essential` rect is fully inside the viewport, and no two rects partly overlap (full containment is allowed). Checked on title, explore, inventory, journal, threat card, combat, trader, night HUD, victory and defeat screens.
- The page must log no console errors or uncaught exceptions.

Before the resolutions, verify plays seeds 1 to 5 to the end through the API with the careful bot, which equips the gear it finds. `scripts/fights.mjs` replays each fight's log through `web/js/playback.js`, the same code the client uses to show hp. Each replay must end at the server's hp. No fallen unit may act or be targeted, and heals and buffs must stay on the caster's side. The step fails if no heal or buff came up. Run it alone with `node scripts/fights.mjs <base> [seeds]`.

Every fight the harness clicks also checks the playback. When the fight ends, the client's shown hp for each hero who did not level up must equal the server's, and every foe must show 0 after a win. `window.__game.drift()` lists any difference, and `driver.mjs` fails the step on it. This catches a client that plays an event as the wrong kind, such as a heal played as an attack.

`TestCombatLogFollowsTheRules` in `internal/sim` replays the log of about 600 seeded fights, with fallen heroes and gear that grants Rally, Mend and lifesteal. Nobody fallen acts or is targeted, heals and buffs stay on the caster's side, damage only crosses sides, and the replayed hp equals the fight's result. It fails if any of those cases never came up.

## Reviewing the output

The harness cannot judge looks. After a run, read every PNG for at least 1280x720, 800x600 and 390x844, and the recording frames, and write down what is wrong: clipped text, unreadable sizes, empty panels, UI covering the board, an unplayable end state. Assertions passing does not mean the UI is good.

## `window.__game`

Defined in `web/js/debug.js`, installed after boot.

| Member | Meaning |
| --- | --- |
| `mode` | `loading`, `title`, `explore`, `threat`, `prepare`, `combat`, `camp`, `end` |
| `busy` | true while a move, walk or fight animation runs |
| `drift()` | hp the last fight showed that differs from the server, empty when the playback is right |
| `state()` | phase, seed, party x/y/gold/potions/inventory/unit levels and hp, threat, shop, clock, summary, game, feedback |
| `perf()` | frame time over the last 240 frames (`fps`, `p50`, `p95`, `max` in ms), draw calls, triangles and the GPU name |
| `rects()` | `[{name, x, y, w, h, essential}]` in CSS pixels |
| `setRects(fn)` | replace the rect provider (use when the UI is drawn in Three.js) |
| `tileScreen(x, y)` | screen position of a tile, for real clicks |
| `focusTile(x, y)` | move the camera to a tile; `focusTile(null)` releases it |
| `pickAt(x, y)` | the hex under a screen point, so clicks can avoid hexes hidden behind hills |

## Rules

- Never claim done from one screenshot. Attach the harness output and a written frame review.
- Game UI is drawn in Three.js with textures. DOM panels are not accepted.
- A failing assertion is a bug in the game or in the harness. Fix it. Do not delete the check.
