#!/usr/bin/env python3 """Run isolated native live/still captures with local temporary files.""" import argparse import json import os from pathlib import Path import re import shutil import signal import subprocess import time def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("kind", choices=["live", "styles"]) parser.add_argument("device", choices=["iphone", "ipad"]) parser.add_argument("--source", type=Path, required=True) parser.add_argument("--derived", type=Path, required=True) parser.add_argument("--archive", type=Path, required=True) parser.add_argument("--name") parser.add_argument("--language", default="en-US") parser.add_argument("--data-file") args = parser.parse_args() name = args.name or f"{args.device}-{args.kind}" root = Path("/tmp/jotley-video-refresh-20261023/ios") / name root.mkdir(parents=True, exist_ok=False) uid = {"iphone": "61A85A85-28BA-4611-9602-A78041E58E9E", "ipad": "BE809385-F372-4B0A-95A4-E68F40EE6474"}[args.device] subprocess.run(["xcrun", "simctl", "bootstatus", uid, "-b"], check=True, stdout=subprocess.DEVNULL) subprocess.run(["xcrun", "simctl", "status_bar", uid, "override", "--time", "9:41", "--dataNetwork", "wifi", "--wifiMode", "active", "--wifiBars", "3", "--batteryState", "charged", "--batteryLevel", "100"], check=True) marks = root / "marks.txt" env = os.environ.copy() env.update(TEST_RUNNER_CAPTURE_MARKS=str(marks), TEST_RUNNER_CAPTURE_LANGUAGE=args.language, TEST_RUNNER_CAPTURE_STILLS_ROOT=str(root / "shots"), TEST_RUNNER_CAPTURE_DATA_FILE=args.data_file or "") recorder = audio = None start = None raw = root / "take.mov" if args.kind == "live": subprocess.run(["xcrun", "simctl", "privacy", uid, "grant", "microphone", "com.protomota.jotley.ios.local-development"], check=True) recorder = subprocess.Popen(["xcrun", "simctl", "io", uid, "recordVideo", "--codec=h264", str(raw)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) for line in recorder.stdout: if "Recording started" in line: start = time.time() break if start is None: raise RuntimeError("Simulator recording did not start") test_name = "testCaptureRealLiveTranscription" if args.kind == "live" else "testCaptureRealSummaryStyles" command = ["xcodebuild", "test-without-building", "-collect-test-diagnostics", "never", "-parallel-testing-enabled", "NO", "-project", str(args.source / "Jotley.xcodeproj"), "-scheme", "Jotley-iOS", "-configuration", "Debug", "-destination", f"platform=iOS Simulator,id={uid}", "-derivedDataPath", str(args.derived), "-only-testing:Jotley-iOSUITests/JotleyVideoCaptureUITests/" + test_name, "-resultBundlePath", str(root / "result.xcresult")] status = {"kind": args.kind, "device": args.device, "name": name, "simulator": uid, "state": "capturing", "record_start_wall": start, "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"} (root / "capture.json").write_text(json.dumps(status, indent=2)) with (root / "test.log").open("w") as log: test = subprocess.Popen(command, env=env, stdout=log, stderr=subprocess.STDOUT) deadline = time.monotonic() + 360 try: while test.poll() is None: text = marks.read_text() if marks.exists() else "" ready = re.search(r"CAPTURE-MARK live-ready ([0-9.]+)", text) if ready and audio is None and time.time() >= float(ready[1]) + 1: voice = Path("/Volumes/Music/Jotley/Jotley-2.0-App-Store-Videos/audio/demo-photography-original.mp3") status["audio_start_wall"] = time.time() audio = subprocess.Popen(["afplay", str(voice)]) if time.monotonic() > deadline: test.terminate() raise TimeoutError("Capture test exceeded six minutes") time.sleep(0.2) finally: if recorder: recorder.send_signal(signal.SIGINT) recorder.communicate(timeout=30) if audio and audio.poll() is None: audio.terminate() subprocess.run(["xcrun", "simctl", "status_bar", uid, "clear"], check=False) log_text = (root / "test.log").read_text() executed = bool(re.search(r"Executed 1 test,", log_text)) passed = test.returncode == 0 and executed status.update(state="passed" if passed else "failed", test_exit_code=test.returncode, one_test_executed=executed) if not executed: status["failure"] = "No matching test executed; rebuild the capture runner" if recorder: probe = subprocess.run(["ffprobe", "-v", "error", "-show_format", "-show_streams", "-of", "json", str(raw)], capture_output=True, text=True) status["raw_probe_ok"] = probe.returncode == 0 if probe.returncode == 0: status["raw_probe"] = json.loads(probe.stdout) (root / "capture.json").write_text(json.dumps(status, indent=2) + "\n") if args.archive.is_dir(): target = args.archive / name shutil.copytree(root, target, dirs_exist_ok=False) status["archived_to"] = str(target) print(json.dumps(status, indent=2), flush=True) raise SystemExit(0 if passed else test.returncode or 2) if __name__ == "__main__": main()