import os, subprocess, sys, time
from pathlib import Path
root = Path(__file__).resolve().parent
side = sys.argv[1]
# Two identical captures, separated by 500 ms, establish that the still is settled.
for attempt in range(20):
    subprocess.run(["import", "-window", "root", str(root / f"{side}-a.png")], check=True)
    time.sleep(0.5)
    subprocess.run(["import", "-window", "root", str(root / f"{side}.png")], check=True)
    if subprocess.run(["compare", "-metric", "AE", str(root / f"{side}-a.png"), str(root / f"{side}.png"), "null:"], capture_output=True).returncode == 0:
        print(f"{side}: stable, identical screenshots 500 ms apart")
        break
else:
    raise RuntimeError("Desktop did not settle")
