# -*- coding: utf-8 -*- """让测试跟上这一轮的行为改动。 1. 自检:改代码路径时把日志弄丢了,补回来。 2. 静默模式测试:现在"一台设备都没配对过时配对是开的"(自愈,用户报的 bug)。 要测"静默模式拒绝新配对",得先预置一台已配设备。 3. 端到端:爆破测试原本排在成功配对之后 —— 那时配对窗口已经关了, 测不到限速。要挪到成功配对之前。 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") SERVER = ROOT / "windows" / "lanremote" / "server.py" TESTS = ROOT / "_tests" # ---------------- 1. 自检日志 ---------------- LOG_OLD = ''' return self._selfcheck_report(from_ip)''' LOG_NEW = ''' note = self._selfcheck_report(from_ip) first = note.splitlines()[0] if note else "" self._log(f"[自检] 收到来自 {from_ip} 的自检请求,已生成诊断结论({first})") return note''' # ---------------- 2. test_silent_mode 预置已配设备 ---------------- SILENT_SEED_OLD = '''def seed_setup_done(data_dir):''' SILENT_SEED_NEW = '''def seed_paired_device(data_dir): """预置一台已配设备。 现在的规则是:**一台设备都没配对过时,配对是开着的** (这是修用户报的"设完码却连不上")。所以想测 "静默模式拒绝新配对",得先让电脑认为已经有设备配过了。 """ import json as _json data_dir = Path(data_dir) data_dir.mkdir(parents=True, exist_ok=True) (data_dir / "devices.json").write_text( _json.dumps({"already-paired": { "id": "already-paired", "name": "已配的手表", "token": "seed-token", "last": 0}}, ensure_ascii=False), encoding="utf-8") def seed_setup_done(data_dir):''' # ---------------- 3. test_e2e_features 爆破测试挪到最前 ---------------- E2E_MARK = ''' print() print("六、★ 配对防爆破")''' E2E_MOVE_OLD = ''' check("配对成功", reply.get("ok") is True, str(reply)[:120]) print() print("二、★ 功能列表(这是之前完全失败的那一项)")''' E2E_MOVE_NEW = ''' check("配对成功", reply.get("ok") is True, str(reply)[:120]) print() print("一b、★ 配对防爆破(必须在成功配对之前测," "否则配对窗口已经关了)") breaker = Client("127.0.0.1", PORT) wrong = "000000" if code != "000000" else "111111" limited = False for i in range(12): r = breaker.call({"cmd": "pair", "deviceId": f"brute-{i}", "deviceName": "爆破", "code": wrong}, timeout=10) if not r.get("ok") and "过多" in str(r.get("error", "")): limited = True print(f" 第 {i + 1} 次开始被限速") break check("连续错码会被限速", limited, f"12 次都没触发限速,最后一次: {r}") breaker.close() print() print("二、★ 功能列表(这是之前完全失败的那一项)")''' def main() -> int: ok = True # 1. text = SERVER.read_text(encoding="utf-8") if LOG_OLD in text: SERVER.write_text(text.replace(LOG_OLD, LOG_NEW, 1), encoding="utf-8") print(" server: 自检日志已补回") else: print(" [跳过] server 自检日志 —— 锚点没找到") ok = False # 2. path = TESTS / "test_silent_mode.py" text = path.read_text(encoding="utf-8") if "seed_paired_device" in text: print(" test_silent_mode: 加过了") elif SILENT_SEED_OLD in text: text = text.replace(SILENT_SEED_OLD, SILENT_SEED_NEW, 1) # 在 seed_setup_done(DATA) 之后也预置一台设备 if "seed_setup_done(DATA)" in text: text = text.replace("seed_setup_done(DATA)", "seed_setup_done(DATA)\n seed_paired_device(DATA)", 1) path.write_text(text, encoding="utf-8") print(" test_silent_mode: 已加预置设备") else: print(" [跳过] test_silent_mode —— 锚点没找到") ok = False # 3. path = TESTS / "test_e2e_features.py" text = path.read_text(encoding="utf-8") if "一b、" in text: print(" test_e2e_features: 挪过了") elif E2E_MOVE_OLD in text: text = text.replace(E2E_MOVE_OLD, E2E_MOVE_NEW, 1) # 删掉后面那段旧的爆破测试 start = text.find(' print()\n print("六、★ 配对防爆破")') if start > 0: end = text.find(' print()\n print("七、', start) if end > 0: text = text[:start] + text[end:] print(" test_e2e_features: 旧爆破测试已删") path.write_text(text, encoding="utf-8") print(" test_e2e_features: 爆破测试已挪到最前") else: print(" [跳过] test_e2e_features —— 锚点没找到") ok = False import ast for p in (SERVER, TESTS / "test_silent_mode.py", TESTS / "test_e2e_features.py"): try: ast.parse(p.read_text(encoding="utf-8")) except SyntaxError as exc: print(f" {p.name}: 语法错误 {exc}") ok = False print(" 语法检查完成") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())