refactor(fudai): 抽 run_once_basic 2 段独立逻辑到 run_once_basic_helpers (3233 → 3174)
新增 2 个 helper: - handle_just_recorded_reservation:刚记录预约后强制切换直播间 - handle_skip_stage_diagnosis:0.5 跳过阶段检测分支 新增 5 个单元测试。行为保持不变。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
695a84d8b5
commit
3aca02fae4
3 files changed
+132
-62
No files matched your search
@@ -1754,74 +1754,15 @@ class fudai_analyse:
|
||||
pass
|
||||
|
||||
# 检查是否刚刚记录了预约,如果是,强制切换直播间,避免在同一直播间循环
|
||||
try:
|
||||
just_recorded = getattr(self, "_just_recorded_reservation", False)
|
||||
if just_recorded:
|
||||
self._just_recorded_reservation = False # 清除标志
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 刚刚记录预约,继续在新直播间搜索福袋",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
needswitch_cfg = bool(getattr(self, "needswitch", True))
|
||||
except Exception:
|
||||
needswitch_cfg = True
|
||||
if needswitch_cfg:
|
||||
try:
|
||||
self.swipe_up_to_next_live()
|
||||
except Exception as exc:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 执行上滑切换失败",
|
||||
error=str(exc),
|
||||
error_type=type(exc).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] just_recorded_reservation 触发,但 needswitch=False,未切换",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(记录预约后切换直播间)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
if _ronce_helpers.handle_just_recorded_reservation(self, start_time):
|
||||
return
|
||||
|
||||
# 预约检查耗时记录
|
||||
reservation_elapsed = time.time() - reservation_check_start
|
||||
self._log_kv("预约", f"预约检查完成,耗时: {reservation_elapsed:.3f}s")
|
||||
|
||||
# 0.5. 检查是否需要跳过阶段检测,直接查找福袋(点击参与抽奖后)
|
||||
skip_stage_diagnosis = getattr(
|
||||
self, "_skip_stage_diagnosis_and_find_fudai", False
|
||||
)
|
||||
if skip_stage_diagnosis:
|
||||
try:
|
||||
self._skip_stage_diagnosis_and_find_fudai = False # 清除标志
|
||||
self._log_kv("流程", "点击参与抽奖后,跳过阶段检测,直接查找福袋")
|
||||
except Exception:
|
||||
pass
|
||||
# 直接查找福袋,跳过阶段检测
|
||||
self._handle_live_room_has_or_not_fudai()
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(跳过阶段检测)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
if _ronce_helpers.handle_skip_stage_diagnosis(self, start_time):
|
||||
return
|
||||
|
||||
# 1. 截图 + 阶段诊断
|
||||
|
||||
@@ -891,3 +891,83 @@ def _handle_fansclub_join(self: "fudai_analyse", start_time: float) -> None:
|
||||
"run_once_basic完成(处理粉丝团)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
|
||||
|
||||
def handle_just_recorded_reservation(self: "fudai_analyse", start_time: float) -> bool:
|
||||
"""检查 _just_recorded_reservation 标志,若刚记录了预约则强制切换直播间。
|
||||
|
||||
返回 True 表示已切换并应当 `return`;False 表示未触发,继续后续步骤。
|
||||
"""
|
||||
try:
|
||||
just_recorded = getattr(self, "_just_recorded_reservation", False)
|
||||
if not just_recorded:
|
||||
return False
|
||||
|
||||
self._just_recorded_reservation = False # 清除标志
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 刚刚记录预约,继续在新直播间搜索福袋",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
needswitch_cfg = bool(getattr(self, "needswitch", True))
|
||||
except Exception:
|
||||
needswitch_cfg = True
|
||||
if needswitch_cfg:
|
||||
try:
|
||||
self.swipe_up_to_next_live()
|
||||
except Exception as exc:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 执行上滑切换失败",
|
||||
error=str(exc),
|
||||
error_type=type(exc).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] just_recorded_reservation 触发,但 needswitch=False,未切换",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(记录预约后切换直播间)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def handle_skip_stage_diagnosis(self: "fudai_analyse", start_time: float) -> bool:
|
||||
"""0.5 跳过阶段检测:点击参与抽奖后直接查找福袋。
|
||||
|
||||
返回 True 表示已处理并应当 `return`;False 表示标志未设置,继续后续步骤。
|
||||
"""
|
||||
skip_stage_diagnosis = getattr(
|
||||
self, "_skip_stage_diagnosis_and_find_fudai", False
|
||||
)
|
||||
if not skip_stage_diagnosis:
|
||||
return False
|
||||
|
||||
try:
|
||||
self._skip_stage_diagnosis_and_find_fudai = False
|
||||
self._log_kv("流程", "点击参与抽奖后,跳过阶段检测,直接查找福袋")
|
||||
except Exception:
|
||||
pass
|
||||
self._handle_live_room_has_or_not_fudai()
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(跳过阶段检测)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return True
|
||||
@@ -195,3 +195,52 @@ class TestDispatchStageBranch:
|
||||
s._handle_in_fudai_detail = MagicMock(return_value=None)
|
||||
ronce.dispatch_stage_branch(s, "FUDai_DETAIL", {}, 0.0)
|
||||
s._handle_in_fudai_detail.assert_called_once()
|
||||
|
||||
|
||||
# ============================================================
|
||||
# handle_just_recorded_reservation
|
||||
# ============================================================
|
||||
|
||||
class TestHandleJustRecordedReservation:
|
||||
def test_flag_not_set_returns_false(self):
|
||||
s = _mk_self()
|
||||
s._just_recorded_reservation = False
|
||||
assert ronce.handle_just_recorded_reservation(s, 0.0) is False
|
||||
|
||||
def test_flag_set_with_needswitch_swipes(self):
|
||||
s = _mk_self()
|
||||
s._just_recorded_reservation = True
|
||||
s.needswitch = True
|
||||
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
||||
assert ronce.handle_just_recorded_reservation(s, 0.0) is True
|
||||
# 标志应被清除
|
||||
assert s._just_recorded_reservation is False
|
||||
s.swipe_up_to_next_live.assert_called_once()
|
||||
|
||||
def test_flag_set_without_needswitch_no_swipe(self):
|
||||
s = _mk_self()
|
||||
s._just_recorded_reservation = True
|
||||
s.needswitch = False
|
||||
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
||||
assert ronce.handle_just_recorded_reservation(s, 0.0) is True
|
||||
s.swipe_up_to_next_live.assert_not_called()
|
||||
|
||||
|
||||
# ============================================================
|
||||
# handle_skip_stage_diagnosis
|
||||
# ============================================================
|
||||
|
||||
class TestHandleSkipStageDiagnosis:
|
||||
def test_flag_not_set_returns_false(self):
|
||||
s = _mk_self()
|
||||
s._skip_stage_diagnosis_and_find_fudai = False
|
||||
assert ronce.handle_skip_stage_diagnosis(s, 0.0) is False
|
||||
|
||||
def test_flag_set_finds_fudai_directly(self):
|
||||
s = _mk_self()
|
||||
s._skip_stage_diagnosis_and_find_fudai = True
|
||||
s._handle_live_room_has_or_not_fudai = MagicMock(return_value=None)
|
||||
assert ronce.handle_skip_stage_diagnosis(s, 0.0) is True
|
||||
# 标志应被清除
|
||||
assert s._skip_stage_diagnosis_and_find_fudai is False
|
||||
s._handle_live_room_has_or_not_fudai.assert_called_once()
|
||||
Reference in new issue
Block a user