"""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")