282 lines
9.8 KiB
Python
282 lines
9.8 KiB
Python
"""douyin/fudai/douyin_fudai_finder_pkg/ 子包单元测试。
|
||
|
||
覆盖 5 个类(FudaiImageAcquirer / FudaiDetector / FudaiClickExecutor /
|
||
FudaiDetailVerifier / FudaiFinder)的实例化与组合关系,以及顶层函数
|
||
check_have_fudai_with_ready_screenshot_optimized。
|
||
|
||
业务行为(实际识别 / 点击)依赖真实截图与 ADB,留给端到端测试。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
import pytest
|
||
|
||
try:
|
||
from PIL import Image
|
||
except ImportError:
|
||
pytest.skip("PIL unavailable", allow_module_level=True)
|
||
|
||
from douyin.fudai.douyin_fudai_finder import (
|
||
FudaiClickExecutor,
|
||
FudaiDetailVerifier,
|
||
FudaiDetector,
|
||
FudaiFinder,
|
||
FudaiImageAcquirer,
|
||
check_have_fudai_with_ready_screenshot_optimized,
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# Fake analyser 提供构造时需要的最小 attribute 集合
|
||
# ============================================================
|
||
|
||
class FakeAnalyser:
|
||
"""构造 FudaiFinder 链路所需的最小 attribute。"""
|
||
|
||
def __init__(self):
|
||
self.device_id = "test_device"
|
||
# FudaiDetector._init_search_regions 用到这些
|
||
self.resolution_ratio_x = 1080
|
||
self.resolution_ratio_y = 2400
|
||
self.y_pianyi = 0
|
||
self.last_fudai_x = 123
|
||
self.pixel_detect_coupon_popup_enabled = True
|
||
self._log_kv = MagicMock(return_value=None)
|
||
# FudaiClickExecutor 可能用 operation
|
||
class _Op:
|
||
def __init__(self):
|
||
self.click_back = MagicMock(return_value=None)
|
||
self.delay = MagicMock(return_value=None)
|
||
|
||
def delay(self, _seconds):
|
||
return None
|
||
|
||
self.operation = _Op()
|
||
|
||
|
||
# ============================================================
|
||
# FudaiImageAcquirer
|
||
# ============================================================
|
||
|
||
class TestFudaiImageAcquirer:
|
||
def test_construct(self):
|
||
acquirer = FudaiImageAcquirer(FakeAnalyser())
|
||
assert acquirer is not None
|
||
|
||
def test_acquire_screenshot_callable(self):
|
||
acquirer = FudaiImageAcquirer(FakeAnalyser())
|
||
assert callable(getattr(acquirer, "acquire_screenshot", None))
|
||
|
||
|
||
# ============================================================
|
||
# FudaiDetector
|
||
# ============================================================
|
||
|
||
class TestFudaiDetector:
|
||
def test_construct(self):
|
||
d = FudaiDetector(FakeAnalyser())
|
||
assert d.analyser is not None
|
||
assert d.rx == 1080
|
||
assert d.ry == 2400
|
||
|
||
def test_search_regions_initialized(self):
|
||
d = FudaiDetector(FakeAnalyser())
|
||
assert hasattr(d, "search_regions")
|
||
assert isinstance(d.search_regions, list)
|
||
|
||
def test_detect_methods_callable(self):
|
||
d = FudaiDetector(FakeAnalyser())
|
||
for name in (
|
||
"detect",
|
||
"detect_by_pixel",
|
||
"detect_by_template",
|
||
"confirm_current_icon_template",
|
||
"confirm_pixel_candidate_shape",
|
||
):
|
||
assert callable(getattr(d, name, None)), f"{name} 不可调用"
|
||
|
||
def test_current_icon_template_confirms_clean_countdown_sample(self):
|
||
from pathlib import Path
|
||
|
||
sample = Path(
|
||
"artifacts/live_fudai_flow_20260618_0530_valid/snapshots/"
|
||
"screen_current_for_icon_template.png"
|
||
)
|
||
if not sample.exists():
|
||
pytest.skip("live debug sample unavailable")
|
||
|
||
analyser = FakeAnalyser()
|
||
analyser.fudai_current_icon_template_threshold = 0.80
|
||
d = FudaiDetector(analyser)
|
||
|
||
assert d.confirm_current_icon_template(Image.open(sample).convert("RGB"), 175)
|
||
|
||
def test_current_icon_template_rejects_banner_edge_sample(self):
|
||
from pathlib import Path
|
||
|
||
sample = Path(
|
||
"pic/debug_captures/2026-06-18/"
|
||
"20260618_074702_110140_px_fdicon_ok_x262y373w60h60_"
|
||
"src_flow_on_detect_click_x_292_click_y_403_detected_x_scaled_292.png"
|
||
)
|
||
if not sample.exists():
|
||
pytest.skip("live debug sample unavailable")
|
||
|
||
analyser = FakeAnalyser()
|
||
analyser.fudai_current_icon_template_threshold = 0.80
|
||
d = FudaiDetector(analyser)
|
||
|
||
assert not d.confirm_current_icon_template(
|
||
Image.open(sample).convert("RGB"),
|
||
292,
|
||
)
|
||
|
||
def test_pixel_candidate_shape_accepts_other_real_icon_style(self):
|
||
from pathlib import Path
|
||
|
||
sample = Path(
|
||
"pic/debug_captures/2026-06-18/"
|
||
"20260618_070004_938228_px_fdicon_ok_x53y373w60h60_"
|
||
"src_flow_on_detect_click_x_83_click_y_403_detected_x_scaled_83.png"
|
||
)
|
||
if not sample.exists():
|
||
pytest.skip("live debug sample unavailable")
|
||
|
||
d = FudaiDetector(FakeAnalyser())
|
||
|
||
assert d.confirm_pixel_candidate_shape(Image.open(sample).convert("RGB"), 83)
|
||
|
||
def test_pixel_candidate_shape_rejects_banner_edge_sample(self):
|
||
from pathlib import Path
|
||
|
||
sample = Path(
|
||
"pic/debug_captures/2026-06-18/"
|
||
"20260618_074702_110140_px_fdicon_ok_x262y373w60h60_"
|
||
"src_flow_on_detect_click_x_292_click_y_403_detected_x_scaled_292.png"
|
||
)
|
||
if not sample.exists():
|
||
pytest.skip("live debug sample unavailable")
|
||
|
||
d = FudaiDetector(FakeAnalyser())
|
||
|
||
assert not d.confirm_pixel_candidate_shape(Image.open(sample).convert("RGB"), 292)
|
||
|
||
|
||
# ============================================================
|
||
# FudaiClickExecutor
|
||
# ============================================================
|
||
|
||
class TestFudaiClickExecutor:
|
||
def test_construct(self):
|
||
executor = FudaiClickExecutor(FakeAnalyser())
|
||
assert executor is not None
|
||
|
||
|
||
# ============================================================
|
||
# FudaiDetailVerifier
|
||
# ============================================================
|
||
|
||
class TestFudaiDetailVerifier:
|
||
def test_construct(self):
|
||
verifier = FudaiDetailVerifier(FakeAnalyser())
|
||
assert verifier is not None
|
||
|
||
|
||
# ============================================================
|
||
# FudaiFinder(组合 4 个子组件)
|
||
# ============================================================
|
||
|
||
class TestFudaiFinder:
|
||
def test_construct_includes_all_components(self):
|
||
finder = FudaiFinder(FakeAnalyser())
|
||
assert isinstance(finder.acquirer, FudaiImageAcquirer)
|
||
assert isinstance(finder.detector, FudaiDetector)
|
||
assert isinstance(finder.executor, FudaiClickExecutor)
|
||
assert isinstance(finder.verifier, FudaiDetailVerifier)
|
||
|
||
def test_components_share_analyser(self):
|
||
analyser = FakeAnalyser()
|
||
finder = FudaiFinder(analyser)
|
||
# 4 个子组件都应该持有同一个 analyser 引用
|
||
assert finder.acquirer.analyser is analyser
|
||
assert finder.detector.analyser is analyser
|
||
|
||
def test_coupon_popup_guard_skips_click(self):
|
||
analyser = FakeAnalyser()
|
||
finder = FudaiFinder(analyser)
|
||
pic = Image.new("RGB", (1080, 2400), (255, 255, 255))
|
||
finder.acquirer.acquire_screenshot = MagicMock(return_value=pic)
|
||
finder.detector.detect = MagicMock(return_value=(83, "pixel"))
|
||
finder.executor.click = MagicMock(return_value=True)
|
||
|
||
with patch(
|
||
"douyin.detection.pixel.popups.detect_coupon_popup_background",
|
||
return_value=(True, {"template_score": 0.9}),
|
||
):
|
||
ok, x = finder.find_and_open()
|
||
|
||
assert ok is False
|
||
assert x is None
|
||
assert analyser.last_fudai_x == 0
|
||
analyser.operation.click_back.assert_called_once_with(analyser.device_id)
|
||
finder.detector.detect.assert_not_called()
|
||
finder.executor.click.assert_not_called()
|
||
|
||
|
||
# ============================================================
|
||
# 顶层函数
|
||
# ============================================================
|
||
|
||
class TestCheckHaveFudaiWithReadyScreenshotOptimized:
|
||
def test_callable(self):
|
||
assert callable(check_have_fudai_with_ready_screenshot_optimized)
|
||
|
||
def test_coupon_popup_guard_returns_zero(self):
|
||
analyser = FakeAnalyser()
|
||
pic = Image.new("RGB", (1080, 2400), (255, 255, 255))
|
||
|
||
with patch(
|
||
"douyin.fudai.douyin_fudai_finder_pkg.optimized_entry.FudaiImageAcquirer"
|
||
) as acquirer_cls, patch(
|
||
"douyin.fudai.douyin_fudai_finder_pkg.optimized_entry.FudaiDetector"
|
||
) as detector_cls, patch(
|
||
"douyin.detection.pixel.popups.detect_coupon_popup_background",
|
||
return_value=(True, {"template_score": 0.9}),
|
||
):
|
||
acquirer_cls.return_value.acquire_screenshot.return_value = pic
|
||
result = check_have_fudai_with_ready_screenshot_optimized(analyser)
|
||
|
||
assert result == 0
|
||
assert analyser.last_fudai_x == 0
|
||
analyser.operation.click_back.assert_called_once_with(analyser.device_id)
|
||
detector_cls.assert_not_called()
|
||
|
||
|
||
# ============================================================
|
||
# detect_by_template 已拆出为 module-level 函数
|
||
# ============================================================
|
||
|
||
class TestDetectByTemplateAsModuleLevel:
|
||
"""detect_by_template 现在是 module-level 函数,类方法 1 行 forwarding。"""
|
||
|
||
def test_module_level_callable(self):
|
||
from douyin.fudai.douyin_fudai_finder_pkg.detect_by_template import (
|
||
detect_by_template as module_fn,
|
||
)
|
||
assert callable(module_fn)
|
||
|
||
def test_class_method_callable(self):
|
||
# 类方法仍可访问
|
||
d = FudaiDetector(FakeAnalyser())
|
||
assert callable(d.detect_by_template)
|
||
|
||
def test_class_method_signature_unchanged(self):
|
||
"""类方法签名应仍是 (self, pic, region),对外 API 不变。"""
|
||
import inspect
|
||
sig = inspect.signature(FudaiDetector.detect_by_template)
|
||
params = list(sig.parameters.keys())
|
||
# 第一个是 self(unbound),后两个 pic, region
|
||
assert "pic" in params
|
||
assert "region" in params
|