fix: speed up fansclub reservation prejoin
This commit is contained in:
1 parent
0efdc83d0b
commit
a449b7634c
15 files changed
+312
-31
No files matched your search
@@ -289,6 +289,25 @@ class TestFudaiDetectionDefaults:
|
||||
assert analyser.reservation_direct_attend_max_failures == 3
|
||||
assert analyser.reservation_direct_attend_open_max_wait_seconds == 2.25
|
||||
|
||||
def test_config_loader_loads_reservation_prejoin_fansclub_delays(self):
|
||||
from douyin.utils.douyin_config import ConfigManager
|
||||
|
||||
analyser = SimpleNamespace()
|
||||
config = {
|
||||
"reservation_prejoin_fansclub_participate_wait_seconds": 0.25,
|
||||
"reservation_prejoin_fansclub_confirm_wait_seconds": 0.35,
|
||||
}
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as f:
|
||||
json.dump(config, f)
|
||||
tmp_path = f.name
|
||||
try:
|
||||
ConfigManager.load_config_from_file(analyser, tmp_path)
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
assert analyser.reservation_prejoin_fansclub_participate_wait_seconds == 0.25
|
||||
assert analyser.reservation_prejoin_fansclub_confirm_wait_seconds == 0.35
|
||||
|
||||
|
||||
class TestCoreModules:
|
||||
"""core 子模块存在性测试。"""
|
||||
|
||||
@@ -15,6 +15,7 @@ import pytest
|
||||
|
||||
from douyin.fudai.core.fudai_attend_pkg.fansclub_handler import (
|
||||
handle_fansclub_branch,
|
||||
handle_reservation_prejoin_fansclub,
|
||||
)
|
||||
|
||||
|
||||
@@ -113,3 +114,31 @@ class TestHandleFansclubBranch:
|
||||
a = _mk_analyser(auto_join_fansclub=True)
|
||||
result = handle_fansclub_branch(a, "加入粉丝团")
|
||||
assert result is True
|
||||
|
||||
|
||||
class TestReservationPrejoinFansclub:
|
||||
def test_fast_prejoin_uses_fixed_clicks_without_ocr_or_recovery(self):
|
||||
a = _mk_analyser(auto_join_fansclub=True)
|
||||
|
||||
result = handle_reservation_prejoin_fansclub(a, "加入直播粉丝团并关注主播")
|
||||
|
||||
assert result is True
|
||||
assert a.last_attend_fail_reason == "fansclub_required"
|
||||
assert any(c.args == (543, 2070) for c in a.click.call_args_list)
|
||||
assert any(c.args == (500, 470) for c in a.click.call_args_list)
|
||||
assert any(c.args == ("MOCK", 540, 2006) for c in a.operation.click.call_args_list)
|
||||
a.cut_pic.assert_not_called()
|
||||
a.operation.analyse_pic_word.assert_not_called()
|
||||
a._shot_and_diagnose.assert_not_called()
|
||||
log_msgs = [str(c) for c in a._log_kv.call_args_list]
|
||||
assert any("快速预处理" in m for m in log_msgs)
|
||||
|
||||
def test_fast_prejoin_uses_short_configured_delays(self):
|
||||
a = _mk_analyser(auto_join_fansclub=True)
|
||||
a.reservation_prejoin_fansclub_participate_wait_seconds = 0.2
|
||||
a.reservation_prejoin_fansclub_confirm_wait_seconds = 0.3
|
||||
|
||||
handle_reservation_prejoin_fansclub(a, "加入购物粉丝团并关注主播")
|
||||
|
||||
delay_args = [c.args[0] for c in a.operation.delay.call_args_list]
|
||||
assert delay_args == [0.2, 0.3]
|
||||
@@ -57,6 +57,32 @@ def test_attend_choujiang_treat_notice_text_as_attended() -> None:
|
||||
analyser.click.assert_called_once_with(*DETAIL_OUTSIDE_CLICK)
|
||||
|
||||
|
||||
def test_attend_choujiang_blocks_insufficient_time_text_even_if_status_actionable() -> None:
|
||||
analyser = MagicMock()
|
||||
analyser._fudai_button_status = "可以参与"
|
||||
analyser._fudai_button_text = "抽奖剩余时间不足,下次再来参与吧"
|
||||
analyser._fudai_button_ocr_time = 25_000.0
|
||||
analyser._fudai_button_room_pos = 0
|
||||
analyser.room_pos = 0
|
||||
analyser.click = MagicMock()
|
||||
analyser._log_kv = MagicMock()
|
||||
analyser.operation = MagicMock()
|
||||
analyser.operation.delay = MagicMock()
|
||||
|
||||
import time as _time
|
||||
|
||||
old_time = _time.time
|
||||
_time.time = lambda: 25_001.0
|
||||
try:
|
||||
ok = attend_choujiang(analyser)
|
||||
finally:
|
||||
_time.time = old_time
|
||||
|
||||
assert ok is False
|
||||
assert analyser.last_attend_fail_reason == "button_not_actionable"
|
||||
analyser.click.assert_called_once_with(*DETAIL_OUTSIDE_CLICK)
|
||||
|
||||
|
||||
def test_attend_choujiang_does_not_reuse_fresh_button_cache_from_other_room() -> None:
|
||||
analyser = MagicMock()
|
||||
analyser._fudai_button_status = "可以参与"
|
||||
|
||||
@@ -12,6 +12,10 @@ def test_decide_button_status_attended_for_notice_text() -> None:
|
||||
assert decide_button_status("直播间,否则会失去抽奖机会") == "已参与"
|
||||
|
||||
|
||||
def test_decide_button_status_blocks_insufficient_time_text() -> None:
|
||||
assert decide_button_status("抽奖剩余时间不足,下次再来参与吧") == "不可参与"
|
||||
|
||||
|
||||
def test_apply_button_ocr_result_records_room_pos_when_enabled() -> None:
|
||||
log_calls = []
|
||||
analyser = SimpleNamespace(room_pos=12)
|
||||
|
||||
@@ -147,6 +147,15 @@ class TestShouldSkipFudaiByConfig:
|
||||
assert isinstance(skip, bool)
|
||||
assert isinstance(reason, str)
|
||||
|
||||
def test_insufficient_time_button_skips(self):
|
||||
analyser = self._mock()
|
||||
analyser.button_text_raw = "抽奖剩余时间不足,下次再来参与吧"
|
||||
detail = ('现金1000元', '00:28', 50, 1, 1000.0,
|
||||
(time.time()+28, time.time()+28))
|
||||
skip, reason = should_skip_fudai_by_config(analyser, detail)
|
||||
assert skip is True
|
||||
assert "button_unable_to_participate" in reason
|
||||
|
||||
|
||||
# ============================================================
|
||||
# join_decision.py — basic_join_decision
|
||||
|
||||
@@ -73,7 +73,7 @@ def test_high_value_reservation_prejoins_fansclub_before_recording():
|
||||
"douyin.fudai.core.fudai_filter_pkg.join_decision.should_skip_fudai_by_config",
|
||||
return_value=(True, "countdown_gt_118"),
|
||||
), patch(
|
||||
"douyin.fudai.core.fudai_attend_pkg.fansclub_handler.handle_fansclub_branch",
|
||||
"douyin.fudai.core.fudai_attend_pkg.fansclub_handler.handle_reservation_prejoin_fansclub",
|
||||
side_effect=_fake_prejoin,
|
||||
) as mock_prejoin, patch(
|
||||
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
|
||||
@@ -111,7 +111,7 @@ def test_high_value_reservation_prejoins_live_fansclub_variant():
|
||||
"douyin.fudai.core.fudai_filter_pkg.join_decision.should_skip_fudai_by_config",
|
||||
return_value=(True, "countdown_gt_118"),
|
||||
), patch(
|
||||
"douyin.fudai.core.fudai_attend_pkg.fansclub_handler.handle_fansclub_branch",
|
||||
"douyin.fudai.core.fudai_attend_pkg.fansclub_handler.handle_reservation_prejoin_fansclub",
|
||||
return_value=True,
|
||||
) as mock_prejoin, patch(
|
||||
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
|
||||
|
||||
@@ -33,6 +33,25 @@ def _has_log_reason(analyser, reason: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def test_reservation_button_fallback_blocks_insufficient_time_text():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg import direct_attend
|
||||
|
||||
analyser = _mk_analyser()
|
||||
item = {
|
||||
"button_text": "抽奖剩余时间不足,下次再来参与吧",
|
||||
"button_status": "可以参与",
|
||||
}
|
||||
|
||||
ok, button_text, button_status = direct_attend._reservation_button_fallback_is_actionable(
|
||||
analyser,
|
||||
item,
|
||||
)
|
||||
|
||||
assert ok is False
|
||||
assert button_text == "抽奖剩余时间不足,下次再来参与吧"
|
||||
assert button_status == "可以参与"
|
||||
|
||||
|
||||
def test_direct_attend_logs_fixed_success_marker():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
|
||||
Reference in new issue
Block a user