From 87d0fe2db8f45e718ae98d0b2924db94c782c103 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 21 May 2026 14:15:14 +0800 Subject: [PATCH] =?UTF-8?q?tests:=20=E6=8A=8A=20GUI=20=E2=87=84=20WebUI=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=90=8C=E6=AD=A5=E6=A3=80=E6=9F=A5=E7=BA=B3?= =?UTF-8?q?=E5=85=A5=20pytest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 包装 tools/diagnostics/check_web_gui_config_sync.py 为单元测试: - test_gui_webui_keys_synced:用 check_sync() 检测 GUI 与 WebUI 配置 key 漂移; 失败时打印 "缺哪些 key" + "怎么修复" - test_manifest_shape:build_manifest() 应产出预期形状 + 共享 key 数量级合理 每次有人改 webui/index.html 或 gui/gui_app.py,pytest 都能立即发现 key 漂移, 避免之前那种"GUI 改了 WebUI 没改"的常见缺陷。 回归:tests/ 244 passed / 1 skipped。 Co-Authored-By: Claude Opus 4.7 --- tests/test_gui_webui_sync.py | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/test_gui_webui_sync.py diff --git a/tests/test_gui_webui_sync.py b/tests/test_gui_webui_sync.py new file mode 100644 index 0000000..99a0439 --- /dev/null +++ b/tests/test_gui_webui_sync.py @@ -0,0 +1,82 @@ +"""GUI / WebUI 配置项同步检查的 pytest 包装。 + +把 tools/diagnostics/check_web_gui_config_sync.py 作为单元测试自动跑: +每次 main 上改了 webui/index.html 或 gui/gui_app.py,pytest 都能立即 +发现 GUI ⇄ WebUI 的 key 漂移。 +""" +from __future__ import annotations + +import importlib +import importlib.util +import sys +from pathlib import Path + +import pytest + + +# 把 tools/diagnostics/check_web_gui_config_sync.py 作为模块加载 +PROJECT_ROOT = Path(__file__).resolve().parents[1] +SCRIPT_PATH = PROJECT_ROOT / "tools" / "diagnostics" / "check_web_gui_config_sync.py" + + +@pytest.fixture(scope="module") +def sync_module(): + """加载 check_web_gui_config_sync 模块。""" + if not SCRIPT_PATH.is_file(): + pytest.skip(f"sync 检查脚本不存在: {SCRIPT_PATH}") + spec = importlib.util.spec_from_file_location( + "check_web_gui_config_sync_for_test", str(SCRIPT_PATH) + ) + assert spec and spec.loader, "无法加载 sync 检查脚本" + mod = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = mod + spec.loader.exec_module(mod) + return mod + + +# ============================================================ +# 同步状态:GUI ⇄ WebUI key 不应漂移 +# ============================================================ + +def test_gui_webui_keys_synced(sync_module): + """gui_app.py 与 webui/index.html 的配置 key 必须保持同步。 + + 任何 GUI 独有的 key 没在 WebUI 暴露、WebUI key 没在 GUI 加载/保存的情况, + 都会让本测试失败,提示开发者补全。 + """ + problems, manifest = sync_module.check_sync() + + # 详细打印问题清单,便于失败时定位 + if problems: + msg_lines = ["GUI ⇄ WebUI 配置 key 不同步:\n"] + for title, keys in problems: + msg_lines.append(f" {title}:") + for k in keys[:20]: # 最多列 20 个,避免 traceback 爆屏 + msg_lines.append(f" - {k}") + if len(keys) > 20: + msg_lines.append(f" ... 还有 {len(keys) - 20} 个未列出") + msg_lines.append("\n要么补齐缺失的 key,要么用 'python tools/diagnostics/check_web_gui_config_sync.py --sync' 重生成 manifest。") + pytest.fail("\n".join(msg_lines)) + + +def test_manifest_shape(sync_module): + """build_manifest 应产出预期结构的字典。""" + manifest = sync_module.build_manifest() + + assert isinstance(manifest, dict) + for required_key in ( + "shared_keys", + "gui_runtime_only_keys", + "gui_saved_keys", + "gui_loaded_keys", + "web_keys", + ): + assert required_key in manifest, f"manifest 缺少键 {required_key}" + assert isinstance(manifest[required_key], list), ( + f"manifest['{required_key}'] 应为 list" + ) + + # 共享 key 应该有数量级(防止脚本静默坏掉返回空集) + assert len(manifest["shared_keys"]) > 50, ( + f"共享 key 太少({len(manifest['shared_keys'])}),疑似 sync 检查脚本本身坏了" + )