refactor(stage): 拆出抽奖结果弹窗 + 购物红包弹窗 OCR 检测(1284→1204,-80 行)
把 douyin_stage 中两个独立的"像素初筛 + OCR 二次确认"块抽到 douyin_stage_helpers: - detect_lottery_result_popup:优先级1,抽奖结果弹窗(FUDai_RESULT) - detect_shopping_redpacket_popup:优先级1.5,购物红包弹窗(SHOPPING_REDPACKET_POPUP) 两者都返回 (matched, stage, reason),命中时主流程直接 return。 新增 6 个单元测试,1273 → 1279 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
feaf555657
commit
4cb5daa79e
3 files changed
+250
-100
No files matched your search
@@ -7,7 +7,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
import time as _time
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from douyin.stage import douyin_stage_helpers as h
|
||||
|
||||
@@ -103,3 +104,99 @@ def test_summarize_pixel_state_swallows_exceptions():
|
||||
"""log_callback 抛错也不应传播"""
|
||||
bad_log = MagicMock(side_effect=RuntimeError("boom"))
|
||||
h.summarize_pixel_state({}, 0.0, bad_log)
|
||||
|
||||
|
||||
# ---- detect_lottery_result_popup ---- #
|
||||
|
||||
def test_detect_lottery_result_matched_returns_true():
|
||||
a = MagicMock()
|
||||
log = MagicMock()
|
||||
log_summary = MagicMock()
|
||||
log_sep = MagicMock()
|
||||
from douyin.utils import douyin_helpers
|
||||
|
||||
with patch.object(
|
||||
douyin_helpers.HelperFunctions,
|
||||
"check_lottery_result_by_ocr",
|
||||
return_value=True,
|
||||
):
|
||||
matched, stage, reason = h.detect_lottery_result_popup(
|
||||
a, _time.time(), log, log_summary, log_sep
|
||||
)
|
||||
assert matched is True
|
||||
assert stage == "FUDai_RESULT"
|
||||
|
||||
|
||||
def test_detect_lottery_result_not_matched_returns_false():
|
||||
a = MagicMock()
|
||||
log = MagicMock()
|
||||
log_summary = MagicMock()
|
||||
log_sep = MagicMock()
|
||||
from douyin.utils import douyin_helpers
|
||||
|
||||
with patch.object(
|
||||
douyin_helpers.HelperFunctions,
|
||||
"check_lottery_result_by_ocr",
|
||||
return_value=False,
|
||||
):
|
||||
matched, _, _ = h.detect_lottery_result_popup(
|
||||
a, _time.time(), log, log_summary, log_sep
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
def test_detect_lottery_result_exception_returns_false():
|
||||
a = MagicMock()
|
||||
log = MagicMock()
|
||||
log_summary = MagicMock()
|
||||
log_sep = MagicMock()
|
||||
from douyin.utils import douyin_helpers
|
||||
|
||||
with patch.object(
|
||||
douyin_helpers.HelperFunctions,
|
||||
"check_lottery_result_by_ocr",
|
||||
side_effect=RuntimeError("boom"),
|
||||
):
|
||||
matched, _, _ = h.detect_lottery_result_popup(
|
||||
a, _time.time(), log, log_summary, log_sep
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
# ---- detect_shopping_redpacket_popup ---- #
|
||||
|
||||
def test_detect_shopping_redpacket_keyword_match():
|
||||
"""OCR 文本含关键词 → 命中"""
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = "邀你抢红包啦"
|
||||
log = MagicMock()
|
||||
log_summary = MagicMock()
|
||||
log_sep = MagicMock()
|
||||
matched, stage, _ = h.detect_shopping_redpacket_popup(
|
||||
a, (10, 20, 30, 40), _time.time(), 30.0, 5.0,
|
||||
lambda _x, _y: "preprocessed", log, log_summary, log_sep,
|
||||
)
|
||||
assert matched is True
|
||||
assert stage == "SHOPPING_REDPACKET_POPUP"
|
||||
|
||||
|
||||
def test_detect_shopping_redpacket_no_keyword_returns_false():
|
||||
a = MagicMock()
|
||||
a.operation.analyse_pic_word.return_value = "随便的文本"
|
||||
log = MagicMock()
|
||||
matched, _, _ = h.detect_shopping_redpacket_popup(
|
||||
a, (10, 20, 30, 40), _time.time(), 30.0, 5.0,
|
||||
lambda _x, _y: "p", log, MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
|
||||
|
||||
def test_detect_shopping_redpacket_ocr_exception_returns_false():
|
||||
a = MagicMock()
|
||||
a.cut_pic.side_effect = RuntimeError("boom")
|
||||
log = MagicMock()
|
||||
matched, _, _ = h.detect_shopping_redpacket_popup(
|
||||
a, (10, 20, 30, 40), _time.time(), 30.0, 5.0,
|
||||
lambda _x, _y: "p", log, MagicMock(), MagicMock(),
|
||||
)
|
||||
assert matched is False
|
||||
Reference in new issue
Block a user