import json import time import urllib.error import urllib.request from typing import Optional BASE = "http://127.0.0.1:8765" def _get(path: str, timeout: float = 5.0) -> dict: with urllib.request.urlopen(BASE + path, timeout=timeout) as r: return json.loads(r.read().decode("utf-8")) def _post(path: str, payload: dict, timeout: float = 10.0) -> dict: data = json.dumps(payload).encode("utf-8") req = urllib.request.Request( BASE + path, data=data, headers={"Content-Type": "application/json"}, method="POST", ) with urllib.request.urlopen(req, timeout=timeout) as r: return json.loads(r.read().decode("utf-8")) def main() -> int: devices_resp = _get("/api/devices") devices = devices_resp.get("devices") or [] print("[smoke] devices:", devices_resp) if not devices: print("[smoke] ERROR: no adb devices") return 2 device_id = devices[0] payload = { "device_id": device_id, "y_pianyi": 0, "needswitch": True, "wait_minutes": 5, } # best-effort: stop any existing run to avoid mixed logs / 409 try: _post("/api/stop", {}) time.sleep(0.5) except Exception: pass # baseline logs (avoid old history) try: base_logs = _get("/api/logs?since=0") last_idx = int(base_logs.get("next") or 0) except Exception: last_idx = 0 try: start_resp = _post("/api/start-run", payload) print("[smoke] start:", start_resp) except urllib.error.HTTPError as e: body = e.read().decode("utf-8", errors="replace") print("[smoke] start HTTPError:", e.code, body) return 3 # poll logs start_ts = time.time() hits: list[str] = [] all_lines: list[str] = [] # 轮询最多 240 秒;命中首次“[异常] 时间/价值异常仍未恢复”后再额外等待一小段时间, # 尽量抓到 reopen 识别结果或“持续异常”最终分支。 post_abnormal_grace_seconds = 20 abnormal_marker_ts: Optional[float] = None while time.time() - start_ts < 240: j = _get(f"/api/logs?since={last_idx}") last_idx = int(j.get("next") or last_idx) lines = j.get("lines") or [] if lines: all_lines.extend(lines) for ln in lines: if any(k in ln for k in ("[截图]", "[详情]", "[异常]")): hits.append(ln) if abnormal_marker_ts is None and any("[异常] 时间/价值异常仍未恢复" in x for x in hits): abnormal_marker_ts = time.time() if any("[异常] 时间/价值持续异常" in x for x in hits): break if any("[详情] reopen" in x for x in hits): break if abnormal_marker_ts is not None and (time.time() - abnormal_marker_ts) >= post_abnormal_grace_seconds: break time.sleep(1) # stop try: stop_resp = _post("/api/stop", {}) print("[smoke] stop:", stop_resp) except Exception as e: print("[smoke] stop ERROR:", e) print("\n[smoke] ==== KEY LINES ([截图]/[详情]/[异常]) tail 80 ====") for ln in hits[-80:]: print(ln) # simple verdict has_shot = any("[截图]" in x for x in hits) has_detail = any("[详情]" in x for x in hits) has_abnormal = any("[异常]" in x for x in hits) saw_first_abnormal = any(("[详情] first" in x and "异常=True" in x) for x in hits) saw_retry1 = any("[详情] retry1" in x for x in hits) saw_reopen = any("[详情] reopen" in x for x in hits) saw_normal = any(("[详情]" in x and "异常=False" in x) for x in hits) print("\n[smoke] verdict:") print("- saw [截图]:", has_shot) print("- saw [详情]:", has_detail) print("- saw [异常]:", has_abnormal) print("- saw first abnormal (first):", saw_first_abnormal) print("- saw retry1:", saw_retry1) print("- saw reopen:", saw_reopen) print("- saw any normal detail:", saw_normal) if not has_shot: print("[smoke] NOTE: 未看到[截图],通常表示子进程未真正跑起来或日志未回流") if has_shot and (not has_detail): print("[smoke] NOTE: 已看到[截图]但未看到[详情],可能还没打开到福袋详情,或 OCR 初始化/识别较慢") # also show last 30 lines for context print("\n[smoke] ==== LAST 50 LOG LINES ====") for ln in all_lines[-50:]: print(ln) return 0 if __name__ == "__main__": raise SystemExit(main())