-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathe2e_test.py
More file actions
257 lines (212 loc) · 8.67 KB
/
Copy pathe2e_test.py
File metadata and controls
257 lines (212 loc) · 8.67 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
"""
Android-MCP end-to-end validation script.
Starts the MCP server over stdio, exercises ListDevices and Snapshot, and
verifies that the server selects a physical device rather than an emulator.
Usage (from the repo root):
python3 e2e_test.py # auto-detects any connected device
python3 e2e_test.py RFCW70CZWKV # assert a specific serial is selected
Requires: a physical Android device connected via USB with USB debugging enabled.
"""
import json
import os
import shutil
import subprocess
import sys
import threading
import time
from typing import Optional
REPO = os.path.dirname(os.path.abspath(__file__))
BOLD = "\033[1m"
GREEN = "\033[32m"
RED = "\033[31m"
CYAN = "\033[36m"
YELLOW = "\033[33m"
RESET = "\033[0m"
EXPECT_SERIAL = sys.argv[1] if len(sys.argv) > 1 else None
def log(tag, msg, color=CYAN):
print(f"{color}{BOLD}[{tag}]{RESET} {msg}", flush=True)
# ── Resolve ADB ───────────────────────────────────────────────────────────────
def _find_adb() -> Optional[str]:
candidates = [
shutil.which("adb"),
"/opt/homebrew/bin/adb",
os.path.expanduser("~/Library/Android/sdk/platform-tools/adb"),
"/usr/local/bin/adb",
"/usr/bin/adb",
]
for c in candidates:
if c and os.path.isfile(c) and os.access(c, os.X_OK):
return c
return None
ADB = _find_adb()
def _make_env() -> dict:
env = os.environ.copy()
extra_dirs = [
"/opt/homebrew/bin",
"/opt/homebrew/sbin",
"/usr/local/bin",
os.path.expanduser("~/Library/Android/sdk/platform-tools"),
]
if ADB:
extra_dirs.insert(0, os.path.dirname(ADB))
current_path = env.get("PATH", "")
new_dirs = [d for d in extra_dirs if d not in current_path.split(":")]
env["PATH"] = ":".join(new_dirs) + (":" + current_path if current_path else "")
return env
ENV = _make_env()
SERVER_CMD = ["uv", "--directory", REPO, "run", "android-mcp"]
def _is_emulator(serial: str) -> bool:
return serial.startswith("emulator-")
# ── MCP stdio helpers (newline-delimited JSON) ─────────────────────────────────
def _send(proc, obj):
proc.stdin.write((json.dumps(obj) + "\n").encode())
proc.stdin.flush()
def _recv(proc, timeout=60):
deadline = time.time() + timeout
while True:
if time.time() > deadline:
raise TimeoutError(f"No response within {timeout}s")
line = proc.stdout.readline()
if not line:
raise EOFError("Server stdout closed")
line = line.strip()
if line:
return json.loads(line)
# ── Test ───────────────────────────────────────────────────────────────────────
def run():
print()
log("ENV", f"ADB binary : {ADB or 'NOT FOUND'}", YELLOW)
log("ENV", f"PATH (first): {ENV['PATH'][:120]}…", YELLOW)
if not ADB:
log("ERROR", "adb not found — install it or add to PATH", RED)
return 1
# Confirm at least one physical device is visible before starting the server
log("BOOT", "Running: adb devices", GREEN)
adb_out = subprocess.run([ADB, "devices"], capture_output=True, text=True, env=ENV)
print(adb_out.stdout.strip())
adb_lines = adb_out.stdout.strip().splitlines()
physical = [
ln.split()[0]
for ln in adb_lines[1:] # skip "List of devices attached"
if ln.strip() and "device" in ln.split()[-1:]
and not _is_emulator(ln.split()[0])
]
emulators = [
ln.split()[0]
for ln in adb_lines[1:]
if ln.strip() and _is_emulator(ln.split()[0])
]
if not physical:
log("WARN", "No physical device in adb devices — is one connected?", RED)
else:
log("INFO", f"Physical device(s): {', '.join(physical)}", GREEN)
if emulators:
log("INFO", f"Emulator(s) also present: {', '.join(emulators)} (bug scenario)", YELLOW)
print()
log("BOOT", f"Starting: {' '.join(SERVER_CMD)}", GREEN)
proc = subprocess.Popen(
SERVER_CMD,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=ENV,
)
stderr_lines = []
def _drain():
for raw in proc.stderr:
line = raw.decode(errors="replace").rstrip()
stderr_lines.append(line)
print(f" {CYAN}[stderr]{RESET} {line}", flush=True)
threading.Thread(target=_drain, daemon=True).start()
try:
# 1. Initialize
log("MCP", "→ initialize")
_send(proc, {
"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "e2e-test", "version": "1.0"},
},
})
resp = _recv(proc)
if resp.get("error"):
raise RuntimeError(f"initialize error: {resp['error']}")
info = resp.get("result", {}).get("serverInfo", {})
log("MCP", f"← server: {info.get('name')} {info.get('version', '')}", GREEN)
_send(proc, {"jsonrpc": "2.0", "method": "notifications/initialized"})
time.sleep(0.3)
# 2. ListDevices — assert physical device is present
log("MCP", "→ tools/call ListDevices")
_send(proc, {
"jsonrpc": "2.0", "id": 2,
"method": "tools/call",
"params": {"name": "ListDevices", "arguments": {}},
})
resp = _recv(proc)
if resp.get("error"):
raise RuntimeError(f"ListDevices error: {resp['error']}")
device_text = " ".join(c.get("text", "") for c in resp["result"]["content"])
log("MCP", f"← ListDevices:\n{device_text}", GREEN)
if EXPECT_SERIAL:
assert EXPECT_SERIAL in device_text, \
f"Expected serial {EXPECT_SERIAL} not in ListDevices:\n{device_text}"
log("CHECK", f"Expected serial {EXPECT_SERIAL} present ✓", GREEN)
else:
# At least one non-emulator serial must appear
assert any(s in device_text for s in physical), \
f"No physical device serial found in ListDevices:\n{device_text}"
log("CHECK", "Physical device present in ListDevices ✓", GREEN)
# 3. Snapshot (connects to device — real hardware test)
log("MCP", "→ tools/call Snapshot use_vision=false")
_send(proc, {
"jsonrpc": "2.0", "id": 3,
"method": "tools/call",
"params": {"name": "Snapshot", "arguments": {"use_vision": False}},
})
resp = _recv(proc, timeout=120)
if resp.get("error"):
raise RuntimeError(f"Snapshot error: {resp['error']}")
content = resp["result"]["content"]
tree = " ".join(c.get("text", "") for c in content if c.get("type") == "text")
preview = tree[:800] + ("…" if len(tree) > 800 else "")
log("MCP", f"← Snapshot (first 800 chars):\n{preview}", GREEN)
assert len(tree) > 50, "Snapshot returned suspiciously little text"
log("CHECK", "Snapshot returned a UI tree ✓", GREEN)
# 4. When a physical device is available, no emulator serial should appear
# in server logs (the core regression check for this fix).
time.sleep(0.5)
if emulators and physical:
picked_emulator = next(
(em for em in emulators if any(em in l for l in stderr_lines)),
None,
)
if picked_emulator:
log("WARN",
f"{picked_emulator} appeared in server logs despite physical "
f"device being available — fix may not be active", RED)
else:
log("CHECK",
f"No emulator serial in server logs (physical device preferred) ✓",
GREEN)
print()
log("RESULT", "ALL CHECKS PASSED — Android-MCP correctly targets physical device ✓", GREEN)
return 0
except Exception as exc:
print()
log("RESULT", f"FAILED: {exc}", RED)
time.sleep(1)
if stderr_lines:
print("\nRecent server stderr:")
for l in stderr_lines[-20:]:
print(f" {l}")
return 1
finally:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
if __name__ == "__main__":
sys.exit(run())