"""douyin/detection/detection_pkg/finder_pixel_scan 单元测试。 scan_pixel_by_pixel 用真实 PIL.Image + numpy 构造小图测试,避免大量 mock。 """ from __future__ import annotations from unittest.mock import MagicMock import pytest try: import numpy as np from PIL import Image except ImportError: pytest.skip("numpy/PIL unavailable", allow_module_level=True) def _mk_analyser(): a = MagicMock() a.last_fudai_x = 0 a.last_find_fudai_time = 0.0 a._log_kv = MagicMock(return_value=None) return a def _make_solid(rgb, w=1080, h=2400): return Image.new("RGB", (w, h), rgb) def _make_with_target(target_rgb, x_start, x_end, y_start, y_end, w=1080, h=2400): img = Image.new("RGB", (w, h), (0, 0, 0)) arr = np.array(img) arr[y_start:y_end, x_start:x_end] = list(target_rgb) return Image.fromarray(arr) TARGET = (200, 185, 240) # ============================================================ # _is_target_pixel / _safe_check_pixel 纯函数 # ============================================================ class TestIsTargetPixel: def test_in_range(self): from douyin.detection.detection_pkg.finder_pixel_scan import _is_target_pixel assert _is_target_pixel((200, 185, 240)) is True assert _is_target_pixel((190, 173, 234)) is True # 边界下限 assert _is_target_pixel((206, 196, 250)) is True # 边界上限 def test_out_of_range(self): from douyin.detection.detection_pkg.finder_pixel_scan import _is_target_pixel assert _is_target_pixel((189, 185, 240)) is False # r 太低 assert _is_target_pixel((200, 197, 240)) is False # g 太高 assert _is_target_pixel((200, 185, 233)) is False # b 太低 def test_invalid_input(self): from douyin.detection.detection_pkg.finder_pixel_scan import _is_target_pixel assert _is_target_pixel((200, 185)) is False # 长度不够 assert _is_target_pixel(None) is False assert _is_target_pixel(200) is False class TestSafeCheckPixel: def test_valid_lookup(self): from douyin.detection.detection_pkg.finder_pixel_scan import _safe_check_pixel pix = MagicMock() pix.__getitem__ = lambda self, key: TARGET assert _safe_check_pixel(pix, 100, 200) is True def test_index_error(self): from douyin.detection.detection_pkg.finder_pixel_scan import _safe_check_pixel pix = MagicMock() pix.__getitem__ = MagicMock(side_effect=IndexError()) assert _safe_check_pixel(pix, 99999, 99999) is False # ============================================================ # _pick_optimal_x # ============================================================ class TestPickOptimalX: def test_uses_history_when_in_range(self): from douyin.detection.detection_pkg.finder_pixel_scan import _pick_optimal_x a = _mk_analyser() a.last_fudai_x = 200 # 1080 基准 # rx=1080: 200 * 1080 // 1080 = 200,落在 [41, 410] 范围 result = _pick_optimal_x(a, scan_x_start=41, scan_x_end=410, rx=1080) assert result == 200 def test_uses_default_when_history_out_of_range(self): from douyin.detection.detection_pkg.finder_pixel_scan import _pick_optimal_x a = _mk_analyser() a.last_fudai_x = 800 # 超出扫描范围 # 默认热区中心 169 * 1080 // 1080 = 169 result = _pick_optimal_x(a, scan_x_start=41, scan_x_end=410, rx=1080) assert result == 169 def test_uses_default_when_no_history(self): from douyin.detection.detection_pkg.finder_pixel_scan import _pick_optimal_x a = _mk_analyser() a.last_fudai_x = 0 result = _pick_optimal_x(a, scan_x_start=41, scan_x_end=410, rx=1080) assert result == 169 # ============================================================ # scan_pixel_by_pixel 主入口 # ============================================================ class TestScanPixelByPixel: def test_blank_image_returns_false(self): from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() pic = _make_solid((50, 50, 50)) detected, x = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0) assert detected is False assert x is None def test_target_at_scan_y_detects(self): """在 scan_y=403 行某段 x 上画上目标色 → 应该检测到。""" from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() # 在 x ∈ [200, 250], y=403 画目标色 pic = _make_with_target(TARGET, 200, 250, 403, 405) detected, x = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0) assert detected is True assert 200 <= x <= 250 def test_target_at_offset_y(self): """目标在 scan_y±1 也应命中(offset_y 检查范围)。""" from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() # 在 y=402(即 scan_y - 1)画 pic = _make_with_target(TARGET, 200, 250, 402, 403) detected, x = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0) assert detected is True def test_target_outside_offset_y_misses(self): """目标在 scan_y+10(超过 offset 范围 ±2)→ 应失败。""" from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() pic = _make_with_target(TARGET, 200, 250, 413, 415) detected, _ = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0) assert detected is False def test_writes_back_last_fudai_x(self): from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() a.last_fudai_x = 0 pic = _make_with_target(TARGET, 200, 250, 403, 405) detected, x = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=0) assert detected is True assert a.last_fudai_x == x def test_y_offset_applied(self): """y_pianyi_val=100 时扫描带应在 y=503 附近。""" from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() pic = _make_with_target(TARGET, 200, 250, 503, 505) detected, _ = scan_pixel_by_pixel(a, pic, rx=1080, ry=2400, y_pianyi_val=100) assert detected is True def test_invalid_pic_does_not_crash(self): """传入异常对象时应安全返回 (False, None)。""" from douyin.detection.detection_pkg.finder_pixel_scan import scan_pixel_by_pixel a = _mk_analyser() broken = MagicMock() broken.load = MagicMock(side_effect=RuntimeError("broken")) detected, x = scan_pixel_by_pixel(a, broken, rx=1080, ry=2400, y_pianyi_val=0) assert detected is False assert x is None