|
| 1 | +"""Tests logging of game states to JSONL files.""" |
| 2 | + |
| 3 | +import copy |
| 4 | +import json |
| 5 | +import time |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import pytest |
| 10 | +from deepdiff import DeepDiff |
| 11 | + |
| 12 | +from balatrobot.client import BalatroClient |
| 13 | + |
| 14 | + |
| 15 | +def get_jsonl_files() -> list[Path]: |
| 16 | + """Get all JSONL files from the runs directory.""" |
| 17 | + runs_dir = Path(__file__).parent.parent / "runs" |
| 18 | + return list(runs_dir.glob("*.jsonl")) |
| 19 | + |
| 20 | + |
| 21 | +def load_jsonl_run(file_path: Path) -> list[dict[str, Any]]: |
| 22 | + """Load a JSONL file and return list of run steps.""" |
| 23 | + steps = [] |
| 24 | + with open(file_path, "r") as f: |
| 25 | + for line in f: |
| 26 | + line = line.strip() |
| 27 | + if line: # Skip empty lines |
| 28 | + steps.append(json.loads(line)) |
| 29 | + return steps |
| 30 | + |
| 31 | + |
| 32 | +def normalize_step(step: dict[str, Any]) -> dict[str, Any]: |
| 33 | + """Normalize a step by removing non-deterministic fields.""" |
| 34 | + normalized = copy.deepcopy(step) |
| 35 | + |
| 36 | + # Remove timestamp as it's non-deterministic |
| 37 | + normalized.pop("timestamp_ms", None) |
| 38 | + |
| 39 | + # Remove log_path from start_run function arguments as it's non-deterministic |
| 40 | + if "function" in normalized and normalized["function"]["name"] == "start_run": |
| 41 | + if "arguments" in normalized["function"]: |
| 42 | + normalized["function"]["arguments"].pop("log_path", None) |
| 43 | + |
| 44 | + # Remove non-deterministic fields from game states |
| 45 | + for state_key in ["game_state_before", "game_state_after"]: |
| 46 | + if state_key in normalized: |
| 47 | + game_state = normalized[state_key] |
| 48 | + if "hand" in game_state and "cards" in game_state["hand"]: |
| 49 | + for card in game_state["hand"]["cards"]: |
| 50 | + card.pop("highlighted", None) |
| 51 | + card.pop("sort_id", None) |
| 52 | + |
| 53 | + return normalized |
| 54 | + |
| 55 | + |
| 56 | +def assert_steps_equal( |
| 57 | + actual: dict[str, Any], expected: dict[str, Any], context: str = "" |
| 58 | +): |
| 59 | + """Assert two steps are equal with clear diff output.""" |
| 60 | + normalized_actual = normalize_step(actual) |
| 61 | + normalized_expected = normalize_step(expected) |
| 62 | + |
| 63 | + diff = DeepDiff( |
| 64 | + normalized_actual, |
| 65 | + normalized_expected, |
| 66 | + ignore_order=True, |
| 67 | + verbose_level=2, |
| 68 | + ) |
| 69 | + |
| 70 | + if diff: |
| 71 | + error_msg = "Steps are not equal" |
| 72 | + if context: |
| 73 | + error_msg += f" ({context})" |
| 74 | + error_msg += f"\n\n{diff.pretty()}" |
| 75 | + pytest.fail(error_msg) |
| 76 | + |
| 77 | + |
| 78 | +class TestLog: |
| 79 | + """Tests for the log module.""" |
| 80 | + |
| 81 | + @pytest.fixture(scope="session", params=get_jsonl_files()) |
| 82 | + def replay_logs(self, request, tmp_path_factory) -> tuple[Path, Path, Path]: |
| 83 | + """Fixture that replays a run and generates two JSONL log files. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Tuple of (original_jsonl_path, lua_generated_path, python_generated_path) |
| 87 | + """ |
| 88 | + original_jsonl: Path = request.param |
| 89 | + |
| 90 | + # Create temporary file paths |
| 91 | + tmp_path = tmp_path_factory.mktemp("replay_logs") |
| 92 | + base_name = original_jsonl.stem |
| 93 | + lua_log_path = tmp_path / f"{base_name}_lua.jsonl" |
| 94 | + python_log_path = tmp_path / f"{base_name}_python.jsonl" |
| 95 | + |
| 96 | + print( |
| 97 | + "\nJSONL files:\n" |
| 98 | + f"- original: {original_jsonl}\n" |
| 99 | + f"- lua: {lua_log_path}\n" |
| 100 | + f"- python: {python_log_path}\n" |
| 101 | + ) |
| 102 | + |
| 103 | + # Load original steps |
| 104 | + original_steps = load_jsonl_run(original_jsonl) |
| 105 | + |
| 106 | + with BalatroClient() as client: |
| 107 | + # Initialize game state |
| 108 | + current_state = client.send_message("go_to_menu", {}) |
| 109 | + |
| 110 | + python_log_entries = [] |
| 111 | + |
| 112 | + # Process all steps |
| 113 | + for step in original_steps: |
| 114 | + function_call = step["function"] |
| 115 | + |
| 116 | + # The current state becomes the "before" state for this function call |
| 117 | + game_state_before = current_state |
| 118 | + |
| 119 | + # For start_run, we need to add the log_path parameter to trigger Lua logging |
| 120 | + if function_call["name"] == "start_run": |
| 121 | + call_args = function_call["arguments"].copy() |
| 122 | + call_args["log_path"] = str(lua_log_path) |
| 123 | + else: |
| 124 | + call_args = function_call["arguments"] |
| 125 | + |
| 126 | + # Call the function and get the new state |
| 127 | + current_state = client.send_message(function_call["name"], call_args) |
| 128 | + |
| 129 | + # Create Python log entry (use original arguments, not modified ones) |
| 130 | + log_entry = { |
| 131 | + "timestamp_ms": int(time.time_ns() // 1_000_000), |
| 132 | + "function": function_call, |
| 133 | + "game_state_before": game_state_before, |
| 134 | + "game_state_after": current_state, |
| 135 | + } |
| 136 | + python_log_entries.append(log_entry) |
| 137 | + |
| 138 | + # Write Python log file |
| 139 | + with open(python_log_path, "w") as f: |
| 140 | + for entry in python_log_entries: |
| 141 | + f.write(json.dumps(entry, sort_keys=True) + "\n") |
| 142 | + |
| 143 | + return original_jsonl, lua_log_path, python_log_path |
| 144 | + |
| 145 | + # @pytest.mark.skip(reason="Skipping until we can get the log file to match") |
| 146 | + def test_compare_lua_logs_with_original_run( |
| 147 | + self, replay_logs: tuple[Path, Path, Path] |
| 148 | + ) -> None: |
| 149 | + """Test that Lua-generated and Python-generated logs are equivalent. |
| 150 | +
|
| 151 | + This test the log file "writing" (lua_log_path) and compare with the |
| 152 | + original jsonl file (original_jsonl). |
| 153 | + """ |
| 154 | + original_jsonl, lua_log_path, _ = replay_logs |
| 155 | + |
| 156 | + # Load both generated log files |
| 157 | + lua_steps = load_jsonl_run(lua_log_path) |
| 158 | + orig_steps = load_jsonl_run(original_jsonl) |
| 159 | + |
| 160 | + assert len(lua_steps) == len(orig_steps), ( |
| 161 | + f"Different number of steps: Lua={len(lua_steps)}, Python={len(orig_steps)}" |
| 162 | + ) |
| 163 | + |
| 164 | + # Compare each step |
| 165 | + for i, (original_step, lua_step) in enumerate(zip(orig_steps, lua_steps)): |
| 166 | + if i == 0: |
| 167 | + continue # BUG: The first game_state_before is not the same as the original |
| 168 | + context = f"step {i} in {original_jsonl.name} (Origianl vs Lua logs)" |
| 169 | + assert_steps_equal(lua_step, original_step, context) |
| 170 | + |
| 171 | + def test_compare_python_logs_with_original_run( |
| 172 | + self, replay_logs: tuple[Path, Path, Path] |
| 173 | + ) -> None: |
| 174 | + """Test that generated logs match the original run game states. |
| 175 | +
|
| 176 | + This test the log file "reading" (original_jsonl) and test the ability |
| 177 | + to replicate the run (python_log_path). |
| 178 | + """ |
| 179 | + original_jsonl, _, python_log_path = replay_logs |
| 180 | + |
| 181 | + # Load original and generated logs |
| 182 | + orig_steps = load_jsonl_run(original_jsonl) |
| 183 | + python_steps = load_jsonl_run(python_log_path) |
| 184 | + |
| 185 | + assert len(orig_steps) == len(python_steps), ( |
| 186 | + f"Different number of steps: Original={len(orig_steps)}, Generated={len(python_steps)}" |
| 187 | + ) |
| 188 | + |
| 189 | + # Compare each step |
| 190 | + for i, (original_step, python_step) in enumerate(zip(orig_steps, python_steps)): |
| 191 | + if i == 0: |
| 192 | + continue # BUG: The first game_state_before is not the same as the original |
| 193 | + context = f"step {i} in {original_jsonl.name} (Original vs Python logs)" |
| 194 | + assert_steps_equal(python_step, original_step, context) |
0 commit comments