Files
CodexandClaude Opus 4.7 e417acfb7c 为 10 个已拆出的子包补单元测试(+226 用例)
新增 tests/ 下 10 个测试文件,把已拆出的子包从"被外部测试间接覆盖"提升到
"独立可测":

- test_web_gui_pkg (52)
  覆盖 logger / ansi / adb_compat / nested / stats_helpers
  含 _wg_log/_LOGGER 日志、ANSI 清理、点号路径、_parse_countdown_seconds 等
- test_underlying_operations_pkg (35)
  覆盖 adb_helpers / bounds / countdown / ocr_content / pic_path
  含 _adb_path 环境变量覆盖、check_countdown 多种格式、OCR 字符纠错
- test_reservation_logic_helpers (21)
  覆盖 helpers.py 5 个纯函数(_now_ts / _normalize_room_title /
  _calculate_string_similarity / _extract_content_core /
  _sort_reservations_inplace 排序稳定性)
- test_screenshot_manager_pkg (47)
  覆盖 paths.get_category_for_picname(5 类关键词分类)+
  get_latest_attempt_path / get_last_success_path(按 operation_type 路径推断)
  + 异步计数器
- test_gui_core_pkg (13)
  覆盖 RunParams dataclass 28 字段构造 + adb 函数 + read_config / write_config
  roundtrip
- test_fudai_core_pkg (17)
  覆盖 _FudaiOcrExecutorState 单例 + _get_fudai_ocr_executor 缓存复用 + 11
  个 thin_wrappers 与原实现等价行为
- test_handler_shells (8)
  覆盖 ReservationHandler / StageHandler / LoggingWrapper 三个薄壳类的
  staticmethod forwarding 完整性
- test_yanghao_pkg (14)
  覆盖 _yanghao_log(_log_kv 优先 + logger fallback + broken analyser
  兜底 + 未知 level 不抛)+ yanghao_get_recommend_seconds 默认值
- test_pixel_common (6)
  覆盖 _safe_open_screenshot_rgb 用 tmp_path 构造合法/损坏/截断/RGBA PNG
- test_fudai_finder_pkg (13)
  覆盖 5 个类(FudaiImageAcquirer / FudaiDetector / FudaiClickExecutor /
  FudaiDetailVerifier / FudaiFinder)实例化与组合关系,verify
  detect_by_template 类方法签名仍兼容(拆出后)

回归:
- 全套测试 355 → 581(+226 用例)
- 全部 581 passed / 1 skipped in 27s
- 新拆出的 12 个子包现在都有专门的单元测试覆盖

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 20:54:35 +08:00

145 lines
4.9 KiB
Python

"""gui/gui_core_pkg/ 子包单元测试。
覆盖 run_params dataclass + adb 模块函数(无副作用部分)。
adb 真实命令 / wifi_adb 涉及网络与设备,仅做"可调用 + 不抛"冒烟测试。
"""
from __future__ import annotations
import dataclasses
import os
import pytest
from gui.gui_core_pkg import (
CONFIG_PATH,
RunParams,
find_adb_executable,
list_adb_devices,
read_config,
resolve_adb_device,
write_config,
)
# ============================================================
# run_params.py — RunParams dataclass
# ============================================================
class TestRunParams:
def test_is_dataclass(self):
assert dataclasses.is_dataclass(RunParams)
def test_field_count(self):
# RunParams 当前有 28 个字段(与拆分时一致)
fields = dataclasses.fields(RunParams)
assert len(fields) == 28
def test_field_types(self):
"""关键字段的类型注解。"""
fields = {f.name: f for f in dataclasses.fields(RunParams)}
# 一些已知的字段
assert "device_id" in fields
assert "y_pianyi" in fields
assert "needswitch" in fields
assert "wait_minutes" in fields
assert "min_prize_value_yuan" in fields
assert "yanghao_only" in fields
def test_construct_with_all_fields(self):
# 构造一个完整实例,验证所有字段可赋值
params = RunParams(
device_id="test123",
y_pianyi=0,
needswitch=True,
wait_minutes=5,
skip_if_countdown_gt_seconds=300,
reset_after_swipes_enabled=True,
reset_after_swipes=30,
enable_back_on_misses=True,
min_prize_value_yuan=100.0,
min_participants=10,
max_participants=10000,
auto_join_fansclub=True,
fansclub_fail_action="skip",
high_value_room_revisit_enabled=False,
high_value_room_revisit_only_current_run=False,
high_value_room_revisit_min_interval_seconds=30,
high_value_room_revisit_max_per_run=10,
high_value_room_revisit_min_prize_value=500.0,
high_value_room_revisit_max_age_days=7,
high_value_fudai_poll_before_seconds=1.0,
reservation_back_verify_enabled=True,
reservation_back_swipe_delay=0.5,
fudai_find_max_attempts=3,
enable_ocr_mode1=False,
fudai_template_match_enabled=True,
fudai_template_match_threshold=0.85,
yanghao_only=False,
yanghao_seconds=216,
)
assert params.device_id == "test123"
assert params.y_pianyi == 0
assert params.needswitch is True
assert params.fudai_template_match_threshold == 0.85
# ============================================================
# config.py
# ============================================================
class TestConfigPath:
def test_is_str(self):
assert isinstance(CONFIG_PATH, str)
def test_endswith_config_json(self):
# 路径分隔符兼容
assert CONFIG_PATH.endswith("config.json") or CONFIG_PATH.endswith(os.sep + "config.json")
class TestReadConfig:
def test_returns_dict(self):
cfg = read_config()
assert isinstance(cfg, dict)
def test_idempotent(self):
# 多次调用应返回内容一致的 dict(不强求同一对象)
cfg1 = read_config()
cfg2 = read_config()
assert cfg1 == cfg2
class TestWriteConfig:
def test_write_and_read_roundtrip(self):
# 读出 → 写回(不改任何字段)→ 再读出,应一致
original = read_config()
write_config(original)
after = read_config()
# 至少键集相等(具体值可能因写入时序略有变动)
assert set(original.keys()) == set(after.keys())
# ============================================================
# adb.py — find_adb_executable / list_adb_devices / resolve_adb_device
# ============================================================
class TestAdbFunctions:
def test_find_adb_executable_returns_str(self):
# 不要求真的找到 adb,只看返回 str
result = find_adb_executable()
assert isinstance(result, str)
def test_list_adb_devices_returns_list(self):
# 即使 adb_exe 是空 / 错的,也应返回 list(可能为空)
result = list_adb_devices("")
assert isinstance(result, list)
def test_list_adb_devices_with_invalid_exe(self):
# 不存在的 adb_exe 应返回空 list,不抛
result = list_adb_devices("/nonexistent/path/to/adb")
assert isinstance(result, list)
def test_resolve_adb_device_with_invalid_id_raises(self):
# 指定了不存在的 device_id 应抛 RuntimeError
with pytest.raises(RuntimeError):
resolve_adb_device("", "nonexistent_device_xyz")