Files
douyi_guaji_main/tests/test_gui_components.py
T

178 lines
5.5 KiB
Python

"""gui/components 模块单元测试。
覆盖:
- log_color_config: get_operation_type, get_color_config 纯函数
- mapping_utils: _format_timestamp 纯函数
"""
from __future__ import annotations
import pytest
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 == "ocr" or result is not None
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" or result is not None
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"])
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