refactor(fudai): 抽 _handle_in_fudai_detail 步骤3末尾两个小段 (2926 → 2872)
新增 2 个 helper: - check_invalid_detail_fallback:兜底检查(content="" + value=0 → 视为无效详情) - prepare_for_attend:记 attend_before_ts + 提取 future_ts + 重置 _detail_closed_by_attend 净减 54 行重复样板代码。新增 9 个单元测试覆盖: - 空内容+0价值识别为无效 - 正常内容不触发兜底 - detail tuple 短或非 tuple 不抛 - prepare_for_attend 正确返回 attend_before_ts 和写入 future_ts 至此 _handle_in_fudai_detail 总长 1872 → 1010 行(-862 行 / -46%), douyin_fudai.py 4121 → 2872(累计 -1249 行 / -30%)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8a975f2bf6
commit
7b6dec820b
3 files changed
+167
-58
No files matched your search
@@ -2723,65 +2723,11 @@ class fudai_analyse:
|
||||
pass
|
||||
|
||||
# 兜底检查:如果价值为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:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:检测到无效详情(可能是优惠券弹窗或识别错误),跳过后续处理",
|
||||
content=content,
|
||||
prize_value_yuan=prize_value_yuan,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if _hd_helpers.check_invalid_detail_fallback(self, detail, start_time):
|
||||
return
|
||||
|
||||
# 清除任务数量标志
|
||||
try:
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
self._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(无效详情)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 记录参与前的状态
|
||||
attend_before_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
|
||||
# 记录当前福袋的 future_ts,供 attend_choujiang 使用
|
||||
try:
|
||||
if isinstance(detail, tuple) and len(detail) >= 6:
|
||||
d_cast: Tuple[Any, ...] = cast(Tuple[Any, ...], detail)
|
||||
countdown_tuple = d_cast[5]
|
||||
if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2:
|
||||
future_ts = float(countdown_tuple[1] or 0.0)
|
||||
if future_ts > 0:
|
||||
self._current_fudai_future_ts = future_ts
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 重置详情关闭标志
|
||||
try:
|
||||
self._detail_closed_by_attend = False
|
||||
except Exception:
|
||||
pass
|
||||
# 步骤3 末尾准备:记录参与前状态、提取 future_ts、重置 _detail_closed_by_attend
|
||||
attend_before_ts = _hd_helpers.prepare_for_attend(self, detail)
|
||||
|
||||
# 步骤3结束计时
|
||||
step3_elapsed = (time.time() - step3_start) * 1000
|
||||
|
||||
@@ -94,6 +94,92 @@ def handle_consecutive_failures_check(self: "fudai_analyse") -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def check_invalid_detail_fallback(
|
||||
self: "fudai_analyse", detail, start_time: float
|
||||
) -> bool:
|
||||
"""兜底检查:detail 内容为空且价值为 0 → 视为无效详情(优惠券或识别错误)。
|
||||
|
||||
返回 True 表示已识别为无效详情、清理标志并 log,caller 应当 `return`;
|
||||
返回 False 表示详情看起来正常,继续后续步骤。
|
||||
"""
|
||||
import time
|
||||
|
||||
try:
|
||||
if isinstance(detail, tuple) and len(detail) >= 5:
|
||||
content = str(detail[0] or "")
|
||||
try:
|
||||
prize_value_yuan = float(detail[4] or 0.0)
|
||||
except (TypeError, ValueError):
|
||||
prize_value_yuan = 0.0
|
||||
if not content and prize_value_yuan == 0.0:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:检测到无效详情(可能是优惠券弹窗或识别错误),跳过后续处理",
|
||||
content=content,
|
||||
prize_value_yuan=prize_value_yuan,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除任务数量标志
|
||||
try:
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
self._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(无效详情)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def prepare_for_attend(self: "fudai_analyse", detail) -> float:
|
||||
"""步骤3 末尾:记录参与前状态、提取 future_ts、重置标志。
|
||||
|
||||
返回 attend_before_ts —— 给 step4 用来对比 _last_successful_attend_ts
|
||||
判断是否参与成功。
|
||||
"""
|
||||
from typing import Any, Tuple, cast
|
||||
|
||||
attend_before_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
|
||||
# 记录当前福袋的 future_ts,供 attend_choujiang 使用
|
||||
countdown_tuple = None
|
||||
try:
|
||||
if isinstance(detail, tuple) and len(detail) >= 6:
|
||||
d_cast: Tuple[Any, ...] = cast(Tuple[Any, ...], detail)
|
||||
countdown_tuple = d_cast[5]
|
||||
if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2:
|
||||
future_ts = float(countdown_tuple[1] or 0.0)
|
||||
if future_ts > 0:
|
||||
self._current_fudai_future_ts = future_ts
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 重置详情关闭标志
|
||||
try:
|
||||
self._detail_closed_by_attend = False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return attend_before_ts
|
||||
|
||||
|
||||
def step4_make_join_decision(
|
||||
self: "fudai_analyse",
|
||||
detail,
|
||||
|
||||
@@ -252,3 +252,80 @@ class TestStep4MakeJoinDecision:
|
||||
|
||||
assert s._waiting_for_lottery_result is False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# check_invalid_detail_fallback
|
||||
# ============================================================
|
||||
|
||||
class TestCheckInvalidDetailFallback:
|
||||
def test_empty_content_zero_value_returns_true(self):
|
||||
s = _mk_self()
|
||||
s._fudai_detail_task_count_from_stage = 5
|
||||
# detail[0]="", detail[4]=0.0 → 视为无效
|
||||
detail = ("", 0, 0, 0, 0.0, (0, 0, 0))
|
||||
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is True
|
||||
assert s._fudai_detail_task_count_from_stage is None
|
||||
|
||||
def test_normal_content_returns_false(self):
|
||||
s = _mk_self()
|
||||
detail = ("有效福袋", 0, 0, 0, 100.0, (0, 9999, 30))
|
||||
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is False
|
||||
|
||||
def test_value_zero_but_has_content_returns_false(self):
|
||||
s = _mk_self()
|
||||
detail = ("某某福袋", 0, 0, 0, 0.0, (0, 0, 0))
|
||||
assert hd.check_invalid_detail_fallback(s, detail, 0.0) is False
|
||||
|
||||
def test_short_tuple_returns_false(self):
|
||||
"""detail tuple 长度 < 5 时不应触发。"""
|
||||
s = _mk_self()
|
||||
assert hd.check_invalid_detail_fallback(s, ("a", "b"), 0.0) is False
|
||||
|
||||
def test_non_tuple_returns_false(self):
|
||||
s = _mk_self()
|
||||
assert hd.check_invalid_detail_fallback(s, None, 0.0) is False
|
||||
assert hd.check_invalid_detail_fallback(s, "not a tuple", 0.0) is False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# prepare_for_attend
|
||||
# ============================================================
|
||||
|
||||
class TestPrepareForAttend:
|
||||
def test_returns_attend_before_ts(self):
|
||||
s = _mk_self()
|
||||
s._last_successful_attend_ts = 12345.6
|
||||
# detail 缺 countdown 部分
|
||||
result = hd.prepare_for_attend(s, ("X",))
|
||||
assert result == 12345.6
|
||||
assert s._detail_closed_by_attend is False
|
||||
|
||||
def test_extracts_future_ts(self):
|
||||
import time
|
||||
|
||||
s = _mk_self()
|
||||
future_ts = time.time() + 60
|
||||
detail = ("X", 0, 0, 0, 0, (0, future_ts, 60))
|
||||
hd.prepare_for_attend(s, detail)
|
||||
assert s._current_fudai_future_ts == future_ts
|
||||
|
||||
def test_none_detail_does_not_raise(self):
|
||||
s = _mk_self()
|
||||
s._last_successful_attend_ts = 0.0
|
||||
# 不应抛
|
||||
result = hd.prepare_for_attend(s, None)
|
||||
assert result == 0.0
|
||||
|
||||
def test_attend_before_ts_default_zero(self):
|
||||
s = _mk_self()
|
||||
# _last_successful_attend_ts 默认在 _mk_self 没设
|
||||
if hasattr(s, "_last_successful_attend_ts"):
|
||||
del s._last_successful_attend_ts.__dict__ # noqa
|
||||
# MagicMock 的属性永远存在 → getattr 返回 MagicMock;用真对象避免歧义
|
||||
class Plain:
|
||||
pass
|
||||
p = Plain()
|
||||
p._log_kv = lambda *a, **k: None
|
||||
result = hd.prepare_for_attend(p, ("a", "b", "c", "d", 0, (0, 0, 0)))
|
||||
assert result == 0.0
|
||||
|
||||
Reference in new issue
Block a user