Spaces:
Sleeping
Sleeping
| """Pure reconstruction of per-turn color grids from a MemoryCheckpoint.""" | |
| from __future__ import annotations | |
| from proteus.game.runtime.memory import MemoryCheckpoint, MemoryTurn, memory_frames | |
| _GRID_LEGEND = {5: ".", 1: "A", 2: "B", 3: "#"} | |
| def _ck(turns, scenario="x", wall_rects=None): | |
| return MemoryCheckpoint( | |
| model="m", scenario=scenario, difficulty="easy", seed=1, | |
| created_at="t", outcome="survived", transparent_prompt="p", | |
| memory_turns=turns, wall_rects=wall_rects or [], | |
| ) | |
| def test_grid_frame_inverts_ascii_via_legend(): | |
| ck = _ck([MemoryTurn(turn_idx=1, frame_ascii="A.\n.#", action="right", | |
| focal_pos=(0, 0), predator_pos=(1, 1))]) | |
| frames = memory_frames(ck, legend=_GRID_LEGEND, grid_size=(2, 2)) | |
| assert frames[0]["action"] == "right" | |
| assert frames[0]["grid"] == [[1, 5], [5, 3]] # A . / . # | |
| def test_prose_frame_paints_positions_and_walls(): | |
| ck = _ck( | |
| [MemoryTurn(turn_idx=1, frame_ascii="Open field 8x8. You are A ...", | |
| action="up", focal_pos=(0, 0), predator_pos=(5, 5))], | |
| wall_rects=[(2, 2, 3, 3)], | |
| ) | |
| g = memory_frames(ck, legend=_GRID_LEGEND, grid_size=(8, 8))[0]["grid"] | |
| # focal is 2x2 at (0,0): cells (0,0),(1,0),(0,1),(1,1) => g[y][x] | |
| assert g[0][0] == 1 and g[0][1] == 1 and g[1][0] == 1 and g[1][1] == 1 | |
| assert g[2][2] != 1 # outside focal 2x2 | |
| # predator is 3x3 at (5,5): cells (5..7, 5..7) | |
| assert g[5][5] == 2 and g[7][7] == 2 | |
| assert g[3][3] == 3 # wall cell visible | |
| assert g[0][4] == 5 # background elsewhere | |
| def test_prose_frame_paints_food_cells(): | |
| ck = _ck( | |
| [MemoryTurn(turn_idx=1, frame_ascii="Open field 8x8. You are A ...", | |
| action="up", focal_pos=(0, 0), predator_pos=(6, 6))], | |
| wall_rects=[(5, 1, 5, 1)], # a clear cell (not under focal/predator/food) | |
| ) | |
| ck.food_cells = [(4, 1), (1, 4)] | |
| g = memory_frames(ck, legend=_GRID_LEGEND, grid_size=(8, 8))[0]["grid"] | |
| assert g[1][4] == 14 and g[4][1] == 14 # food (idx 14) painted | |
| assert g[1][5] == 3 # wall still there | |
| assert g[0][0] == 1 # focal still on top | |
| def test_checkpoint_food_cells_roundtrips(): | |
| from proteus.game.runtime.memory import MemoryCheckpoint | |
| ck = MemoryCheckpoint( | |
| model="m", scenario="template", difficulty="easy", seed=1, | |
| created_at="t", outcome="survived", transparent_prompt="x", | |
| food_cells=[(3, 4)], | |
| ) | |
| ck2 = MemoryCheckpoint.model_validate_json(ck.model_dump_json()) | |
| assert ck2.food_cells == [(3, 4)] | |