163 lines
5.8 KiB
Python
163 lines
5.8 KiB
Python
from __future__ import annotations
|
||
|
||
import time
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
from douyin.fudai.core.fudai_filter_pkg.join_decision import (
|
||
_looks_like_fansclub_join_button,
|
||
basic_join_decision,
|
||
)
|
||
|
||
|
||
def _mk_analyser():
|
||
a = MagicMock()
|
||
a._log_kv = MagicMock()
|
||
a._run_id = "test-run"
|
||
a.needswitch = True
|
||
a.high_value_fudai_enabled = True
|
||
a.high_value_fudai_threshold_yuan = 500.0
|
||
a.high_value_fudai_join_ahead_seconds = 100
|
||
a.high_value_fudai_max_reservations = 3
|
||
a.switch_room_wait_after_stage_diag_seconds = 1.5
|
||
a.wait_minutes = 5
|
||
a.room_pos = 0
|
||
a.last_fudai_x = 0
|
||
a.current_room_title = "测试直播间"
|
||
a.current_room_title_raw = "测试直播间"
|
||
a._fudai_detail_task_count_from_stage = 1
|
||
a.reservations = []
|
||
a.operation = MagicMock()
|
||
a.device_id = "MOCK"
|
||
a.click = MagicMock()
|
||
a.swipe_up_to_next_live = MagicMock()
|
||
return a
|
||
|
||
|
||
def test_high_value_countdown_gt_records_reservation_and_returns_true():
|
||
analyser = _mk_analyser()
|
||
future_ts = time.time() + 300 # > join_ahead_seconds
|
||
detail = ("高价值福袋", "05:00", 10, 1, 1999.0, (time.time(), future_ts, 300))
|
||
|
||
with patch(
|
||
"douyin.fudai.core.fudai_filter_pkg.join_decision.should_skip_fudai_by_config",
|
||
return_value=(True, "countdown_gt_118"),
|
||
), patch(
|
||
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
|
||
return_value=False,
|
||
) as mock_record:
|
||
ok = basic_join_decision(analyser, detail)
|
||
|
||
assert ok is True
|
||
mock_record.assert_called_once()
|
||
# 记录预约后应立即切换直播间(needswitch=True)
|
||
analyser.swipe_up_to_next_live.assert_called_once()
|
||
|
||
|
||
def test_high_value_reservation_prejoins_fansclub_before_recording():
|
||
analyser = _mk_analyser()
|
||
analyser.auto_join_fansclub = True
|
||
analyser.button_text_raw = "加入购物粉丝团并关注主播"
|
||
future_ts = time.time() + 300
|
||
detail = ("高价值福袋", "05:00", 10, 1, 1999.0, (time.time(), future_ts, 300))
|
||
events = []
|
||
|
||
def _fake_prejoin(_analyser, _button_text):
|
||
events.append("prejoin")
|
||
return True
|
||
|
||
def _fake_record(**_kwargs):
|
||
events.append("record")
|
||
return False
|
||
|
||
with patch(
|
||
"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_reservation_prejoin_fansclub",
|
||
side_effect=_fake_prejoin,
|
||
) as mock_prejoin, patch(
|
||
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
|
||
side_effect=_fake_record,
|
||
) as mock_record:
|
||
ok = basic_join_decision(analyser, detail)
|
||
|
||
assert ok is True
|
||
assert events == ["prejoin", "record"]
|
||
mock_prejoin.assert_called_once_with(analyser, "加入购物粉丝团并关注主播")
|
||
mock_record.assert_called_once()
|
||
assert analyser._reservation_prejoined_fansclub is True
|
||
analyser.swipe_up_to_next_live.assert_called_once()
|
||
|
||
|
||
def test_fansclub_button_match_accepts_live_and_ocr_variants():
|
||
assert _looks_like_fansclub_join_button("加入直播粉丝团并关注主播")
|
||
assert _looks_like_fansclub_join_button("加入购物粉丝团并关注主播")
|
||
assert _looks_like_fansclub_join_button("加人直播粉丝团并关注主播")
|
||
assert _looks_like_fansclub_join_button(
|
||
"加入直播粉丝并关注主播",
|
||
"需要加入粉丝团",
|
||
)
|
||
assert not _looks_like_fansclub_join_button("一键发表评论")
|
||
|
||
|
||
def test_high_value_reservation_prejoins_live_fansclub_variant():
|
||
analyser = _mk_analyser()
|
||
analyser.auto_join_fansclub = True
|
||
analyser.button_text_raw = "加入直播粉丝团并关注主播"
|
||
future_ts = time.time() + 300
|
||
detail = ("高价值福袋", "05:00", 10, 1, 1999.0, (time.time(), future_ts, 300))
|
||
|
||
with patch(
|
||
"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_reservation_prejoin_fansclub",
|
||
return_value=True,
|
||
) as mock_prejoin, patch(
|
||
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
|
||
return_value=False,
|
||
):
|
||
ok = basic_join_decision(analyser, detail)
|
||
|
||
assert ok is True
|
||
mock_prejoin.assert_called_once_with(analyser, "加入直播粉丝团并关注主播")
|
||
|
||
|
||
def test_high_value_countdown_within_join_window_attends():
|
||
analyser = _mk_analyser()
|
||
future_ts = time.time() + 30 # <= join_ahead_seconds
|
||
detail = ("高价值福袋", "00:30", 10, 1, 1999.0, (time.time(), future_ts, 30))
|
||
|
||
with patch(
|
||
"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.attend_choujiang",
|
||
return_value=True,
|
||
) as mock_attend:
|
||
ok = basic_join_decision(analyser, detail)
|
||
|
||
assert ok is True
|
||
mock_attend.assert_called_once()
|
||
|
||
|
||
def test_single_room_mode_bypasses_reservation_path():
|
||
analyser = _mk_analyser()
|
||
analyser.needswitch = False
|
||
future_ts = time.time() + 300
|
||
detail = ("高价值福袋", "05:00", 10, 1, 1999.0, (time.time(), future_ts, 300))
|
||
|
||
with patch(
|
||
"douyin.fudai.core.fudai_filter_pkg.join_decision.should_skip_fudai_by_config",
|
||
return_value=(True, "countdown_gt_118"),
|
||
), patch(
|
||
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
|
||
return_value=False,
|
||
) as mock_record:
|
||
ok = basic_join_decision(analyser, detail)
|
||
|
||
# 单间模式走“直接参与”路径,不会进入“高价值预约记录”分支
|
||
assert ok is False
|
||
mock_record.assert_not_called()
|
||
analyser.swipe_up_to_next_live.assert_not_called()
|