Files
douyi_guaji_main/tests/test_pixel_common.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

70 lines
2.4 KiB
Python

"""douyin/detection/pixel/_common.py 单元测试。
只测 _safe_open_screenshot_rgb:用 tmp_path 构造合法 / 非法 / 不存在的图片文件。
"""
from __future__ import annotations
import os
import pytest
from PIL import Image
from douyin.detection.pixel import _safe_open_screenshot_rgb
# ============================================================
# 工具:构造合法 / 非法 PNG
# ============================================================
def _make_valid_png(path: str, size: tuple = (10, 10)) -> None:
"""生成一张最小的合法 PNG。"""
img = Image.new("RGB", size, color=(255, 0, 0))
img.save(path, "PNG")
# ============================================================
# 测试
# ============================================================
class TestSafeOpenScreenshotRgb:
def test_valid_png(self, tmp_path):
p = tmp_path / "ok.png"
_make_valid_png(str(p))
result = _safe_open_screenshot_rgb(str(p))
assert result is not None
assert result.mode == "RGB"
assert result.size == (10, 10)
def test_missing_file_returns_none(self, tmp_path):
p = tmp_path / "nonexistent.png"
assert _safe_open_screenshot_rgb(str(p)) is None
def test_empty_path_returns_none(self):
assert _safe_open_screenshot_rgb("") is None
def test_corrupt_file_returns_none(self, tmp_path):
# 写入非 PNG 内容
p = tmp_path / "corrupt.png"
p.write_bytes(b"not a real png")
result = _safe_open_screenshot_rgb(str(p))
# PIL 可能抛 OSError/SyntaxError,函数应吞掉返回 None
assert result is None
def test_truncated_file_returns_none(self, tmp_path):
# 先生成合法 PNG,再截断到一半
full = tmp_path / "full.png"
_make_valid_png(str(full))
truncated = tmp_path / "truncated.png"
truncated.write_bytes(full.read_bytes()[:50])
result = _safe_open_screenshot_rgb(str(truncated))
# truncated PNG 应被 catch 并返回 None
assert result is None
def test_returns_rgb_mode(self, tmp_path):
# 即使源文件是 RGBA / L 模式,也应转成 RGB
p = tmp_path / "rgba.png"
Image.new("RGBA", (5, 5), color=(0, 255, 0, 128)).save(str(p), "PNG")
result = _safe_open_screenshot_rgb(str(p))
assert result is not None
assert result.mode == "RGB"