Files

146 lines
5.4 KiB
Python

"""douyin_fudai_core_pkg.cut_regions 单元测试。
覆盖:
- _select_countdown_box 4 种 renwu 值返回正确常量
- cut_detail_regions 调用 cut_pic 3 次(内容 + 整行倒计时 + 价值)
- 倒计时不再分割小框,避免裁偏误识别
- save_debug_images 开关效果
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from douyin.fudai.douyin_fudai_core_pkg.cut_regions import (
_select_countdown_box,
cut_detail_regions,
)
def _mk_analyser():
a = MagicMock()
a.y_pianyi = 0
a.cut_pic = MagicMock(return_value=True)
return a
# ============================================================
# _select_countdown_box
# ============================================================
class TestSelectCountdownBox:
def test_renwu_1(self):
from douyin.fudai.core.coords import COUNTDOWN_1140_1290
assert _select_countdown_box(1) == COUNTDOWN_1140_1290
def test_renwu_2(self):
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
assert _select_countdown_box(2) == COUNTDOWN_1110_1230
def test_renwu_3(self):
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
assert _select_countdown_box(3) == COUNTDOWN_1010_1130
def test_renwu_other_falls_to_default(self):
from douyin.fudai.core.coords import COUNTDOWN_1460_1580
assert _select_countdown_box(0) == COUNTDOWN_1460_1580
assert _select_countdown_box(99) == COUNTDOWN_1460_1580
assert _select_countdown_box(-1) == COUNTDOWN_1460_1580
# ============================================================
# cut_detail_regions
# ============================================================
class TestCutDetailRegions:
def test_returns_countdown_box(self):
a = _mk_analyser()
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
result = cut_detail_regions(a, renwu=2)
assert result == COUNTDOWN_1110_1230
def test_calls_cut_pic_3_times(self):
"""内容 + 整行倒计时 + 价值 = 3 次 cut_pic 调用。"""
a = _mk_analyser()
cut_detail_regions(a, renwu=2)
assert a.cut_pic.call_count == 3
def test_cut_names_in_order(self):
a = _mk_analyser()
cut_detail_regions(a, renwu=2)
# 第 1 个 positional arg 是坐标,第 6 个是 name
# 调用签名:cut_pic(x1, y1, x2, y2, target, name, y_pianyi)
names = [c.args[5] for c in a.cut_pic.call_args_list]
assert names == ["福袋内容", "福袋倒计时_整行", "福袋价值"]
def test_countdown_uses_full_line_region(self):
"""倒计时只切整行,不再分割分钟/秒小框。"""
a = _mk_analyser()
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
cut_detail_regions(a, renwu=2)
countdown_call = a.cut_pic.call_args_list[1]
assert countdown_call.args[:4] == COUNTDOWN_1110_1230
assert countdown_call.args[5] == "福袋倒计时_整行"
def test_profile_countdown_uses_full_line_for_v2_chip(self):
a = _mk_analyser()
profile = {"countdown_full": (760, 1328, 1038, 1398)}
cut_detail_regions(a, renwu=2, profile=profile)
countdown_call = a.cut_pic.call_args_list[1]
assert countdown_call.args[:4] == (760, 1328, 1038, 1398)
assert countdown_call.args[5] == "福袋倒计时_整行"
def test_v2_two_task_tall_profile_uses_full_line(self):
a = _mk_analyser()
profile = {"countdown_full": (760, 1265, 1040, 1398)}
cut_detail_regions(a, renwu=2, profile=profile)
countdown_call = a.cut_pic.call_args_list[1]
assert countdown_call.args[:4] == (760, 1265, 1040, 1398)
assert countdown_call.args[5] == "福袋倒计时_整行"
def test_save_debug_images_disabled_default(self):
a = _mk_analyser()
with patch(
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
) as mock_save:
cut_detail_regions(a, renwu=2, save_debug_images=False)
mock_save.assert_not_called()
def test_save_debug_images_enabled_does_not_save_per_field(self):
"""详情页 debug 已改为主流程统一保存一张综合标注图。"""
a = _mk_analyser()
with patch(
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
) as mock_save:
cut_detail_regions(a, renwu=2, save_debug_images=True)
mock_save.assert_not_called()
def test_renwu_1_uses_correct_box(self):
a = _mk_analyser()
from douyin.fudai.core.coords import COUNTDOWN_1140_1290
result = cut_detail_regions(a, renwu=1)
assert result == COUNTDOWN_1140_1290
def test_one_task_profile_countdown_box_uses_new_coords(self):
a = _mk_analyser()
profile = {"countdown_full": (700, 1390, 990, 1470)}
result = cut_detail_regions(a, renwu=1, profile=profile)
assert result == (700, 1390, 990, 1470)
countdown_call = a.cut_pic.call_args_list[1]
assert countdown_call.args[:4] == (700, 1390, 990, 1470)
assert countdown_call.args[5] == "福袋倒计时_整行"
def test_renwu_3_uses_correct_box(self):
a = _mk_analyser()
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
result = cut_detail_regions(a, renwu=3)
assert result == COUNTDOWN_1010_1130