refactor(fudai): 抽 _handle_in_fudai_detail 步骤1 第二次打开分支 (2216 → 2079)
新增 handle_second_open_branch helper: - 取 _last_fudai_detail_tuple;缺失/无效则重新读取(兜底) - 有效缓存:从 countdown_tuple 提取 future_ts/原始秒数 + 设到 self - 调 attend_choujiang:成功清标志 / 失败累加计数 / 异常 fall through 顺手修了一个 latent bug:first-open 抽出后 caller 还在用 step3_start / skip / skip_reason 这些已不在作用域里的变量。改成从 self._last_fudai_detail_tuple 重新取 detail,去掉无意义的 step3_elapsed 日志,step4 的 skip 参数 直接传 (False, "") —— skip 已在 first-open 内部处理完。 净减 146 行嵌套样板。新增 6 个 second-open 单元测试覆盖: - 缓存缺失重读 + fall through - 短 tuple 触发重读 - attend 成功清标志清失败计数 - attend 失败累加计数 - attend 异常 fall through - 缓存有效时正确设置 _current_fudai_future_ts 至此 _handle_in_fudai_detail 总长 506 → 215 行(-58%), douyin_fudai.py 累计 4121 → 2079(-2042 行 / -50%)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ca953cb4ba
commit
61cdf7b445
3 files changed
+276
-146
No files matched your search
@@ -596,3 +596,113 @@ class TestHandleFirstOpenBranch:
|
||||
assert result is True
|
||||
join_called.assert_called_once()
|
||||
|
||||
|
||||
# ============================================================
|
||||
# handle_second_open_branch
|
||||
# ============================================================
|
||||
|
||||
class TestHandleSecondOpenBranch:
|
||||
def _mk_for_second_open(self):
|
||||
s = _mk_self()
|
||||
s.device_id = "MOCK"
|
||||
s.operation = MagicMock()
|
||||
s.operation.delay = MagicMock(return_value=None)
|
||||
s._fudai_detail_first_read = True
|
||||
s._consecutive_attend_failures = 0
|
||||
s._fudai_detail_task_count_from_stage = 0
|
||||
return s
|
||||
|
||||
def test_no_cached_detail_refetches_and_returns_false(self):
|
||||
"""没有缓存的 detail → 重新读取后保存,return False 让 caller 继续走正常流程。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_second_open()
|
||||
s._last_fudai_detail_tuple = None
|
||||
new_detail = ("X", 0, 0, 0, 100.0, (time.time(), time.time() + 60, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=new_detail,
|
||||
):
|
||||
result = hd.handle_second_open_branch(s, 0.0, 0.0)
|
||||
assert result is False
|
||||
# 重读成功应保存到 self
|
||||
assert s._last_fudai_detail_tuple == new_detail
|
||||
assert s._current_fudai_content == "X"
|
||||
|
||||
def test_short_tuple_triggers_refetch(self):
|
||||
"""缓存 tuple 长度 < 6 → 触发重新读取。"""
|
||||
s = self._mk_for_second_open()
|
||||
s._last_fudai_detail_tuple = ("a", "b") # 太短
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=None,
|
||||
) as mock_read:
|
||||
result = hd.handle_second_open_branch(s, 0.0, 0.0)
|
||||
assert result is False
|
||||
mock_read.assert_called_once()
|
||||
|
||||
def test_attend_success_returns_true_clears_failures(self):
|
||||
"""attend_choujiang 成功 → 清标志 + 清失败计数 + return True。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_second_open()
|
||||
s._consecutive_attend_failures = 3
|
||||
future_ts = time.time() + 60
|
||||
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang",
|
||||
return_value=True,
|
||||
):
|
||||
result = hd.handle_second_open_branch(s, 0.0, 0.0)
|
||||
assert result is True
|
||||
assert s._consecutive_attend_failures == 0
|
||||
# 清标志:first_read 重置、tuple 清掉
|
||||
assert s._fudai_detail_first_read is False
|
||||
assert s._last_fudai_detail_tuple is None
|
||||
# 任务数量标志被清掉
|
||||
assert s._fudai_detail_task_count_from_stage is None
|
||||
|
||||
def test_attend_failure_returns_true_increments_counter(self):
|
||||
"""attend_choujiang 失败 → 增加连续失败计数 + return True。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_second_open()
|
||||
s._consecutive_attend_failures = 2
|
||||
future_ts = time.time() + 60
|
||||
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang",
|
||||
return_value=False,
|
||||
):
|
||||
result = hd.handle_second_open_branch(s, 0.0, 0.0)
|
||||
assert result is True
|
||||
assert s._consecutive_attend_failures == 3
|
||||
|
||||
def test_attend_exception_returns_false(self):
|
||||
"""attend_choujiang 抛异常 → log + return False(fall through 到正常流程)。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_second_open()
|
||||
future_ts = time.time() + 60
|
||||
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang",
|
||||
side_effect=RuntimeError("boom"),
|
||||
):
|
||||
result = hd.handle_second_open_branch(s, 0.0, 0.0)
|
||||
assert result is False
|
||||
|
||||
def test_sets_current_fudai_future_ts_from_cache(self):
|
||||
"""缓存 detail 有效时设置 self._current_fudai_future_ts。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_second_open()
|
||||
future_ts = time.time() + 60
|
||||
s._last_fudai_detail_tuple = ("X", 0, 0, 0, 100.0, (time.time(), future_ts, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang",
|
||||
return_value=True,
|
||||
):
|
||||
hd.handle_second_open_branch(s, 0.0, 0.0)
|
||||
assert s._current_fudai_future_ts == future_ts
|
||||
|
||||
Reference in new issue
Block a user