# SPDX-License-Identifier: MIT """The BlastEm event-log reader: what it reads of the ``blastem-events`` tool's output. The log itself is decoded by the GPL tool ``$EMU_TOOLS_GPL/tools/blastem-events`` (docs/provenance.md, 2026-11-26), whose own tests cover the log's format. These build the tool's *output* (its README's manifest and ``trace/events`` records) and check this repository reads it; BlastEm is a detector (docs/validation.md), so none of this says anything it logs is right. The last test runs the tool itself and skips, naming ``EMU_TOOLS_GPL``, when it is not there. """ from __future__ import annotations import gzip import json import subprocess import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) import blastem_events as be # noqa: E402 OD2_LOG = ROOT / "reference/blastem-reset25-log/events.bel" def write_output(directory: Path, records: list[dict], *, video: int = 0, status: str = "ok", compress: bool = False, **log) -> Path: # fmt: skip """A directory as the tool writes it: ``manifest.json`` and the events file.""" directory.mkdir(parents=True, exist_ok=True) name = "events.jsonl.gz" if compress else "events.jsonl" text = "".join(json.dumps(dict(r, clock="mclk")) + "\n" for r in records) if compress: with gzip.open(directory / name, "wt") as f: f.write(text) else: (directory / name).write_text(text) t_end = records[-1]["t"] if records else 0 manifest = { "contract": 1, "tool": "blastem-events", "tool_version": "0.1.0", "upstream": {"name": "BlastEm", "commit": "542164d"}, "log": {"version": 512, "system_type": 1, "video_std": video, "name": "TEST", "line_mclk": 3420, "frame_mclk": be.FRAME_CYCLES[video & 1], "body_bytes": 10, "decoded_bytes": 10, "t_end": t_end, "counter_end": t_end, **log}, "outputs": [{"path": name, "format": "trace/events", "records": len(records)}], "status": status, "evidence": "OBSERVED", } # fmt: skip if status != "ok": manifest["error"] = "desync: decoded to byte 11 of 10" (directory / "manifest.json").write_text(json.dumps(manifest)) return directory def test_the_manifest_is_read(tmp_path): log = be.Log(write_output(tmp_path, [], video=1)) assert (log.name, log.video_std, log.version) == ("TEST", 1, 512) assert log.frame_cycles == 313 * 3420 # PAL: a cycle is a master clock assert list(log) == [] assert log.tag == "OBSERVED (BlastEm @ 542164d log format, via blastem-events 0.1.0)" log.check() def test_a_frame_is_taken_from_the_cycle_and_not_from_flush(tmp_path): log = be.Log(write_output(tmp_path, [])) assert log.frame_of(0) == 0 assert log.frame_of(log.frame_cycles - 1) == 0 assert log.frame_of(log.frame_cycles) == 1 assert log.frame_cycles == 262 * 3420 # NTSC def test_register_writes_are_grouped_by_frame(tmp_path): frame = be.FRAME_CYCLES[0] records = [ {"t": 20, "event": "vdp-reg", "register": 0, "value": 0x04}, {"t": frame - 1, "event": "flush"}, {"t": frame + 20, "event": "vdp-reg", "register": 1, "value": 0x74}, ] log = be.Log(write_output(tmp_path, records)) assert be.register_writes(log) == {0: [(0, 0x04)], 1: [(1, 0x74)]} def test_gzipped_events_read_the_same(tmp_path): records = [{"t": 16 * i, "event": "psg", "value": i} for i in range(1, 6)] plain = list(be.Log(write_output(tmp_path / "plain", records))) packed = list(be.Log(write_output(tmp_path / "gz", records, compress=True))) assert plain == packed and [r["value"] for r in plain] == [1, 2, 3, 4, 5] def test_a_failed_decode_is_refused(tmp_path): log = be.Log(write_output(tmp_path, [], status="failed")) with pytest.raises(be.ToolError, match="desync"): log.check() def test_an_unset_variable_is_a_missing_input(monkeypatch, capsys): monkeypatch.delenv("EMU_TOOLS_GPL", raising=False) assert be.tool_path() is None with pytest.raises(SystemExit) as stop: be.require_tool() assert stop.value.code == 3 assert "EMU_TOOLS_GPL is unset" in capsys.readouterr().err def test_the_tool_decodes_a_real_log(tmp_path): if be.tool_path() is None: pytest.skip(f"EMU_TOOLS_GPL unset or $EMU_TOOLS_GPL/{be.TOOL_PATH} absent") if not OD2_LOG.is_file(): pytest.skip(f"{OD2_LOG.relative_to(ROOT)} not here (a local BlastEm log)") log = be.Log.decode(OD2_LOG, tmp_path / "out") log.check() assert log.manifest["tool_version"] == be.TOOL_VERSION assert (log.name, log.video_std) == ("OVERDRIVE 2", 1) assert log.decoded_bytes == log.body_bytes assert log.records == sum(1 for _ in log) out = subprocess.run([sys.executable, str(ROOT / "scripts/blastem_events.py"), str(OD2_LOG)], capture_output=True, text=True, check=True).stdout # fmt: skip assert "decoded to byte 577,216 of 577,216" in out