from __future__ import annotations import sys from types import ModuleType, SimpleNamespace from unittest.mock import MagicMock def test_main_finder_writes_back_last_fudai_x(monkeypatch): from douyin.detection.detection_pkg import finder as mod class _Pic: size = (1080, 2400) def load(self): return None def convert(self, _mode): return self def close(self): return None analyser = MagicMock() analyser.fudai_finder_optimized_enabled = False analyser.device_id = "MOCK" analyser.resolution_ratio_x = 1080 analyser.resolution_ratio_y = 2400 analyser.y_pianyi = 0 analyser.last_fudai_x = 0 analyser.operation = MagicMock() analyser.operation.get_screenshot = MagicMock(return_value=True) analyser.operation.delay = MagicMock(return_value=None) analyser._log_kv = MagicMock(return_value=None) pil_mod = ModuleType("PIL") pil_mod.Image = SimpleNamespace(open=lambda _p: _Pic()) monkeypatch.setitem(sys.modules, "PIL", pil_mod) monkeypatch.setattr("douyin.detection.detection_pkg.finder.path_config.get_screenshot_path", lambda: "fake.png") monkeypatch.setattr("douyin.detection.detection_pkg.finder.os.path.exists", lambda _p: True) monkeypatch.setattr("douyin.detection.detection_pkg.finder.os.path.getsize", lambda _p: 2048) monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_with_numpy", lambda *a, **k: (True, 233)) monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_pixel_by_pixel", lambda *a, **k: (False, None)) monkeypatch.setattr("douyin.detection.detection_pkg.finder.match_template", lambda *a, **k: (False, None)) result = mod.check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=True) assert result == 233 assert analyser.last_fudai_x == 233 def test_main_finder_uses_scaled_history_on_non_1080_width(monkeypatch): from douyin.detection.detection_pkg import finder as mod class _Pic: size = (720, 1600) def load(self): return None def convert(self, _mode): return self def close(self): return None captured = {} analyser = MagicMock() analyser.fudai_finder_optimized_enabled = False analyser.device_id = "MOCK" analyser.resolution_ratio_x = 720 analyser.resolution_ratio_y = 1600 analyser.y_pianyi = 0 analyser.last_fudai_x = 233 # 1080 εŸΊε‡†εζ ‡ analyser.operation = MagicMock() analyser.operation.get_screenshot = MagicMock(return_value=True) analyser.operation.delay = MagicMock(return_value=None) analyser._log_kv = MagicMock(return_value=None) pil_mod = ModuleType("PIL") pil_mod.Image = SimpleNamespace(open=lambda _p: _Pic()) monkeypatch.setitem(sys.modules, "PIL", pil_mod) monkeypatch.setattr("douyin.detection.detection_pkg.finder.path_config.get_screenshot_path", lambda: "fake.png") monkeypatch.setattr("douyin.detection.detection_pkg.finder.os.path.exists", lambda _p: True) monkeypatch.setattr("douyin.detection.detection_pkg.finder.os.path.getsize", lambda _p: 2048) def fake_numpy(_analyser, _pic, rx, ry, y_pianyi_val, optimal_x_scaled): # noqa: ARG001 captured["optimal_x_scaled"] = optimal_x_scaled return False, None monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_with_numpy", fake_numpy) monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_pixel_by_pixel", lambda *a, **k: (False, None)) monkeypatch.setattr("douyin.detection.detection_pkg.finder.match_template", lambda *a, **k: (False, None)) result = mod.check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=True) assert result is False assert captured["optimal_x_scaled"] == 233 * 720 // 1080 def test_main_finder_zero_wait_still_runs_one_detection(monkeypatch): from douyin.detection.detection_pkg import finder as mod class _Pic: size = (1080, 2400) def load(self): return None def convert(self, _mode): return self def close(self): return None analyser = MagicMock() analyser.fudai_finder_optimized_enabled = False analyser.device_id = "MOCK" analyser.resolution_ratio_x = 1080 analyser.resolution_ratio_y = 2400 analyser.y_pianyi = 0 analyser.last_fudai_x = 0 analyser.fudai_icon_max_wait_seconds = 0 analyser.operation = MagicMock() analyser.operation.get_screenshot = MagicMock(return_value=True) analyser.operation.delay = MagicMock(return_value=None) analyser._log_kv = MagicMock(return_value=None) pil_mod = ModuleType("PIL") pil_mod.Image = SimpleNamespace(open=lambda _p: _Pic()) monkeypatch.setitem(sys.modules, "PIL", pil_mod) monkeypatch.setattr("douyin.detection.detection_pkg.finder.path_config.get_screenshot_path", lambda: "fake.png") monkeypatch.setattr("douyin.detection.detection_pkg.finder.os.path.exists", lambda _p: True) monkeypatch.setattr("douyin.detection.detection_pkg.finder.os.path.getsize", lambda _p: 2048) calls = {"numpy": 0} def fake_numpy(*args, **kwargs): # noqa: ARG001 calls["numpy"] += 1 return False, None monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_with_numpy", fake_numpy) monkeypatch.setattr("douyin.detection.detection_pkg.finder.scan_pixel_by_pixel", lambda *a, **k: (False, None)) monkeypatch.setattr("douyin.detection.detection_pkg.finder.match_template", lambda *a, **k: (False, None)) result = mod.check_have_fudai_with_ready_screenshot(analyser, screenshot_ready=True) assert result is False assert calls["numpy"] == 1