280 lines
9.6 KiB
Python
280 lines
9.6 KiB
Python
"""gui/components 模块单元测试。
|
|
|
|
覆盖:
|
|
- log_color_config: get_operation_type, get_color_config 纯函数
|
|
- mapping_utils: _format_timestamp 纯函数
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
class _FakeVar:
|
|
def __init__(self, value):
|
|
self.value = value
|
|
|
|
def get(self):
|
|
return self.value
|
|
|
|
def set(self, value):
|
|
self.value = value
|
|
|
|
|
|
class TestGetOperationType:
|
|
"""get_operation_type 纯函数测试。"""
|
|
|
|
def test_ocr_keywords(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[GUI] - OCR识别成功")
|
|
assert result == "success"
|
|
|
|
def test_click_keywords(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[GUI] - 点击参与按钮")
|
|
assert result == "click" or result is not None
|
|
|
|
def test_swipe_keywords(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[GUI] - 滑动向上滑动切换直播间")
|
|
assert result == "swipe" or result is not None
|
|
|
|
def test_screenshot_keywords(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[GUI] - 截图成功")
|
|
assert result == "screenshot" or result is not None
|
|
|
|
def test_error_keywords(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[ERROR] 操作失败")
|
|
assert result == "error"
|
|
|
|
def test_error_status_has_priority_over_business_type(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[INFO] [福袋详情] OCR识别失败")
|
|
assert result == "error"
|
|
|
|
def test_warning_status_has_priority(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[WARNING] [验证] 倒计时OCR结果完全为空")
|
|
assert result == "warning"
|
|
|
|
def test_fudai_detail_has_own_color_category(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[INFO] [福袋详情] read_fudai_detail_once完成")
|
|
assert result == "detail"
|
|
|
|
def test_debug_noise_is_gray_category(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("[INFO] [调试图像] 福袋详情综合标注图已保存")
|
|
assert result == "debug"
|
|
|
|
def test_unknown_line_returns_none_or_default(self):
|
|
from gui.components.log_color_config import get_operation_type
|
|
|
|
result = get_operation_type("random text without keywords")
|
|
# 可能返回 None 或默认类型
|
|
assert result is None or isinstance(result, str)
|
|
|
|
|
|
class TestGetColorConfig:
|
|
"""get_color_config 纯函数测试。"""
|
|
|
|
def test_default_theme(self):
|
|
from gui.components.log_color_config import get_color_config
|
|
|
|
config = get_color_config()
|
|
assert isinstance(config, dict)
|
|
assert len(config) > 0
|
|
|
|
def test_litera_theme(self):
|
|
from gui.components.log_color_config import get_color_config
|
|
|
|
config = get_color_config("litera")
|
|
assert isinstance(config, dict)
|
|
|
|
def test_darkly_theme(self):
|
|
from gui.components.log_color_config import get_color_config
|
|
|
|
config = get_color_config("darkly")
|
|
assert isinstance(config, dict)
|
|
|
|
def test_config_has_operation_types(self):
|
|
from gui.components.log_color_config import get_color_config
|
|
|
|
config = get_color_config()
|
|
# 应该包含一些操作类型的颜色配置
|
|
assert any(key in config for key in ["ocr", "click", "error", "info"])
|
|
assert "detail" in config
|
|
assert "warning" in config
|
|
assert "debug" in config
|
|
|
|
|
|
class TestApplyOperationColor:
|
|
"""apply_operation_color 函数测试。"""
|
|
|
|
def test_function_exists(self):
|
|
from gui.components.log_color_config import apply_operation_color
|
|
|
|
assert callable(apply_operation_color)
|
|
|
|
|
|
class TestMappingUtils:
|
|
"""mapping_utils 模块测试。"""
|
|
|
|
def test_format_timestamp_with_int(self):
|
|
from gui.components.mapping_utils import _format_timestamp
|
|
|
|
result = _format_timestamp(1700000000)
|
|
assert isinstance(result, str)
|
|
assert len(result) > 0
|
|
|
|
def test_format_timestamp_with_float(self):
|
|
from gui.components.mapping_utils import _format_timestamp
|
|
|
|
result = _format_timestamp(1700000000.5)
|
|
assert isinstance(result, str)
|
|
|
|
def test_format_timestamp_with_none(self):
|
|
from gui.components.mapping_utils import _format_timestamp
|
|
|
|
result = _format_timestamp(None)
|
|
assert isinstance(result, str)
|
|
|
|
def test_format_timestamp_with_invalid(self):
|
|
from gui.components.mapping_utils import _format_timestamp
|
|
|
|
result = _format_timestamp("invalid")
|
|
assert isinstance(result, str)
|
|
|
|
def test_load_reservations_function_exists(self):
|
|
from gui.components.mapping_utils import load_reservations
|
|
|
|
assert callable(load_reservations)
|
|
|
|
def test_parse_entries_function_exists(self):
|
|
from gui.components.mapping_utils import parse_entries
|
|
|
|
assert callable(parse_entries)
|
|
|
|
def test_load_reservations_prefers_path_config(self):
|
|
import inspect
|
|
from gui.components import mapping_utils
|
|
|
|
src = inspect.getsource(mapping_utils.load_reservations)
|
|
assert "path_config.get_reservations_path()" in src
|
|
assert "os.getcwd()" not in src
|
|
|
|
|
|
class TestMappingTablePaths:
|
|
def test_mapping_table_no_cwd_fallback(self):
|
|
import inspect
|
|
from gui.components import mapping_table
|
|
|
|
src = inspect.getsource(mapping_table.create_mapping_tab)
|
|
assert "path_config.get_project_root()" in src
|
|
assert "path_config.get_reservations_path()" in src
|
|
assert "os.getcwd()" not in src
|
|
|
|
|
|
class TestGuiComponentsModules:
|
|
"""gui/components 模块存在性测试。"""
|
|
|
|
def test_device_selector_importable(self):
|
|
from gui.components import device_selector
|
|
|
|
assert device_selector is not None
|
|
|
|
def test_log_viewer_importable(self):
|
|
from gui.components import log_viewer
|
|
|
|
assert log_viewer is not None
|
|
|
|
def test_error_log_viewer_importable(self):
|
|
from gui.components import error_log_viewer
|
|
|
|
assert error_log_viewer is not None
|
|
|
|
def test_actions_log_viewer_importable(self):
|
|
from gui.components import actions_log_viewer
|
|
|
|
assert actions_log_viewer is not None
|
|
|
|
def test_mapping_table_importable(self):
|
|
from gui.components import mapping_table
|
|
|
|
assert mapping_table is not None
|
|
|
|
|
|
class TestLoggingConfigMode:
|
|
def _logging_vars(self, mode="simple"):
|
|
return {
|
|
"logging_beautify_var": _FakeVar(True),
|
|
"logging_color_var": _FakeVar(True),
|
|
"logging_show_timestamps_var": _FakeVar(True),
|
|
"logging_use_dated_logs_var": _FakeVar(True),
|
|
"logging_enable_monitor_var": _FakeVar(True),
|
|
"logging_keep_days_var": _FakeVar(30),
|
|
"logging_compress_logs_var": _FakeVar(True),
|
|
"logging_max_total_size_gb_var": _FakeVar(10.0),
|
|
"logging_compress_after_days_var": _FakeVar(7),
|
|
"logging_debug_level_var": _FakeVar("normal"),
|
|
"logging_error_level_var": _FakeVar("full"),
|
|
"logging_action_format_var": _FakeVar("concise"),
|
|
"logging_format_mode_var": _FakeVar(mode),
|
|
"stage_diag_verbose_var": _FakeVar(False),
|
|
"logging_monitor_warning_mb_var": _FakeVar(500.0),
|
|
"logging_monitor_critical_mb_var": _FakeVar(2000.0),
|
|
"logging_performance_enabled_var": _FakeVar(True),
|
|
"logging_track_functions_var": _FakeVar(True),
|
|
"log_unified_enabled_var": _FakeVar(True),
|
|
"log_unified_path_var": _FakeVar("logs/unified.log"),
|
|
}
|
|
|
|
def test_write_logging_to_cfg_derives_detail_fields_from_top_mode(self):
|
|
from gui.components.config_io_helpers import write_logging_to_cfg
|
|
|
|
cfg = {}
|
|
write_logging_to_cfg(cfg, self._logging_vars("detailed"))
|
|
|
|
assert cfg["logging"]["log_format_mode"] == "detailed"
|
|
assert cfg["logging"]["debug_detail_level"] == "detailed"
|
|
assert cfg["logging"]["action_log_format"] == "detailed"
|
|
assert cfg["logging"]["error_detail_level"] == "full"
|
|
|
|
def test_apply_logging_from_cfg_normalizes_invalid_mode_to_simple(self):
|
|
from gui.components.config_io_helpers import apply_logging_from_cfg
|
|
|
|
vars_ = self._logging_vars("detailed")
|
|
apply_logging_from_cfg(vars_, {"logging": {"log_format_mode": "verbose"}})
|
|
|
|
assert vars_["logging_format_mode_var"].get() == "simple"
|
|
assert vars_["logging_debug_level_var"].get() == "normal"
|
|
assert vars_["logging_action_format_var"].get() == "concise"
|
|
|
|
def test_logging_tabs_keep_only_top_mode_control(self):
|
|
from pathlib import Path
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
desktop_src = (root / "gui" / "components" / "logging_tab.py").read_text(
|
|
encoding="utf-8"
|
|
)
|
|
web_src = (root / "webui" / "js" / "config_panel_templates.js").read_text(
|
|
encoding="utf-8"
|
|
)
|
|
|
|
assert "logging_format_mode_var" in desktop_src
|
|
assert "logging_log_format_mode" in web_src
|
|
assert 'id="logging_debug_detail_level"' not in web_src
|
|
assert 'id="logging_error_detail_level"' not in web_src
|
|
assert 'id="logging_action_log_format"' not in web_src
|