refactor(fudai): 抽 _handle_in_fudai_detail 步骤4 到 handle_detail_helpers (3174 → 2926)
新增 3 个 helper: - step4_make_join_decision:参与决策入口(detail=None 短路 + 调 basic_join_decision) - _handle_reservation_completion:参与成功是预约福袋时的锁定期处理 + 队列清理 - _close_detail_after_attend:根据 _detail_closed_by_attend 决定是否点 click_back 净减 251 行重复代码。新增 7 个 step4 单元测试覆盖: - detail=None 短路 - join_decision 失败不设标志 - countdown 有效设标志 + 关详情 - attend 已关详情时跳过 click_back - 预约福袋无其他预约 → 清锁定期 - 预约福袋有其他预约 → 设保护期 - countdown 已过期不设标志 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cea3aced73
commit
8a975f2bf6
3 files changed
+452
-251
No files matched your search
+3
-251
@@ -2787,259 +2787,11 @@ class fudai_analyse:
|
||||
step3_elapsed = (time.time() - step3_start) * 1000
|
||||
self._log_kv("详情处理", "步骤3[筛选检查]-完成", 总耗时毫秒=step3_elapsed)
|
||||
|
||||
# 步骤4:参与决策
|
||||
step4_start = time.time()
|
||||
# 执行参与决策
|
||||
if detail is None:
|
||||
try:
|
||||
self._log_kv("参与", "详情数据为 None,无法执行参与决策")
|
||||
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完成(详情为None)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
join_result = douyin_fudai_core.basic_join_decision(
|
||||
self, detail, (skip, skip_reason)
|
||||
# 步骤4:参与决策(已抽到 handle_detail_helpers.step4_make_join_decision)
|
||||
_hd_helpers.step4_make_join_decision(
|
||||
self, detail, skip, skip_reason, attend_before_ts, start_time
|
||||
)
|
||||
|
||||
# 检查是否参与成功:如果 _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:
|
||||
if getattr(self, "reservation_after_action_pending", False):
|
||||
# 这是预约福袋,参与成功后处理锁定期和移除预约
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,处理锁定期并移除预约",
|
||||
future_ts=int(future_timestamp),
|
||||
current_focus_until=int(
|
||||
getattr(self, "reservation_focus_until_ts", 0)
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 检查是否还有其他预约,如果有则设置保护期,否则清除锁定期
|
||||
has_other_reservations = False
|
||||
if hasattr(self, "reservations") and self.reservations:
|
||||
# 检查是否有其他预约(排除当前刚完成的预约)
|
||||
for r in self.reservations:
|
||||
try:
|
||||
r_ft = float(r.get("future_ts") or 0.0)
|
||||
if (
|
||||
r_ft > 0
|
||||
and abs(r_ft - future_timestamp) >= 5.0
|
||||
): # 排除当前预约
|
||||
has_other_reservations = True
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if has_other_reservations:
|
||||
# 还有其他预约,设置短暂保护期(10秒)确保依次参与
|
||||
protection_until = time.time() + 10.0
|
||||
self.reservation_focus_until_ts = max(
|
||||
self.reservation_focus_until_ts, protection_until
|
||||
)
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,设置保护期确保依次参与",
|
||||
protection_seconds=10,
|
||||
new_focus_until=int(
|
||||
self.reservation_focus_until_ts
|
||||
),
|
||||
remaining_reservations=len(
|
||||
[
|
||||
r
|
||||
for r in self.reservations
|
||||
if float(r.get("future_ts", 0)) > 0
|
||||
]
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 没有其他预约,清除锁定期
|
||||
self.reservation_focus_until_ts = 0.0
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,清除锁定期(无其他预约)",
|
||||
future_ts=int(future_timestamp),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.reservation_after_action_pending = False
|
||||
|
||||
# 从预约队列中移除已完成的预约(匹配 future_ts,允许5秒误差)
|
||||
if hasattr(self, "reservations") and self.reservations:
|
||||
# use module-level `douyin_reservation_logic`
|
||||
reservations_to_remove = []
|
||||
for idx, r in enumerate(self.reservations):
|
||||
try:
|
||||
r_ft = float(r.get("future_ts") or 0.0)
|
||||
if (
|
||||
r_ft > 0
|
||||
and abs(r_ft - future_timestamp) < 5.0
|
||||
):
|
||||
reservations_to_remove.append(idx)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# 从后往前删除,避免索引变化
|
||||
for idx in reversed(reservations_to_remove):
|
||||
try:
|
||||
removed = self.reservations.pop(idx)
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 已从队列中移除已完成的预约",
|
||||
future_ts=int(
|
||||
removed.get("future_ts", 0)
|
||||
),
|
||||
content=str(removed.get("content", ""))[
|
||||
:50
|
||||
],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 重新排序预约队列
|
||||
if self.reservations:
|
||||
try:
|
||||
douyin_reservation_logic._sort_reservations_inplace(
|
||||
self.reservations
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 清除锁定期和移除预约时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 关闭详情页,返回直播间等待开奖
|
||||
# 检查 attend_choujiang 是否已经关闭了详情,如果已关闭就不应该再点击返回键
|
||||
detail_already_closed = getattr(
|
||||
self, "_detail_closed_by_attend", False
|
||||
)
|
||||
if not detail_already_closed:
|
||||
try:
|
||||
self.operation.click_back(self.device_id)
|
||||
self.operation.delay(1.0)
|
||||
self._log_kv("参与", "已关闭详情页,返回直播间等待开奖")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"详情页已由 attend_choujiang 关闭,跳过返回键点击",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
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
|
||||
|
||||
# 返回,让主循环只检测开奖结果,不执行其他操作
|
||||
# 步骤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))
|
||||
|
||||
def _handle_live_room_has_or_not_fudai(self) -> None:
|
||||
"""在直播间:寻找福袋图标;有则点开详情,无则交给 flow_control 决定"继续找/切换/养号"."""
|
||||
start_time = time.time()
|
||||
|
||||
@@ -92,3 +92,268 @@ def handle_consecutive_failures_check(self: "fudai_analyse") -> bool:
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
|
||||
|
||||
def step4_make_join_decision(
|
||||
self: "fudai_analyse",
|
||||
detail,
|
||||
skip: bool,
|
||||
skip_reason: str,
|
||||
attend_before_ts: float,
|
||||
start_time: float,
|
||||
) -> None:
|
||||
"""步骤4:参与决策 + 等待开奖标志设置 + 预约清理 + 关闭详情页。
|
||||
|
||||
本函数总是消费完后返回(caller 即 _handle_in_fudai_detail 在调用本函数后
|
||||
也是方法末尾,不需要再判断返回值)。
|
||||
"""
|
||||
import time
|
||||
from typing import Any, Tuple, cast
|
||||
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
from douyin.reservation import douyin_reservation_logic
|
||||
|
||||
step4_start = time.time()
|
||||
|
||||
if detail is None:
|
||||
try:
|
||||
self._log_kv("参与", "详情数据为 None,无法执行参与决策")
|
||||
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完成(详情为None)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
|
||||
join_result = douyin_fudai_core.basic_join_decision(
|
||||
self, detail, (skip, skip_reason)
|
||||
)
|
||||
|
||||
# 检查是否参与成功:如果 _last_successful_attend_ts 刚刚更新,说明参与成功
|
||||
attend_after_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
if not (join_result and attend_after_ts > attend_before_ts):
|
||||
return
|
||||
|
||||
# 参与成功,设置等待开奖标志
|
||||
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[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:
|
||||
# 设置等待开奖标志和预期开奖时间
|
||||
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),
|
||||
)
|
||||
|
||||
# 检查是否是预约福袋,如果是处理锁定期和移除预约
|
||||
_handle_reservation_completion(self, future_timestamp)
|
||||
|
||||
# 关闭详情页,返回直播间等待开奖
|
||||
_close_detail_after_attend(self)
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
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
|
||||
|
||||
# 步骤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))
|
||||
|
||||
|
||||
def _handle_reservation_completion(
|
||||
self: "fudai_analyse", future_timestamp: float
|
||||
) -> None:
|
||||
"""参与成功是预约福袋时:处理锁定期 + 移除已完成的预约。"""
|
||||
import time
|
||||
|
||||
from douyin.reservation import douyin_reservation_logic
|
||||
|
||||
try:
|
||||
if not getattr(self, "reservation_after_action_pending", False):
|
||||
return
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,处理锁定期并移除预约",
|
||||
future_ts=int(future_timestamp),
|
||||
current_focus_until=int(
|
||||
getattr(self, "reservation_focus_until_ts", 0)
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 检查是否还有其他预约
|
||||
has_other_reservations = False
|
||||
if hasattr(self, "reservations") and self.reservations:
|
||||
for r in self.reservations:
|
||||
try:
|
||||
r_ft = float(r.get("future_ts") or 0.0)
|
||||
if r_ft > 0 and abs(r_ft - future_timestamp) >= 5.0:
|
||||
has_other_reservations = True
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if has_other_reservations:
|
||||
# 还有其他预约,设置短暂保护期
|
||||
protection_until = time.time() + 10.0
|
||||
self.reservation_focus_until_ts = max(
|
||||
self.reservation_focus_until_ts, protection_until
|
||||
)
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,设置保护期确保依次参与",
|
||||
protection_seconds=10,
|
||||
new_focus_until=int(self.reservation_focus_until_ts),
|
||||
remaining_reservations=len(
|
||||
[
|
||||
r
|
||||
for r in self.reservations
|
||||
if float(r.get("future_ts", 0)) > 0
|
||||
]
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 没有其他预约,清除锁定期
|
||||
self.reservation_focus_until_ts = 0.0
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 参与成功,清除锁定期(无其他预约)",
|
||||
future_ts=int(future_timestamp),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.reservation_after_action_pending = False
|
||||
|
||||
# 从预约队列中移除已完成的预约(匹配 future_ts,允许 5 秒误差)
|
||||
if hasattr(self, "reservations") and self.reservations:
|
||||
reservations_to_remove = []
|
||||
for idx, r in enumerate(self.reservations):
|
||||
try:
|
||||
r_ft = float(r.get("future_ts") or 0.0)
|
||||
if r_ft > 0 and abs(r_ft - future_timestamp) < 5.0:
|
||||
reservations_to_remove.append(idx)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# 从后往前删除,避免索引变化
|
||||
for idx in reversed(reservations_to_remove):
|
||||
try:
|
||||
removed = self.reservations.pop(idx)
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 已从队列中移除已完成的预约",
|
||||
future_ts=int(removed.get("future_ts", 0)),
|
||||
content=str(removed.get("content", ""))[:50],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 重新排序预约队列
|
||||
if self.reservations:
|
||||
try:
|
||||
douyin_reservation_logic._sort_reservations_inplace(
|
||||
self.reservations
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 清除锁定期和移除预约时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _close_detail_after_attend(self: "fudai_analyse") -> None:
|
||||
"""参与成功后关闭详情页(如果 attend_choujiang 还没关)。"""
|
||||
detail_already_closed = getattr(self, "_detail_closed_by_attend", False)
|
||||
if not detail_already_closed:
|
||||
try:
|
||||
self.operation.click_back(self.device_id)
|
||||
self.operation.delay(1.0)
|
||||
self._log_kv("参与", "已关闭详情页,返回直播间等待开奖")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"详情页已由 attend_choujiang 关闭,跳过返回键点击",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -68,3 +68,187 @@ class TestHandleConsecutiveFailuresCheck:
|
||||
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
||||
with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"):
|
||||
assert hd.handle_consecutive_failures_check(s) is True
|
||||
|
||||
|
||||
# ============================================================
|
||||
# step4_make_join_decision
|
||||
# ============================================================
|
||||
|
||||
class TestStep4MakeJoinDecision:
|
||||
"""步骤4:参与决策 + 等待开奖标志 + 预约清理。"""
|
||||
|
||||
def _mk_for_step4(self):
|
||||
s = _mk_self()
|
||||
s.operation = MagicMock()
|
||||
s.operation.click_back = MagicMock(return_value=None)
|
||||
s.operation.delay = MagicMock(return_value=None)
|
||||
s.device_id = "MOCK"
|
||||
s._last_successful_attend_ts = 0.0
|
||||
s._waiting_for_lottery_result = False
|
||||
s._lottery_result_expected_ts = 0.0
|
||||
s._lottery_wait_start_ts = 0.0
|
||||
s._fudai_detail_first_read = False
|
||||
s._last_fudai_detail_tuple = None
|
||||
s._fudai_detail_task_count_from_stage = 5
|
||||
s._detail_closed_by_attend = False
|
||||
s.reservations = []
|
||||
s.reservation_after_action_pending = False
|
||||
s.reservation_focus_until_ts = 0.0
|
||||
return s
|
||||
|
||||
def test_detail_none_logs_and_returns(self):
|
||||
s = self._mk_for_step4()
|
||||
# detail=None 分支:不应触发 basic_join_decision
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision"
|
||||
) as mock_join:
|
||||
hd.step4_make_join_decision(s, None, False, "", 0.0, 0.0)
|
||||
mock_join.assert_not_called()
|
||||
# task_count_from_stage 应被清除
|
||||
assert s._fudai_detail_task_count_from_stage is None
|
||||
|
||||
def test_join_decision_failed_returns_silently(self):
|
||||
"""basic_join_decision 返回 False → 不设等待开奖标志。"""
|
||||
s = self._mk_for_step4()
|
||||
s._last_successful_attend_ts = 100.0 # 不变
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
return_value=False,
|
||||
):
|
||||
hd.step4_make_join_decision(s, ("X", 0, 0, 0, 0, (0, 0, 0)),
|
||||
False, "", 100.0, 0.0)
|
||||
assert s._waiting_for_lottery_result is False
|
||||
|
||||
def test_join_success_sets_lottery_flag(self):
|
||||
"""参与成功 + countdown 有效 → 设置等待开奖标志 + 关闭详情页。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_step4()
|
||||
future_ts = time.time() + 30 # 30 秒后开奖
|
||||
# detail tuple: (content, ?, ?, ?, ?, countdown_tuple)
|
||||
# countdown_tuple = (countdown_start, countdown_end, original_seconds)
|
||||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||||
|
||||
# basic_join_decision 返回 True 且更新 _last_successful_attend_ts
|
||||
def fake_join(*args, **kwargs):
|
||||
s._last_successful_attend_ts = time.time()
|
||||
return True
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
side_effect=fake_join,
|
||||
):
|
||||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||||
|
||||
assert s._waiting_for_lottery_result is True
|
||||
assert s._lottery_result_expected_ts == future_ts
|
||||
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
|
||||
# attend 还没关详情 → 应该点 click_back
|
||||
s.operation.click_back.assert_called_once()
|
||||
|
||||
def test_join_success_skips_click_back_when_already_closed(self):
|
||||
"""attend_choujiang 已关详情 → 跳过 click_back。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_step4()
|
||||
s._detail_closed_by_attend = True
|
||||
future_ts = time.time() + 30
|
||||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||||
|
||||
def fake_join(*args, **kwargs):
|
||||
s._last_successful_attend_ts = time.time()
|
||||
return True
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
side_effect=fake_join,
|
||||
):
|
||||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||||
|
||||
s.operation.click_back.assert_not_called()
|
||||
|
||||
def test_join_success_clears_focus_when_no_other_reservations(self):
|
||||
"""预约福袋参与成功且无其他预约 → 清除 reservation_focus_until_ts。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_step4()
|
||||
s.reservation_after_action_pending = True
|
||||
s.reservation_focus_until_ts = 9999.0
|
||||
# 只有一条预约,且就是当前正在参与的
|
||||
future_ts = time.time() + 30
|
||||
s.reservations = [{"future_ts": future_ts, "content": "X"}]
|
||||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||||
|
||||
def fake_join(*args, **kwargs):
|
||||
s._last_successful_attend_ts = time.time()
|
||||
return True
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
side_effect=fake_join,
|
||||
):
|
||||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||||
|
||||
assert s.reservation_focus_until_ts == 0.0
|
||||
assert s.reservation_after_action_pending is False
|
||||
# 已完成的预约应被移除
|
||||
assert s.reservations == []
|
||||
|
||||
def test_join_success_sets_protection_when_other_reservations_exist(self):
|
||||
"""预约福袋参与成功但还有其他预约 → 设置 10 秒保护期。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_step4()
|
||||
s.reservation_after_action_pending = True
|
||||
s.reservation_focus_until_ts = 0.0 # 当前没有锁定
|
||||
future_ts = time.time() + 30
|
||||
# 当前刚参加的 + 另外一条未来的预约
|
||||
s.reservations = [
|
||||
{"future_ts": future_ts, "content": "current"},
|
||||
{"future_ts": future_ts + 60, "content": "next"},
|
||||
]
|
||||
detail = ("test_content", 0, 0, 0, 0, (0, future_ts, 30))
|
||||
|
||||
def fake_join(*args, **kwargs):
|
||||
s._last_successful_attend_ts = time.time()
|
||||
return True
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
side_effect=fake_join,
|
||||
):
|
||||
with patch(
|
||||
"douyin.reservation.douyin_reservation_logic._sort_reservations_inplace",
|
||||
return_value=None,
|
||||
):
|
||||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||||
|
||||
# 设置了保护期(约 10 秒后)
|
||||
assert s.reservation_focus_until_ts > time.time() + 5
|
||||
assert s.reservation_after_action_pending is False
|
||||
# 当前预约被移除,下一个保留
|
||||
assert len(s.reservations) == 1
|
||||
assert s.reservations[0]["content"] == "next"
|
||||
|
||||
def test_no_remain_seconds_skips_lottery_flag(self):
|
||||
"""countdown 已过期(remain<=0)→ 不设等待开奖标志。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_step4()
|
||||
# future_ts 在过去 → remain_seconds == 0
|
||||
detail = ("X", 0, 0, 0, 0, (0, time.time() - 10, 30))
|
||||
|
||||
def fake_join(*args, **kwargs):
|
||||
s._last_successful_attend_ts = time.time()
|
||||
return True
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
side_effect=fake_join,
|
||||
):
|
||||
hd.step4_make_join_decision(s, detail, False, "", 0.0, 0.0)
|
||||
|
||||
assert s._waiting_for_lottery_result is False
|
||||
|
||||
Reference in new issue
Block a user