"""\ 动态模拟:验证预约回退 preempted=True 后,run_once_basic 不会提前退出。 不连真机/不跑 ADB: - monkeypatch `douyin.reservation.douyin_reservation_logic.process_reservation_attend_window` 令其返回 True(模拟已触发回退/抢占)。 - 给 analyser 注入一个 FakeOperation,在后续流程的截图阶段抛出哨兵异常。 判定标准: - 若能捕获到“进入截图阶段”的哨兵异常 => 说明 preempted=True 后仍继续执行到后续流程(PASS) - 若 run_once_basic 正常 return 或在更早阶段异常退出 => FAIL 运行: .venv/Scripts/python.exe tools/simulate_reservation_preempted_flow_dynamic.py """ from __future__ import annotations import os import sys import time import traceback from pathlib import Path class _ReachedScreenshot(Exception): pass class _FakeOperation: def get_screenshot(self, *_args, **_kwargs): raise _ReachedScreenshot("reached operation.get_screenshot") def delay(self, _s: float) -> None: return def get_top_package(self, _device_id: str) -> str: return "com.ss.android.ugc.aweme" def main() -> None: # 避免导入底层 OCR 时的初始化开销 os.environ.setdefault("SKIP_OCR_INIT", "1") # 确保能导入本仓库代码(避免因工作目录不同导致 ModuleNotFoundError) repo_root = Path(__file__).resolve().parents[1] if str(repo_root) not in sys.path: sys.path.insert(0, str(repo_root)) # 1) 导入 analyser from douyin.core.douyin_fudai import fudai_analyse # noqa: WPS433 # 2) monkeypatch reservation 回退逻辑:强制 preempted=True from douyin.reservation import douyin_reservation_logic # noqa: WPS433 original_fn = douyin_reservation_logic.process_reservation_attend_window def _fake_process_reservation_attend_window( analyser, reservations, room_pos, reservation_focus_until_ts_ref, reservation_after_action_pending_ref, **_kwargs, ) -> bool: # 模拟:已经回退成功并进入锁定期 now = time.time() if reservation_focus_until_ts_ref: reservation_focus_until_ts_ref[0] = now + 60.0 if reservation_after_action_pending_ref: reservation_after_action_pending_ref[0] = True return True douyin_reservation_logic.process_reservation_attend_window = ( _fake_process_reservation_attend_window ) try: # 3) 构造一个尽量“瘦”的 analyser(避免 __init__ 做太多事) analyser = fudai_analyse.__new__(fudai_analyse) # 必要字段:日志、配置、状态 analyser._log_kv = lambda *_a, **_k: None analyser.operation = _FakeOperation() analyser.device_id = "FAKE_DEVICE" analyser._waiting_for_lottery_result = False analyser._lottery_result_expected_ts = 0.0 # 防止 30 分钟超时分支触发复杂自救 analyser._last_successful_attend_ts = time.time() analyser._no_attend_timeout_minutes = 30.0 analyser._run_start_ts = time.time() # 预约相关 analyser.high_value_fudai_enabled = True analyser.needswitch = True analyser.room_pos = 7 analyser.reservation_back_swipe_delay = 0.6 analyser.reservations = [ { "future_ts": time.time() + 120.0, "prize_value_yuan": 999.0, "pos_seen": 4, "content": "", "room_title": "", "room_title_raw": "", } ] analyser._first_run_once_basic = False analyser._skip_stage_diagnosis_and_find_fudai = False # 4) 执行:若 preempted=True 后继续,必然会到截图阶段并触发哨兵异常 try: analyser.run_once_basic() except _ReachedScreenshot: print("PASS: preempted=True did not early-return; reached screenshot stage") return print( "FAIL: run_once_basic returned before reaching screenshot stage " "(likely early-return or unexpected short-circuit)" ) raise SystemExit(1) except Exception as exc: print(f"FAIL: unexpected exception: {type(exc).__name__}: {exc}") traceback.print_exc() raise SystemExit(2) finally: # 恢复 monkeypatch douyin_reservation_logic.process_reservation_attend_window = original_fn if __name__ == "__main__": main()