Repository navigation
Expand file tree
/
Copy pathrecord-ios-feedback.py
More file actions
executable file
·118 lines (117 loc) · 6.1 KB
/
Copy pathrecord-ios-feedback.py
File metadata and controls
executable file
·118 lines (117 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
"""Record identical offline scenarios using the actual iOS core and native renderer."""
import argparse
import json
import os
from pathlib import Path
import shutil
import signal
import subprocess
import time
import http.server
import threading
parser = argparse.ArgumentParser()
parser.add_argument("phase", choices=["before", "after"])
parser.add_argument("simulator")
parser.add_argument("scenarios", nargs="+")
parser.add_argument("--build-root", type=Path)
parser.add_argument("--display", help="Simulator display ID or name, as reported by simctl io enumerate")
args = parser.parse_args()
root = Path(__file__).resolve().parents[1]
build = args.build_root.resolve() if args.build_root else root
out = root / "tests" / "e2e" / "artifacts" / "feedback-recordings" / args.phase
out.mkdir(parents=True, exist_ok=True)
env = os.environ.copy()
env["LOGSEQ_IOS_SIMULATOR"] = args.simulator
live_app_ready = False
display_args = [f"--display={args.display}"] if args.display else []
for scenario in args.scenarios:
server = None
requests = []
if scenario == "responsiveness":
class CatalogHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
requests.append(self.path)
time.sleep(5)
body = b'{"graphs":[]}'
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *_):
pass
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), CatalogHandler)
threading.Thread(target=server.serve_forever, daemon=True).start()
env["LOGSEQ_FEEDBACK_CATALOG_URL"] = f"http://127.0.0.1:{server.server_port}"
else:
env.pop("LOGSEQ_FEEDBACK_CATALOG_URL", None)
env["LOGSEQ_FEEDBACK_SCENARIO"] = scenario
math = scenario == "latex-1466"
source = "feedback-math.swift" if math else "feedback-mobile.swift"
shutil.copy2(root / "apple/Tests/UI" / source, build / "apple/Tests/UI" / source) if build != root else None
fixture = build / ("_build/feedback-empty" if math else "_build/feedback-graph")
fixture.mkdir(parents=True, exist_ok=True)
if not math and not (fixture / "graphs/feedback/graph.sqlite").exists():
shutil.copytree(root / "android/app/src/androidTest/assets/feedback", fixture / "graphs/feedback", dirs_exist_ok=True)
if scenario == "notifications":
shutil.copy2(root / "android/app/src/androidTest/assets/feedback/notifications.json",
fixture / "graphs/feedback/notifications.json")
log_path = out / f"ios-{scenario}.log"
if not math and live_app_ready:
subprocess.run(["xcrun", "simctl", "terminate", args.simulator, "com.logseq.logseq.PageBenchmark"], capture_output=True)
container = Path(subprocess.check_output(["xcrun", "simctl", "get_app_container", args.simulator,
"com.logseq.logseq.PageBenchmark", "data"], text=True).strip())
documents = container / "Documents"
shutil.rmtree(documents)
shutil.copytree(fixture, documents)
launch_env = env.copy()
for name, value in env.items():
if name.startswith("LOGSEQ_"):
launch_env["SIMCTL_CHILD_" + name] = value
with log_path.open("w") as log:
subprocess.run(["xcrun", "simctl", "launch", "--terminate-running-process", args.simulator,
"com.logseq.logseq.PageBenchmark"], env=launch_env, stdout=log, stderr=subprocess.STDOUT, check=True, timeout=30)
else:
with log_path.open("w") as log:
subprocess.run(["python3", str(build / "apple/Tests/UI/run-simulator-runtime-check.py"),
str(build / "apple/Tests/UI" / source), "--interactive", str(fixture)],
env=env, stdout=log, stderr=subprocess.STDOUT, check=True, timeout=180)
live_app_ready = not math
recorder = subprocess.Popen(["xcrun", "simctl", "io", args.simulator, "recordVideo", *display_args, "--codec=h264", "--force", str(out / f"ios-{scenario}.mp4")],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
if math and args.phase == "before":
time.sleep(8)
else:
container = Path(subprocess.check_output(["xcrun", "simctl", "get_app_container", args.simulator,
"com.logseq.logseq.PageBenchmark", "data"], text=True).strip())
result_file = container / "Documents/feedback-result.json"
deadline = time.monotonic() + 55
while not result_file.exists() and time.monotonic() < deadline:
time.sleep(.3)
if not result_file.exists():
raise RuntimeError("Scenario did not complete: " + scenario)
result = json.loads(result_file.read_text())
if server:
result["catalog-requests"] = requests
if not requests:
raise RuntimeError("No catalog request reached the delayed test server")
(out / f"ios-{scenario}.json").write_text(json.dumps(result, indent=2))
imported = container / "Documents/feedback-import.json"
if imported.exists():
shutil.copy2(imported, out / f"ios-{scenario}-import.json")
for screenshot in (container / "Documents").glob("feedback-*.png"):
shutil.copy2(screenshot, out / ("ios-" + screenshot.name))
if "failure" in result:
raise RuntimeError(str(result))
if args.phase == "after" and not all(result["observations"].values()):
raise RuntimeError(str(result))
time.sleep(2)
subprocess.run(["xcrun", "simctl", "io", args.simulator, "screenshot", *display_args, str(out / f"ios-{scenario}.png")], check=True)
finally:
recorder.send_signal(signal.SIGINT)
recorder.communicate(timeout=20)
if server:
server.shutdown(); server.server_close()
print("Recorded", args.phase, scenario, flush=True)