from __future__ import annotations import json from types import SimpleNamespace import pytest from PIL import Image def test_v2_profile_keeps_legacy_and_adds_expected_regions(monkeypatch): from douyin.fudai.core import coords as coords_mod from douyin.fudai.core.coords import ( DETAIL_UI_LEGACY, DETAIL_UI_V2_0, DETAIL_UI_V2_0_ONE_TASK, get_fudai_detail_profile, ) monkeypatch.setattr(coords_mod, "_read_runtime_region_config", lambda: {}) legacy = get_fudai_detail_profile("legacy") v2 = get_fudai_detail_profile("v2_0", task_count=2) auto = get_fudai_detail_profile("auto", task_count=2) one_task = get_fudai_detail_profile("v2_0", task_count=1) assert legacy is DETAIL_UI_LEGACY assert v2 is DETAIL_UI_V2_0 assert auto is DETAIL_UI_V2_0 assert one_task is DETAIL_UI_V2_0_ONE_TASK assert DETAIL_UI_LEGACY["content"] == (389, 1273, 1000, 1534) assert DETAIL_UI_V2_0["content"] == (365, 1368, 1040, 1465) assert DETAIL_UI_V2_0["value"] == (300, 1518, 820, 1695) assert DETAIL_UI_V2_0["countdown_full"] == (760, 1265, 1040, 1398) assert DETAIL_UI_V2_0["task_list"] == (60, 1840, 1030, 2005) assert DETAIL_UI_V2_0["task_count"] == (860, 1770, 1035, 1875) assert DETAIL_UI_V2_0["participants"] == (520, 1325, 760, 1415) assert DETAIL_UI_V2_0["button"] == (70, 2020, 1010, 2165) assert DETAIL_UI_V2_0_ONE_TASK["content"] == (360, 1460, 1035, 1545) assert DETAIL_UI_V2_0_ONE_TASK["value"] == (350, 1640, 760, 1735) assert DETAIL_UI_V2_0_ONE_TASK["countdown_full"] == (700, 1390, 990, 1470) assert DETAIL_UI_V2_0_ONE_TASK["info_line"] == (430, 1315, 1055, 1435) assert DETAIL_UI_V2_0_ONE_TASK["task_list"] == (60, 1845, 885, 2005) assert DETAIL_UI_V2_0_ONE_TASK["task_count"] == (890, 1855, 1040, 1960) assert DETAIL_UI_V2_0_ONE_TASK["participants"] == (516, 1396, 743, 1443) assert DETAIL_UI_V2_0_ONE_TASK["button"] == (70, 2050, 1010, 2195) assert DETAIL_UI_V2_0_ONE_TASK["task_list"][1] > DETAIL_UI_V2_0["task_list"][1] assert DETAIL_UI_V2_0_ONE_TASK["button"][1] > DETAIL_UI_V2_0["button"][1] def test_detail_profile_prefers_config_overrides(monkeypatch): from douyin.fudai.core import coords override = { "fudai_detail_regions": { "v2_0_two_task": { "content": [1, 2, 3, 4], }, "v2_0_one_task": { "info_line": [11, 22, 33, 44], }, "legacy": { "button": [101, 202, 303, 404], }, }, "pixel_regions": { "lottery_result": { "pixel": [7, 8, 9, 10], "click": [66, 77, 0, 0], } }, } monkeypatch.setattr(coords, "_read_runtime_region_config", lambda: override) legacy = coords.get_fudai_detail_profile("legacy") v2_two = coords.get_fudai_detail_profile("v2_0", task_count=2) v2_one = coords.get_fudai_detail_profile("v2_0", task_count=1) lottery = coords.get_lottery_result_confirm_profile("v2_0") assert legacy["button"] == (101, 202, 303, 404) assert v2_two["content"] == (1, 2, 3, 4) assert v2_two["version"] == "v2_0" assert v2_one["version"] == "v2_0" assert legacy["version"] == "legacy" assert v2_one["info_line"] == (11, 22, 33, 44) assert lottery["pixel"] == (7, 8, 9, 10) assert lottery["click"] == (66, 77) def test_detail_profile_ignores_invalid_config_overrides(monkeypatch): from douyin.fudai.core import coords monkeypatch.setattr( coords, "_read_runtime_region_config", lambda: { "fudai_detail_regions": { "v2_0_two_task": { "content": [1, 2, 3], } } }, ) profile = coords.get_fudai_detail_profile("v2_0", task_count=2) assert profile["content"] == coords.DETAIL_UI_V2_0["content"] def test_detail_profile_ignores_stale_v2_two_task_runtime_defaults(monkeypatch): from douyin.fudai.core import coords monkeypatch.setattr( coords, "_read_runtime_region_config", lambda: { "fudai_detail_regions": { "v2_0_two_task": { "content": [360, 1410, 1035, 1495], "value": [300, 1585, 650, 1685], "countdown_full": [760, 1328, 1038, 1398], } } }, ) profile = coords.get_fudai_detail_profile("v2_0", task_count=2) assert profile["content"] == coords.DETAIL_UI_V2_0["content"] assert profile["value"] == coords.DETAIL_UI_V2_0["value"] assert profile["countdown_full"] == coords.DETAIL_UI_V2_0["countdown_full"] def test_detail_profile_ignores_stale_v2_one_task_narrow_countdown(monkeypatch): from douyin.fudai.core import coords monkeypatch.setattr( coords, "_read_runtime_region_config", lambda: { "fudai_detail_regions": { "v2_0_one_task": { "countdown_full": [769, 1384, 1029, 1442], } } }, ) profile = coords.get_fudai_detail_profile("v2_0", task_count=1) assert profile["countdown_full"] == coords.DETAIL_UI_V2_0_ONE_TASK["countdown_full"] def test_detail_profile_ignores_previous_default_v2_one_task_countdown(monkeypatch): from douyin.fudai.core import coords monkeypatch.setattr( coords, "_read_runtime_region_config", lambda: { "fudai_detail_regions": { "v2_0_one_task": { "countdown_full": [730, 1338, 1038, 1418], } } }, ) profile = coords.get_fudai_detail_profile("v2_0", task_count=1) assert profile["countdown_full"] == coords.DETAIL_UI_V2_0_ONE_TASK["countdown_full"] def test_detail_profile_ignores_high_v2_one_task_countdown(monkeypatch): from douyin.fudai.core import coords monkeypatch.setattr( coords, "_read_runtime_region_config", lambda: { "fudai_detail_regions": { "v2_0_one_task": { "countdown_full": [700, 1320, 930, 1415], } } }, ) profile = coords.get_fudai_detail_profile("v2_0", task_count=1) assert profile["countdown_full"] == coords.DETAIL_UI_V2_0_ONE_TASK["countdown_full"] def test_v2_two_task_profile_covers_batu_4999_sample_regions(): from PIL import Image from pathlib import Path from douyin.fudai.core.coords import DETAIL_UI_V2_0 sample = Path("tests/fixtures/fudai_detail_v2_two_task_batu_4999_regions.png") if not sample.exists(): pytest.skip("batu 4999 debug sample is unavailable") img = Image.open(sample).convert("RGB") def crop_has_bright_text(box): crop = img.crop(box) pixels = list(crop.getdata()) bright = sum(1 for r, g, b in pixels if r > 185 and g > 185 and b > 185) return bright / max(1, len(pixels)) assert crop_has_bright_text(DETAIL_UI_V2_0["countdown_full"]) > 0.01 assert crop_has_bright_text(DETAIL_UI_V2_0["content"]) > 0.01 def test_v2_two_task_value_region_covers_reference_value_line(): from pathlib import Path from douyin.fudai.core.coords import DETAIL_UI_V2_0 samples = [ Path("tests/fixtures/fudai_detail_v2_two_task_redmi_2999_regions.png"), Path("tests/fixtures/fudai_detail_v2_two_task_guohao_1199_regions.png"), ] missing = [sample for sample in samples if not sample.exists()] if missing: pytest.skip("v2 two-task value samples are unavailable") value_box = DETAIL_UI_V2_0["value"] assert value_box[2] >= 760 assert value_box[3] >= 1690 def crop_has_reference_line_text(sample: Path) -> float: img = Image.open(sample).convert("RGB") crop = img.crop((340, 1585, min(value_box[2], 820), min(value_box[3], 1695))) pixels = list(crop.getdata()) bright = sum(1 for r, g, b in pixels if r > 185 and g > 185 and b > 185) return bright / max(1, len(pixels)) for sample in samples: assert crop_has_reference_line_text(sample) > 0.03 def test_v2_two_task_countdown_region_covers_lower_countdown_line(): from pathlib import Path from douyin.fudai.core.coords import DETAIL_UI_V2_0 samples = [ Path("tests/fixtures/fudai_detail_v2_two_task_redmi_2999_regions.png"), Path("tests/fixtures/fudai_detail_v2_two_task_guohao_1199_regions.png"), ] missing = [sample for sample in samples if not sample.exists()] if missing: pytest.skip("v2 two-task countdown samples are unavailable") countdown_box = DETAIL_UI_V2_0["countdown_full"] assert countdown_box[1] <= 1328 assert countdown_box[3] >= 1398 def crop_has_countdown_text(sample: Path) -> float: img = Image.open(sample).convert("RGB") crop = img.crop((760, 1328, min(countdown_box[2], 1040), min(countdown_box[3], 1398))) pixels = list(crop.getdata()) bright = sum(1 for r, g, b in pixels if r > 185 and g > 185 and b > 185) return bright / max(1, len(pixels)) for sample in samples: assert crop_has_countdown_text(sample) > 0.02 def test_ocr_switch_defaults_and_legacy_disable_compat(): from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( FudaiDetailOcrSwitches, resolve_fudai_detail_ocr_switches, ) defaults = resolve_fudai_detail_ocr_switches({}) assert defaults == FudaiDetailOcrSwitches( content=True, value=True, countdown=True, task=True, task_count=True, button=True, participants=False, live_title=True, ) legacy = resolve_fudai_detail_ocr_switches( { "fudai_disable_content_recognition": True, "fudai_disable_task_recognition": True, } ) assert legacy.content is False assert legacy.task is False assert legacy.value is True explicit = resolve_fudai_detail_ocr_switches( { "fudai_disable_content_recognition": True, "fudai_detail_ocr_content_enabled": True, "fudai_detail_ocr_participants_enabled": True, } ) assert explicit.content is True assert explicit.participants is True def test_code_defaults_include_v2_ocr_keys(): from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) defaults = resolve_fudai_detail_ocr_switches({}) assert defaults.content is True assert defaults.value is True assert defaults.countdown is True assert defaults.task is True assert defaults.task_count is True assert defaults.button is True assert defaults.participants is False assert defaults.live_title is True def test_task_list_ocr_is_not_in_detail_critical_path_by_default(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import ( _should_schedule_task_list_ocr, ) from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_task_enabled": True} ) assert _should_schedule_task_list_ocr(SimpleNamespace(), switches) is False def test_task_list_ocr_critical_path_requires_explicit_opt_in(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import ( _should_schedule_task_list_ocr, ) from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_task_enabled": True} ) disabled_switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_task_enabled": False} ) analyser = SimpleNamespace( fudai_detail_task_ocr_in_critical_path_enabled=True, ) assert _should_schedule_task_list_ocr(analyser, switches) is True assert _should_schedule_task_list_ocr(analyser, disabled_switches) is False def test_countdown_full_ocr_is_in_detail_critical_path_by_default(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import ( _should_schedule_countdown_full_ocr, ) from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_countdown_enabled": True} ) assert _should_schedule_countdown_full_ocr(SimpleNamespace(), switches) is True def test_countdown_full_ocr_critical_path_respects_countdown_switch(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import ( _should_schedule_countdown_full_ocr, ) from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_countdown_enabled": True} ) disabled_switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_countdown_enabled": False} ) assert _should_schedule_countdown_full_ocr(SimpleNamespace(), switches) is True assert _should_schedule_countdown_full_ocr(SimpleNamespace(), disabled_switches) is False def test_countdown_text_fallback_uses_existing_ocr_without_extra_ocr(): from douyin.fudai.douyin_fudai_contain_impl import ( _apply_countdown_text_fallback, ) analyser = SimpleNamespace(fudai_detail_verbose_logs=False) mm, ss = _apply_countdown_text_fallback( analyser, "", "", ( ("info_line", "免费购129人已参与00 : 00后开奖"), ("countdown_full", ""), ), ) assert (mm, ss) == ("00", "00") def test_countdown_full_result_can_override_split_when_scheduled(): from douyin.fudai.douyin_fudai_contain_impl import _parse_mmss_from_full_text assert _parse_mmss_from_full_text("45秒后开奖") == ("00", "45") def test_info_line_ocr_is_not_in_detail_critical_path_by_default(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import ( _should_schedule_info_line_ocr, ) from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_countdown_enabled": True} ) assert _should_schedule_info_line_ocr(SimpleNamespace(), switches) is False def test_info_line_ocr_critical_path_requires_explicit_opt_in(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import ( _should_schedule_info_line_ocr, ) from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( resolve_fudai_detail_ocr_switches, ) monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) switches = resolve_fudai_detail_ocr_switches( {"fudai_detail_ocr_countdown_enabled": True} ) analyser = SimpleNamespace( fudai_detail_info_line_ocr_in_critical_path_enabled=True, ) assert _should_schedule_info_line_ocr(analyser, switches) is True def test_detail_ocr_workers_default_to_one_and_clamp(monkeypatch): from douyin.fudai.douyin_fudai_contain_impl import _get_detail_ocr_workers monkeypatch.setattr( "douyin.core.path_config.get_config_path", lambda: "__missing__.json", ) assert _get_detail_ocr_workers(SimpleNamespace(), 8) == 1 assert _get_detail_ocr_workers( SimpleNamespace(fudai_detail_ocr_workers=2), 8, ) == 2 assert _get_detail_ocr_workers( SimpleNamespace(fudai_detail_ocr_workers=99), 8, ) == 3 def test_config_manager_loads_fudai_detail_ui_and_ocr_switches(tmp_path): from douyin.utils.douyin_config import ConfigManager cfg_path = tmp_path / "config.json" cfg_path.write_text( json.dumps( { "fudai_detail_ui_mode": "legacy", "fudai_detail_ocr_content_enabled": False, "fudai_detail_ocr_button_enabled": True, "fudai_detail_ocr_participants_enabled": True, }, ensure_ascii=False, ), encoding="utf-8", ) analyser = SimpleNamespace(config_path=str(cfg_path), needswitch=None) ConfigManager.load_config_from_file(analyser, str(cfg_path)) assert analyser.fudai_detail_ui_mode == "legacy" assert analyser.fudai_detail_ocr_content_enabled is False assert analyser.fudai_detail_ocr_button_enabled is True assert analyser.fudai_detail_ocr_participants_enabled is True def test_check_detail_height_v2_uses_template_match_path(monkeypatch): from douyin.detection.detection_pkg import detail monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser, pic_rgb=None: 2) analyser = SimpleNamespace(fudai_detail_ui_mode="v2_0") assert detail.check_detail_height(analyser) == 2 def test_v2_task_count_dacheng_shape_template_path(tmp_path, monkeypatch): from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2 from douyin.core import path_config import numpy as np screenshot_path = tmp_path / "task.png" assets_dir = tmp_path / "assets" assets_dir.mkdir() Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path) Image.new("RGB", (90, 57), (255, 255, 255)).save( assets_dir / "fudai_task_status_dacheng_shape.png" ) monkeypatch.setattr( "douyin.core.path_config.get_screenshot_path", lambda: str(screenshot_path), ) monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(assets_dir)) monkeypatch.setattr( "douyin.detection.detection_pkg.detail._task_status_shape_edges", lambda image, _cv2, _np: np.zeros( (57, 90), dtype=np.uint8 ) if image.size == (90, 57) else np.zeros((220, 230), dtype=np.uint8), ) fake_result = np.zeros((164, 141), dtype=np.float32) fake_result[123, 130] = 0.91 monkeypatch.setattr( "cv2.matchTemplate", lambda *_args, **_kwargs: fake_result, ) analyser = SimpleNamespace( fudai_detail_ui_mode="auto", resolution_ratio_x=1080, resolution_ratio_y=2400, _log_kv=lambda *_args, **_kwargs: None, ) assert _check_detail_task_count_v2(analyser) == 1 assert analyser._fudai_detail_task_count_source == "v2_dacheng_shape" def test_v2_task_count_dacheng_shape_real_samples(): from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2 from pathlib import Path two_task = Path( r"D:\Wechat\WeChat Files\wxid_bfjcta0smtbm21\xwechat_files\wxid_bfjcta0smtbm21_2f0b\temp\RWTemp\2026-06\9e20f478899dc29eb19741386f9343c8\231bbc6fe0220ae826eef52a9696381e.png" ) one_task = Path( r"C:\Users\12742\Desktop\douyin_guaji-main\pic\debug_captures\2026-06-04\20260604_115302_493312_px_pxstage_ov_ok_3.png" ) if not two_task.exists() or not one_task.exists(): pytest.skip("real fudai task-count samples are not available on this machine") analyser = SimpleNamespace( fudai_detail_ui_mode="v2_0", resolution_ratio_x=1080, resolution_ratio_y=2400, _log_kv=lambda *_args, **_kwargs: None, ) assert _check_detail_task_count_v2(analyser, pic_rgb=Image.open(two_task)) == 2 assert _check_detail_task_count_v2(analyser, pic_rgb=Image.open(one_task)) == 1 def test_get_detail_task_count_digit_box_targets_trailing_digit_for_one_and_two_task(): from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box assert get_detail_task_count_digit_box((890, 1855, 1040, 1960)) == (976, 1871, 988, 1901) assert get_detail_task_count_digit_box((860, 1770, 1035, 1875)) == (974, 1814, 995, 1845) def test_v2_task_count_pixels_disabled_for_legacy(tmp_path, monkeypatch): from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2_pixels screenshot_path = tmp_path / "legacy.png" Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path) monkeypatch.setattr( "douyin.core.path_config.get_screenshot_path", lambda: str(screenshot_path), ) analyser = SimpleNamespace( fudai_detail_ui_mode="legacy", resolution_ratio_x=1080, resolution_ratio_y=2400, ) assert _check_detail_task_count_v2_pixels(analyser) == 0 def test_check_detail_height_v2_does_not_fallback_to_legacy_pixels(monkeypatch): from douyin.detection.detection_pkg import detail monkeypatch.setattr(detail, "_check_detail_task_count_v2_pixels", lambda _analyser: 0) monkeypatch.setattr(detail, "_check_detail_task_count_v2", lambda _analyser, pic_rgb=None: 0) def _legacy_should_not_run(_analyser): raise AssertionError("v2 mode must not fallback to legacy pixel detection") monkeypatch.setattr(detail, "_check_detail_task_count_legacy_pixels", _legacy_should_not_run) analyser = SimpleNamespace(fudai_detail_ui_mode="v2_0") assert detail.check_detail_height(analyser) == 0 def test_check_detail_height_legacy_uses_legacy_pixels(monkeypatch): from douyin.detection.detection_pkg import detail monkeypatch.setattr(detail, "_check_detail_task_count_legacy_pixels", lambda _analyser: 3) analyser = SimpleNamespace(fudai_detail_ui_mode="legacy") assert detail.check_detail_height(analyser) == 3