# -*- coding: utf-8 -*- """界面重做之后的测试收尾。 * test_image_modes.py 改用 HeadlessUI(AgentUI 已删) * test_indicator_visible.py 启动前先置 setup_done(否则会弹设置窗口) * verify_all.ps1 去掉已删的测试阶段 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") TESTS = ROOT / "_tests" # ---------------- 1. test_image_modes.py ---------------- IMAGE = TESTS / "test_image_modes.py" IMAGE_FIXES = [ ("from lanremote.ui import AgentUI # noqa: E402", "from lanremote.headless import HeadlessUI # noqa: E402", "改导入"), (" agent = AgentUI(store)", " agent = HeadlessUI(store)", "改构造"), (" try:\n agent.root.destroy()\n except tk.TclError:\n pass", " try:\n agent.stop()\n except Exception:\n pass", "改收尾"), ] # ---------------- 2. test_indicator_visible.py ---------------- IND = TESTS / "test_indicator_visible.py" IND_SEED = ''' def _seed_setup_done(data_dir): """这些测试测的是提示条,不是首次设置窗口。 界面重做之后,没设置过配对码就会弹设置窗口,会干扰测试, 所以先往数据目录里写一份 setup_done。 """ import json as _json data_dir = Path(data_dir) data_dir.mkdir(parents=True, exist_ok=True) settings = data_dir / "settings.json" payload = {} if settings.is_file(): try: payload = _json.loads(settings.read_text(encoding="utf-8")) except Exception: payload = {} payload["setup_done"] = True settings.write_text(_json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8") ''' def patch_file(path: Path, pairs) -> int: if not path.is_file(): print(f" [跳过] {path.name} 不存在") return 0 text = path.read_text(encoding="utf-8") hits = 0 for old, new, tag in pairs: if old not in text: print(f" [跳过] {path.name} / {tag} —— 锚点没找到") continue text = text.replace(old, new, 1) hits += 1 print(f" {path.name} / {tag}: OK") if hits: path.write_text(text, encoding="utf-8") return hits def main() -> int: print("一、test_image_modes.py → HeadlessUI") patch_file(IMAGE, IMAGE_FIXES) print() print("二、test_indicator_visible.py 预置 setup_done") if IND.is_file(): text = IND.read_text(encoding="utf-8") if "_seed_setup_done" in text: print(" 已经加过") else: text = text.replace("def main() -> int:", IND_SEED + "def main() -> int:", 1) print(" 已插入辅助函数") # 在启动子进程之前调用(找 Popen 前一行) marker = " agent = subprocess.Popen(" if marker in text: text = text.replace( marker, " _seed_setup_done(DATA_DIR)\n\n" + marker, 1) print(" 已在启动前调用(用 DATA_DIR 变量名)") IND.write_text(text, encoding="utf-8") else: print(" 文件不存在") print() print("三、verify_all.ps1 去掉已删的阶段") va = TESTS / "verify_all.ps1" raw = va.read_bytes() bom = raw[:3] == b"\xef\xbb\xbf" text = raw.decode("utf-8-sig") # 删掉引用已删测试的整段 import re for name in ("test_pc_gui.py", "test_simple_ui.py", "test_quit.py"): pattern = re.compile( r'\n# -+\s*\nBanner "[^"]*"\n(?:.*?\n)*?Record "[^"]*"[^\n]*\n', re.M) # 简单做法:把包含该文件名的 Banner 段整段删掉 lines = text.splitlines(keepends=True) out = [] i = 0 removed = 0 while i < len(lines): line = lines[i] if name in line: # 往上回溯,删掉这段的 Banner / Stop-Strays / 注释 while out and (out[-1].strip().startswith("#") or out[-1].strip().startswith("Banner") or out[-1].strip() == "Stop-Strays" or out[-1].strip() == ""): out.pop() # 往下删到 Record 那一行 j = i while j < len(lines) and 'Record "' not in lines[j]: j += 1 i = j + 1 if j < len(lines) else len(lines) removed += 1 continue out.append(line) i += 1 if removed: text = "".join(out) print(f" 已移除 {name} 相关阶段({removed} 处)") data = text.encode("utf-8") if bom: data = b"\xef\xbb\xbf" + data va.write_bytes(data) print(" verify_all.ps1 已更新") print() print("四、语法检查") import ast ok = True for path in (IMAGE, IND): if not path.is_file(): continue try: ast.parse(path.read_text(encoding="utf-8")) print(f" {path.name}: OK") except SyntaxError as exc: print(f" {path.name}: 语法错误 {exc}") ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())