Files
douyi_guaji_main/tests/test_underlying_operations_pkg.py
T
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

217 lines
7.0 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/core/underlying_operations_pkg/ 子包单元测试。
覆盖纯函数子模块:adb_helpers / bounds / countdown / ocr_content / pic_path
"""
from __future__ import annotations
import os
import pytest
from douyin.core.underlying_operations_pkg import (
_adb_args,
_adb_path,
_find_pic_path,
_parse_bounds,
_pick_adbkeyboard_ime,
check_countdown,
extract_ocr_content,
)
# ============================================================
# adb_helpers.py
# ============================================================
class TestAdbPath:
def test_default_returns_adb(self, monkeypatch):
# 默认走 ADB_EXECUTABLE 环境变量;如果未设,应返回 'adb'
monkeypatch.delenv("ADB_EXECUTABLE", raising=False)
assert _adb_path() == "adb"
def test_env_override(self, monkeypatch):
monkeypatch.setenv("ADB_EXECUTABLE", "/usr/local/bin/adb")
assert _adb_path() == "/usr/local/bin/adb"
def test_strips_outer_quotes(self, monkeypatch):
monkeypatch.setenv("ADB_EXECUTABLE", '"C:\\Program Files\\adb.exe"')
# 包了引号的应当去引号返回 raw 路径
assert _adb_path() == "C:\\Program Files\\adb.exe"
class TestAdbArgs:
def test_basic(self):
result = _adb_args("AB123", "shell", "ls")
assert "adb" in result[0]
assert result[1] == "-s"
assert result[2] == "AB123"
assert "shell" in result and "ls" in result
def test_no_extra_args(self):
result = _adb_args("AB123")
assert len(result) == 3
assert result[1] == "-s"
def test_args_coerced_to_str(self):
# 非 str 参数应被 str() 转换
result = _adb_args("AB123", "shell", 42)
assert "42" in result
class TestPickAdbKeyboardIme:
def test_pick_existing(self):
imes = ["com.android.adbkeyboard/.AdbIME", "com.example/.X"]
assert _pick_adbkeyboard_ime(imes) == "com.android.adbkeyboard/.AdbIME"
def test_no_match_returns_empty(self):
imes = ["com.example/.X", "com.foo/.Y"]
# 没匹配应返回空串或 None;具体取决于实现
result = _pick_adbkeyboard_ime(imes)
assert result in ("", None) or "adbkeyboard" not in result.lower()
def test_empty_list(self):
result = _pick_adbkeyboard_ime([])
assert result in ("", None)
# ============================================================
# bounds.py — _parse_bounds
# ============================================================
class TestParseBounds:
@pytest.mark.parametrize(
"text,expected",
[
("[100,200][300,400]", (100, 200, 300, 400)),
("[0,0][1080,2400]", (0, 0, 1080, 2400)),
("[50,50][150,150]", (50, 50, 150, 150)),
],
)
def test_valid(self, text, expected):
assert _parse_bounds(text) == expected
@pytest.mark.parametrize(
"text",
[
"",
"abc",
"[100,200]", # 单个 bound
"100,200,300,400", # 缺方括号
],
)
def test_invalid_returns_none(self, text):
assert _parse_bounds(text) is None
# ============================================================
# countdown.py — check_countdown
# ============================================================
class TestCheckCountdown:
def test_mm_ss_full_format(self):
result = check_countdown("5:30")
assert result is not False
seconds, future_ts = result
assert seconds == 330 # 5*60 + 30
def test_chinese_format(self):
result = check_countdown("3分20秒")
assert result is not False
assert result[0] == 200
def test_seconds_only(self):
result = check_countdown("45秒")
assert result is not False
assert result[0] == 45
@pytest.mark.parametrize(
"text,expected_seconds",
[
("0:30", 30),
("1:00", 60),
("10:00", 600),
("12:34", 12 * 60 + 34),
],
)
def test_various(self, text, expected_seconds):
result = check_countdown(text)
assert result is not False
assert result[0] == expected_seconds
def test_empty_returns_false(self):
assert check_countdown("") is False
def test_garbage_returns_false(self):
assert check_countdown("xyz") is False
def test_overlong_capped(self):
"""超过 15 分钟的输入会被截断到 14分50秒,避免误识别卡死。"""
result = check_countdown("99:99")
if result is not False:
seconds, _ = result
# 实际逻辑:>900 截到 890
assert seconds <= 900
def test_ocr_char_corrections(self):
"""O→0, l→1 等字符纠错。"""
# 'l:30' 应识别为 '1:30'(l→1)
result = check_countdown("l:30")
if result is not False:
assert result[0] == 90
def test_with_chinese_colon(self):
result = check_countdown("5:30")
assert result is not False
assert result[0] == 330
# ============================================================
# ocr_content.py — extract_ocr_content
# ============================================================
class TestExtractOcrContent:
def test_empty_returns_empty_string(self):
assert extract_ocr_content(None) == ""
assert extract_ocr_content([]) == ""
def test_with_confidence_returns_tuple(self):
# return_confidence=True 应返回 (text, confidence_info)
result = extract_ocr_content([], return_confidence=True)
assert isinstance(result, tuple) and len(result) == 2
text, info = result
assert isinstance(text, str)
assert isinstance(info, dict)
assert "avg_confidence" in info
def test_default_returns_str_only(self):
result = extract_ocr_content(None, return_confidence=False)
assert isinstance(result, str)
# ============================================================
# pic_path.py — _find_pic_path
# ============================================================
class TestFindPicPath:
def test_runtime_file_screenshot(self):
# 'screenshot' 是运行时必需文件,应返回 pic_base/screenshot.png 或 None
result = _find_pic_path("screenshot", for_reading=True)
# 不强求文件存在,只要不抛异常
assert result is None or isinstance(result, str)
def test_screenshot_for_writing(self):
# for_reading=False 时应返回路径(即使文件不存在)
result = _find_pic_path("screenshot", for_reading=False)
assert isinstance(result, str)
assert "screenshot" in result and result.endswith(".png")
def test_cut_file_for_writing(self):
result = _find_pic_path("cut", for_reading=False)
assert isinstance(result, str)
assert result.endswith("cut.png")
def test_unknown_picname(self):
# 不存在的 picname 应返回 None 或一个写入路径,不抛
result = _find_pic_path("nonexistent_pic_name_12345", for_reading=True)
assert result is None or isinstance(result, str)