import json
import subprocess
import sys
import time
from pathlib import Path
from PIL import Image, ImageChops

session, output = sys.argv[1:]
output = Path(output)
output.mkdir(parents=True, exist_ok=True)
# Start on the seeded thread, paired with the server and Metro for the tested commit.
commands = ["Worked for 1.1s", "Ran 2 commands", "osascript -e 'set x to 1' -e 'return x + 1'", "osascript -l JavaScript -e 'const x = 1' -e 'x + 1'"]
for label in commands:
    subprocess.run(["agent-device", "click", "label=" + json.dumps(label), "--settle", "--session", session], check=True)
for name in ["a", "b"]:
    subprocess.run(["agent-device", "screenshot", str(output / f"{name}.png"), "--session", session], check=True)
    time.sleep(0.5)
first, second = [Image.open(output / f"{name}.png") for name in ["a", "b"]]
print("Pixel difference:", ImageChops.difference(first, second).getbbox())
