Files
douyi_guaji_main/tests/test_countdown_split.py
T

144 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""douyin_fudai_core_pkg.countdown_split 单元测试。
覆盖:
- _extract_digits 纯函数边界
- read_countdown_split 主路径成功 / OCR 抛异常 / 秒>=60 警告 / cut_pic 失败
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from douyin.fudai.douyin_fudai_core_pkg.countdown_split import (
_extract_digits,
read_countdown_split,
)
# ============================================================
# _extract_digits 纯函数
# ============================================================
class TestExtractDigits:
def test_two_digits(self):
assert _extract_digits("23") == "23"
def test_one_digit_pads_zero(self):
assert _extract_digits("5") == "05"
def test_more_than_max_takes_first(self):
# "002" -> 前两位 "00",全是 0 且后面有非 0 数字
# → 取移除前导 0 后的 "2",zfill → "02"
assert _extract_digits("002") == "02"
def test_strips_non_digits(self):
assert _extract_digits("12:34") == "12" # 取前 2 位(分钟)
def test_empty(self):
assert _extract_digits("") == ""
def test_none(self):
assert _extract_digits(None) == "" # type: ignore[arg-type]
def test_all_zeros(self):
assert _extract_digits("00") == "00"
def test_leading_zero_three_digits(self):
# "025" -> 前 2 位 "02",不是全 0 → 直接返回
assert _extract_digits("025") == "02"
# ============================================================
# read_countdown_split
# ============================================================
def _mk_analyser():
a = MagicMock()
a.y_pianyi = 0
a.cut_pic = MagicMock(return_value=True)
a._log_kv = MagicMock(return_value=None)
a.operation = MagicMock()
return a
class TestReadCountdownSplit:
def test_main_path_success(self):
a = _mk_analyser()
# mm OCR=23, ss OCR=45
a.operation.analyse_pic_digits = MagicMock(side_effect=["23", "45"])
result = read_countdown_split(a, (100, 200, 400, 250))
assert result == "23:45"
# cut_pic 应被调用 2 次(分钟 + 秒)
assert a.cut_pic.call_count == 2
def test_ocr_returns_only_minute(self):
"""秒识别失败(空字符串)→ 整体失败返回 ""。"""
a = _mk_analyser()
a.operation.analyse_pic_digits = MagicMock(side_effect=["23", ""])
result = read_countdown_split(a, (100, 200, 400, 250))
assert result == ""
def test_ocr_throws(self):
"""OCR 抛异常 → 返回 "",不传播。"""
a = _mk_analyser()
a.operation.analyse_pic_digits = MagicMock(side_effect=RuntimeError("OCR fail"))
result = read_countdown_split(a, (100, 200, 400, 250))
assert result == ""
def test_ss_over_60_logs_warning_but_returns_result(self):
"""秒>=60 触发警告日志但仍返回结果(保持原行为)。"""
a = _mk_analyser()
a.operation.analyse_pic_digits = MagicMock(side_effect=["23", "75"])
result = read_countdown_split(a, (100, 200, 400, 250))
assert result == "23:75"
# 应至少有一条 level=WARNING 的日志(验证子分支)
warn_calls = [
c for c in a._log_kv.call_args_list
if c.kwargs.get("level") == "WARNING"
]
assert any("秒数>=60" in str(c) for c in warn_calls)
def test_cut_pic_failure_does_not_crash(self):
"""cut_pic 抛异常 → 整体失败返回 ""。"""
a = _mk_analyser()
a.cut_pic = MagicMock(side_effect=RuntimeError("cut_pic fail"))
result = read_countdown_split(a, (100, 200, 400, 250))
assert result == ""
def test_box_dimensions_split_correctly(self):
"""验证切割比例:宽度 300 → seg1=90 (30%), seg2=90 (30%)。"""
a = _mk_analyser()
a.operation.analyse_pic_digits = MagicMock(side_effect=["23", "45"])
read_countdown_split(a, (100, 200, 400, 250)) # w=300
# 第 1 次 cut_pic:分钟段 (100, 200, 100+90, 250) = (100, 200, 190, 250)
# 第 2 次 cut_pic:秒段 (100+90, 200, 100+90+90, 250) = (190, 200, 280, 250)
first_call = a.cut_pic.call_args_list[0]
second_call = a.cut_pic.call_args_list[1]
assert first_call[0][:4] == (100, 200, 190, 250)
assert second_call[0][:4] == (190, 200, 280, 250)
def test_save_debug_images_disabled_by_default(self):
"""save_debug_images=False 时不应调用 _save_full_screenshot_with_ocr_region。"""
a = _mk_analyser()
a.operation.analyse_pic_digits = MagicMock(side_effect=["23", "45"])
with patch(
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
) as mock_save:
read_countdown_split(a, (100, 200, 400, 250), 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()
a.operation.analyse_pic_digits = MagicMock(side_effect=["23", "45"])
with patch(
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
) as mock_save:
read_countdown_split(a, (100, 200, 400, 250), save_debug_images=True)
mock_save.assert_not_called()