Files
douyi_guaji_main/tests/test_fudai_detail_ui_v2.py
T

378 lines
14 KiB
Python

from __future__ import annotations
import json
from types import SimpleNamespace
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"] == (360, 1410, 1035, 1495)
assert DETAIL_UI_V2_0["value"] == (300, 1585, 650, 1685)
assert DETAIL_UI_V2_0["countdown_full"] == (760, 1328, 1038, 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"] == (730, 1338, 1038, 1418)
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_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_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=False,
)
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 False
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: 2)
analyser = SimpleNamespace(fudai_detail_ui_mode="v2_0")
assert detail.check_detail_height(analyser) == 2
def test_v2_task_count_pixels_choose_one_task_before_ocr(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
import cv2
screenshot_path = tmp_path / "one_task.png"
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.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))
scores = iter([0.99, 0.98, 0.81, 0.80])
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
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_template"
def test_v2_task_count_pixels_choose_two_task_from_dark_card(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
import cv2
screenshot_path = tmp_path / "two_task.png"
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.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))
scores = iter([0.81, 0.80, 0.99, 1.0])
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
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) == 2
def test_v2_task_count_pixels_does_not_misread_two_task_when_lower_text_is_white(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
import cv2
screenshot_path = tmp_path / "two_task_with_white_text.png"
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.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))
scores = iter([0.97, 0.96, 0.98, 0.99])
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
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) == 2
def test_v2_task_count_uses_digit_templates_instead_of_full_card_templates(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
import cv2
screenshot_path = tmp_path / "digit_based.png"
assets_dir = tmp_path / "assets"
assets_dir.mkdir()
Image.new("RGB", (1080, 2400), (255, 255, 255)).save(screenshot_path)
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_1.png")
Image.new("L", (40, 80), 255).save(assets_dir / "fudai_task_count_digit_2.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))
seen_paths = []
def _fake_imread(path, _flag):
seen_paths.append(path)
return np.full((80, 40), 255, dtype=np.uint8)
scores = iter([0.80, 0.79, 0.99, 0.98])
monkeypatch.setattr(cv2, "imread", _fake_imread)
monkeypatch.setattr(cv2, "matchTemplate", lambda *_a, **_k: np.array([[next(scores)]], dtype=np.float32))
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) == 2
assert any(path.endswith("fudai_task_count_digit_1.png") for path in seen_paths)
assert any(path.endswith("fudai_task_count_digit_2.png") for path in seen_paths)
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: 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