Files
douyin_guaji-main/tests/test_gui_webui_sync.py
T
CodexandClaude Opus 4.7 87d0fe2db8 tests: 把 GUI ⇄ WebUI 配置同步检查纳入 pytest
包装 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 <noreply@anthropic.com>
2026-05-21 14:15:14 +08:00

83 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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 检查脚本本身坏了"
)