193 lines
5.9 KiB
Python
193 lines
5.9 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
from douyin.fudai.core.coords import DETAIL_OUTSIDE_CLICK
|
|
from douyin.fudai.core.fudai_attend import attend_choujiang
|
|
|
|
|
|
def test_attend_choujiang_fallback_click_on_unknown_button_text() -> None:
|
|
analyser = MagicMock()
|
|
analyser._fudai_button_status = "未知状态"
|
|
analyser._fudai_button_text = "按钮文案发生变化"
|
|
analyser._fudai_button_ocr_time = 10_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: 10_001.0
|
|
try:
|
|
ok = attend_choujiang(analyser)
|
|
finally:
|
|
_time.time = old_time
|
|
|
|
assert ok is True
|
|
analyser.click.assert_called_once()
|
|
|
|
|
|
def test_attend_choujiang_treat_notice_text_as_attended() -> None:
|
|
analyser = MagicMock()
|
|
analyser._fudai_button_status = "已参与"
|
|
analyser._fudai_button_text = "直播间,否则会失去抽奖机会"
|
|
analyser._fudai_button_ocr_time = 20_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: 20_001.0
|
|
try:
|
|
ok = attend_choujiang(analyser)
|
|
finally:
|
|
_time.time = old_time
|
|
|
|
assert ok is True
|
|
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 = "可以参与"
|
|
analyser._fudai_button_text = "参与抽奖"
|
|
analyser._fudai_button_ocr_time = 30_000.0
|
|
analyser._fudai_button_room_pos = 3
|
|
analyser.room_pos = 8
|
|
analyser.button_text_raw = None
|
|
analyser._room_pos_to_title_map = {}
|
|
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: 30_001.0
|
|
try:
|
|
ok = attend_choujiang(analyser)
|
|
finally:
|
|
_time.time = old_time
|
|
|
|
assert ok is False
|
|
assert analyser.last_attend_fail_reason == "no_parallel_ocr_result"
|
|
analyser.click.assert_called_once_with(*DETAIL_OUTSIDE_CLICK)
|
|
|
|
|
|
def test_attend_choujiang_does_not_reuse_button_text_raw_from_other_room() -> None:
|
|
analyser = MagicMock()
|
|
analyser._fudai_button_status = None
|
|
analyser._fudai_button_text = ""
|
|
analyser._fudai_button_ocr_time = 0.0
|
|
analyser._fudai_button_room_pos = 3
|
|
analyser.room_pos = 8
|
|
analyser.button_text_raw = "参与抽奖"
|
|
analyser._room_pos_to_title_map = {}
|
|
analyser.click = MagicMock()
|
|
analyser._log_kv = MagicMock()
|
|
analyser.operation = MagicMock()
|
|
analyser.operation.delay = MagicMock()
|
|
|
|
ok = attend_choujiang(analyser)
|
|
|
|
assert ok is False
|
|
assert analyser.last_attend_fail_reason == "no_parallel_ocr_result"
|
|
analyser.click.assert_called_once_with(*DETAIL_OUTSIDE_CLICK)
|
|
|
|
|
|
def test_attend_choujiang_can_recover_button_text_from_room_title_map() -> None:
|
|
analyser = MagicMock()
|
|
analyser._fudai_button_status = None
|
|
analyser._fudai_button_text = ""
|
|
analyser._fudai_button_ocr_time = 0.0
|
|
analyser._fudai_button_room_pos = 0
|
|
analyser.room_pos = 8
|
|
analyser.current_room_title = "测试直播间"
|
|
analyser.current_room_title_raw = "测试直播间原始"
|
|
analyser.button_text_raw = None
|
|
analyser._room_pos_to_title_map = {
|
|
3: {
|
|
"title": "测试直播间",
|
|
"title_raw": "测试直播间原始",
|
|
"fudai_detail": {"button_text": "参与抽奖"},
|
|
}
|
|
}
|
|
analyser.click = MagicMock()
|
|
analyser._log_kv = MagicMock()
|
|
analyser.operation = MagicMock()
|
|
analyser.operation.delay = MagicMock()
|
|
|
|
ok = attend_choujiang(analyser)
|
|
|
|
assert ok is True
|
|
assert analyser._fudai_button_status == "可以参与"
|
|
assert analyser._fudai_button_text == "参与抽奖"
|
|
|
|
|
|
def test_attend_choujiang_debug_json_is_best_effort(monkeypatch, tmp_path: Path) -> None:
|
|
analyser = MagicMock()
|
|
analyser._fudai_button_status = "可以参与"
|
|
analyser._fudai_button_text = "参与抽奖"
|
|
analyser._fudai_button_ocr_time = 40_000.0
|
|
analyser._fudai_button_room_pos = 0
|
|
analyser.room_pos = 0
|
|
analyser._current_fudai_future_ts = MagicMock()
|
|
analyser.click = MagicMock()
|
|
analyser._log_kv = MagicMock()
|
|
analyser.operation = MagicMock()
|
|
analyser.operation.delay = MagicMock()
|
|
|
|
debug_log = tmp_path / "debug.log"
|
|
monkeypatch.setattr(
|
|
"douyin.core.path_config.get_debug_log_path",
|
|
lambda: str(debug_log),
|
|
)
|
|
|
|
import time as _time
|
|
|
|
old_time = _time.time
|
|
_time.time = lambda: 40_001.0
|
|
try:
|
|
ok = attend_choujiang(analyser)
|
|
finally:
|
|
_time.time = old_time
|
|
|
|
assert ok is True
|