192 lines
6.7 KiB
Python
192 lines
6.7 KiB
Python
"""douyin/handlers/screenshot_manager/ 子包单元测试。
|
||
|
||
覆盖纯函数子模块:paths(分类逻辑、路径构造)、_common(异步计数器)。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from datetime import datetime
|
||
|
||
import pytest
|
||
|
||
from douyin.handlers.screenshot_manager import (
|
||
_SKIP_DEBUG_SAVE_IN_HOTLOOPS,
|
||
ensure_debug_captures_dir,
|
||
ensure_ocr_dir,
|
||
get_async_save_counters,
|
||
get_category_for_picname,
|
||
get_debug_captures_base_dir,
|
||
get_debug_captures_dir,
|
||
get_last_success_path,
|
||
get_latest_attempt_path,
|
||
get_ocr_dir,
|
||
reset_async_save_counters,
|
||
)
|
||
from douyin.handlers.screenshot_manager.cleanup import cleanup_debug_captures
|
||
|
||
|
||
# ============================================================
|
||
# paths.py — get_category_for_picname
|
||
# ============================================================
|
||
|
||
class TestGetCategoryForPicname:
|
||
@pytest.mark.parametrize(
|
||
"picname,expected",
|
||
[
|
||
("福袋详情", "fudai"),
|
||
("fudai_detail", "fudai"),
|
||
("直播间", "live_room"),
|
||
("live_room_state", "live_room"),
|
||
("直播列表", "live_room"),
|
||
("直播广场", "live_room"),
|
||
("coupon_popup", "popups"),
|
||
("redpacket_dialog", "popups"),
|
||
("抽奖结果", "popups"),
|
||
("无奖励", "popups"),
|
||
("订单确认", "popups"),
|
||
("参与按钮", "buttons"),
|
||
("attend_button_red", "buttons"),
|
||
("粉丝团", "buttons"),
|
||
("fans_club_join", "buttons"),
|
||
("推荐页", "stage_diag"),
|
||
("recommend_tab", "stage_diag"),
|
||
("关注列表", "stage_diag"),
|
||
("follow_list", "stage_diag"),
|
||
# 注意:"直播列表标签" 含"直播列表",按关键词优先级先归 live_room
|
||
("live_list_tab", "stage_diag"),
|
||
],
|
||
)
|
||
def test_known_categories(self, picname, expected):
|
||
assert get_category_for_picname(picname) == expected
|
||
|
||
@pytest.mark.parametrize("picname", ["random_pic", "unknown", "abc123", "test"])
|
||
def test_unknown_falls_back_to_others(self, picname):
|
||
assert get_category_for_picname(picname) == "others"
|
||
|
||
def test_case_insensitive(self):
|
||
# 实现里 picname.lower() 后匹配
|
||
assert get_category_for_picname("FUDAI_DETAIL") == "fudai"
|
||
assert get_category_for_picname("LIVE_ROOM") == "live_room"
|
||
|
||
|
||
# ============================================================
|
||
# paths.py — get_latest_attempt_path / get_last_success_path
|
||
# ============================================================
|
||
|
||
class TestPaths:
|
||
@pytest.mark.parametrize(
|
||
"operation_type,expected_suffix",
|
||
[
|
||
("pixel", "latest_attempt_pixel.png"),
|
||
("pixel_detect", "latest_attempt_pixel.png"),
|
||
("ocr", "latest_attempt_ocr.png"),
|
||
("cut_pic", "latest_attempt_ocr.png"),
|
||
("template", "latest_attempt_template.png"),
|
||
("template_match", "latest_attempt_template.png"),
|
||
("general", "latest_attempt_general.png"),
|
||
("", "latest_attempt_general.png"),
|
||
("unknown_type", "latest_attempt_general.png"),
|
||
],
|
||
)
|
||
def test_latest_attempt_path(self, operation_type, expected_suffix):
|
||
result = get_latest_attempt_path(operation_type)
|
||
assert result.endswith(expected_suffix), f"got {result}"
|
||
|
||
@pytest.mark.parametrize(
|
||
"operation_type,expected_suffix",
|
||
[
|
||
("pixel", "last_success_pixel.png"),
|
||
("ocr", "last_success_ocr.png"),
|
||
("template", "last_success_template.png"),
|
||
("general", "last_success_general.png"),
|
||
],
|
||
)
|
||
def test_last_success_path(self, operation_type, expected_suffix):
|
||
result = get_last_success_path(operation_type)
|
||
assert result.endswith(expected_suffix), f"got {result}"
|
||
|
||
def test_default_operation_type_is_general(self):
|
||
latest = get_latest_attempt_path()
|
||
success = get_last_success_path()
|
||
assert latest.endswith("latest_attempt_general.png")
|
||
assert success.endswith("last_success_general.png")
|
||
|
||
def test_get_ocr_dir_returns_str(self):
|
||
d = get_ocr_dir()
|
||
assert isinstance(d, str)
|
||
assert d.endswith("ocr")
|
||
|
||
def test_get_debug_captures_dir_includes_date(self):
|
||
# 该函数会按今天的日期返回路径
|
||
d = get_debug_captures_dir()
|
||
assert isinstance(d, str)
|
||
assert "debug_captures" in d
|
||
# 日期目录形如 YYYY-MM-DD
|
||
assert any(part.count("-") == 2 and len(part) == 10 for part in d.split(os.sep))
|
||
|
||
def test_get_debug_captures_base_dir_is_parent(self):
|
||
base = get_debug_captures_base_dir()
|
||
assert isinstance(base, str)
|
||
# base 应是 dated 目录的上级(包含 'debug_captures')
|
||
assert base.endswith("debug_captures")
|
||
|
||
|
||
# ============================================================
|
||
# paths.py — ensure_*
|
||
# ============================================================
|
||
|
||
class TestEnsureDirs:
|
||
def test_ensure_ocr_dir_idempotent(self):
|
||
# 应不抛
|
||
ensure_ocr_dir()
|
||
ensure_ocr_dir()
|
||
|
||
def test_ensure_debug_captures_dir_idempotent(self):
|
||
ensure_debug_captures_dir()
|
||
ensure_debug_captures_dir()
|
||
|
||
def test_cleanup_keeps_today_debug_captures_when_file_limit_exceeded(
|
||
self, tmp_path, monkeypatch
|
||
):
|
||
base_dir = tmp_path / "debug_captures"
|
||
today_dir = base_dir / datetime.now().strftime("%Y-%m-%d")
|
||
today_dir.mkdir(parents=True)
|
||
for index in range(3):
|
||
(today_dir / f"capture_{index}.png").write_bytes(b"png")
|
||
|
||
monkeypatch.setattr(
|
||
"douyin.handlers.screenshot_manager.cleanup.get_debug_captures_base_dir",
|
||
lambda: str(base_dir),
|
||
)
|
||
|
||
deleted = cleanup_debug_captures(keep_days=999, max_files=1)
|
||
|
||
assert deleted == 0
|
||
assert today_dir.exists()
|
||
assert len(list(today_dir.glob("*.png"))) == 3
|
||
|
||
|
||
# ============================================================
|
||
# _common.py — 异步计数器
|
||
# ============================================================
|
||
|
||
class TestAsyncSaveCounters:
|
||
def test_returns_tuple_of_int(self):
|
||
s, f = get_async_save_counters()
|
||
assert isinstance(s, int)
|
||
assert isinstance(f, int)
|
||
|
||
def test_reset(self):
|
||
reset_async_save_counters()
|
||
s, f = get_async_save_counters()
|
||
assert s == 0 and f == 0
|
||
|
||
|
||
# ============================================================
|
||
# 环境开关
|
||
# ============================================================
|
||
|
||
class TestSkipDebugFlag:
|
||
def test_is_bool(self):
|
||
assert isinstance(_SKIP_DEBUG_SAVE_IN_HOTLOOPS, bool)
|