Repository Public
1#!/usr/bin/env python32"""Control channel for the iOS UI tests.34The XCUITest runner lives in the simulator: it cannot stop or start the local backend, run SQL or drive `simctl`.5This helper, started by Scripts/test.sh on the Mac, listens on 127.0.0.1 and executes exactly these commands sent6by the runner over a plain TCP connection (the simulator shares the Mac's loopback):78 stop\\n stop the local control-api + orchestrator (the ones started by local-backend.sh, by PID)9 start\\n start them again on the same database10 touch\\n make the synthetic nodes fresh again (freshness is computed from elapsed time)11 openurl <url>\\n `xcrun simctl openurl <udid> <url>` — only mcc-ios-debug:// links12 shot <name> <bytes>\\n… save a PNG screenshot (only when a screenshot directory was given)1314Usage: ui-test-control.py <port> <local-backend.sh> <database> <simulator-udid> [screenshot-dir]15"""16import os, re, socketserver, subprocess, sys1718PORT, BACKEND, DB, UDID = int(sys.argv[1]), sys.argv[2], sys.argv[3], sys.argv[4]19SHOTS = sys.argv[5] if len(sys.argv) > 5 and sys.argv[5] else None20PSQL = subprocess.run(["/bin/sh", "-c", "command -v psql || echo /opt/homebrew/bin/psql"], capture_output=True, text=True).stdout.strip()2122TOUCH_SQL = """23update nodes set last_seen_at = now() where alias in ('ios-test-node-01', 'ios-test-node-02');24update node_state set ts = now(), received_at = now() where node_id in (select id from nodes where alias in ('ios-test-node-01', 'ios-test-node-02'));25update agents set last_seen_at = now();26update services set last_seen_at = now() where gone_at is null;27update inference_servers set last_seen_at = now();28update volumes set last_seen_at = now();29-- keep the synthetic alert open (the alert engine clears it after a while: no real metric is above the threshold)30update alerts set state = (case when acknowledged_at is not null then 'acknowledged' else 'firing' end)::alert_state, resolved_at = null, clear_since = null, last_eval_at = now()31 where id = '105e0000-0000-7000-8000-0000000a1e47';32"""3334class Handler(socketserver.StreamRequestHandler):35 def handle(self):36 line = self.rfile.readline().decode("utf-8", "replace").strip()37 ok = False38 if line == "stop":39 ok = subprocess.run(["/bin/bash", BACKEND, "stop"], capture_output=True).returncode == 040 elif line == "start":41 ok = subprocess.run(["/bin/bash", BACKEND, "start", DB], capture_output=True).returncode == 042 elif line == "touch":43 ok = subprocess.run([PSQL, "-X", "-q", "-d", DB, "-c", TOUCH_SQL], capture_output=True).returncode == 044 elif line.startswith("openurl "):45 url = line[len("openurl "):]46 if re.fullmatch(r"mcc-ios-debug://[A-Za-z0-9/_#.-]{1,300}", url):47 ok = subprocess.run(["xcrun", "simctl", "openurl", UDID, url], capture_output=True).returncode == 048 elif line.startswith("shot "):49 m = re.fullmatch(r"shot ([A-Za-z0-9._-]{1,80}) (\d{1,9})", line)50 if m and SHOTS:51 data = self.rfile.read(int(m.group(2)))52 os.makedirs(SHOTS, exist_ok=True)53 with open(os.path.join(SHOTS, m.group(1) + ".png"), "wb") as f:54 f.write(data)55 ok = True56 self.wfile.write(b"ok\n" if ok else b"error\n")5758class Server(socketserver.TCPServer):59 allow_reuse_address = True6061with Server(("127.0.0.1", PORT), Handler) as server:62 server.serve_forever()63