Files
douyi_guaji_main/tests/test_fudai_detail_ui_v2.py
T

242 lines
8.4 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():
from douyin.fudai.core.coords import (
DETAIL_UI_LEGACY,
DETAIL_UI_V2_0,
DETAIL_UI_V2_0_ONE_TASK,
get_fudai_detail_profile,
)
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, 1472, 1035, 1545)
assert DETAIL_UI_V2_0["value"] == (300, 1585, 650, 1685)
assert DETAIL_UI_V2_0["countdown_full"] == (760, 1355, 1035, 1455)
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, 1472, 1035, 1545)
assert DETAIL_UI_V2_0_ONE_TASK["value"] == (340, 1680, 690, 1765)
assert DETAIL_UI_V2_0_ONE_TASK["countdown_full"] == (760, 1355, 1035, 1455)
assert DETAIL_UI_V2_0_ONE_TASK["task_list"] == (55, 1935, 900, 2055)
assert DETAIL_UI_V2_0_ONE_TASK["task_count"] == (890, 1855, 1040, 1950)
assert DETAIL_UI_V2_0_ONE_TASK["participants"] == (535, 1375, 760, 1475)
assert DETAIL_UI_V2_0_ONE_TASK["button"] == (70, 2055, 1010, 2225)
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_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_static_config_defaults_include_v2_ocr_keys():
cfg = json.loads(open("config/config.json", encoding="utf-8").read())
assert cfg["fudai_detail_ui_mode"] == "auto"
assert cfg["fudai_detail_ocr_content_enabled"] is True
assert cfg["fudai_detail_ocr_value_enabled"] is True
assert cfg["fudai_detail_ocr_countdown_enabled"] is True
assert cfg["fudai_detail_ocr_task_enabled"] is True
assert cfg["fudai_detail_ocr_task_count_enabled"] is True
assert cfg["fudai_detail_ocr_button_enabled"] is True
assert cfg["fudai_detail_ocr_participants_enabled"] is False
assert cfg["fudai_detail_ocr_live_title_enabled"] is True
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_v2_task_count_ocr_tries_one_task_shifted_region_first():
from douyin.detection.detection_pkg.detail import _check_detail_task_count_v2
calls = []
class _Operation:
def analyse_pic_word(self, name, _mode):
calls.append(name)
if name == "福袋任务数量_v2_1task":
return "(0/1)"
return ""
def analyse_pic_digits(self, name, _mode):
calls.append(name)
return ""
class _Analyser:
y_pianyi = 0
operation = _Operation()
def cut_pic(self, x1, y1, x2, y2, _prefix, name, _offset):
calls.append((name, (x1, y1, x2, y2)))
def _log_kv(self, *_args, **_kwargs):
return None
assert _check_detail_task_count_v2(_Analyser()) == 1
assert ("福袋任务数量_v2_1task", (890, 1855, 1040, 1950)) in calls
assert "福袋任务数量_v2" not in calls
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_pixels
screenshot_path = tmp_path / "one_task.png"
img = Image.new("RGB", (1080, 2400), (10, 15, 46))
img.putpixel((536, 1970), (255, 255, 255))
img.save(screenshot_path)
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(screenshot_path),
)
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_pixels(analyser) == 1
assert analyser._fudai_detail_task_count_source == "v2_pixels"
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_pixels
screenshot_path = tmp_path / "two_task.png"
img = Image.new("RGB", (1080, 2400), (10, 15, 46))
img.save(screenshot_path)
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(screenshot_path),
)
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_pixels(analyser) == 2
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