Files
douyi_guaji_main/tests/test_other_modules.py
T

291 lines
9.5 KiB
Python

"""douyin/other 模块单元测试。
覆盖:
- douyin_high_value_room: _normalize_room_title 纯函数
- douyin_popups: 弹窗检测函数签名验证
- douyin_lottery: 抽奖函数签名验证
"""
from __future__ import annotations
import time
from types import SimpleNamespace
from unittest.mock import MagicMock
from unittest.mock import patch
import pytest
class TestNormalizeRoomTitle:
"""_normalize_room_title 纯函数测试。"""
def test_removes_trailing_numbers(self):
from douyin.other.douyin_high_value_room import _normalize_room_title
result = _normalize_room_title("王媛媛2")
assert "2" not in result or result == "王媛媛"
def test_removes_spaces(self):
from douyin.other.douyin_high_value_room import _normalize_room_title
result = _normalize_room_title("王 媛 媛")
assert " " not in result
def test_removes_common_words(self):
from douyin.other.douyin_high_value_room import _normalize_room_title
result = _normalize_room_title("王媛媛直播中")
assert "直播中" not in result
def test_empty_string(self):
from douyin.other.douyin_high_value_room import _normalize_room_title
result = _normalize_room_title("")
assert result == ""
def test_none_input(self):
from douyin.other.douyin_high_value_room import _normalize_room_title
result = _normalize_room_title(None)
assert result == ""
def test_removes_newlines(self):
from douyin.other.douyin_high_value_room import _normalize_room_title
result = _normalize_room_title("王媛媛\n直播")
assert "\n" not in result
class TestHighValueRoomFunctions:
"""douyin_high_value_room 模块函数测试。"""
def test_touch_high_value_room_exists(self):
from douyin.other.douyin_high_value_room import touch_high_value_room
assert callable(touch_high_value_room)
def test_get_revisit_candidates_exists(self):
from douyin.other.douyin_high_value_room import get_revisit_candidates
assert callable(get_revisit_candidates)
def test_should_trigger_revisit_exists(self):
from douyin.other.douyin_high_value_room import should_trigger_revisit
assert callable(should_trigger_revisit)
def test_execute_room_revisit_exists(self):
from douyin.other.douyin_high_value_room import execute_room_revisit
assert callable(execute_room_revisit)
class TestDouyinPopups:
"""douyin_popups 模块测试。"""
def test_check_coupon_popup_exists(self):
from douyin.other.douyin_popups import check_coupon_popup
assert callable(check_coupon_popup)
def test_check_shopping_redpacket_popup_exists(self):
from douyin.other.douyin_popups import check_shopping_redpacket_popup
assert callable(check_shopping_redpacket_popup)
def test_handle_lottery_result_popup_exists(self):
from douyin.other.douyin_popups import handle_lottery_result_popup
assert callable(handle_lottery_result_popup)
class TestDouyinLottery:
"""douyin_lottery 模块测试。"""
def test_wait_for_lottery_result_exists(self):
from douyin.other.douyin_lottery import wait_for_lottery_result
assert callable(wait_for_lottery_result)
def test_process_lottery_result_exists(self):
from douyin.other.douyin_lottery import process_lottery_result
assert callable(process_lottery_result)
class TestDouyinGuaji:
"""douyin_guaji 模块测试。"""
def test_fudai_guaji_class_exists(self):
from douyin.other.douyin_guaji import fudai_guaji
assert fudai_guaji is not None
class TestDouyinLiveRoom:
"""douyin_live_room 模块测试。"""
def test_module_importable(self):
from douyin.other import douyin_live_room
assert douyin_live_room is not None
class TestDouyinRobot:
"""douyin_robot 模块测试。"""
def test_module_importable(self):
from douyin.other import douyin_robot
assert douyin_robot is not None
class TestDouyinYanghaoShimConsistency:
"""验证 yanghao shim 与子模块导出一致。"""
def test_yanghao_recommend_flow_same_object(self):
from douyin.other.douyin_yanghao import yanghao_recommend_flow as f1
from douyin.other.douyin_yanghao_pkg import yanghao_recommend_flow as f2
assert f1 is f2
def test_yanghao_wait_same_object(self):
from douyin.other.douyin_yanghao import yanghao_wait as f1
from douyin.other.douyin_yanghao_pkg import yanghao_wait as f2
assert f1 is f2
def test_yanghao_get_recommend_seconds_same_object(self):
from douyin.other.douyin_yanghao import yanghao_get_recommend_seconds as f1
from douyin.other.douyin_yanghao_pkg import yanghao_get_recommend_seconds as f2
assert f1 is f2
class TestTimedYanghaoTrigger:
def test_maybe_run_yanghao_uses_recommend_flow(self):
from douyin.core.douyin_fudai import fudai_analyse
s = SimpleNamespace()
s.yanghao_recommend_interval_seconds = 60
s.yanghao_recommend_duration_seconds = 30
s._waiting_for_lottery_result = False
s._current_attending_reservation_fudai = False
s.reservations = []
s.reservation_focus_until_ts = 0.0
s._last_yanghao_ts = 0.0
s._log_kv = MagicMock(return_value=None)
s._yanghao_recommend_flow = MagicMock(return_value=None)
s._yanghao_wait = MagicMock(return_value=None)
fudai_analyse._maybe_run_yanghao_during_run(s)
s._yanghao_recommend_flow.assert_called_once_with(30)
s._yanghao_wait.assert_not_called()
assert s._last_yanghao_ts > 0.0
class TestRunOnceBasicReservationDirectAttend:
def test_run_once_basic_uses_direct_attend_after_reservation_preempt(self):
from douyin.core.douyin_fudai import fudai_analyse
s = SimpleNamespace()
s._log_kv = MagicMock(return_value=None)
s._handle_reservation_direct_attend = MagicMock(return_value=None)
s._current_attending_reservation_fudai = False
s.reservations = [{"future_ts": 9999999999.0}]
with patch(
"douyin.core.fudai_modules.run_once_basic_helpers.run_once_basic_prelude",
return_value=None,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.handle_waiting_for_lottery_result",
return_value=False,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.handle_no_attend_timeout",
return_value=False,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.handle_reservation_pipeline",
side_effect=lambda self_obj, start_time: setattr(self_obj, "_current_attending_reservation_fudai", True) or False,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.run_once_basic_post_reservation",
return_value=None,
):
fudai_analyse.run_once_basic(s)
s._handle_reservation_direct_attend.assert_called_once_with()
def test_run_once_basic_clears_stale_direct_attend_when_queue_empty(self):
from douyin.core.douyin_fudai import fudai_analyse
s = SimpleNamespace()
s._log_kv = MagicMock(return_value=None)
s._handle_reservation_direct_attend = MagicMock(return_value=None)
s._current_attending_reservation_fudai = True
s.reservation_after_action_pending = True
s.reservation_focus_until_ts = 123.0
s.reservations = []
with patch(
"douyin.core.fudai_modules.run_once_basic_helpers.run_once_basic_prelude",
return_value=None,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.handle_waiting_for_lottery_result",
return_value=False,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.handle_no_attend_timeout",
return_value=False,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.handle_reservation_pipeline",
return_value=False,
), patch(
"douyin.core.fudai_modules.run_once_basic_helpers.run_once_basic_post_reservation",
return_value=None,
) as post_reservation:
fudai_analyse.run_once_basic(s)
s._handle_reservation_direct_attend.assert_not_called()
post_reservation.assert_called_once()
assert s._current_attending_reservation_fudai is False
assert s.reservation_after_action_pending is False
assert s.reservation_focus_until_ts == 0.0
class TestOtherModules:
"""other 子模块存在性测试。"""
def test_douyin_guaji_importable(self):
from douyin.other import douyin_guaji
assert douyin_guaji is not None
def test_douyin_high_value_room_importable(self):
from douyin.other import douyin_high_value_room
assert douyin_high_value_room is not None
def test_douyin_live_room_importable(self):
from douyin.other import douyin_live_room
assert douyin_live_room is not None
def test_douyin_lottery_importable(self):
from douyin.other import douyin_lottery
assert douyin_lottery is not None
def test_douyin_popups_importable(self):
from douyin.other import douyin_popups
assert douyin_popups is not None
def test_douyin_robot_importable(self):
from douyin.other import douyin_robot
assert douyin_robot is not None
def test_douyin_yanghao_importable(self):
from douyin.other import douyin_yanghao
assert douyin_yanghao is not None