refactor(fudai): 抽 _handle_in_fudai_detail last_status 检查到 helper (2872 → 2614)

新增 3 个 helper:
- check_last_status_branch:根据 _last_fudai_detail_status 路由 4 个状态
  (coupon_popup / watch_task / validation_failed / error)
- _clear_task_count_flag:清除 _fudai_detail_task_count_from_stage(多分支共用)
- _handle_error_status:error + reason=task_count_zero 时连续 3 次切直播间

净减 260 行重复样板代码。新增 9 个单元测试覆盖:
- 4 种已知 status 各自分支
- 未知/空 status 不触发
- task_count_zero 第 1 次仅累计、第 3 次强制切房 / needswitch=False 不切

至此 _handle_in_fudai_detail 总长 1010 → 752 行,
douyin_fudai.py 累计 4121 → 2614(-1507 行 / -37%)。

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 21:23:26 +08:00
1 parent 7b6dec820b
commit 0540610c75
3 files changed
+331 -260

No files matched your search

+88
View File
@@ -329,3 +329,91 @@ class TestPrepareForAttend:
result = hd.prepare_for_attend(p, ("a", "b", "c", "d", 0, (0, 0, 0)))
assert result == 0.0
# ============================================================
# check_last_status_branch
# ============================================================
class TestCheckLastStatusBranch:
def _mk_for_status(self, last_status: str = "", last_extra=None):
s = _mk_self()
s._last_fudai_detail_status = last_status
if last_extra is not None:
s._last_fudai_detail_extra = last_extra
s._fudai_detail_task_count_from_stage = 5
s._fudai_detail_consecutive_failures = 0
s.device_id = "MOCK"
s.operation = MagicMock()
s.operation.delay = MagicMock(return_value=None)
s.operation.swipe = MagicMock(return_value=None)
return s
def test_empty_status_returns_false(self):
s = self._mk_for_status("")
assert hd.check_last_status_branch(s, 0.0) is False
def test_unknown_status_returns_false(self):
s = self._mk_for_status("some_other_status")
assert hd.check_last_status_branch(s, 0.0) is False
def test_coupon_popup_returns_true_clears_task_count(self):
s = self._mk_for_status("coupon_popup")
assert hd.check_last_status_branch(s, 0.0) is True
assert s._fudai_detail_task_count_from_stage is None
def test_watch_task_returns_true_clears_task_count(self):
s = self._mk_for_status("watch_task")
assert hd.check_last_status_branch(s, 0.0) is True
assert s._fudai_detail_task_count_from_stage is None
def test_validation_failed_returns_true_clears_task_count(self):
s = self._mk_for_status("validation_failed")
assert hd.check_last_status_branch(s, 0.0) is True
assert s._fudai_detail_task_count_from_stage is None
def test_error_without_task_count_zero_reason_returns_false(self):
"""error 但 reason 不是 task_count_zero → 不在此处理。"""
s = self._mk_for_status("error", last_extra={"reason": "other_error"})
assert hd.check_last_status_branch(s, 0.0) is False
def test_error_task_count_zero_first_failure(self):
"""task_count_zero 第 1 次:仅累加计数 + 清标志,不切直播间。"""
s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"})
s._fudai_detail_consecutive_failures = 0
s.swipe_up_to_next_live = MagicMock(return_value=None)
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
result = hd.check_last_status_branch(s, 0.0)
assert result is True
assert s._fudai_detail_consecutive_failures == 1
s.swipe_up_to_next_live.assert_not_called()
def test_error_task_count_zero_third_failure_swipes(self):
"""task_count_zero 第 3 次:强制关详情 + 切直播间 + 重置计数。"""
s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"})
s._fudai_detail_consecutive_failures = 2 # 加 1 后达到 3
s.needswitch = True
s.swipe_up_to_next_live = MagicMock(return_value=None)
with patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
) as mock_close:
result = hd.check_last_status_branch(s, 0.0)
assert result is True
mock_close.assert_called_once()
s.swipe_up_to_next_live.assert_called_once()
# 计数应被重置
assert s._fudai_detail_consecutive_failures == 0
# 任务数量标志被清除
assert s._fudai_detail_task_count_from_stage is None
def test_error_task_count_zero_third_failure_no_switch_when_needswitch_false(self):
"""needswitch=False 时第 3 次也不切直播间。"""
s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"})
s._fudai_detail_consecutive_failures = 2
s.needswitch = False
s.swipe_up_to_next_live = MagicMock(return_value=None)
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
result = hd.check_last_status_branch(s, 0.0)
assert result is True
s.swipe_up_to_next_live.assert_not_called()
assert s._fudai_detail_consecutive_failures == 0