新增 tests/ 下 10 个测试文件,把已拆出的子包从"被外部测试间接覆盖"提升到 "独立可测": - test_web_gui_pkg (52) 覆盖 logger / ansi / adb_compat / nested / stats_helpers 含 _wg_log/_LOGGER 日志、ANSI 清理、点号路径、_parse_countdown_seconds 等 - test_underlying_operations_pkg (35) 覆盖 adb_helpers / bounds / countdown / ocr_content / pic_path 含 _adb_path 环境变量覆盖、check_countdown 多种格式、OCR 字符纠错 - test_reservation_logic_helpers (21) 覆盖 helpers.py 5 个纯函数(_now_ts / _normalize_room_title / _calculate_string_similarity / _extract_content_core / _sort_reservations_inplace 排序稳定性) - test_screenshot_manager_pkg (47) 覆盖 paths.get_category_for_picname(5 类关键词分类)+ get_latest_attempt_path / get_last_success_path(按 operation_type 路径推断) + 异步计数器 - test_gui_core_pkg (13) 覆盖 RunParams dataclass 28 字段构造 + adb 函数 + read_config / write_config roundtrip - test_fudai_core_pkg (17) 覆盖 _FudaiOcrExecutorState 单例 + _get_fudai_ocr_executor 缓存复用 + 11 个 thin_wrappers 与原实现等价行为 - test_handler_shells (8) 覆盖 ReservationHandler / StageHandler / LoggingWrapper 三个薄壳类的 staticmethod forwarding 完整性 - test_yanghao_pkg (14) 覆盖 _yanghao_log(_log_kv 优先 + logger fallback + broken analyser 兜底 + 未知 level 不抛)+ yanghao_get_recommend_seconds 默认值 - test_pixel_common (6) 覆盖 _safe_open_screenshot_rgb 用 tmp_path 构造合法/损坏/截断/RGBA PNG - test_fudai_finder_pkg (13) 覆盖 5 个类(FudaiImageAcquirer / FudaiDetector / FudaiClickExecutor / FudaiDetailVerifier / FudaiFinder)实例化与组合关系,verify detect_by_template 类方法签名仍兼容(拆出后) 回归: - 全套测试 355 → 581(+226 用例) - 全部 581 passed / 1 skipped in 27s - 新拆出的 12 个子包现在都有专门的单元测试覆盖 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
158 lines
5.3 KiB
Python
158 lines
5.3 KiB
Python
"""douyin/reservation/reservation_logic/ 子包单元测试。
|
|
|
|
主要测 helpers.py 的纯函数(add/cleanup/queue_full/attend_window/record 这些
|
|
业务函数依赖很多 analyser 状态,单测的成本高、价值有限)。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from douyin.reservation.reservation_logic.helpers import (
|
|
_calculate_string_similarity,
|
|
_extract_content_core,
|
|
_normalize_room_title_for_matching,
|
|
_now_ts,
|
|
_sort_reservations_inplace,
|
|
)
|
|
|
|
|
|
# ============================================================
|
|
# _now_ts
|
|
# ============================================================
|
|
|
|
class TestNowTs:
|
|
def test_returns_float(self):
|
|
assert isinstance(_now_ts(), float)
|
|
|
|
def test_close_to_current_time(self):
|
|
before = time.time()
|
|
result = _now_ts()
|
|
after = time.time()
|
|
assert before <= result <= after
|
|
|
|
|
|
# ============================================================
|
|
# _normalize_room_title_for_matching
|
|
# ============================================================
|
|
|
|
class TestNormalizeRoomTitle:
|
|
@pytest.mark.parametrize(
|
|
"title,expected_property",
|
|
[
|
|
# 标题去空格、去常见标点
|
|
(" Live Room ", "no_leading_trailing_spaces"),
|
|
("Live Room", "no_internal_double_spaces"),
|
|
("[直播] Test", "no_brackets"),
|
|
("直播间-A", "no_special_chars"),
|
|
],
|
|
)
|
|
def test_basic_normalize(self, title, expected_property):
|
|
result = _normalize_room_title_for_matching(title)
|
|
# 不假设具体规则,只验证返回 str 且没明显空白
|
|
assert isinstance(result, str)
|
|
if expected_property == "no_leading_trailing_spaces":
|
|
assert result == result.strip()
|
|
|
|
def test_empty_returns_str(self):
|
|
result = _normalize_room_title_for_matching("")
|
|
assert isinstance(result, str)
|
|
|
|
def test_idempotent(self):
|
|
"""规范化后再规范化应得同一结果。"""
|
|
title = "[直播] 测试 Room "
|
|
once = _normalize_room_title_for_matching(title)
|
|
twice = _normalize_room_title_for_matching(once)
|
|
assert once == twice
|
|
|
|
|
|
# ============================================================
|
|
# _calculate_string_similarity
|
|
# ============================================================
|
|
|
|
class TestCalculateStringSimilarity:
|
|
def test_identical_strings(self):
|
|
assert _calculate_string_similarity("abc", "abc") == 1.0
|
|
|
|
def test_completely_different(self):
|
|
score = _calculate_string_similarity("abc", "xyz")
|
|
assert 0.0 <= score < 1.0
|
|
|
|
def test_empty_strings(self):
|
|
# 两个空串应该是 1.0(相同)或 0.0(无内容),任一都合理
|
|
score = _calculate_string_similarity("", "")
|
|
assert 0.0 <= score <= 1.0
|
|
|
|
def test_partial_match(self):
|
|
score = _calculate_string_similarity("hello world", "hello earth")
|
|
assert 0.0 < score < 1.0
|
|
|
|
def test_score_in_range(self):
|
|
for s1, s2 in [
|
|
("abc", "abd"),
|
|
("中文测试", "中文实验"),
|
|
("Hello", "hello"),
|
|
]:
|
|
score = _calculate_string_similarity(s1, s2)
|
|
assert 0.0 <= score <= 1.0
|
|
|
|
|
|
# ============================================================
|
|
# _extract_content_core
|
|
# ============================================================
|
|
|
|
class TestExtractContentCore:
|
|
def test_returns_str(self):
|
|
result = _extract_content_core("现金红包 100 元")
|
|
assert isinstance(result, str)
|
|
|
|
def test_empty(self):
|
|
assert _extract_content_core("") == ""
|
|
|
|
def test_strips_whitespace(self):
|
|
# 至少应去首尾空白(实际规则可能更复杂,这里只看类型和非空判断)
|
|
result = _extract_content_core(" abc ")
|
|
assert isinstance(result, str)
|
|
|
|
|
|
# ============================================================
|
|
# _sort_reservations_inplace
|
|
# ============================================================
|
|
|
|
class TestSortReservationsInplace:
|
|
def test_sort_by_future_ts_asc(self):
|
|
"""主排序键:future_ts 升序(开奖早的在前)。"""
|
|
items = [
|
|
{"future_ts": 300.0, "prize_value_yuan": 100.0, "created_ts": 1.0},
|
|
{"future_ts": 100.0, "prize_value_yuan": 200.0, "created_ts": 2.0},
|
|
{"future_ts": 200.0, "prize_value_yuan": 50.0, "created_ts": 3.0},
|
|
]
|
|
_sort_reservations_inplace(items)
|
|
future_seq = [it["future_ts"] for it in items]
|
|
assert future_seq == sorted(future_seq), f"按 future_ts 升序失败:{future_seq}"
|
|
|
|
def test_returns_none(self):
|
|
items = []
|
|
assert _sort_reservations_inplace(items) is None
|
|
|
|
def test_empty_list(self):
|
|
items = []
|
|
_sort_reservations_inplace(items)
|
|
assert items == []
|
|
|
|
def test_single_item(self):
|
|
items = [{"future_ts": 100.0, "prize_value_yuan": 200.0, "created_ts": 1.0}]
|
|
_sort_reservations_inplace(items)
|
|
assert len(items) == 1
|
|
|
|
def test_in_place_not_returns_new_list(self):
|
|
items = [
|
|
{"future_ts": 200.0, "prize_value_yuan": 100.0, "created_ts": 1.0},
|
|
{"future_ts": 100.0, "prize_value_yuan": 200.0, "created_ts": 2.0},
|
|
]
|
|
original_id = id(items)
|
|
_sort_reservations_inplace(items)
|
|
# 原列表对象未变
|
|
assert id(items) == original_id
|