"""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): """超过 60 分钟的输入会被截断到 59分50秒,避免误识别卡死。""" result = check_countdown("99:99") if result is not False: seconds, _ = result # 实际逻辑:>3600 截到 3590 assert seconds <= 3600 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)