Fix reservation direct attend timing guard
This commit is contained in:
1 parent
8fac904ba9
commit
b533c4e96e
8 files changed
+609
-22
No files matched your search
@@ -24,6 +24,15 @@ def _mk_analyser():
|
||||
return analyser
|
||||
|
||||
|
||||
def _has_log_reason(analyser, reason: str) -> bool:
|
||||
for call in analyser._log_kv.call_args_list:
|
||||
if str(call.kwargs.get("reason", "")) == reason:
|
||||
return True
|
||||
if str(call.kwargs.get("原因", "")) == reason:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def test_direct_attend_logs_fixed_success_marker():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
@@ -54,6 +63,33 @@ def test_direct_attend_logs_fixed_success_marker():
|
||||
assert marker_logs
|
||||
|
||||
|
||||
def test_direct_attend_rescans_fudai_after_join_click_for_verification():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
|
||||
with patch(
|
||||
"douyin.core.douyin_flow_control.find_and_open_fudai_detail",
|
||||
side_effect=[True, False],
|
||||
) as finder, patch(
|
||||
"douyin.core.path_config.get_screenshot_path",
|
||||
return_value="C:/mock/screenshot.png",
|
||||
), patch(
|
||||
"PIL.Image.open",
|
||||
) as mock_open, patch(
|
||||
"douyin.detection.douyin_pixel.detect_attend_button_red_area",
|
||||
return_value=(True, {"mock": True}),
|
||||
):
|
||||
mock_image = MagicMock()
|
||||
mock_open.return_value = mock_image
|
||||
handle_reservation_direct_attend(analyser)
|
||||
|
||||
assert finder.call_count == 2
|
||||
analyser.operation.click.assert_called()
|
||||
|
||||
|
||||
def test_direct_attend_logs_fixed_failure_marker_when_button_still_exists():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
@@ -165,3 +201,80 @@ def test_direct_attend_expired_reservation_clears_flags_without_detail_handler()
|
||||
if len(call.args) >= 2 and str(call.args[1]) == "预约回退 direct_attend 失败"
|
||||
]
|
||||
assert marker_logs
|
||||
|
||||
|
||||
def test_direct_attend_aborts_when_remaining_window_too_small():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser.reservations = [
|
||||
{
|
||||
"future_ts": time.time() + 2.0,
|
||||
"prize_value_yuan": 5980.0,
|
||||
"content": "soon",
|
||||
}
|
||||
]
|
||||
analyser._current_attending_reservation_fudai = True
|
||||
|
||||
with patch("douyin.core.douyin_flow_control.find_and_open_fudai_detail") as finder:
|
||||
handle_reservation_direct_attend(analyser)
|
||||
|
||||
finder.assert_not_called()
|
||||
assert analyser.reservation_after_action_pending is False
|
||||
assert analyser._current_attending_reservation_fudai is False
|
||||
assert _has_log_reason(analyser, "direct_attend_abort_due_to_insufficient_time")
|
||||
|
||||
|
||||
def test_direct_attend_never_clicks_cached_fudai_x_without_current_scan():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser.reservations = [
|
||||
{
|
||||
"future_ts": time.time() + 2.0,
|
||||
"prize_value_yuan": 5980.0,
|
||||
"content": "soon",
|
||||
}
|
||||
]
|
||||
analyser.last_fudai_x = 162
|
||||
analyser._current_attending_reservation_fudai = True
|
||||
|
||||
with patch("douyin.core.douyin_flow_control.find_and_open_fudai_detail") as finder:
|
||||
handle_reservation_direct_attend(analyser)
|
||||
|
||||
finder.assert_not_called()
|
||||
analyser.operation.click.assert_not_called()
|
||||
assert _has_log_reason(analyser, "direct_attend_abort_due_to_insufficient_time")
|
||||
|
||||
|
||||
def test_direct_attend_uses_reservation_text_fallback_when_button_pixel_misses():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import (
|
||||
handle_reservation_direct_attend,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser.reservations[0]["button_text"] = "一键发表评论"
|
||||
analyser.reservations[0]["button_status"] = "需要评论"
|
||||
|
||||
with patch(
|
||||
"douyin.core.douyin_flow_control.find_and_open_fudai_detail",
|
||||
side_effect=[True, False],
|
||||
), patch(
|
||||
"douyin.core.path_config.get_screenshot_path",
|
||||
return_value="C:/mock/screenshot.png",
|
||||
), patch(
|
||||
"PIL.Image.open",
|
||||
) as mock_open, patch(
|
||||
"douyin.detection.douyin_pixel.detect_attend_button_red_area",
|
||||
return_value=(False, {"reason": "red_ratio_below_threshold"}),
|
||||
):
|
||||
mock_image = MagicMock()
|
||||
mock_open.return_value = mock_image
|
||||
handle_reservation_direct_attend(analyser)
|
||||
|
||||
analyser.operation.click.assert_called()
|
||||
assert _has_log_reason(analyser, "reservation_button_text_fallback_click_sent")
|
||||
@@ -16,6 +16,9 @@ from douyin.reservation.reservation_logic.helpers import (
|
||||
_now_ts,
|
||||
_sort_reservations_inplace,
|
||||
)
|
||||
from douyin.reservation.reservation_logic.attend_window_helpers import (
|
||||
compute_dynamic_ahead,
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
@@ -155,3 +158,23 @@ class TestSortReservationsInplace:
|
||||
_sort_reservations_inplace(items)
|
||||
# 原列表对象未变
|
||||
assert id(items) == original_id
|
||||
|
||||
|
||||
class TestComputeDynamicAhead:
|
||||
def test_includes_runtime_capture_and_verify_budget(self):
|
||||
class _A:
|
||||
reservation_back_verify_wait_seconds = 5.0
|
||||
reservation_direct_attend_min_action_seconds = 8.0
|
||||
reservation_screenshot_budget_seconds = 8.0
|
||||
high_value_fudai_max_swipe_back = 60
|
||||
|
||||
dynamic_ahead, est_back_times = compute_dynamic_ahead(
|
||||
_A(),
|
||||
{"pos_seen": 0},
|
||||
room_pos=2,
|
||||
reservation_back_swipe_delay_s=0.5,
|
||||
join_ahead=10.0,
|
||||
)
|
||||
|
||||
assert est_back_times == 2
|
||||
assert dynamic_ahead >= 25
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -93,6 +95,32 @@ class TestDouyinReservation:
|
||||
|
||||
assert callable(write_reservations_state)
|
||||
|
||||
def test_write_reservations_state_preserves_button_fields(self, tmp_path):
|
||||
from douyin.reservation.douyin_reservation import write_reservations_state
|
||||
|
||||
state_path = tmp_path / "reservations.json"
|
||||
write_reservations_state(
|
||||
state_path=str(state_path),
|
||||
reservations=[
|
||||
{
|
||||
"future_ts": 9999999999.0,
|
||||
"prize_value_yuan": 899.0,
|
||||
"content": "X",
|
||||
"button_text": "一键发表评论",
|
||||
"button_status": "需要评论",
|
||||
}
|
||||
],
|
||||
room_pos=1,
|
||||
current_room_title="room",
|
||||
current_room_title_raw="room",
|
||||
reason="unit",
|
||||
)
|
||||
|
||||
data = json.loads(state_path.read_text(encoding="utf-8"))
|
||||
item = data["reservations"][0]
|
||||
assert item["button_text"] == "一键发表评论"
|
||||
assert item["button_status"] == "需要评论"
|
||||
|
||||
def test_load_high_value_rooms_map_exists(self):
|
||||
from douyin.reservation.douyin_reservation import load_high_value_rooms_map
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def test_record_high_value_reservation_preserves_button_fields():
|
||||
from douyin.reservation.reservation_logic.record import record_high_value_reservation
|
||||
|
||||
analyser = SimpleNamespace(
|
||||
_log_kv=MagicMock(return_value=None),
|
||||
_fudai_button_text="一键发表评论",
|
||||
_fudai_button_status="需要评论",
|
||||
)
|
||||
reservations = []
|
||||
|
||||
ok = record_high_value_reservation(
|
||||
analyser=analyser,
|
||||
fudai_content_text="儿童电话手表",
|
||||
prize_value_yuan=899.0,
|
||||
lastsecond=120,
|
||||
future_timestamp=time.time() + 600,
|
||||
x=162,
|
||||
needswitch=True,
|
||||
wait_minutes=0,
|
||||
post_switch_delay_s=0.0,
|
||||
room_pos=1,
|
||||
reservations=reservations,
|
||||
reservation_focus_until_ts_ref=[0.0],
|
||||
close_detail_callback=lambda: None,
|
||||
swipe_up_switch_callback=lambda: None,
|
||||
swipe_down_back_callback=lambda: None,
|
||||
add_reservation_callback=lambda items, item: items.append(item),
|
||||
sort_reservations_callback=lambda: None,
|
||||
write_state_callback=lambda: None,
|
||||
room_title="学而思",
|
||||
room_title_raw="学而思",
|
||||
task_count=2,
|
||||
time_text="10:00",
|
||||
participants=0,
|
||||
copies=1,
|
||||
)
|
||||
|
||||
assert ok is False
|
||||
assert reservations[0]["button_text"] == "一键发表评论"
|
||||
assert reservations[0]["button_status"] == "需要评论"
|
||||
Reference in new issue
Block a user