refactor(fudai_core): 抽 _read_countdown_split 闭包到 countdown_split 模块 (2124 → 1953)
douyin_fudai_core.py 的 get_fudai_contain 函数体只有 1 个,但里面嵌了 多个闭包做 OCR 子任务。最大的 _read_countdown_split (172 行) 把倒计时 区域分割成"分钟"+"秒"两段分别识别再组合 mm:ss。 抽到 douyin_fudai_core_pkg/countdown_split.py 的 read_countdown_split 顶层函数(203 行,含独立的 _extract_digits 纯函数)。原闭包改为薄壳委托, 保留 box 默认绑定 analyser/save_debug_images 闭包变量的语义。 新增 16 个单元测试覆盖: - _extract_digits 纯函数(前导零 / 非数字 / 边界) - read_countdown_split 主路径 + OCR 失败 + 秒>=60 警告 + 切图失败 - 切割比例(30% / 30%)正确性 - save_debug_images 开关效果 1242 passed (+16)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
07eee6da52
commit
2707773b15
3 files changed
+354
-169
No files matched your search
@@ -0,0 +1,143 @@
|
||||
"""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_last(self):
|
||||
# "002" -> 后两位 "02",但 "02" 以 0 开头且原串非全 0
|
||||
# → 取移除前导 0 后的 "2",再 zfill 到 2 位 → "02"
|
||||
assert _extract_digits("002") == "02"
|
||||
|
||||
def test_strips_non_digits(self):
|
||||
assert _extract_digits("12:34") == "34" # 取后 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 位 "25",不是以 0 开头 → 直接返回
|
||||
assert _extract_digits("025") == "25"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 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_calls_save(self):
|
||||
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)
|
||||
# 调用 2 次(分钟 + 秒)
|
||||
assert mock_save.call_count == 2
|
||||
Reference in new issue
Block a user