**Final verification pass — phase 03 `simulation_movement_and_interaction` (all 5 task files already in `complete/`)** - Verified all phase-03 deliverables exist and work: `sim/` kernel (`step`, `state_build`, `rng`, `movement`, `collision`, `interact`, `carry`, `replay`), replay fixtures + 3 committed goldens, `docs/SIMULATION.md`. - `bash .agents/validate.sh` → **passed**: ruff `All checks passed!`, pyright `0 errors`, `undercooked-levels validate` green, full `pytest` **1033 passed** (coverage **99.71%** ≥ 90% floor), slow sim stage green, repo-hygiene clean. - Perf smoke (4 players, 2 000 ticks): mean **0.049 ms/tick, 20 423 ticks/s** — ≥ 10 000 ticks/s criterion met (test lives in `test_perf_smoke.py`, which the gate's slow stage runs; the overview names it `test_replay.py`). - Completion criteria: import `undercooked.sim.step.step` ✅ · `tests/sim` green incl. determinism + goldens ✅ · perf ✅ · constant-change-fails-by-design ✅ (committed-hash equality + mutation test naming first bad tick/field; goldens never rewritten by tests) · `docs/SIMULATION.md` ✅ (constants, 8-step order, purity/determinism, re-bless) · full suite + coverage ✅ · app launches: `/healthz` 200, `/` 200, `/api/levels` serves content ✅ · replay CLI `check walk_solo.json` exit 0 ✅ · no regression in phases 01/02 (their suites green) ✅. - Documentation: README.md — fixed one stale inconsistency (Status opening listed only 01/02 complete with 03 pending; now lists 01–03 complete, next pending `04_kitchen_pipeline_and_scoring`); all other README claims (CLI flags, config table, shell text, replay CLI exit codes) verified against code. AGENTS.md — verified consistent with the project as it stands, no changes needed. - No deviations. Next pending phase: **`04_kitchen_pipeline_and_scoring`**.
586 lines
24 KiB
Python
586 lines
24 KiB
Python
"""The replay harness against its committed scripts and goldens (A21, PLAN §9 layer 3).
|
|
|
|
`tests/sim/test_replay_carry.py` pinned the first golden by hand; this file is the
|
|
formalised contract: the two committed replay scripts (`walk_solo`, the 600-tick
|
|
movement-only tour of level 1; `pick_drop_pass`, two cooks running the whole
|
|
pick → set-down → hand-off → long-press-trash chain on the fixture kitchen) each
|
|
match their committed `tests/goldens/<name>.golden.json`, a deliberately mutated
|
|
script diverges and `compare()` names the first bad tick and the changed field,
|
|
the record cadence is stable, a replay is bit-identical within one process, and
|
|
the goldens are never rewritten by the test run.
|
|
|
|
The levels are loaded explicitly (level 1 from the shipped `levels/`, the fixture
|
|
kitchen from `tests/fixtures/levels/_replay/`), so the session fixture's
|
|
level-free directory cannot leak in — and the CLI tests point
|
|
`UNDERCOOKED_LEVELS_DIR` at `levels/` for the same reason.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from dataclasses import replace
|
|
from itertools import pairwise
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from undercooked.sim.content import load_level_file
|
|
from undercooked.sim.recipe import load_recipe_book
|
|
from undercooked.sim.replay import (
|
|
EXIT_MISMATCH,
|
|
EXIT_OK,
|
|
EXIT_UNUSABLE,
|
|
Frame,
|
|
InputScript,
|
|
ReplayError,
|
|
ScriptItem,
|
|
_field_diff, # private, exercised directly below
|
|
compare,
|
|
main,
|
|
run_script,
|
|
)
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
SHIPPED_LEVELS = REPO_ROOT / "levels"
|
|
SHIPPED_RECIPES = SHIPPED_LEVELS / "recipes.yaml"
|
|
|
|
WALK_SOLO_SCRIPT = REPO_ROOT / "tests" / "fixtures" / "replays" / "walk_solo.json"
|
|
PICK_DROP_PASS_SCRIPT = (
|
|
REPO_ROOT / "tests" / "fixtures" / "replays" / "pick_drop_pass.json"
|
|
)
|
|
PICK_DROP_PASS_LEVEL = (
|
|
REPO_ROOT / "tests" / "fixtures" / "levels" / "_replay" / "pick_drop_pass.yaml"
|
|
)
|
|
WALK_SOLO_GOLDEN = REPO_ROOT / "tests" / "goldens" / "walk_solo.golden.json"
|
|
PICK_DROP_PASS_GOLDEN = REPO_ROOT / "tests" / "goldens" / "pick_drop_pass.golden.json"
|
|
|
|
|
|
def golden_map(path: Path) -> dict[int, str]:
|
|
"""A golden file's `hashes` as the `tick -> hash` mapping `compare` takes."""
|
|
golden = json.loads(path.read_text(encoding="utf-8"))
|
|
return {int(tick): digest for tick, digest in golden["hashes"].items()}
|
|
|
|
|
|
def golden_final(path: Path) -> str:
|
|
return json.loads(path.read_text(encoding="utf-8"))["hash"]
|
|
|
|
|
|
def load_walk_solo() -> tuple[Any, Any, InputScript]:
|
|
"""Level 1, the recipe book and the committed walk_solo script."""
|
|
level = load_level_file(SHIPPED_LEVELS / "01_first_kitchen.yaml")
|
|
book = load_recipe_book(SHIPPED_RECIPES)
|
|
return level, book, InputScript.load(WALK_SOLO_SCRIPT)
|
|
|
|
|
|
def load_pick_drop_pass() -> tuple[Any, Any, InputScript]:
|
|
"""The fixture kitchen, the recipe book and the committed pick_drop_pass script."""
|
|
level = load_level_file(PICK_DROP_PASS_LEVEL)
|
|
book = load_recipe_book(SHIPPED_RECIPES)
|
|
return level, book, InputScript.load(PICK_DROP_PASS_SCRIPT)
|
|
|
|
|
|
# -- the committed goldens ----------------------------------------------------
|
|
|
|
|
|
def test_walk_solo_matches_its_committed_golden() -> None:
|
|
"""600 ticks of movement-only walking on level 1, every tick recorded."""
|
|
level, book, script = load_walk_solo()
|
|
result = run_script(level, book, script)
|
|
assert result.state.tick == 600
|
|
assert len(result.events) == 0 # movement only: no press, no event
|
|
assert result.state.players[0].vel.length < 1e-6 # the tour ends settled
|
|
assert [tick for tick, _ in result.hashes] == list(range(601))
|
|
assert result.state.state_hash() == golden_final(WALK_SOLO_GOLDEN)
|
|
assert compare(result, golden_map(WALK_SOLO_GOLDEN)) == []
|
|
|
|
|
|
def test_pick_drop_pass_matches_its_committed_golden_and_event_sequence() -> None:
|
|
"""The whole hand chain, pinned both by hash and by the events it must cause."""
|
|
level, book, script = load_pick_drop_pass()
|
|
result = run_script(level, book, script)
|
|
assert result.state.tick == 139
|
|
expected: tuple[tuple[str, dict[str, object]], ...] = (
|
|
(
|
|
"picked",
|
|
{"player_id": "p0", "station": "spawn_shelf_0", "item": "tomato", "form": "raw"},
|
|
),
|
|
(
|
|
"dropped",
|
|
{"player_id": "p0", "station": "prep_0", "item": "tomato", "form": "raw"},
|
|
),
|
|
("handed_off", {"from": "p0", "to": "p1", "item": "tomato", "form": "raw"}),
|
|
("trashed", {"player_id": "p1", "item": "tomato", "score_delta": -2}),
|
|
)
|
|
assert len(result.events) == len(expected)
|
|
for event, (want_kind, want_payload) in zip(result.events, expected, strict=True):
|
|
assert event.kind == want_kind
|
|
for key, value in want_payload.items():
|
|
assert event.payload.get(key) == value, (event.kind, event.payload)
|
|
assert result.state.counters["trashed"] == 1
|
|
for player in result.state.players: # the round ends settled, hands empty
|
|
assert player.carrying is None
|
|
assert player.vel.length < 1e-6
|
|
assert result.state.state_hash() == golden_final(PICK_DROP_PASS_GOLDEN)
|
|
assert compare(result, golden_map(PICK_DROP_PASS_GOLDEN)) == []
|
|
|
|
|
|
def test_a_mutated_coordinate_diverges_and_compare_names_the_bad_tick_and_field() -> None:
|
|
"""One coordinate edited in the fixture: the first bad tick and the moved field."""
|
|
level, book, script = load_walk_solo()
|
|
frames = list(script.frames)
|
|
frames[0] = replace(frames[0], move=(0.0, -1.0)) # up, not down
|
|
mutated = replace(script, frames=tuple(frames))
|
|
result = run_script(level, book, mutated)
|
|
mismatches = compare(result, golden_map(WALK_SOLO_GOLDEN))
|
|
assert mismatches, "a mutated script must not match its golden"
|
|
first = mismatches[0]
|
|
assert first.kind == "hash"
|
|
assert first.tick == 1, "the frame at tick 0 is consumed by substep 0 -> tick 1"
|
|
assert first.actual is not None and first.actual != first.expected
|
|
assert "players[0].pos.y" in first.changed, (
|
|
"the diff must name the field the edit moved, " + str(first.changed)
|
|
)
|
|
assert "players[0].vel.y" in first.changed
|
|
assert all(m.tick >= first.tick for m in mismatches)
|
|
# the tour re-converges on its walls, so the *final* state may agree with the
|
|
# golden again — what is pinned is the divergence itself:
|
|
assert len(mismatches) > 1
|
|
divergent = [m for m in mismatches if m.kind == "hash"]
|
|
assert divergent and all(m.actual != m.expected for m in divergent)
|
|
|
|
|
|
# -- determinism and cadence --------------------------------------------------
|
|
|
|
|
|
def test_replaying_the_same_script_twice_in_one_process_is_identical() -> None:
|
|
"""No hidden global state: two runs of each script in one process agree exactly."""
|
|
for loader in (load_walk_solo, load_pick_drop_pass):
|
|
level, book, script = loader()
|
|
first = run_script(level, book, script)
|
|
second = run_script(level, book, script)
|
|
assert first.hashes == second.hashes
|
|
assert first.events == second.events
|
|
assert first.state.state_hash() == second.state.state_hash()
|
|
assert first.wall_seconds == second.wall_seconds == 0.0 # no clock injected
|
|
|
|
|
|
def test_the_record_cadence_is_stable_and_the_final_tick_is_always_recorded() -> None:
|
|
level, book, script = load_walk_solo()
|
|
sparse = replace(script, record_every=5)
|
|
first = run_script(level, book, sparse)
|
|
second = run_script(level, book, sparse)
|
|
assert first.hashes == second.hashes
|
|
ticks = [tick for tick, _ in first.hashes]
|
|
assert ticks[0] == 0
|
|
assert ticks[-1] == 600 # the final tick is always recorded, cadence or not
|
|
assert set(ticks) == {tick for tick in range(0, 601) if tick % 5 == 0} | {600}
|
|
silent = replace(script, record_every=0)
|
|
unrecorded = run_script(level, book, silent)
|
|
assert unrecorded.hashes == ()
|
|
assert dict(unrecorded.states) == {}
|
|
assert unrecorded.state.tick == 600 # the run still runs; only the recording stops
|
|
|
|
|
|
def test_a_run_with_a_clock_measures_wall_seconds_and_a_pinned_length_clips() -> None:
|
|
"""`clock` is the seam the perf smoke times through; `max_ticks` pins the run."""
|
|
level, book, script = load_walk_solo()
|
|
measured = run_script(level, book, script, clock=time.perf_counter)
|
|
assert measured.wall_seconds > 0.0
|
|
clipped = run_script(level, book, script, max_ticks=10)
|
|
assert clipped.state.tick == 10
|
|
assert [tick for tick, _ in clipped.hashes] == list(range(11)) # cadence 1 + final
|
|
|
|
|
|
def test_compare_reports_a_cadence_mismatch_as_missing_and_extra() -> None:
|
|
"""A golden that records ticks the run did not (and vice versa) is visible."""
|
|
level, book, script = load_walk_solo()
|
|
sparse = run_script(level, book, replace(script, record_every=5))
|
|
by_tick = dict(sparse.hashes)
|
|
golden = {0: by_tick[0], 7: by_tick[5], 10: by_tick[10]}
|
|
mismatches = compare(sparse, golden)
|
|
kinds = {mismatch.tick: mismatch.kind for mismatch in mismatches}
|
|
assert kinds[7] == "missing" # the golden records a tick the run did not
|
|
assert kinds[5] == "extra" # the run records a tick the golden does not have
|
|
assert 0 not in kinds and 10 not in kinds # the matching ticks produce nothing
|
|
assert all(mismatch.changed == () for mismatch in mismatches)
|
|
|
|
|
|
# -- the goldens are read-only for the tests ----------------------------------
|
|
|
|
|
|
def test_the_field_diff_walks_mappings_and_missing_keys() -> None:
|
|
"""`counters['trashed']` appears mid-run: the diff must cross the mapping.
|
|
|
|
The `walk_solo` states all share one empty `counters` object (so the early
|
|
`a is b` return short-circuits it); the `pick_drop_pass` run is what makes
|
|
the mapping branch and a missing key on one side actually run.
|
|
"""
|
|
level, book, script = load_pick_drop_pass()
|
|
result = run_script(level, book, script)
|
|
ticks = sorted(result.states)
|
|
pair: tuple[Any, Any] | None = None
|
|
for a, b in pairwise(ticks):
|
|
if bool(result.states[a].counters) != bool(result.states[b].counters):
|
|
pair = (result.states[a], result.states[b])
|
|
break
|
|
assert pair is not None, "the trashed tick must flip counters from empty to non-empty"
|
|
changed = _field_diff(*pair)
|
|
assert "counters['trashed']" in changed, changed
|
|
|
|
|
|
def test_the_golden_files_are_never_rewritten_by_the_tests(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
"""The whole check flow — harness and CLI — must not touch the committed goldens."""
|
|
before = (
|
|
WALK_SOLO_GOLDEN.read_bytes(),
|
|
PICK_DROP_PASS_GOLDEN.read_bytes(),
|
|
)
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
level, book, script = load_pick_drop_pass() # the harness flow
|
|
assert compare(run_script(level, book, script), golden_map(PICK_DROP_PASS_GOLDEN)) == []
|
|
assert main(["check", str(WALK_SOLO_SCRIPT)]) == EXIT_OK # the CLI flow
|
|
capsys.readouterr()
|
|
after = (
|
|
WALK_SOLO_GOLDEN.read_bytes(),
|
|
PICK_DROP_PASS_GOLDEN.read_bytes(),
|
|
)
|
|
assert before == after
|
|
|
|
|
|
# -- the CLI -------------------------------------------------------------------
|
|
|
|
|
|
def test_check_cli_exits_zero_and_reports_the_match(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
assert main(["check", str(WALK_SOLO_SCRIPT)]) == EXIT_OK
|
|
out = capsys.readouterr().out
|
|
assert "ok: 601 hashes match walk_solo.golden.json" in out
|
|
|
|
|
|
def test_check_cli_exits_one_and_names_the_bad_tick_when_the_fixture_is_mutated(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
"""The task's acceptance test: edit one coordinate, `check` fails and diagnoses."""
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
original = WALK_SOLO_SCRIPT.read_bytes()
|
|
data = json.loads(original)
|
|
data["frames"][0]["move"] = [0.0, -1.0]
|
|
WALK_SOLO_SCRIPT.write_text(json.dumps(data, indent=2), encoding="utf-8")
|
|
try:
|
|
assert main(["check", str(WALK_SOLO_SCRIPT)]) == EXIT_MISMATCH
|
|
finally:
|
|
WALK_SOLO_SCRIPT.write_bytes(original)
|
|
out = capsys.readouterr().out
|
|
assert "first divergent tick 1" in out
|
|
assert "players[0].pos.y" in out
|
|
assert WALK_SOLO_SCRIPT.read_bytes() == original # the fixture is left untouched
|
|
|
|
|
|
def test_check_cli_exits_two_without_a_golden(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
script = InputScript(
|
|
name="no_golden_here",
|
|
seed=1,
|
|
level_id="01_first_kitchen",
|
|
tick_hz=20,
|
|
frames=(Frame(tick=0, player_id="p0", move=(1.0, 0.0)),),
|
|
)
|
|
path = tmp_path / "no_golden_here.json"
|
|
script.dump(path)
|
|
code = main(["check", str(path)])
|
|
assert code == EXIT_UNUSABLE
|
|
|
|
|
|
def test_check_cli_exits_two_when_the_level_is_not_in_the_content_dir(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
"""The pick_drop_pass kitchen is a test fixture, not campaign content."""
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
assert main(["check", str(PICK_DROP_PASS_SCRIPT)]) == EXIT_UNUSABLE
|
|
err = capsys.readouterr().err
|
|
assert "99_replay_pick_drop_pass" in err
|
|
|
|
|
|
def test_record_cli_writes_the_golden_and_check_reads_it_back(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
tmp_path: Path,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
"""`record` is the manual re-bless; into a temp goldens dir, the real one is untouched."""
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
committed_before = WALK_SOLO_GOLDEN.read_bytes()
|
|
script = InputScript.load(WALK_SOLO_SCRIPT)
|
|
path = tmp_path / "record_smoke.json"
|
|
replace(script, name="record_smoke").dump(path)
|
|
assert main(["record", str(path)], goldens_dir=tmp_path) == EXIT_OK
|
|
golden = tmp_path / "record_smoke.golden.json"
|
|
assert golden.exists()
|
|
assert main(["check", str(path)], goldens_dir=tmp_path) == EXIT_OK
|
|
capsys.readouterr()
|
|
assert main(["record", str(path)], goldens_dir=tmp_path) == EXIT_OK # the re-bless note
|
|
assert "overwriting" in capsys.readouterr().out
|
|
# and the committed golden never moves:
|
|
assert WALK_SOLO_GOLDEN.read_bytes() == committed_before
|
|
# while a fresh record reproduces the committed hash bit for bit:
|
|
fresh = json.loads(golden.read_text(encoding="utf-8"))
|
|
assert fresh["hash"] == json.loads(committed_before.decode("utf-8"))["hash"]
|
|
|
|
|
|
def test_check_cli_exits_two_on_a_broken_script_file(
|
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
bad = tmp_path / "broken_script.json"
|
|
bad.write_text(json.dumps({"seed": 1, "level_id": "x", "bogus": 1}), encoding="utf-8")
|
|
assert main(["check", str(bad)]) == EXIT_UNUSABLE
|
|
assert "unknown script key(s): bogus" in capsys.readouterr().err
|
|
|
|
|
|
def test_check_cli_exits_two_when_the_content_would_not_load(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(tmp_path / "no_such_dir"))
|
|
assert main(["check", str(WALK_SOLO_SCRIPT)]) == EXIT_UNUSABLE
|
|
assert "would not load" in capsys.readouterr().err
|
|
|
|
|
|
def test_check_cli_reports_cadence_notes_and_the_missing_and_empty_diff_paths(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
tmp_path: Path,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
"""The three diagnostic branches of a failed check, each named in the report."""
|
|
monkeypatch.setenv("UNDERCOOKED_LEVELS_DIR", str(SHIPPED_LEVELS))
|
|
level, book, script = load_walk_solo()
|
|
sparse = replace(script, name="cadence_check", record_every=5)
|
|
path = tmp_path / "cadence_check.json"
|
|
sparse.dump(path)
|
|
assert main(["record", str(path)], goldens_dir=tmp_path) == EXIT_OK
|
|
|
|
# 1. the cadence note: the golden was recorded at 5, the script now says 1
|
|
replace(sparse, name="cadence_check", record_every=1).dump(path)
|
|
assert main(["check", str(path)], goldens_dir=tmp_path) == EXIT_MISMATCH
|
|
out = capsys.readouterr().out
|
|
assert "recorded at record_every=5" in out
|
|
|
|
# 2. a `missing` first mismatch: the golden records a tick the run did not
|
|
missing_path = tmp_path / "missing_first.json"
|
|
replace(sparse, name="missing_first").dump(missing_path)
|
|
first_zero = run_script(level, book, sparse).hashes[0][1]
|
|
(tmp_path / "missing_first.golden.json").write_text(
|
|
json.dumps(
|
|
{"name": "missing_first", "tick": 600, "record_every": 5,
|
|
"hashes": {"0": first_zero, "3": "x" * 64}}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
assert main(["check", str(missing_path)], goldens_dir=tmp_path) == EXIT_MISMATCH
|
|
out = capsys.readouterr().out
|
|
assert "a golden tick this run did not record" in out
|
|
|
|
# 3. a hash mismatch at tick 0 carries no field diff (the baseline is itself)
|
|
empty_path = tmp_path / "empty_diff.json"
|
|
replace(sparse, name="empty_diff").dump(empty_path)
|
|
(tmp_path / "empty_diff.golden.json").write_text(
|
|
json.dumps(
|
|
{"name": "empty_diff", "tick": 1, "record_every": 5,
|
|
"hashes": {"0": "y" * 64}}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
assert main(["check", str(empty_path)], goldens_dir=tmp_path) == EXIT_MISMATCH
|
|
out = capsys.readouterr().out
|
|
assert "no field diff" in out
|
|
|
|
|
|
# -- the script format ---------------------------------------------------------
|
|
|
|
|
|
def test_validate_refuses_a_broken_script_naming_every_problem() -> None:
|
|
script = InputScript(
|
|
seed=-1, # < 0
|
|
level_id="", # empty
|
|
tick_hz=10.0, # not 20
|
|
record_every=-3, # negative
|
|
frames=(Frame(tick=-5, player_id="", move=(float("nan"), 1.0), action=0, face="sideways"),), # type: ignore[arg-type] — the guard under test
|
|
items=(ScriptItem(station="", item="kale", form="soggy"),),
|
|
)
|
|
with pytest.raises(ReplayError) as excinfo:
|
|
script.validate()
|
|
message = str(excinfo.value)
|
|
assert "seed must be an integer in [0, 2**64)" in message
|
|
assert "level_id must be a non-empty string" in message
|
|
assert "tick_hz must be 20" in message
|
|
assert "record_every must be >= 0" in message
|
|
assert "tick must be a non-negative integer" in message
|
|
assert "move must be two finite numbers" in message
|
|
assert "action must be a bool" in message
|
|
assert "face must be one of" in message
|
|
assert "item must be an ItemKind value" in message
|
|
assert "form must be an ItemForm value" in message
|
|
|
|
|
|
def test_validate_refuses_a_non_integer_record_every() -> None:
|
|
script = InputScript(
|
|
seed=1,
|
|
level_id="x",
|
|
tick_hz=20,
|
|
record_every=1.5, # type: ignore[arg-type] — the guard exists for exactly this
|
|
)
|
|
with pytest.raises(ReplayError, match=r"record_every must be an integer"):
|
|
script.validate()
|
|
|
|
|
|
def test_validate_accepts_the_committed_scripts() -> None:
|
|
for path in (WALK_SOLO_SCRIPT, PICK_DROP_PASS_SCRIPT):
|
|
InputScript.load(path).validate()
|
|
|
|
|
|
def test_load_refuses_unknown_keys_at_every_level(tmp_path: Path) -> None:
|
|
bad = tmp_path / "bad.json"
|
|
bad.write_text(
|
|
json.dumps(
|
|
{
|
|
"seed": 1,
|
|
"level_id": "x",
|
|
"tick_hz": 20,
|
|
"bogus": 1,
|
|
"frames": [{"tick": 0, "player_id": "p", "move": [1, 0], "extra": 1}],
|
|
"items": [{"station": "s", "item": "tomato", "form": "raw", "mystery": 2}],
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
with pytest.raises(ReplayError, match=r"unknown script key\(s\): bogus"):
|
|
InputScript.load(bad)
|
|
bad_frame = tmp_path / "bad_frame.json"
|
|
bad_frame.write_text(
|
|
json.dumps(
|
|
{
|
|
"seed": 1,
|
|
"level_id": "x",
|
|
"tick_hz": 20,
|
|
"frames": [{"tick": 0, "player_id": "p", "move": [1, 0], "extra": 1}],
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
with pytest.raises(ReplayError, match=r"frame 0 has unknown key\(s\): extra"):
|
|
InputScript.load(bad_frame)
|
|
bad_item = tmp_path / "bad_item.json"
|
|
bad_item.write_text(
|
|
json.dumps(
|
|
{
|
|
"seed": 1,
|
|
"level_id": "x",
|
|
"tick_hz": 20,
|
|
"items": [
|
|
{"station": "s", "item": "tomato", "form": "raw", "mystery": 2}
|
|
],
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
with pytest.raises(ReplayError, match=r"item 0 has unknown key\(s\): mystery"):
|
|
InputScript.load(bad_item)
|
|
|
|
|
|
def test_load_refuses_every_malformed_shape(tmp_path: Path) -> None:
|
|
def writes(name: str, text: str) -> Path:
|
|
path = tmp_path / name
|
|
path.write_text(text, encoding="utf-8")
|
|
return path
|
|
|
|
with pytest.raises(ReplayError, match=r"must be a JSON object, got list"):
|
|
InputScript.load(writes("top_list.json", "[1, 2, 3]"))
|
|
with pytest.raises(ReplayError, match=r"missing required key 'seed'"):
|
|
InputScript.load(writes("no_seed.json", json.dumps({"level_id": "x", "tick_hz": 20})))
|
|
with pytest.raises(ReplayError, match=r"frame 0 must be a JSON object"):
|
|
InputScript.load(
|
|
writes("frame_scalar.json", json.dumps(
|
|
{"seed": 1, "level_id": "x", "tick_hz": 20, "frames": [42]}))
|
|
)
|
|
with pytest.raises(ReplayError, match=r"frame 0 is missing required key 'move'"):
|
|
InputScript.load(
|
|
writes("frame_no_move.json", json.dumps(
|
|
{"seed": 1, "level_id": "x", "tick_hz": 20,
|
|
"frames": [{"tick": 0, "player_id": "p"}]}))
|
|
)
|
|
with pytest.raises(ReplayError, match=r"move must be a two-element array"):
|
|
InputScript.load(
|
|
writes("move_not_list.json", json.dumps(
|
|
{"seed": 1, "level_id": "x", "tick_hz": 20,
|
|
"frames": [{"tick": 0, "player_id": "p", "move": 1}]}))
|
|
)
|
|
with pytest.raises(ReplayError, match=r"item 0 must be a JSON object"):
|
|
InputScript.load(
|
|
writes("item_scalar.json", json.dumps(
|
|
{"seed": 1, "level_id": "x", "tick_hz": 20, "items": [42]}))
|
|
)
|
|
with pytest.raises(ReplayError, match=r"item 0 is missing required key 'form'"):
|
|
InputScript.load(
|
|
writes("item_no_form.json", json.dumps(
|
|
{"seed": 1, "level_id": "x", "tick_hz": 20,
|
|
"items": [{"station": "s", "item": "tomato"}]}))
|
|
)
|
|
|
|
|
|
def test_dump_load_roundtrip_is_lossless(tmp_path: Path) -> None:
|
|
_, _, script = load_pick_drop_pass()
|
|
path = tmp_path / "roundtrip.json"
|
|
script.dump(path)
|
|
assert InputScript.load(path) == script
|
|
# an unnamed script dumps without a `name` key; `load` then takes the stem
|
|
unnamed = replace(script, name="")
|
|
unnamed_path = tmp_path / "unnamed.json"
|
|
unnamed.dump(unnamed_path)
|
|
data = json.loads(unnamed_path.read_text(encoding="utf-8"))
|
|
assert "name" not in data
|
|
assert InputScript.load(unnamed_path) == replace(unnamed, name=unnamed_path.stem)
|
|
|
|
|
|
def test_run_script_refuses_a_level_id_mismatch() -> None:
|
|
level, book, script = load_walk_solo()
|
|
other = replace(script, level_id="99_replay_pick_drop_pass")
|
|
with pytest.raises(ReplayError, match="names level"):
|
|
run_script(level, book, other)
|
|
|
|
|
|
def test_run_script_refuses_an_item_on_an_unknown_station() -> None:
|
|
level, book, script = load_walk_solo()
|
|
broken = replace(
|
|
script,
|
|
items=(ScriptItem(station="chop_board_9", item="tomato", form="raw"),),
|
|
)
|
|
with pytest.raises(ReplayError, match="does not have"):
|
|
run_script(level, book, broken)
|
|
|
|
|
|
def test_run_script_refuses_a_negative_max_ticks() -> None:
|
|
level, book, script = load_pick_drop_pass()
|
|
with pytest.raises(ReplayError, match="max_ticks"):
|
|
run_script(level, book, script, max_ticks=-1)
|
|
|
|
|
|
def test_a_frameless_script_runs_zero_ticks_and_records_the_built_state() -> None:
|
|
level, book, _ = load_pick_drop_pass()
|
|
frameless = InputScript(
|
|
name="idle", seed=0x5EED, level_id=level.id, tick_hz=20, frames=()
|
|
)
|
|
result = run_script(level, book, frameless)
|
|
assert result.state.tick == 0
|
|
assert [tick for tick, _ in result.hashes] == [0]
|
|
assert result.events == ()
|