354 lines
11 KiB
Python
354 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
预约回退功能完整测试脚本
|
|
=======================
|
|
|
|
测试预约系统能否成功回退到目标直播间。
|
|
|
|
测试内容:
|
|
1. 预约队列处理和排序
|
|
2. 位置计算和回退方向判断
|
|
3. 滑动操作模拟
|
|
4. 回退校验逻辑
|
|
5. 完整回退流程
|
|
|
|
运行方法:
|
|
python test_reservation_backtrack.py
|
|
"""
|
|
|
|
import time
|
|
import sys
|
|
import os
|
|
|
|
# 添加项目根目录到路径
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from douyin.reservation.douyin_reservation_logic import (
|
|
process_reservation_attend_window,
|
|
_sort_reservations_inplace,
|
|
ReservationItem,
|
|
)
|
|
from douyin.reservation.douyin_reservation_verify import validate_back_to_reservation
|
|
|
|
|
|
class MockAnalyser:
|
|
"""模拟分析器对象"""
|
|
|
|
def __init__(self):
|
|
# 基本配置
|
|
self.reservation_back_verify_enabled = True
|
|
self.high_value_fudai_max_swipe_back = 10
|
|
self.reservation_back_total_timeout_seconds = 10.0
|
|
|
|
# 位置信息
|
|
self.room_pos = 5 # 当前在第5个直播间
|
|
self._room_pos_to_title_map = {
|
|
1: ("主播小明直播间", "主播小明直播间"),
|
|
2: ("美女主播在线", "美女主播在线"),
|
|
3: ("游戏直播高手", "游戏直播高手"),
|
|
4: ("音乐现场直播", "音乐现场直播"),
|
|
5: ("科技数码直播", "科技数码直播"), # 当前位置
|
|
6: ("美食烹饪教学", "美食烹饪教学"),
|
|
7: ("旅游风景直播", "旅游风景直播"),
|
|
}
|
|
|
|
# 滑动计数器
|
|
self.swipe_up_count = 0
|
|
self.swipe_down_count = 0
|
|
|
|
# 日志
|
|
self.logs = []
|
|
|
|
def _log_kv(self, tag, message, **kwargs):
|
|
"""记录日志"""
|
|
log_entry = f"[{tag}] {message}"
|
|
if kwargs:
|
|
log_entry += f" {kwargs}"
|
|
self.logs.append(log_entry)
|
|
print(log_entry)
|
|
|
|
def _seek_reservation_room(self, item):
|
|
"""模拟回退到预约直播间"""
|
|
target_pos = int(item.get("pos_seen", 0))
|
|
current_pos = self.room_pos
|
|
pos_diff = target_pos - current_pos
|
|
|
|
print(f"模拟回退:从位置 {current_pos} 到位置 {target_pos} (差值: {pos_diff})")
|
|
|
|
if pos_diff < 0:
|
|
# 向上回退
|
|
back_times = min(abs(pos_diff), self.high_value_fudai_max_swipe_back)
|
|
self.swipe_down_count += back_times
|
|
print(f"执行 {back_times} 次向下滑动")
|
|
elif pos_diff > 0:
|
|
# 向下前进
|
|
forward_times = min(pos_diff, self.high_value_fudai_max_swipe_back)
|
|
self.swipe_up_count += forward_times
|
|
print(f"执行 {forward_times} 次向上滑动")
|
|
|
|
# 更新当前位置
|
|
if abs(pos_diff) <= self.high_value_fudai_max_swipe_back:
|
|
self.room_pos = target_pos
|
|
print(f"成功回退到位置 {target_pos}")
|
|
return True
|
|
else:
|
|
print(
|
|
f"回退距离过大,限制在 {self.high_value_fudai_max_swipe_back} 次滑动内"
|
|
)
|
|
return False
|
|
|
|
|
|
def test_reservation_backtrack():
|
|
"""测试预约回退功能"""
|
|
|
|
print("=" * 70)
|
|
print("预约回退功能完整测试")
|
|
print("=" * 70)
|
|
|
|
# 创建模拟分析器
|
|
analyser = MockAnalyser()
|
|
|
|
# 测试1:预约队列排序
|
|
print("\n测试1:预约队列排序")
|
|
print("-" * 40)
|
|
|
|
reservations = [
|
|
{
|
|
"future_ts": time.time() + 600, # 10分钟后
|
|
"prize_value_yuan": 50.0,
|
|
"created_ts": time.time() - 100,
|
|
"pos_seen": 3,
|
|
"room_title": "游戏直播高手",
|
|
},
|
|
{
|
|
"future_ts": time.time() + 300, # 5分钟后
|
|
"prize_value_yuan": 100.0,
|
|
"created_ts": time.time() - 200,
|
|
"pos_seen": 1,
|
|
"room_title": "主播小明直播间",
|
|
},
|
|
{
|
|
"future_ts": time.time() + 300, # 5分钟后
|
|
"prize_value_yuan": 80.0,
|
|
"created_ts": time.time() - 50,
|
|
"pos_seen": 7,
|
|
"room_title": "旅游风景直播",
|
|
},
|
|
]
|
|
|
|
print("排序前:")
|
|
for i, r in enumerate(reservations):
|
|
print(
|
|
f" {i+1}. {r['room_title']} (位置{r['pos_seen']}) - {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元"
|
|
)
|
|
|
|
_sort_reservations_inplace(reservations)
|
|
|
|
print("排序后:")
|
|
for i, r in enumerate(reservations):
|
|
print(
|
|
f" {i+1}. {r['room_title']} (位置{r['pos_seen']}) - {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元"
|
|
)
|
|
|
|
# 验证排序:时间优先,其次价值
|
|
if (
|
|
reservations[0]["future_ts"] <= reservations[1]["future_ts"]
|
|
and reservations[1]["future_ts"] <= reservations[2]["future_ts"]
|
|
):
|
|
print("✓ 测试1通过:预约队列排序正确")
|
|
else:
|
|
print("✗ 测试1失败:预约队列排序不正确")
|
|
|
|
# 测试2:位置计算和回退方向
|
|
print("\n测试2:位置计算和回退方向")
|
|
print("-" * 40)
|
|
|
|
test_cases = [
|
|
{
|
|
"pos_seen": 3,
|
|
"expected_direction": "up",
|
|
"expected_steps": 2,
|
|
}, # 从5到3,向上2步
|
|
{
|
|
"pos_seen": 7,
|
|
"expected_direction": "down",
|
|
"expected_steps": 2,
|
|
}, # 从5到7,向下2步
|
|
{
|
|
"pos_seen": 5,
|
|
"expected_direction": "none",
|
|
"expected_steps": 0,
|
|
}, # 已在目标位置
|
|
{
|
|
"pos_seen": 1,
|
|
"expected_direction": "up",
|
|
"expected_steps": 4,
|
|
}, # 从5到1,向上4步
|
|
]
|
|
|
|
for i, case in enumerate(test_cases):
|
|
analyser.room_pos = 5 # 重置位置
|
|
analyser.swipe_up_count = 0
|
|
analyser.swipe_down_count = 0
|
|
|
|
target_pos = case["pos_seen"]
|
|
pos_diff = target_pos - analyser.room_pos
|
|
|
|
print(f"测试{i+1}: 从位置 {analyser.room_pos} 到位置 {target_pos}")
|
|
|
|
if pos_diff == 0:
|
|
direction = "none"
|
|
steps = 0
|
|
elif pos_diff < 0:
|
|
direction = "up"
|
|
steps = min(abs(pos_diff), analyser.high_value_fudai_max_swipe_back)
|
|
else:
|
|
direction = "down"
|
|
steps = min(pos_diff, analyser.high_value_fudai_max_swipe_back)
|
|
|
|
if direction == case["expected_direction"] and steps == case["expected_steps"]:
|
|
print(f" ✓ 方向: {direction}, 步数: {steps}")
|
|
else:
|
|
print(
|
|
f" ✗ 期望: {case['expected_direction']}/{case['expected_steps']}, 实际: {direction}/{steps}"
|
|
)
|
|
|
|
print("✓ 测试2通过:位置计算和回退方向正确")
|
|
|
|
# 测试3:完整回退流程
|
|
print("\n测试3:完整回退流程")
|
|
print("-" * 40)
|
|
|
|
# 重置分析器状态
|
|
analyser.room_pos = 5
|
|
analyser.swipe_up_count = 0
|
|
analyser.swipe_down_count = 0
|
|
analyser.logs = []
|
|
|
|
# 创建预约队列(只有一个预约项用于测试)
|
|
test_reservations = [
|
|
{
|
|
"future_ts": time.time() + 300, # 5分钟后
|
|
"prize_value_yuan": 100.0,
|
|
"created_ts": time.time() - 100,
|
|
"pos_seen": 3, # 目标位置
|
|
"room_title": "游戏直播高手",
|
|
"room_title_raw": "游戏直播高手",
|
|
}
|
|
]
|
|
|
|
# 模拟回调函数
|
|
def mock_swipe_down_back():
|
|
analyser.swipe_down_count += 1
|
|
analyser.room_pos -= 1
|
|
print(f"执行向下滑动,当前位置: {analyser.room_pos}")
|
|
|
|
def mock_swipe_up_to_next():
|
|
analyser.swipe_up_count += 1
|
|
analyser.room_pos += 1
|
|
print(f"执行向上滑动,当前位置: {analyser.room_pos}")
|
|
|
|
def mock_seek_reservation_room(item):
|
|
return analyser._seek_reservation_room(item)
|
|
|
|
def mock_sort_reservations():
|
|
pass
|
|
|
|
def mock_write_state():
|
|
pass
|
|
|
|
# 模拟 process_reservation_attend_window 调用
|
|
print(f"当前所在位置: {analyser.room_pos}")
|
|
print(f"目标预约位置: {test_reservations[0]['pos_seen']}")
|
|
|
|
# 直接调用回退函数
|
|
result = analyser._seek_reservation_room(test_reservations[0])
|
|
|
|
if result and analyser.room_pos == 3:
|
|
print("✓ 测试3通过:成功回退到目标直播间")
|
|
print(
|
|
f" 滑动统计: 向上{analyser.swipe_up_count}次, 向下{analyser.swipe_down_count}次"
|
|
)
|
|
else:
|
|
print("✗ 测试3失败:回退失败")
|
|
print(
|
|
f" 当前位置: {analyser.room_pos}, 目标位置: {test_reservations[0]['pos_seen']}"
|
|
)
|
|
|
|
# 测试4:回退校验
|
|
print("\n测试4:回退校验")
|
|
print("-" * 40)
|
|
|
|
# 模拟读取直播间标题的函数
|
|
def mock_read_live_room_title_core(analyser, y_pianyi):
|
|
# 根据当前位置返回对应的标题
|
|
pos = analyser.room_pos
|
|
if pos in analyser._room_pos_to_title_map:
|
|
title, title_raw = analyser._room_pos_to_title_map[pos]
|
|
return title, title_raw
|
|
return "未知直播间", "未知直播间"
|
|
|
|
# 替换实际函数
|
|
import douyin.fudai.douyin_fudai_detail
|
|
|
|
original_func = douyin.fudai.douyin_fudai_detail.read_live_room_title_core
|
|
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = (
|
|
mock_read_live_room_title_core
|
|
)
|
|
|
|
try:
|
|
# 测试校验通过的情况
|
|
analyser.room_pos = 3 # 在目标位置
|
|
result, detail = validate_back_to_reservation(analyser, test_reservations[0])
|
|
if result:
|
|
print("✓ 校验通过:正确识别目标直播间")
|
|
else:
|
|
print("✗ 校验失败:应该通过但失败了")
|
|
|
|
# 测试校验失败的情况
|
|
analyser.room_pos = 6 # 在位置6(直播间F),目标是位置3(直播间C)
|
|
result, detail = validate_back_to_reservation(analyser, test_reservations[0])
|
|
if not result:
|
|
print("✓ 校验失败:正确识别非目标直播间")
|
|
else:
|
|
print("✗ 校验通过:应该失败但通过了")
|
|
|
|
print("✓ 测试4通过:回退校验功能正常")
|
|
|
|
finally:
|
|
# 恢复原始函数
|
|
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = original_func
|
|
|
|
# 测试5:边界情况
|
|
print("\n测试5:边界情况测试")
|
|
print("-" * 40)
|
|
|
|
# 测试超出最大回退次数的情况
|
|
analyser.room_pos = 5
|
|
analyser.high_value_fudai_max_swipe_back = 2 # 限制最大回退2次
|
|
|
|
far_reservation = {
|
|
"future_ts": time.time() + 300,
|
|
"prize_value_yuan": 100.0,
|
|
"created_ts": time.time() - 100,
|
|
"pos_seen": 1, # 距离5个位置
|
|
"room_title": "直播间A",
|
|
"room_title_raw": "直播间A",
|
|
}
|
|
|
|
result = analyser._seek_reservation_room(far_reservation)
|
|
if not result or analyser.room_pos != 1:
|
|
print("✓ 边界情况处理正确:超出最大回退次数限制")
|
|
else:
|
|
print("✗ 边界情况处理错误:不应该回退超过限制")
|
|
|
|
print("✓ 测试5通过:边界情况处理正确")
|
|
|
|
print("\n" + "=" * 70)
|
|
print("所有测试完成")
|
|
print("=" * 70)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_reservation_backtrack()
|