"""douyin_fudai_core_pkg.cut_regions 单元测试。 覆盖: - _select_countdown_box 4 种 renwu 值返回正确常量 - cut_detail_regions 调用 cut_pic 4 次(内容 + 分钟 + 秒 + 价值) - 倒计时分割比例(30% / 30%)正确 - 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_4_times(self): """内容 + 分钟 + 秒 + 价值 = 4 次 cut_pic 调用。""" a = _mk_analyser() cut_detail_regions(a, renwu=2) assert a.cut_pic.call_count == 4 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_split_ratio(self): """倒计时 box 应按 30% 分钟 + 30% 秒 切分。""" a = _mk_analyser() from douyin.fudai.core.coords import COUNTDOWN_1110_1230 x1, y1, x2, y2 = COUNTDOWN_1110_1230 w = x2 - x1 seg = int(w * 0.3) cut_detail_regions(a, renwu=2) # 倒计时分钟 = 第 2 次 cut_pic 调用 mm_call = a.cut_pic.call_args_list[1] assert mm_call.args[0] == x1 assert mm_call.args[2] == x1 + seg # 倒计时秒 = 第 3 次调用 ss_call = a.cut_pic.call_args_list[2] assert ss_call.args[0] == x1 + seg assert ss_call.args[2] == x1 + seg + seg 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_calls_4_times(self): """启用后每个区域都保存:内容 + 分钟 + 秒 + 价值 = 4 次。""" 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) assert mock_save.call_count == 4 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_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