From cdcabb665d94c135e57b3d1f38da00533f213236 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 03:24:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=E6=AD=A5?= =?UTF-8?q?=E9=AA=A44=E5=8F=82=E4=B8=8E=E6=88=90=E5=8A=9F=E5=96=84?= =?UTF-8?q?=E5=90=8E=E5=A4=84=E7=90=86=EF=BC=881939=E2=86=921879=EF=BC=8C-?= =?UTF-8?q?60=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle_attend_success_post_processing:参与决策成功后从 detail 提取倒计时、 设置等待开奖标志、调用预约善后/关闭详情/重置状态、写步骤4完成日志。 新增 5 个单元测试覆盖:join_result=False / 时间戳无变化 / 正常路径 / remain_seconds=0 / detail 异常吞噬。1167 → 1172 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 68 +--------------- .../core/fudai_modules/fudai_detail_blocks.py | 71 +++++++++++++++++ tests/test_fudai_detail_blocks.py | 77 +++++++++++++++++++ 3 files changed, 152 insertions(+), 64 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 120bd3c..ea1862c 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1786,70 +1786,10 @@ class fudai_analyse: ) # 检查是否参与成功:如果 _last_successful_attend_ts 刚刚更新,说明参与成功 - attend_after_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0 - if join_result and attend_after_ts > attend_before_ts: - # 参与成功,设置等待开奖标志,停止其他行为 - try: - # 从 detail 中提取倒计时信息 - countdown_tuple = None - remain_seconds = 0 - future_timestamp = 0.0 - 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) and len(countdown_tuple) >= 2: - # countdown_tuple[0] 是 countdown_start(开始时间戳) - # countdown_tuple[1] 是 countdown_end(结束时间戳/开奖时间戳) - future_timestamp = float(countdown_tuple[1] or 0.0) - # 计算剩余秒数:结束时间戳 - 当前时间 - current_time = time.time() - if future_timestamp > current_time: - remain_seconds = int(future_timestamp - current_time) - - if remain_seconds > 0: - # 设置等待开奖标志和预期开奖时间 - # 直接保存开奖时间戳,等待时直接用 future_timestamp - time.time() 计算剩余秒数 - self._waiting_for_lottery_result = True - self._lottery_result_expected_ts = future_timestamp - self._lottery_wait_start_ts = time.time() - - self._log_kv( - "参与", - "参与成功,设置等待开奖标志,停止其他行为", - remain_seconds=remain_seconds, - future_ts=int(future_timestamp), - ) - - # 检查是否是预约福袋,如果是,处理锁定期和移除预约 - try: - fudai_detail_blocks.handle_reservation_post_attend( - self, future_timestamp - ) - except Exception as e: - try: - self._log_kv( - "预约", - "[预约] 清除锁定期和移除预约时出错", - error=str(e), - ) - except Exception: - pass - - # 关闭详情页 + 重置详情状态 - fudai_detail_blocks.close_detail_after_successful_attend(self) - fudai_detail_blocks.reset_fudai_detail_state_after_attend(self) - - # 返回,让主循环只检测开奖结果,不执行其他操作 - # 步骤4结束计时 - step4_elapsed = (time.time() - step4_start) * 1000 - self._log_kv( - "详情处理", "步骤4[参与决策]-完成", 总耗时毫秒=step4_elapsed - ) - return - else: - self._log_kv("参与", "参与成功但无法获取倒计时,跳过等待") - except Exception as e: - self._log_kv("参与", "设置等待开奖标志时出错", error=str(e)) + if fudai_detail_blocks.handle_attend_success_post_processing( + self, detail, attend_before_ts, start_time, step4_start, join_result + ): + return def _handle_live_room_has_or_not_fudai(self) -> None: """在直播间:寻找福袋图标;有则点开详情,无则交给 flow_control 决定"继续找/切换/养号".""" diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index c05ea4d..b4beb09 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -10,6 +10,77 @@ import time from typing import Any +def handle_attend_success_post_processing( + analyser: Any, + detail: Any, + attend_before_ts: float, + start_time: float, + step4_start: float, + join_result: bool, +) -> bool: + """步骤4 参与决策成功后的善后处理。 + + 返回 True 表示参与成功且主流程应 return,False 表示未触发善后路径。 + """ + attend_after_ts = getattr(analyser, "_last_successful_attend_ts", 0.0) or 0.0 + if not (join_result and attend_after_ts > attend_before_ts): + return False + + try: + countdown_tuple = None + remain_seconds = 0 + future_timestamp = 0.0 + if isinstance(detail, tuple) and len(detail) >= 6: + countdown_tuple = detail[5] + if isinstance(countdown_tuple, tuple) and len(countdown_tuple) >= 2: + future_timestamp = float(countdown_tuple[1] or 0.0) + current_time = time.time() + if future_timestamp > current_time: + remain_seconds = int(future_timestamp - current_time) + + if remain_seconds > 0: + analyser._waiting_for_lottery_result = True + analyser._lottery_result_expected_ts = future_timestamp + analyser._lottery_wait_start_ts = time.time() + + analyser._log_kv( + "参与", + "参与成功,设置等待开奖标志,停止其他行为", + remain_seconds=remain_seconds, + future_ts=int(future_timestamp), + ) + + try: + handle_reservation_post_attend(analyser, future_timestamp) + except Exception as e: + try: + analyser._log_kv( + "预约", + "[预约] 清除锁定期和移除预约时出错", + error=str(e), + ) + except Exception: + pass + + close_detail_after_successful_attend(analyser) + reset_fudai_detail_state_after_attend(analyser) + + step4_elapsed = (time.time() - step4_start) * 1000 + analyser._log_kv( + "详情处理", "步骤4[参与决策]-完成", 总耗时毫秒=step4_elapsed + ) + return True + + analyser._log_kv("参与", "参与成功但无法获取倒计时,跳过等待") + return False + except Exception as e: + try: + analyser._log_kv("参与", "设置等待开奖标志时出错", error=str(e)) + except Exception: + pass + return False + + def handle_high_value_skip_decide( analyser: Any, detail: Any, skip_reason: str, start_time: float ) -> tuple[bool, bool]: diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index b8d420e..8ad60ab 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -624,3 +624,80 @@ def test_high_value_skip_outer_exception_does_not_crash(): ) assert should_return is False assert new_skip is True + + +# ---- handle_attend_success_post_processing ---- # + +def test_attend_success_join_result_false_returns_false(): + """join_result=False → 不触发善后路径""" + a = MagicMock() + a._last_successful_attend_ts = _time.time() + detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100)) + result = fudai_detail_blocks.handle_attend_success_post_processing( + a, detail, attend_before_ts=0.0, start_time=_time.time(), + step4_start=_time.time(), join_result=False, + ) + assert result is False + + +def test_attend_success_no_attend_ts_change_returns_false(): + """_last_successful_attend_ts 没变化 → 不触发善后""" + now = _time.time() + a = MagicMock() + a._last_successful_attend_ts = now # 与 attend_before_ts 相同 + detail = ("c", "x", "x", "x", 5.0, ("s", now + 100, 100)) + result = fudai_detail_blocks.handle_attend_success_post_processing( + a, detail, attend_before_ts=now, start_time=now, + step4_start=now, join_result=True, + ) + assert result is False + + +def test_attend_success_with_remaining_seconds_sets_waiting_flag(): + """剩余秒数 > 0 → 设置等待开奖标志,return True""" + now = _time.time() + a = MagicMock() + a._last_successful_attend_ts = now + 1 + a.reservation_after_action_pending = False + a._detail_closed_by_attend = False + a._fudai_detail_first_read = True + a._last_fudai_detail_tuple = ("x",) + a._fudai_detail_task_count_from_stage = 5 + detail = ("c", "x", "x", "x", 5.0, (now - 50, now + 60, 110)) # 剩余 60s + result = fudai_detail_blocks.handle_attend_success_post_processing( + a, detail, attend_before_ts=now, start_time=now, + step4_start=now, join_result=True, + ) + assert result is True + assert a._waiting_for_lottery_result is True + assert a._lottery_result_expected_ts > 0 + # 重置详情状态 + assert a._fudai_detail_first_read is False + assert a._last_fudai_detail_tuple is None + + +def test_attend_success_no_remaining_seconds_returns_false(): + """remain_seconds == 0 时只记录日志、不设置标志,返回 False 让主流程继续""" + now = _time.time() + a = MagicMock() + a._last_successful_attend_ts = now + 1 + detail = ("c", "x", "x", "x", 5.0, (now - 200, now - 100, 100)) # 已过期 + result = fudai_detail_blocks.handle_attend_success_post_processing( + a, detail, attend_before_ts=now, start_time=now, + step4_start=now, join_result=True, + ) + assert result is False + + +def test_attend_success_swallows_exception(): + """detail 处理异常被吞噬""" + now = _time.time() + a = MagicMock() + a._last_successful_attend_ts = now + 1 + detail = "not a tuple" # 让 isinstance 检查不通过,countdown_tuple 保持 None + result = fudai_detail_blocks.handle_attend_success_post_processing( + a, detail, attend_before_ts=now, start_time=now, + step4_start=now, join_result=True, + ) + # remain_seconds=0,应该返回 False + assert result is False