286 lines
8.9 KiB
Python
286 lines
8.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
预约回退校验测试脚本
|
|
===================
|
|
|
|
测试预约校验是否能正确回退直播间。
|
|
|
|
测试内容:
|
|
1. 基本校验通过:直播间标题匹配时校验通过
|
|
2. 标题不匹配:直播间标题不匹配时校验失败
|
|
3. 校验禁用:当校验开关关闭时直接通过
|
|
4. 福袋详情匹配:预约项与实际福袋详情匹配
|
|
5. 福袋详情不匹配:预约项与实际福袋详情不匹配
|
|
6. 观看任务检测:自动检测并跳过观看任务类型的福袋
|
|
7. 预约队列排序:验证预约队列按时间和价值正确排序
|
|
|
|
运行方法:
|
|
python test_reservation_verify.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_verify import (
|
|
validate_back_to_reservation,
|
|
match_reservation,
|
|
)
|
|
from douyin.reservation.douyin_reservation_logic import ReservationItem
|
|
|
|
|
|
class MockAnalyser:
|
|
"""模拟分析器对象"""
|
|
|
|
def __init__(self):
|
|
self.reservation_back_verify_enabled = True
|
|
self._room_pos_to_title_map = {}
|
|
self.y_pianyi = 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 get_screenshot(self, device_id, use_service=False):
|
|
"""模拟截图"""
|
|
pass
|
|
|
|
|
|
def mock_read_live_room_title_core(analyser, y_pianyi):
|
|
"""模拟读取直播间标题"""
|
|
# 模拟当前在目标直播间
|
|
return "测试直播间", "测试直播间"
|
|
|
|
|
|
def test_reservation_verify():
|
|
"""测试预约回退校验"""
|
|
|
|
print("=" * 60)
|
|
print("预约回退校验测试")
|
|
print("=" * 60)
|
|
|
|
# 创建模拟分析器
|
|
analyser = MockAnalyser()
|
|
|
|
# 模拟替换实际的读取函数
|
|
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:
|
|
# 测试1:基本校验通过
|
|
print("\n测试1:基本校验通过")
|
|
print("-" * 30)
|
|
|
|
reservation_item = {
|
|
"room_title": "测试直播间",
|
|
"room_title_raw": "测试直播间",
|
|
"pos_seen": 1,
|
|
"content": "测试福袋",
|
|
"prize_value_yuan": 100.0,
|
|
"future_ts": time.time() + 300, # 5分钟后
|
|
}
|
|
|
|
# 设置运行时映射表
|
|
analyser._room_pos_to_title_map = {1: ("测试直播间", "测试直播间")}
|
|
|
|
result, detail = validate_back_to_reservation(analyser, reservation_item)
|
|
print(f"校验结果: {result}")
|
|
print(f"详情: {detail}")
|
|
|
|
if result:
|
|
print("✓ 测试1通过:校验正确通过")
|
|
else:
|
|
print("✗ 测试1失败:校验应该通过但失败了")
|
|
|
|
# 测试2:标题不匹配
|
|
print("\n测试2:标题不匹配")
|
|
print("-" * 30)
|
|
|
|
analyser.logs = [] # 清空日志
|
|
|
|
def mock_read_live_room_title_core_wrong(analyser, y_pianyi):
|
|
"""模拟读取到错误的直播间标题"""
|
|
return "完全不同的直播间名称", "完全不同的直播间名称"
|
|
|
|
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = (
|
|
mock_read_live_room_title_core_wrong
|
|
)
|
|
|
|
result, detail = validate_back_to_reservation(analyser, reservation_item)
|
|
print(f"校验结果: {result}")
|
|
print(f"详情: {detail}")
|
|
|
|
if not result:
|
|
print("✓ 测试2通过:校验正确失败")
|
|
else:
|
|
print("✗ 测试2失败:校验应该失败但通过了")
|
|
|
|
# 测试3:校验禁用
|
|
print("\n测试3:校验禁用")
|
|
print("-" * 30)
|
|
|
|
analyser.logs = [] # 清空日志
|
|
analyser.reservation_back_verify_enabled = False
|
|
|
|
result, detail = validate_back_to_reservation(analyser, reservation_item)
|
|
print(f"校验结果: {result}")
|
|
print(f"详情: {detail}")
|
|
|
|
if result:
|
|
print("✓ 测试3通过:校验禁用时直接通过")
|
|
else:
|
|
print("✗ 测试3失败:校验禁用时应该直接通过")
|
|
|
|
# 测试4:福袋详情匹配
|
|
print("\n测试4:福袋详情匹配")
|
|
print("-" * 30)
|
|
|
|
analyser.reservation_back_verify_enabled = True
|
|
analyser.logs = [] # 清空日志
|
|
|
|
# 模拟福袋详情
|
|
detail_tuple = (
|
|
"测试福袋", # content
|
|
"05:00", # time_text
|
|
100, # participants
|
|
10, # copies
|
|
100.0, # prize_value_yuan
|
|
(time.time(), time.time() + 300), # countdown_tuple
|
|
)
|
|
|
|
match_result, reason, debug_info = match_reservation(
|
|
reservation_item, detail_tuple
|
|
)
|
|
print(f"匹配结果: {match_result}")
|
|
print(f"原因: {reason}")
|
|
print(f"调试信息: {debug_info}")
|
|
|
|
if match_result:
|
|
print("✓ 测试4通过:福袋详情匹配成功")
|
|
else:
|
|
print("✗ 测试4失败:福袋详情应该匹配")
|
|
|
|
# 测试5:福袋详情不匹配
|
|
print("\n测试5:福袋详情不匹配")
|
|
print("-" * 30)
|
|
|
|
analyser.logs = [] # 清空日志
|
|
|
|
# 模拟不匹配的福袋详情
|
|
detail_tuple_mismatch = (
|
|
"不同的福袋", # content
|
|
"05:00", # time_text
|
|
100, # participants
|
|
10, # copies
|
|
200.0, # prize_value_yuan (不匹配)
|
|
(time.time(), time.time() + 600), # countdown_tuple (时间不匹配)
|
|
)
|
|
|
|
match_result, reason, debug_info = match_reservation(
|
|
reservation_item, detail_tuple_mismatch
|
|
)
|
|
print(f"匹配结果: {match_result}")
|
|
print(f"原因: {reason}")
|
|
print(f"调试信息: {debug_info}")
|
|
|
|
# 测试6:观看任务检测
|
|
print("\n测试6:观看任务检测")
|
|
print("-" * 30)
|
|
|
|
analyser.logs = [] # 清空日志
|
|
|
|
# 模拟观看任务的福袋详情
|
|
watch_task_detail = (
|
|
"观看5分钟领取福袋奖励", # content
|
|
"05:00", # time_text
|
|
100, # participants
|
|
10, # copies
|
|
100.0, # prize_value_yuan
|
|
(time.time(), time.time() + 300), # countdown_tuple
|
|
)
|
|
|
|
match_result, reason, debug_info = match_reservation(
|
|
reservation_item, watch_task_detail
|
|
)
|
|
print(f"匹配结果: {match_result}")
|
|
print(f"原因: {reason}")
|
|
print(f"调试信息: {debug_info}")
|
|
|
|
if not match_result and reason == "watch_task_detected":
|
|
print("✓ 测试6通过:观看任务正确被检测并跳过")
|
|
else:
|
|
print("✗ 测试6失败:观看任务应该被检测并跳过")
|
|
|
|
# 测试7:预约队列排序
|
|
print("\n测试7:预约队列排序")
|
|
print("-" * 30)
|
|
|
|
from douyin.reservation.douyin_reservation_logic import (
|
|
_sort_reservations_inplace,
|
|
)
|
|
|
|
reservations = [
|
|
{
|
|
"future_ts": time.time() + 600, # 10分钟后
|
|
"prize_value_yuan": 50.0,
|
|
"created_ts": time.time() - 100,
|
|
},
|
|
{
|
|
"future_ts": time.time() + 300, # 5分钟后
|
|
"prize_value_yuan": 100.0,
|
|
"created_ts": time.time() - 200,
|
|
},
|
|
{
|
|
"future_ts": time.time() + 300, # 5分钟后
|
|
"prize_value_yuan": 80.0,
|
|
"created_ts": time.time() - 50,
|
|
},
|
|
]
|
|
|
|
print("排序前:")
|
|
for i, r in enumerate(reservations):
|
|
print(
|
|
f" {i+1}. 时间: {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}. 时间: {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元"
|
|
)
|
|
|
|
# 验证排序:时间优先,其次价值
|
|
expected_order = [1, 2, 0] # 索引顺序
|
|
if (
|
|
reservations[0]["future_ts"] <= reservations[1]["future_ts"]
|
|
and reservations[1]["future_ts"] <= reservations[2]["future_ts"]
|
|
):
|
|
print("✓ 测试7通过:预约队列排序正确")
|
|
else:
|
|
print("✗ 测试7失败:预约队列排序不正确")
|
|
|
|
finally:
|
|
# 恢复原始函数
|
|
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = original_func
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_reservation_verify()
|