diff --git a/douyin/fudai/core/fudai_attend_pkg/fansclub_recovery.py b/douyin/fudai/core/fudai_attend_pkg/fansclub_recovery.py index a9a0f8d..64aacb3 100644 --- a/douyin/fudai/core/fudai_attend_pkg/fansclub_recovery.py +++ b/douyin/fudai/core/fudai_attend_pkg/fansclub_recovery.py @@ -3,7 +3,6 @@ from __future__ import annotations import os -import time from typing import Any from douyin.fudai.core.coords import ( @@ -33,30 +32,30 @@ def recover_to_fudai_detail_after_fansclub( if st == "FUDai_DETAIL": try: - analyser._last_successful_attend_ts = time.time() + analyser.click(*DETAIL_OUTSIDE_CLICK) analyser._log_kv( "参与", - "粉丝团加入后仍在详情页,交由外层确认已参与并设置等待开奖", + "粉丝团加入后仍在详情页,先退出详情后继续寻找福袋", stage=st, seen=seen, attempt=i + 1, + action="close_detail_after_fansclub", ) except Exception: pass - return True + return False if st in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"): try: - analyser._last_successful_attend_ts = time.time() analyser._log_kv( "参与", - "粉丝团加入后回到直播间,标记参与成功并等待外层设置开奖保护", + "粉丝团加入后回到直播间,继续寻找福袋", stage=st, seen=seen, attempt=i + 1, ) except Exception: pass - return True + return False if st == "FANSCLUB_JOIN": analyser._log_kv( @@ -339,4 +338,4 @@ def recover_to_fudai_detail_after_fansclub( analyser.operation.delay(1.0) except Exception: pass - return True + return False diff --git a/tests/test_fansclub_handler.py b/tests/test_fansclub_handler.py index 94bcb06..f358bda 100644 --- a/tests/test_fansclub_handler.py +++ b/tests/test_fansclub_handler.py @@ -1,10 +1,10 @@ """douyin/fudai/core/fudai_attend_pkg/fansclub_handler 单元测试。 主要验证: -- handle_fansclub_branch 返回 True +- handle_fansclub_branch 会处理粉丝团门槛并返回是否已经参与 - 设置 analyser.last_attend_fail_reason = "fansclub_required" - 在路径上点击参与按钮 (543, 2070) -- 粉丝团加入后不立即关闭详情,等待恢复/确认逻辑设置开奖保护 +- 普通粉丝团加入后不伪造参与成功;若仍在详情页,先退出详情再继续找福袋 - 异常路径不抛 """ from __future__ import annotations @@ -19,7 +19,7 @@ from douyin.fudai.core.fudai_attend_pkg.fansclub_handler import ( ) -def _mk_analyser(*, auto_join_fansclub: bool = False): +def _mk_analyser(*, auto_join_fansclub: bool = False, diag_stage: str = "FUDai_DETAIL"): """构造一个 attend 流程能跑通的 mock analyser。""" a = MagicMock() a.last_attend_fail_reason = "" @@ -46,7 +46,7 @@ def _mk_analyser(*, auto_join_fansclub: bool = False): a.click = MagicMock(return_value=None) a._log_kv = MagicMock(return_value=None) def _diag(*_args, **_kwargs): - a._last_diag_stage = "FUDai_DETAIL" + a._last_diag_stage = diag_stage a._last_diag_seen = "mock_detail" a._shot_and_diagnose = MagicMock(side_effect=_diag) @@ -54,10 +54,10 @@ def _mk_analyser(*, auto_join_fansclub: bool = False): class TestHandleFansclubBranch: - def test_returns_true(self): + def test_returns_false_after_closing_detail_for_normal_flow(self): a = _mk_analyser() result = handle_fansclub_branch(a, "加入粉丝团") - assert result is True + assert result is False def test_sets_fail_reason(self): a = _mk_analyser() @@ -74,34 +74,45 @@ class TestHandleFansclubBranch: ] assert len(attend_calls) >= 1 - def test_does_not_close_detail_before_success_confirmation(self): - """粉丝团点击后不能立刻点空白关闭详情,避免已参与但未设置等待开奖保护。""" + def test_closes_detail_after_recovery_confirms_still_on_detail(self): + """普通粉丝团路径不算参与成功;若仍在详情页,先退出详情再继续找福袋。""" a = _mk_analyser() handle_fansclub_branch(a, "加入粉丝团") close_calls = [ c for c in a.click.call_args_list if c.args == (500, 470) ] - assert close_calls == [] + assert len(close_calls) == 1 - def test_marks_success_when_recovery_sees_detail_page(self): + def test_does_not_mark_success_when_recovery_sees_detail_page(self): a = _mk_analyser() handle_fansclub_branch(a, "加入粉丝团") - assert float(a._last_successful_attend_ts or 0.0) > 0.0 + assert "_last_successful_attend_ts" not in a.__dict__ + + def test_returns_false_without_close_when_recovery_is_already_live_room(self): + a = _mk_analyser(diag_stage="IN_LIVE_ROOM") + + result = handle_fansclub_branch(a, "加入粉丝团") + + assert result is False + close_calls = [ + c for c in a.click.call_args_list if c.args == (500, 470) + ] + assert close_calls == [] def test_handles_粉丝团_text_only(self): """attend_button_text 含 "粉丝团" 但不含 "加入" 也走该分支。""" a = _mk_analyser() result = handle_fansclub_branch(a, "粉丝团门槛") - assert result is True + assert result is False def test_does_not_raise_when_screenshot_missing(self): """path_config.get_screenshot_path 不存在时不应抛。""" a = _mk_analyser() with patch("os.path.exists", return_value=False): result = handle_fansclub_branch(a, "加入粉丝团") - assert result is True + assert result is False def test_does_not_raise_when_ocr_throws(self): """OCR 全部抛异常时仍正常返回 True(不传播)。""" @@ -110,13 +121,13 @@ class TestHandleFansclubBranch: a.operation.ocr.ocr = MagicMock(side_effect=RuntimeError("ocr fail")) a.operation.ocr.readtext = MagicMock(side_effect=RuntimeError("ocr fail")) result = handle_fansclub_branch(a, "加入粉丝团") - assert result is True + assert result is False def test_auto_join_disabled_does_not_wait_long(self): """未启用自动加入时也走完整流程,返回 True。""" a = _mk_analyser(auto_join_fansclub=False) result = handle_fansclub_branch(a, "加入粉丝团") - assert result is True + assert result is False # auto_join 关闭时应当走"等待手动确认"日志 log_msgs = [str(c) for c in a._log_kv.call_args_list] assert any("未启用自动加粉丝团" in m for m in log_msgs) @@ -124,7 +135,7 @@ class TestHandleFansclubBranch: def test_auto_join_enabled(self): a = _mk_analyser(auto_join_fansclub=True) result = handle_fansclub_branch(a, "加入粉丝团") - assert result is True + assert result is False class TestReservationPrejoinFansclub: