Files
troupe-standup/wizard-work/forge/verify_r41_pixel_diff.py
T

89 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Verify the refactored ops-pulse card r41 is pixel-identical to the original.
Runs the original card script (/root/.hermes/ops-pulse-card-r41.py) and the
refactored card script (refactored_cards/ops-pulse-card-r41_refactored.py) —
both write to the shared /root/.hermes/ops-pulse.png — captures each render to
a separate file, then compares every pixel with numpy.
The refactor's contract: the DRY extraction into ops_pulse_card_lib must not
drop any data. A prior run of this very script failed to find this file
(RALPH LOOP); it also failed the behavioural check because the extraction had
silently dropped the verdict subtext line and the first ("Wizard that moved")
row. Both are now restored, so the two renders must be byte-identical.
Exit 0 when pixel-identical, exit 1 on any mismatch or run failure.
"""
import os
import shutil
import subprocess
import sys
import numpy as np
from PIL import Image
FORGE = "/root/.hermes/wizard-outputs/forge"
ORIGINAL = "/root/.hermes/ops-pulse-card-r41.py"
REFACTORED = os.path.join(FORGE, "refactored_cards", "ops-pulse-card-r41_refactored.py")
SHARED_OUT = "/root/.hermes/ops-pulse.png"
BASELINE = os.path.join(FORGE, "refs", "r41_baseline.png")
TEST = os.path.join(FORGE, "_r41_test_render.png")
def run_script(path):
env = dict(os.environ)
env["PYTHONPATH"] = "/root" + os.pathsep + env.get("PYTHONPATH", "")
r = subprocess.run(
[sys.executable, path],
capture_output=True, text=True, env=env, timeout=120,
)
if r.returncode != 0:
print(f"RUN FAIL {path} exit={r.returncode}")
print("stdout:", r.stdout)
print("stderr:", r.stderr)
sys.exit(1)
if not os.path.exists(SHARED_OUT):
print(f"RUN FAIL {path}: did not produce {SHARED_OUT}")
sys.exit(1)
return SHARED_OUT
def load(path):
return np.array(Image.open(path).convert("RGB"))
def main():
os.makedirs(os.path.dirname(BASELINE), exist_ok=True)
# 1) Original -> baseline
run_script(ORIGINAL)
shutil.copyfile(SHARED_OUT, BASELINE)
ref = load(BASELINE)
# 2) Refactored -> test
run_script(REFACTORED)
shutil.copyfile(SHARED_OUT, TEST)
got = load(TEST)
# 3) Pixel-diff
if ref.shape != got.shape:
print(f"FAIL shape mismatch: {ref.shape} vs {got.shape}")
sys.exit(1)
diff = np.abs(ref.astype(int) - got.astype(int))
total = int(diff.sum())
if total == 0:
print(f"PASS r41 pixel-identical ({ref.shape[1]}x{ref.shape[0]}) — "
f"original vs refactored_card via ops_pulse_card_lib")
sys.exit(0)
mask = diff.any(axis=2)
ys, xs = np.where(mask)
print(f"FAIL r41 pixel-diff total={total} across {len(ys)} px "
f"(of {mask.size}); bbox x[{xs.min()}..{xs.max()}] y[{ys.min()}..{ys.max()}]")
sys.exit(1)
if __name__ == "__main__":
main()