189 lines
5.2 KiB
Python
189 lines
5.2 KiB
Python
from __future__ import annotations
|
||
|
||
from unittest.mock import MagicMock
|
||
|
||
from douyin.fudai.douyin_fudai_contain_impl import (
|
||
_build_countdown_candidate_boxes,
|
||
_parse_mmss_from_full_text,
|
||
_select_countdown_from_candidates,
|
||
)
|
||
from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import (
|
||
parse_countdown_time_text,
|
||
)
|
||
|
||
|
||
def _mk_analyser():
|
||
a = MagicMock()
|
||
a._log_kv = MagicMock()
|
||
a.current_room_title_raw = "直播间A"
|
||
a._countdown_last_room_title = "直播间A"
|
||
a._last_countdown_total_seconds = 10 * 60
|
||
return a
|
||
|
||
|
||
def test_select_countdown_prefers_valid_non_primary_when_primary_missing():
|
||
analyser = _mk_analyser()
|
||
candidate_meta = [
|
||
{
|
||
"idx": 0,
|
||
"box": (1, 2, 3, 4),
|
||
"is_primary": True,
|
||
"mm_result_key": "countdown_mm",
|
||
"ss_result_key": "countdown_ss",
|
||
},
|
||
{
|
||
"idx": 1,
|
||
"box": (5, 6, 7, 8),
|
||
"is_primary": False,
|
||
"mm_result_key": "countdown_mm_c1",
|
||
"ss_result_key": "countdown_ss_c1",
|
||
},
|
||
]
|
||
|
||
mm, ss, best = _select_countdown_from_candidates(
|
||
analyser,
|
||
candidate_meta,
|
||
primary_mm_raw="",
|
||
primary_ss_raw="",
|
||
extra_results={"countdown_mm_c1": "09", "countdown_ss_c1": "58"},
|
||
room_title_hint="直播间A",
|
||
)
|
||
|
||
assert mm == "09"
|
||
assert ss == "58"
|
||
assert best is not None
|
||
assert best.get("idx") == 1
|
||
assert bool(best.get("valid")) is True
|
||
|
||
|
||
def test_select_countdown_keeps_primary_when_extra_invalid():
|
||
analyser = _mk_analyser()
|
||
candidate_meta = [
|
||
{
|
||
"idx": 0,
|
||
"box": (1, 2, 3, 4),
|
||
"is_primary": True,
|
||
"mm_result_key": "countdown_mm",
|
||
"ss_result_key": "countdown_ss",
|
||
},
|
||
{
|
||
"idx": 1,
|
||
"box": (5, 6, 7, 8),
|
||
"is_primary": False,
|
||
"mm_result_key": "countdown_mm_c1",
|
||
"ss_result_key": "countdown_ss_c1",
|
||
},
|
||
]
|
||
|
||
mm, ss, best = _select_countdown_from_candidates(
|
||
analyser,
|
||
candidate_meta,
|
||
primary_mm_raw="12",
|
||
primary_ss_raw="26",
|
||
extra_results={"countdown_mm_c1": "99", "countdown_ss_c1": "88"},
|
||
room_title_hint="直播间A",
|
||
)
|
||
|
||
assert mm == "12"
|
||
assert ss == "26"
|
||
assert best is not None
|
||
assert bool(best.get("valid")) is True
|
||
|
||
|
||
def test_parse_countdown_sliding_window_filters_noise_digits():
|
||
analyser = _mk_analyser()
|
||
# 分钟 OCR 噪声 "601" 不应被解析成 "60",应优先取合法窗口 "01"
|
||
time_text = parse_countdown_time_text(analyser, "601", "53")
|
||
assert time_text == "01:53"
|
||
|
||
|
||
def test_select_countdown_uses_sliding_window_for_candidate_validity():
|
||
analyser = _mk_analyser()
|
||
candidate_meta = [
|
||
{
|
||
"idx": 0,
|
||
"box": (1, 2, 3, 4),
|
||
"is_primary": True,
|
||
"mm_result_key": "countdown_mm",
|
||
"ss_result_key": "countdown_ss",
|
||
}
|
||
]
|
||
|
||
mm, ss, best = _select_countdown_from_candidates(
|
||
analyser,
|
||
candidate_meta,
|
||
primary_mm_raw="601",
|
||
primary_ss_raw="53",
|
||
extra_results={},
|
||
room_title_hint="直播间A",
|
||
)
|
||
|
||
assert mm == "01"
|
||
assert ss == "53"
|
||
assert best is not None
|
||
assert bool(best.get("valid")) is True
|
||
|
||
|
||
def test_parse_countdown_rejects_out_of_range_mm_ss():
|
||
analyser = _mk_analyser()
|
||
# 分钟与秒都越界时,必须返回空字符串,避免误参与
|
||
time_text = parse_countdown_time_text(analyser, "99", "88")
|
||
assert time_text == ""
|
||
|
||
|
||
def test_select_countdown_returns_empty_when_primary_invalid_and_no_valid_candidate():
|
||
analyser = _mk_analyser()
|
||
candidate_meta = [
|
||
{
|
||
"idx": 0,
|
||
"box": (390, 1110, 690, 1230),
|
||
"is_primary": True,
|
||
"mm_result_key": "countdown_mm",
|
||
"ss_result_key": "countdown_ss",
|
||
}
|
||
]
|
||
|
||
mm, ss, best = _select_countdown_from_candidates(
|
||
analyser,
|
||
candidate_meta,
|
||
primary_mm_raw="99",
|
||
primary_ss_raw="88",
|
||
extra_results={},
|
||
room_title_hint="直播间A",
|
||
)
|
||
|
||
assert mm == ""
|
||
assert ss == ""
|
||
assert best is not None
|
||
assert bool(best.get("valid")) is False
|
||
|
||
|
||
def test_build_countdown_candidate_boxes_excludes_far_value_band():
|
||
# renwu=2 主候选在 1110~1230,1460~1580 的候选应被过滤,避免扫到“参考价/¥0”。
|
||
boxes = _build_countdown_candidate_boxes(2)
|
||
assert (390, 1460, 690, 1580) not in boxes
|
||
|
||
|
||
def test_parse_mmss_from_full_text_extracts_valid_pair():
|
||
mm, ss = _parse_mmss_from_full_text("13:53后开奖")
|
||
assert mm == "13"
|
||
assert ss == "53"
|
||
|
||
|
||
def test_parse_mmss_from_full_text_extracts_compact_4digits_pair():
|
||
mm, ss = _parse_mmss_from_full_text("1340后开奖")
|
||
assert mm == "13"
|
||
assert ss == "40"
|
||
|
||
|
||
def test_parse_mmss_from_full_text_extracts_compact_3digits_pair():
|
||
mm, ss = _parse_mmss_from_full_text("940后开奖")
|
||
assert mm == "09"
|
||
assert ss == "40"
|
||
|
||
|
||
def test_parse_mmss_from_full_text_ignores_non_countdown_text():
|
||
mm, ss = _parse_mmss_from_full_text("直播间,否则会失去抽奖机会")
|
||
assert mm == ""
|
||
assert ss == ""
|