# SPDX-License-Identifier: GPL-3.0-or-later """Smoke test for blastem-events: the command line end to end on logs written here. python3 -I tests/smoke.py Checks ``--version``, the usage (2) and missing-log (3) exit codes, a refused non-log (1), a desynchronised log (1, with the manifest saying so), and a small log of every payload kind decoded to the same records plain and gzipped, with the manifest's counts. """ from __future__ import annotations import gzip import json import subprocess import sys import tempfile import zlib from pathlib import Path RUN = Path(__file__).resolve().parents[1] / "run.py" def run(*args: str) -> subprocess.CompletedProcess: return subprocess.run([sys.executable, "-I", str(RUN), *args], capture_output=True, text=True) def log(body: bytes, video: int = 1) -> bytes: return b"BLSTEL\x02\x00" + bytes([1, video, 5]) + b"SMOKE" + zlib.compress(body) def short(kind: int) -> bytes: return bytes([kind << 4]) # delta 16 BODY = ( short(4) + bytes([1, 0x04]) # vdp-reg 1 = 04 + short(4) + bytes([15, 0x02]) # vdp-reg 15 = 02 + short(5) + bytes([0x12, 0x34, 0xAB]) # vram-b 1234 = AB + short(6) + bytes([0x10, 0xAC]) # vram-bd +10 + short(7) + bytes([0xAD]) # vram-b1 + short(8) + bytes([0xAE]) # vram-ba (+2) + short(9) + bytes([0x01, 0x00, 0x04, 0x12, 0xCD]) # vram-w 10004 = 12CD + short(10) + bytes([0x02, 0x34, 0x56]) # vram-wd +2 + short(11) + bytes([0x06, 0x0E, 0xEE]) # intram: CRAM word 3 + short(11) + bytes([133, 0x07, 0xFF]) # intram: VSRAM word 5 + short(2) + bytes([0x9F]) # psg + short(3) + bytes([1, 0x28, 0xF0]) # ym part 1 + bytes([0xE1, 0x01, 0x00]) + (1000).to_bytes(4, "big") # adjust, delta 256 + short(0) # flush ) # fmt: skip EXPECTED = [ {"t": 16, "event": "vdp-reg", "register": 1, "value": 4}, {"t": 32, "event": "vdp-reg", "register": 15, "value": 2}, {"t": 48, "event": "vram-b", "address": 0x1234, "value": 0xAB}, {"t": 64, "event": "vram-bd", "address": 0x1244, "value": 0xAC}, {"t": 80, "event": "vram-b1", "address": 0x1245, "value": 0xAD}, {"t": 96, "event": "vram-ba", "address": 0x1247, "value": 0xAE}, {"t": 112, "event": "vram-w", "address": 0x10004, "value": 0x12CD}, {"t": 128, "event": "vram-wd", "address": 0x10006, "value": 0x3456}, {"t": 144, "event": "intram", "memory": "cram", "index": 3, "value": 0x0EEE}, {"t": 160, "event": "intram", "memory": "vsram", "index": 5, "value": 0x07FF}, {"t": 176, "event": "psg", "value": 0x9F}, {"t": 192, "event": "ym", "part": 1, "register": 0x28, "value": 0xF0}, {"t": 448, "event": "adjust", "deduction": 1000}, {"t": 464, "event": "flush"}, ] def main() -> int: v = run("--version") assert v.returncode == 0 and v.stdout.startswith("blastem-events 0.1.0"), v assert run().returncode == 2 with tempfile.TemporaryDirectory() as tmp: tmp = Path(tmp) assert run("--log", str(tmp / "absent.bel"), "--out", str(tmp / "x")).returncode == 3 (tmp / "junk.bel").write_bytes(b"not a log") r = run("--log", str(tmp / "junk.bel"), "--out", str(tmp / "junk")) manifest = json.loads((tmp / "junk" / "manifest.json").read_text()) assert r.returncode == 1 and manifest["status"] == "failed", (r, manifest) assert "BLSTEL" in manifest["error"] (tmp / "short.bel").write_bytes(log(short(5) + b"\x12")) # payload cut short r = run("--log", str(tmp / "short.bel"), "--out", str(tmp / "short")) manifest = json.loads((tmp / "short" / "manifest.json").read_text()) assert r.returncode == 1 and manifest["status"] == "failed", (r, manifest) (tmp / "smoke.bel").write_bytes(log(BODY)) assert run("--log", str(tmp / "smoke.bel"), "--out", str(tmp / "plain")).returncode == 0 assert run("--log", str(tmp / "smoke.bel"), "--out", str(tmp / "gz"), "--gzip").returncode == 0 plain = (tmp / "plain" / "events.jsonl").read_text().splitlines() with gzip.open(tmp / "gz" / "events.jsonl.gz", "rt") as f: packed = f.read().splitlines() assert plain == packed records = [json.loads(line) for line in plain] assert all(r.pop("clock") == "mclk" for r in records) assert records == EXPECTED, records manifest = json.loads((tmp / "plain" / "manifest.json").read_text()) assert manifest["contract"] == 1 and manifest["status"] == "ok" assert manifest["evidence"] == "OBSERVED" assert manifest["outputs"] == [{"path": "events.jsonl", "format": "trace/events", "records": len(EXPECTED)}] # fmt: skip info = manifest["log"] assert (info["name"], info["video_std"], info["frame_mclk"]) == ("SMOKE", 1, 313 * 3420) assert info["decoded_bytes"] == info["body_bytes"] == len(BODY) assert info["t_end"] == 464 and info["counter_end"] == (464 - 1000) & 0xFFFFFFFF assert sum(info["counts"].values()) == len(EXPECTED) print("blastem-events smoke: ok") return 0 if __name__ == "__main__": sys.exit(main())