# -*- coding: utf-8 -*- """重做管理页之后的收尾: 1. 补回「退出登录」链接 我重写页面时把它弄丢了,但 /admin/logout 接口一直都在 —— 测试里"页面上有退出登录"这条抓住了。 2. 更新两条过时的断言 删除按钮的 class 从 `del` 变成了 `btn del`(配合新样式)。 测试查的是 `class=del` / `class="del"`,匹配不上。 改成查 `data-device`(删除按钮必有这个属性)更稳, 也不会因为以后改 class 名又误报。 """ from __future__ import annotations import ast import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") RELAY = ROOT / "relay" / "relay.py" BTN_TEST = ROOT / "_tests" / "test_admin_button.py" DEL_TEST = ROOT / "_tests" / "test_admin_delete.py" # ---------------- 1. 退出登录 ---------------- META_OLD = '''
版本 {_esc(snap['version'])} · 已运行 {_uptime(snap['uptimeSeconds'])}
本进程内存 {snap['memoryKB'] // 1024} MB · 累计请求 {snap['totalRequests']}
''' META_NEW = '''
版本 {_esc(snap['version'])} · 已运行 {_uptime(snap['uptimeSeconds'])}
本进程内存 {snap['memoryKB'] // 1024} MB · 累计请求 {snap['totalRequests']} · 退出登录
''' LOGOUT_CSS_ANCHOR = ''' .meta b{{color:var(--dim);font-weight:500}}''' LOGOUT_CSS_NEW = LOGOUT_CSS_ANCHOR + ''' .meta a.logout{{color:var(--dim);text-decoration:none; border-bottom:1px dashed #484f58;transition:.12s}} .meta a.logout:hover{{color:var(--accent);border-bottom-color:var(--accent)}}''' # ---------------- 2. 测试断言 ---------------- BTN_OLD = ' check("有删除按钮", "button class=del" in html or \'class="del"\' in html)' BTN_NEW = (' # 删除按钮的 class 改过(del -> "btn del"),\n' ' # 用 data-device 判定更稳 —— 删除按钮一定带它。\n' ' check("有删除按钮", "data-device" in html and "删除" in html)') DEL_OLD = " check(\"有「删除」按钮\", 'class=del' in html or 'class=\"del\"' in html)" DEL_NEW = (' # class 名改了(del -> "btn del"),改成按行为判定:\n' ' # 有带 data-device 的按钮、且文字是"删除"。\n' ' check("有「删除」按钮",\n' ' "data-device" in html and "删除" in html,\n' ' "找不到删除按钮")') def main() -> int: ok = True text = RELAY.read_text(encoding="utf-8") if "admin/logout" in text and "a.logout" in text: print(" 退出登录链接已在") else: for old, new, label in ((META_OLD, META_NEW, "退出登录链接"), (LOGOUT_CSS_ANCHOR, LOGOUT_CSS_NEW, "退出登录样式")): if old in text: text = text.replace(old, new, 1) print(f" 补回 {label}") else: print(f" [失败] {label} 锚点没找到") ok = False RELAY.write_text(text, encoding="utf-8", newline="\n") for path, old, new, label in ((BTN_TEST, BTN_OLD, BTN_NEW, "test_admin_button"), (DEL_TEST, DEL_OLD, DEL_NEW, "test_admin_delete")): content = path.read_text(encoding="utf-8") if old in content: content = content.replace(old, new, 1) path.write_text(content, encoding="utf-8", newline="\n") print(f" 更新 {label} 的断言") elif "data-device" in content: print(f" {label} 已经改过") else: print(f" [失败] {label} 锚点没找到") ok = False ast.parse(RELAY.read_text(encoding="utf-8")) print(" relay.py 语法 OK") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())