refactor(fudai): 拆出额外验证 + 兜底无效详情检查(1121→1084,-37 行)

- handle_extra_validation:调用 validate_fudai_detail,失败时关闭详情 + 写日志
- handle_invalid_detail_fallback:价值=0 且内容空时判定为"无效详情"提前 return

新增 9 个单元测试,1239 → 1248 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 04:02:01 +08:00
1 parent f075f94b31
commit b8a35656a0
3 files changed
+132 -41

No files matched your search

+4 -41
View File
@@ -961,49 +961,12 @@ class fudai_analyse:
pass
# 额外验证:使用验证函数检查数据合理性
try:
# 使用已导入的模块,避免重复导入导致作用域问题
if detail is None:
try:
self._log_kv("福袋详情", "详情数据为 None,跳过验证")
except Exception:
pass
else:
is_valid, reason = douyin_fudai_detail.validate_fudai_detail(
detail, self
)
if not is_valid:
douyin_fudai_detail.close_fudai_detail(self)
fudai_detail_blocks.cleanup_and_log_completion(
self,
start_time,
"验证失败",
pre_log_msg="_handle_in_fudai_detail:福袋详情数据验证失败,识别出错",
pre_log_fields={"validation_reason": reason},
)
return
except Exception:
pass
if fudai_detail_blocks.handle_extra_validation(self, detail, start_time):
return
# 兜底检查:如果价值为0且内容为空,也可能是优惠券弹窗或识别错误
try:
if isinstance(detail, tuple) and len(detail) >= 5:
content = str(detail[0] or "")
prize_value_yuan = float(detail[4] or 0.0)
if not content and prize_value_yuan == 0.0:
fudai_detail_blocks.cleanup_and_log_completion(
self,
start_time,
"无效详情",
pre_log_msg="_handle_in_fudai_detail:检测到无效详情(可能是优惠券弹窗或识别错误),跳过后续处理",
pre_log_fields={
"content": content,
"prize_value_yuan": prize_value_yuan,
},
)
return
except Exception:
pass
if fudai_detail_blocks.handle_invalid_detail_fallback(self, detail, start_time):
return
# 记录参与前的状态
attend_before_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
@@ -436,6 +436,64 @@ def extract_future_ts_from_detail(analyser: Any, detail: Any) -> None:
pass
def handle_extra_validation(analyser: Any, detail: Any, start_time: float) -> bool:
"""额外验证:调用 validate_fudai_detail 检查数据合理性。
返回 True 表示验证失败已触发清理,主流程应 return。
返回 False 表示验证通过或 detail 为 None。
"""
from douyin.fudai import douyin_fudai_detail
try:
if detail is None:
try:
analyser._log_kv("福袋详情", "详情数据为 None,跳过验证")
except Exception:
pass
return False
is_valid, reason = douyin_fudai_detail.validate_fudai_detail(detail, analyser)
if not is_valid:
douyin_fudai_detail.close_fudai_detail(analyser)
cleanup_and_log_completion(
analyser,
start_time,
"验证失败",
pre_log_msg="_handle_in_fudai_detail:福袋详情数据验证失败,识别出错",
pre_log_fields={"validation_reason": reason},
)
return True
except Exception:
pass
return False
def handle_invalid_detail_fallback(analyser: Any, detail: Any, start_time: float) -> bool:
"""兜底检查:价值为 0 且内容为空 → 可能是优惠券弹窗或识别错误。
返回 True 表示触发了无效详情分支,主流程应 return。
"""
try:
if isinstance(detail, tuple) and len(detail) >= 5:
content = str(detail[0] or "")
prize_value_yuan = float(detail[4] or 0.0)
if not content and prize_value_yuan == 0.0:
cleanup_and_log_completion(
analyser,
start_time,
"无效详情",
pre_log_msg="_handle_in_fudai_detail:检测到无效详情(可能是优惠券弹窗或识别错误),跳过后续处理",
pre_log_fields={
"content": content,
"prize_value_yuan": prize_value_yuan,
},
)
return True
except Exception:
pass
return False
def reread_detail_for_second_open_fallthrough(analyser: Any) -> Any:
"""第二次打开但 handle_second_open_attend 返回 False 时:尝试重读详情。
+70
View File
@@ -941,3 +941,73 @@ def test_first_open_filter_skip_normal_triggers_close_switch():
)
)
assert should_return is True
# ---- handle_extra_validation ---- #
def test_extra_validation_detail_none_returns_false():
a = MagicMock()
assert fudai_detail_blocks.handle_extra_validation(a, None, _time.time()) is False
def test_extra_validation_valid_returns_false():
a = MagicMock()
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
):
assert fudai_detail_blocks.handle_extra_validation(a, detail, _time.time()) is False
def test_extra_validation_invalid_returns_true_and_closes():
a = MagicMock()
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(False, "bad"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
) as cd:
assert fudai_detail_blocks.handle_extra_validation(a, detail, _time.time()) is True
cd.assert_called_once_with(a)
def test_extra_validation_swallows_exception():
a = MagicMock()
with patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
side_effect=RuntimeError("boom"),
):
assert fudai_detail_blocks.handle_extra_validation(a, ("x",), _time.time()) is False
# ---- handle_invalid_detail_fallback ---- #
def test_invalid_detail_fallback_zero_value_empty_content_returns_true():
a = MagicMock()
detail = ("", "x", "x", "x", 0.0)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is True
def test_invalid_detail_fallback_with_content_returns_false():
a = MagicMock()
detail = ("有内容", "x", "x", "x", 0.0)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is False
def test_invalid_detail_fallback_with_value_returns_false():
a = MagicMock()
detail = ("", "x", "x", "x", 5.0)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is False
def test_invalid_detail_fallback_short_tuple_returns_false():
a = MagicMock()
detail = ("only_one",)
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, detail, _time.time()) is False
def test_invalid_detail_fallback_non_tuple_returns_false():
a = MagicMock()
assert fudai_detail_blocks.handle_invalid_detail_fallback(a, None, _time.time()) is False