# -*- coding: utf-8 -*- """修 test_boot_silent.py 里被我改坏的那一段。 背景: 开机静默运行时,如果配对码被别的设备占了,被控端会弹通知 (这是新功能,用户要求的)。但旧断言要求"开机后一个窗口都没有", 于是新功能一工作测试就失败。 正确的断言是:**允许临时通知窗口,但不许有常驻的控制窗口**。 上一次修改时缩进没处理好,还叠了三引号,用 PowerShell 传就炸了。 这次老老实实写文件。 """ from __future__ import annotations import ast import sys from pathlib import Path TARGET = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\_tests" r"\test_boot_silent.py") # 好的一段(缩进 8 空格,和周围对齐) GOOD = [ " # 通知窗口是**应该**出现的:比如配对码被别的设备占了,\n", " # 必须让用户看到「该配对码已被使用,请换一个配对码」,\n", " # 否则用户永远不知道为什么连不上。\n", " # 要保证的是:除了这种临时通知,没有常驻的控制窗口。\n", ' notices = [w for w in visible if "通知" in (w.get("title") or "")]\n', " others = [w for w in visible if w not in notices]\n", " if notices:\n", ' print(f" (有 {len(notices)} 个通知窗口,这是对的"\n', ' f" —— 说明它在提示用户什么事)")\n', ' check("★ 开机后没有常驻的控制窗口", not others, str(others[:3]))\n', ] def main() -> int: lines = TARGET.read_text(encoding="utf-8").splitlines(keepends=True) # 找出要被替换的区间:从"通知窗口是"那行开始,到 check("★ ... 那行结束 start = end = None for index, line in enumerate(lines): if "通知窗口是" in line and start is None: start = index if start is not None and "开机后没有常驻的控制窗口" in line: end = index break if start is None or end is None: print(" [失败] 找不到要替换的区间") return 1 print(f" 替换第 {start + 1} 到 {end + 1} 行({end - start + 1} 行)") lines[start:end + 1] = GOOD text = "".join(lines) try: ast.parse(text) except SyntaxError as exc: print(f" 语法错误: 行 {exc.lineno}: {exc.msg}") if exc.text: print(f" {exc.text.rstrip()}") return 1 TARGET.write_text(text, encoding="utf-8", newline="\n") print(" 语法 OK,已写入") # 复核 for index, line in enumerate(text.splitlines(), 1): if "常驻的控制窗口" in line or "notices = " in line: print(f" {index}: {line}") return 0 if __name__ == "__main__": sys.exit(main())