Jotley / website / scripts / video-refresh / capture-simulator.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Run isolated native live/still captures with local temporary files.""" |
| 3 | import argparse |
| 4 | import json |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import re |
| 8 | import shutil |
| 9 | import signal |
| 10 | import subprocess |
| 11 | import time |
| 12 | |
| 13 | |
| 14 | def main(): |
| 15 | parser = argparse.ArgumentParser(description=__doc__) |
| 16 | parser.add_argument("kind", choices=["live", "styles"]) |
| 17 | parser.add_argument("device", choices=["iphone", "ipad"]) |
| 18 | parser.add_argument("--source", type=Path, required=True) |
| 19 | parser.add_argument("--derived", type=Path, required=True) |
| 20 | parser.add_argument("--archive", type=Path, required=True) |
| 21 | parser.add_argument("--name") |
| 22 | parser.add_argument("--language", default="en-US") |
| 23 | parser.add_argument("--data-file") |
| 24 | args = parser.parse_args() |
| 25 | name = args.name or f"{args.device}-{args.kind}" |
| 26 | root = Path("/tmp/jotley-video-refresh-20261023/ios") / name |
| 27 | root.mkdir(parents=True, exist_ok=False) |
| 28 | uid = {"iphone": "61A85A85-28BA-4611-9602-A78041E58E9E", "ipad": "BE809385-F372-4B0A-95A4-E68F40EE6474"}[args.device] |
| 29 | subprocess.run(["xcrun", "simctl", "bootstatus", uid, "-b"], check=True, stdout=subprocess.DEVNULL) |
| 30 | subprocess.run(["xcrun", "simctl", "status_bar", uid, "override", "--time", "9:41", "--dataNetwork", "wifi", "--wifiMode", "active", "--wifiBars", "3", "--batteryState", "charged", "--batteryLevel", "100"], check=True) |
| 31 | marks = root / "marks.txt" |
| 32 | env = os.environ.copy() |
| 33 | env.update(TEST_RUNNER_CAPTURE_MARKS=str(marks), TEST_RUNNER_CAPTURE_LANGUAGE=args.language, |
| 34 | TEST_RUNNER_CAPTURE_STILLS_ROOT=str(root / "shots"), TEST_RUNNER_CAPTURE_DATA_FILE=args.data_file or "") |
| 35 | recorder = audio = None |
| 36 | start = None |
| 37 | raw = root / "take.mov" |
| 38 | if args.kind == "live": |
| 39 | subprocess.run(["xcrun", "simctl", "privacy", uid, "grant", "microphone", "com.protomota.jotley.ios.local-development"], check=True) |
| 40 | recorder = subprocess.Popen(["xcrun", "simctl", "io", uid, "recordVideo", "--codec=h264", str(raw)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 41 | for line in recorder.stdout: |
| 42 | if "Recording started" in line: |
| 43 | start = time.time() |
| 44 | break |
| 45 | if start is None: |
| 46 | raise RuntimeError("Simulator recording did not start") |
| 47 | test_name = "testCaptureRealLiveTranscription" if args.kind == "live" else "testCaptureRealSummaryStyles" |
| 48 | command = ["xcodebuild", "test-without-building", "-collect-test-diagnostics", "never", "-parallel-testing-enabled", "NO", |
| 49 | "-project", str(args.source / "Jotley.xcodeproj"), "-scheme", "Jotley-iOS", "-configuration", "Debug", |
| 50 | "-destination", f"platform=iOS Simulator,id={uid}", "-derivedDataPath", str(args.derived), |
| 51 | "-only-testing:Jotley-iOSUITests/JotleyVideoCaptureUITests/" + test_name, "-resultBundlePath", str(root / "result.xcresult")] |
| 52 | status = {"kind": args.kind, "device": args.device, "name": name, "simulator": uid, "state": "capturing", "record_start_wall": start, |
| 53 | "source": str(args.source), "speech": "Real microphone and Apple Speech; no simulated speech hooks" if args.kind == "live" else "No speech capture; seeded transcript with real on-device generation", "model": "Real on-device generation"} |
| 54 | (root / "capture.json").write_text(json.dumps(status, indent=2)) |
| 55 | with (root / "test.log").open("w") as log: |
| 56 | test = subprocess.Popen(command, env=env, stdout=log, stderr=subprocess.STDOUT) |
| 57 | deadline = time.monotonic() + 360 |
| 58 | try: |
| 59 | while test.poll() is None: |
| 60 | text = marks.read_text() if marks.exists() else "" |
| 61 | ready = re.search(r"CAPTURE-MARK live-ready ([0-9.]+)", text) |
| 62 | if ready and audio is None and time.time() >= float(ready[1]) + 1: |
| 63 | voice = Path("/Volumes/Music/Jotley/Jotley-2.0-App-Store-Videos/audio/demo-photography-original.mp3") |
| 64 | status["audio_start_wall"] = time.time() |
| 65 | audio = subprocess.Popen(["afplay", str(voice)]) |
| 66 | if time.monotonic() > deadline: |
| 67 | test.terminate() |
| 68 | raise TimeoutError("Capture test exceeded six minutes") |
| 69 | time.sleep(0.2) |
| 70 | finally: |
| 71 | if recorder: |
| 72 | recorder.send_signal(signal.SIGINT) |
| 73 | recorder.communicate(timeout=30) |
| 74 | if audio and audio.poll() is None: |
| 75 | audio.terminate() |
| 76 | subprocess.run(["xcrun", "simctl", "status_bar", uid, "clear"], check=False) |
| 77 | log_text = (root / "test.log").read_text() |
| 78 | executed = bool(re.search(r"Executed 1 test,", log_text)) |
| 79 | passed = test.returncode == 0 and executed |
| 80 | status.update(state="passed" if passed else "failed", test_exit_code=test.returncode, one_test_executed=executed) |
| 81 | if not executed: |
| 82 | status["failure"] = "No matching test executed; rebuild the capture runner" |
| 83 | if recorder: |
| 84 | probe = subprocess.run(["ffprobe", "-v", "error", "-show_format", "-show_streams", "-of", "json", str(raw)], capture_output=True, text=True) |
| 85 | status["raw_probe_ok"] = probe.returncode == 0 |
| 86 | if probe.returncode == 0: |
| 87 | status["raw_probe"] = json.loads(probe.stdout) |
| 88 | (root / "capture.json").write_text(json.dumps(status, indent=2) + "\n") |
| 89 | if args.archive.is_dir(): |
| 90 | target = args.archive / name |
| 91 | shutil.copytree(root, target, dirs_exist_ok=False) |
| 92 | status["archived_to"] = str(target) |
| 93 | print(json.dumps(status, indent=2), flush=True) |
| 94 | raise SystemExit(0 if passed else test.returncode or 2) |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | main() |