Files
douyi_guaji_main/tests/test_imports.py
T

350 lines
14 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.
"""核心模块导入冒烟测试。
验证 douyin/* 全树关键模块都能正常 import;任何 import-time 错误(语法 / 循环
依赖 / 顶层初始化失败)都会在这里被捕获,避免运行时才发现。
"""
from __future__ import annotations
import importlib
import pytest
# 项目核心包:这些模块顶层 import 不应失败
CORE_MODULES = [
# 入口与基础
"douyin.core.path_config",
"douyin.core.version",
"douyin.core.io_utils",
"douyin.core.underlying_operations",
"douyin.core.douyin_fudai",
"douyin.core.douyin_main_flow",
"douyin.core.douyin_flow_control",
# fudai_modules(Mixin)
"douyin.core.fudai_modules.base_interface",
"douyin.core.fudai_modules.fudai_navigation",
"douyin.core.fudai_modules.fudai_stage_detection",
"douyin.core.fudai_modules.fudai_popup_handler",
"douyin.core.fudai_modules.fudai_lottery_result",
"douyin.core.fudai_modules.fudai_room_utils",
"douyin.core.fudai_modules.fudai_main_logic",
# 检测 / 阶段
"douyin.detection.douyin_detection",
"douyin.detection.douyin_pixel",
"douyin.stage.douyin_stage",
"douyin.stage.douyin_stage_handler",
# 福袋核心
"douyin.fudai.douyin_fudai_core",
"douyin.fudai.douyin_fudai_finder",
"douyin.fudai.douyin_fudai_detail",
"douyin.fudai.douyin_fudai_flow",
"douyin.fudai.core.coords",
"douyin.fudai.core.fudai_filter",
"douyin.fudai.core.fudai_attend",
"douyin.fudai.core.fudai_debug_utils",
"douyin.fudai.core.fudai_result_checker",
# 处理器
"douyin.handlers.douyin_screenshot_handler",
"douyin.handlers.douyin_screenshot_manager",
"douyin.handlers.douyin_swipe_handler",
"douyin.handlers.douyin_swipe_ops",
"douyin.handlers.douyin_popup_handler",
# 预约
"douyin.reservation.douyin_reservation",
"douyin.reservation.douyin_reservation_handler",
"douyin.reservation.douyin_reservation_logic",
"douyin.reservation.douyin_reservation_verify",
"douyin.reservation.core", # 子包
# 日志
"douyin.logging.douyin_logging",
"douyin.logging.douyin_logging_wrapper",
"douyin.logging.unified_logger",
"douyin.logging.action_logger",
"douyin.logging.internal_loggers",
"douyin.logging.log_formatter",
"douyin.logging.performance_tracker",
# 其他业务
"douyin.other.douyin_yanghao",
"douyin.other.douyin_robot",
"douyin.other.douyin_popups",
"douyin.other.douyin_high_value_room",
"douyin.other.douyin_live_room",
"douyin.other.douyin_lottery",
# 工具
"douyin.utils.douyin_config",
"douyin.utils.douyin_helpers",
"douyin.utils.douyin_utils",
"douyin.utils.ocr_preprocess",
"douyin.utils.time_utils",
"douyin.utils.param_usage_tracer",
"douyin.utils.param_usage_tracker",
# operations 兼容包
"operations",
"operations.ocr_operations",
"operations.screenshot_operations",
"operations.screenshot_service",
# GUI(不实例化,仅 import)
"gui.gui_core",
"gui.gui_app",
"gui.web_gui",
"gui.webgui_launcher",
"gui.components.actions_log_viewer",
"gui.components.device_selector",
"gui.components.error_log_viewer",
"gui.components.log_color_config",
"gui.components.log_viewer",
"gui.components.mapping_table",
"gui.components.mapping_utils",
]
@pytest.mark.parametrize("module_name", CORE_MODULES)
def test_module_importable(module_name):
"""每个核心模块都应可以 import 而不抛异常。"""
importlib.import_module(module_name)
# ============================================================
# 关键类/函数应正确导出
# ============================================================
class TestKeyExports:
def test_fudai_analyse_class(self):
from douyin.core.douyin_fudai import fudai_analyse
assert fudai_analyse.__name__ == "fudai_analyse"
# 必有的关键方法
for method in ("fudai_choujiang", "run_once_basic", "load_config"):
assert hasattr(fudai_analyse, method), f"fudai_analyse 缺少 {method}"
def test_underlying_operations_class(self):
from douyin.core.underlying_operations import underlying_operations
assert underlying_operations.__name__ == "underlying_operations"
# 必有的关键方法
for method in ("get_screenshot", "click", "swipe", "analyse_pic_word"):
assert hasattr(underlying_operations, method), (
f"underlying_operations 缺少 {method}"
)
def test_path_config_functions(self):
from douyin.core import path_config
# 关键函数
for fn_name in (
"get_project_root",
"get_config_path",
"get_logs_dir",
"get_state_dir",
"get_reservations_path",
):
assert callable(getattr(path_config, fn_name, None)), (
f"path_config 缺少 {fn_name}"
)
def test_unified_logger_mdc(self):
from douyin.logging.unified_logger import MDC
for method in ("put", "get", "get_all", "remove", "clear"):
assert callable(getattr(MDC, method, None)), f"MDC 缺少 {method}"
def test_operations_package_exports(self):
import operations
# operations 包对外暴露的符号
for name in (
"underlying_operations",
"analyse_pic_word",
"get_screenshot",
"_safe_replace_file",
):
assert hasattr(operations, name), f"operations 包缺少导出 {name}"
def test_operations_package_import_tolerates_missing_optional_runtime_deps(self, monkeypatch):
"""operations 包在缺少 Pillow 这类可安装依赖时,不应因为 eager import 直接炸掉。"""
import sys
to_drop = [k for k in list(sys.modules) if k == "operations" or k.startswith("operations.")]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
real_import = __import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0): # noqa: A002
if name == "PIL" or name.startswith("PIL."):
raise ModuleNotFoundError("No module named 'PIL'")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr("builtins.__import__", fake_import)
try:
mod = importlib.import_module("operations")
assert hasattr(mod, "analyse_pic_word")
finally:
sys.modules.pop("operations", None)
for key in [k for k in list(sys.modules) if k.startswith("operations.")]:
sys.modules.pop(key, None)
sys.modules.update(saved)
def test_screenshot_service_import_tolerates_missing_pillow(self, monkeypatch):
"""截图服务模块不应在 import 阶段强依赖 Pillow。"""
import sys
to_drop = [k for k in list(sys.modules) if k == "operations" or k.startswith("operations.")]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
real_import = __import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0): # noqa: A002
if name == "PIL" or name.startswith("PIL."):
raise ModuleNotFoundError("No module named 'PIL'")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr("builtins.__import__", fake_import)
try:
mod = importlib.import_module("operations.screenshot_service")
assert hasattr(mod, "ScreenshotService")
finally:
for key in [k for k in list(sys.modules) if k == "operations" or k.startswith("operations.")]:
sys.modules.pop(key, None)
sys.modules.update(saved)
def test_read_with_retries_import_tolerates_missing_pillow(self, monkeypatch):
"""read_with_retries 作为高层重试入口,不应被 read_once 的 PIL 依赖卡死。"""
import sys
prefixes = (
"douyin.fudai.core.fudai_detail_orchestrator_pkg",
"douyin.fudai.core.fudai_detail_orchestrator",
)
to_drop = [k for k in list(sys.modules) if k.startswith(prefixes)]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
real_import = __import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0): # noqa: A002
if name == "PIL" or name.startswith("PIL."):
raise ModuleNotFoundError("No module named 'PIL'")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr("builtins.__import__", fake_import)
try:
mod = importlib.import_module("douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries")
assert hasattr(mod, "read_fudai_detail_with_retries")
finally:
for key in [k for k in list(sys.modules) if k.startswith(prefixes)]:
sys.modules.pop(key, None)
sys.modules.update(saved)
def test_read_with_retries_module_import_tolerates_missing_pillow(self, monkeypatch):
"""read_with_retries 自身不应留下无用的 PIL 顶层依赖。"""
import sys
prefixes = ("douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries",)
to_drop = [k for k in list(sys.modules) if k.startswith(prefixes)]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
real_import = __import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0): # noqa: A002
if name == "PIL" or name.startswith("PIL."):
raise ModuleNotFoundError("No module named 'PIL'")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr("builtins.__import__", fake_import)
try:
mod = importlib.import_module("douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries")
assert hasattr(mod, "read_fudai_detail_with_retries")
finally:
for key in [k for k in list(sys.modules) if k.startswith(prefixes)]:
sys.modules.pop(key, None)
sys.modules.update(saved)
def test_douyin_detection_import_tolerates_missing_pillow(self, monkeypatch):
"""detection 包的顶层 shim 不应被 finder 的 PIL 依赖卡死。"""
import sys
prefixes = (
"douyin.detection.detection_pkg",
"douyin.detection.douyin_detection",
)
to_drop = [k for k in list(sys.modules) if k.startswith(prefixes)]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
real_import = __import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0): # noqa: A002
if name == "PIL" or name.startswith("PIL."):
raise ModuleNotFoundError("No module named 'PIL'")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr("builtins.__import__", fake_import)
try:
mod = importlib.import_module("douyin.detection.douyin_detection")
assert hasattr(mod, "check_have_fudai_with_ready_screenshot")
finally:
for key in [k for k in list(sys.modules) if k.startswith(prefixes)]:
sys.modules.pop(key, None)
sys.modules.update(saved)
def test_douyin_fudai_import_tolerates_missing_pillow(self, monkeypatch):
"""douyin_fudai 主入口不应被若干调试/识别模块的 PIL 顶层导入卡死。"""
import sys
prefixes = (
"douyin.core.douyin_fudai",
"douyin.detection",
"douyin.fudai.core.fudai_debug_utils",
)
to_drop = [k for k in list(sys.modules) if k.startswith(prefixes)]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
real_import = __import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0): # noqa: A002
if name == "PIL" or name.startswith("PIL."):
raise ModuleNotFoundError("No module named 'PIL'")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr("builtins.__import__", fake_import)
try:
mod = importlib.import_module("douyin.core.douyin_fudai")
assert hasattr(mod, "fudai_analyse")
finally:
for key in [k for k in list(sys.modules) if k.startswith(prefixes)]:
sys.modules.pop(key, None)
sys.modules.update(saved)
def test_douyin_logging_import_chain_has_no_monitor_cycle(self):
"""日志兼容入口不应再因为 monitor/config 相互导入而初始化失败。"""
import sys
prefixes = (
"douyin.logging.douyin_logging",
"douyin.logging.douyin_logging_pkg",
)
to_drop = [k for k in list(sys.modules) if k.startswith(prefixes)]
saved = {k: sys.modules[k] for k in to_drop}
for key in to_drop:
sys.modules.pop(key, None)
try:
mod = importlib.import_module("douyin.logging.douyin_logging")
assert hasattr(mod, "configure_logging")
assert hasattr(mod, "start_log_monitor")
assert hasattr(mod, "stop_log_monitor")
finally:
for key in [k for k in list(sys.modules) if k.startswith(prefixes)]:
sys.modules.pop(key, None)
sys.modules.update(saved)