import json import time import urllib.request import urllib.error from datetime import datetime 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 = 15.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 _is_click_join(line: str) -> bool: s = line or "" return ("点击参与抽奖" in s) or ("[参与]" in s and "动作=点击参与" in s) def _is_attend_ok(line: str) -> bool: s = line or "" return ( ("已经参与,等待开奖" in s) or ("已经参与,等待看播时间凑齐开奖" in s) or ("已参与成功(或看播中)" in s) or ("[参与]" in s and ("结果=已参与" in s or "结果=看播中" in s)) ) def main() -> int: print("[测试] Web GUI 地址:", BASE) print("[测试] 开始时间:", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) devices_resp = _get("/api/devices") devices = devices_resp.get("devices") or [] print("[测试] ADB:", devices_resp.get("adb")) print("[测试] 设备列表:", devices) if not devices: print("[测试][失败] 未发现任何 ADB 设备(请确认 adb devices 能看到设备)") return 2 device_id = devices[0] print("[测试] 使用设备:", device_id) # 停止旧进程 try: _post("/api/stop", {}) time.sleep(0.5) except Exception: pass # baseline logs last_idx = 0 try: base_logs = _get("/api/logs?since=0") last_idx = int(base_logs.get("next") or 0) except Exception: last_idx = 0 payload = { "device_id": device_id, "y_pianyi": 0, "needswitch": True, "wait_minutes": 5, } try: start_resp = _post("/api/start-run", payload) print("[测试] 启动挂机:", start_resp) except urllib.error.HTTPError as e: body = e.read().decode("utf-8", errors="replace") print("[测试][失败] 启动 HTTPError:", e.code, body) return 3 duration_seconds = 300 print(f"[测试] 监控时长:{duration_seconds} 秒(5 分钟)") start_ts = time.time() key_lines: list[str] = [] saw_click_join = False saw_attend_ok = False abnormal_count = 0 last_stats_ts = 0.0 try: while time.time() - start_ts < duration_seconds: j = _get(f"/api/logs?since={last_idx}") last_idx = int(j.get("next") or last_idx) lines = j.get("lines") or [] for ln in lines: if any(k in ln for k in ("[截图]", "[详情]", "[异常]", "[参与]", "[预约]", "点击打开福袋详情")): key_lines.append(ln) if _is_click_join(ln): saw_click_join = True if _is_attend_ok(ln): saw_attend_ok = True if "[异常]" in ln: abnormal_count += 1 # 每 10 秒打一次 stats(更直观看是否有“参与点击/参与成功”计数) now = time.time() if now - last_stats_ts >= 10: last_stats_ts = now try: st = _get("/api/stats") c = st.get("counters") or {} print( "[测试][统计] 运行=", st.get("running"), " 参与点击=", c.get("fudai_click_attend", 0), " 参与成功=", c.get("fudai_attend_ok", 0), " 打开详情=", c.get("fudai_open_detail", 0), " 异常打开=", c.get("fudai_open_error", 0), ) except Exception: pass # 已经出现“点击参与+参与成功”就提前结束(你要的是5分钟内能参与) if saw_click_join and saw_attend_ok: print("[测试] 已检测到参与点击 + 参与成功标志,提前结束监控。") break time.sleep(1) finally: # stop try: stop_resp = _post("/api/stop", {}) print("[测试] 已停止挂机:", stop_resp) except Exception as e: print("[测试][警告] 停止失败:", e) print("\n[测试] ===== 结果汇总 =====") print("- 5分钟内是否出现【点击参与】:", "是" if saw_click_join else "否") print("- 5分钟内是否出现【参与成功/已参与】:", "是" if saw_attend_ok else "否") print("- [异常] 行计数:", abnormal_count) print("\n[测试] ===== 关键日志(最近 120 行) =====") for ln in key_lines[-120:]: print(ln) if not saw_click_join: print("\n[测试][判定] 5分钟内没有看到‘点击参与’相关日志:可能存在问题(没打开到福袋详情/识别失败/策略跳过/禁抢时段等)。") return 10 if saw_click_join and (not saw_attend_ok): print("\n[测试][判定] 看到‘点击参与’,但没看到‘已参与/看播中’:可能按钮点击后未生效或被门槛/弹窗打断。") return 11 print("\n[测试][通过] 5分钟内参与链路有证据日志。") return 0 if __name__ == "__main__": raise SystemExit(main())