refactor(fudai): 抽出 _handle_in_fudai_detail 通用清理 + 状态分支(3632→3502,-130 行)
发现 _handle_in_fudai_detail 中有 4 个分支重复着同一套清理动作: '清除任务数量标志 → 写完成日志 → return' 抽出两个辅助函数到 fudai_detail_blocks: - cleanup_and_log_completion:通用清理 + 完成日志(取代原 30+ 行重复代码块) - handle_status_based_early_return:根据 _last_fudai_detail_status 分发到 优惠券弹窗 / 观看任务 / 验证失败三个分支(error 分支因含 ADB 调用与计数 逻辑较复杂,留在主流程) 应用到 4 处早返回:coupon_popup / watch_task / validation_failed / 验证失败 / 无效详情。 新增 9 个单元测试覆盖各分支与边界,1073 → 1082 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2d971ca7d8
commit
17f1977a65
3 files changed
+177
-148
No files matched your search
+18
-148
@@ -2890,99 +2890,11 @@ class fudai_analyse:
|
||||
return
|
||||
|
||||
# 检查是否是优惠券弹窗、观看任务或识别错误:如果检测到,应该直接跳过,不继续处理
|
||||
if fudai_detail_blocks.handle_status_based_early_return(self, start_time):
|
||||
return
|
||||
try:
|
||||
last_status = getattr(self, "_last_fudai_detail_status", "")
|
||||
if last_status == "coupon_popup":
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:检测到优惠券弹窗,已关闭,跳过后续处理",
|
||||
)
|
||||
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
|
||||
elif last_status == "watch_task":
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:检测到'观看X分钟'任务,已在OCR阶段跳过,无需后续处理",
|
||||
)
|
||||
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
|
||||
elif last_status == "validation_failed":
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:福袋详情数据验证失败,识别出错,跳过后续处理",
|
||||
)
|
||||
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
|
||||
elif last_status == "error":
|
||||
if last_status == "error":
|
||||
# 检查是否是任务数量为0的错误
|
||||
try:
|
||||
last_extra = getattr(self, "_last_fudai_detail_extra", {})
|
||||
@@ -3170,35 +3082,13 @@ class fudai_analyse:
|
||||
detail, self
|
||||
)
|
||||
if not is_valid:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:福袋详情数据验证失败,识别出错",
|
||||
validation_reason=reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
douyin_fudai_detail.close_fudai_detail(self)
|
||||
|
||||
# 清除任务数量标志
|
||||
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),
|
||||
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:
|
||||
@@ -3210,35 +3100,15 @@ class fudai_analyse:
|
||||
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
|
||||
|
||||
# 清除任务数量标志
|
||||
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),
|
||||
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:
|
||||
|
||||
@@ -153,3 +153,96 @@ def handle_validation_failure_cleanup(
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def cleanup_and_log_completion(
|
||||
analyser: Any,
|
||||
start_time: float,
|
||||
completion_reason: str,
|
||||
*,
|
||||
pre_log_msg: str | None = None,
|
||||
pre_log_fields: dict | None = None,
|
||||
) -> None:
|
||||
"""通用清理 + 完成日志:在 _handle_in_fudai_detail 多个 early-return
|
||||
分支中重复出现的"清除任务数量标志 + 写完成日志"模式。
|
||||
|
||||
参数:
|
||||
completion_reason: 完成日志中括号里的原因,例如 "优惠券弹窗"。
|
||||
pre_log_msg / pre_log_fields: 可选的前置日志(描述触发原因)。
|
||||
"""
|
||||
if pre_log_msg:
|
||||
try:
|
||||
analyser._log_kv("流程", pre_log_msg, **(pre_log_fields or {}))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
elapsed = time.time() - start_time
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
f"_handle_in_fudai_detail完成({completion_reason})",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def handle_status_based_early_return(
|
||||
analyser: Any, start_time: float
|
||||
) -> bool:
|
||||
"""根据 _last_fudai_detail_status 处理优惠券弹窗 / 观看任务 / 验证失败的 early return。
|
||||
|
||||
返回 True 表示已处理某个 status 分支,主流程应 return。
|
||||
返回 False 表示 status 不在已知值里,主流程继续。
|
||||
|
||||
注意:错误状态(last_status == "error")和任务数量为0的复杂分支没有
|
||||
包含在此处,因为它会修改 _fudai_detail_consecutive_failures 并涉及
|
||||
可能的 ADB 调用,逻辑复杂、不适合放在通用清理里。
|
||||
"""
|
||||
try:
|
||||
last_status = getattr(analyser, "_last_fudai_detail_status", "")
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
if last_status == "coupon_popup":
|
||||
cleanup_and_log_completion(
|
||||
analyser,
|
||||
start_time,
|
||||
"优惠券弹窗",
|
||||
pre_log_msg="_handle_in_fudai_detail:检测到优惠券弹窗,已关闭,跳过后续处理",
|
||||
)
|
||||
return True
|
||||
|
||||
if last_status == "watch_task":
|
||||
cleanup_and_log_completion(
|
||||
analyser,
|
||||
start_time,
|
||||
"观看任务",
|
||||
pre_log_msg="_handle_in_fudai_detail:检测到'观看X分钟'任务,已在OCR阶段跳过,无需后续处理",
|
||||
)
|
||||
return True
|
||||
|
||||
if last_status == "validation_failed":
|
||||
cleanup_and_log_completion(
|
||||
analyser,
|
||||
start_time,
|
||||
"验证失败",
|
||||
pre_log_msg="_handle_in_fudai_detail:福袋详情数据验证失败,识别出错,跳过后续处理",
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@@ -121,3 +121,69 @@ def test_validation_cleanup_close_failure_does_not_break():
|
||||
a, _time.time(), _time.time()
|
||||
)
|
||||
assert a._fudai_detail_task_count_from_stage is None
|
||||
|
||||
|
||||
# ---- cleanup_and_log_completion ---- #
|
||||
|
||||
def test_cleanup_and_log_completion_basic():
|
||||
a = MagicMock()
|
||||
a._fudai_detail_task_count_from_stage = 5
|
||||
fudai_detail_blocks.cleanup_and_log_completion(a, _time.time(), "测试原因")
|
||||
assert a._fudai_detail_task_count_from_stage is None
|
||||
|
||||
|
||||
def test_cleanup_and_log_completion_with_pre_log():
|
||||
a = MagicMock()
|
||||
fudai_detail_blocks.cleanup_and_log_completion(
|
||||
a,
|
||||
_time.time(),
|
||||
"无效详情",
|
||||
pre_log_msg="检测到无效",
|
||||
pre_log_fields={"content": "x", "value": 0},
|
||||
)
|
||||
# 至少调用了 pre_log + 完成日志
|
||||
assert a._log_kv.call_count >= 2
|
||||
|
||||
|
||||
def test_cleanup_and_log_completion_swallows_exceptions():
|
||||
a = MagicMock()
|
||||
a._log_kv.side_effect = RuntimeError("boom")
|
||||
fudai_detail_blocks.cleanup_and_log_completion(a, _time.time(), "异常")
|
||||
|
||||
|
||||
# ---- handle_status_based_early_return ---- #
|
||||
|
||||
def test_status_coupon_popup_returns_true():
|
||||
a = MagicMock()
|
||||
a._last_fudai_detail_status = "coupon_popup"
|
||||
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is True
|
||||
|
||||
|
||||
def test_status_watch_task_returns_true():
|
||||
a = MagicMock()
|
||||
a._last_fudai_detail_status = "watch_task"
|
||||
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is True
|
||||
|
||||
|
||||
def test_status_validation_failed_returns_true():
|
||||
a = MagicMock()
|
||||
a._last_fudai_detail_status = "validation_failed"
|
||||
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is True
|
||||
|
||||
|
||||
def test_status_unknown_returns_false():
|
||||
a = MagicMock()
|
||||
a._last_fudai_detail_status = "ok"
|
||||
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is False
|
||||
|
||||
|
||||
def test_status_error_returns_false_for_external_handling():
|
||||
"""error 状态留给主流程处理(含 task_count_zero 复杂分支)"""
|
||||
a = MagicMock()
|
||||
a._last_fudai_detail_status = "error"
|
||||
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is False
|
||||
|
||||
|
||||
def test_status_attribute_missing_returns_false():
|
||||
a = MagicMock(spec=[])
|
||||
assert fudai_detail_blocks.handle_status_based_early_return(a, _time.time()) is False
|
||||
Reference in new issue
Block a user