Files

219 lines
7.1 KiB
Python

from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from douyin.handlers.live_tab_ocr import (
OcrTextBox,
choose_live_tab_candidate,
choose_live_tab_box,
click_live_tab_on_recommend_page,
parse_ocr_text_boxes,
)
def test_parse_easyocr_live_bbox():
raw = [([[10, 5], [50, 5], [50, 25], [10, 25]], "直播", 0.93)]
boxes = parse_ocr_text_boxes(raw)
assert boxes == [OcrTextBox(text="直播", bbox=(10.0, 5.0, 50.0, 25.0), confidence=0.93)]
def test_parse_paddle_dict_boxes():
raw = [{"rec_texts": ["推荐", "直播"], "rec_boxes": [[1, 2, 20, 12], [40, 2, 70, 14]], "rec_scores": [0.8, 0.9]}]
boxes = parse_ocr_text_boxes(raw)
assert boxes[1] == OcrTextBox(text="直播", bbox=(40.0, 2.0, 70.0, 14.0), confidence=0.9)
def test_parse_paddle_rec_polys_boxes():
class ArrayLike:
def __init__(self, value):
self.value = value
def tolist(self):
return self.value
raw = [
{
"rec_texts": ["=购精选商城关注直播推荐"],
"rec_polys": ArrayLike([[[0, 0], [1080, 0], [1080, 80], [0, 80]]]),
"rec_scores": [0.92],
}
]
boxes = parse_ocr_text_boxes(raw)
assert boxes == [
OcrTextBox(text="=购精选商城关注直播推荐", bbox=(0.0, 0.0, 1080.0, 80.0), confidence=0.92)
]
def test_choose_live_tab_ignores_long_text():
selected = choose_live_tab_box(
[
OcrTextBox(text="直播广场", bbox=(1, 1, 50, 20), confidence=0.99),
OcrTextBox(text="直播", bbox=(70, 1, 100, 20), confidence=0.80),
]
)
assert selected is not None
assert selected.text == "直播"
def test_choose_live_tab_from_joined_tab_row_estimates_substring_center():
selected = choose_live_tab_candidate(
[
OcrTextBox(
text="=购精选商城关注直播推荐",
bbox=(0.0, 0.0, 1080.0, 80.0),
confidence=0.92,
)
]
)
assert selected is not None
assert selected.box.text == "=购精选商城关注直播推荐"
assert selected.center_x == 810.0
assert selected.center_y == 40.0
def test_click_live_tab_uses_ocr_bbox(monkeypatch, tmp_path):
pic = tmp_path / "live_tab.png"
from PIL import Image
Image.new("RGB", (500, 60), (255, 255, 255)).save(pic)
analyser = SimpleNamespace()
analyser.device_id = "D"
analyser.resolution_ratio_x = 1080
analyser.resolution_ratio_y = 2400
analyser.cut_pic = MagicMock()
analyser._log_kv = MagicMock()
analyser.operation = SimpleNamespace()
analyser.operation.click = MagicMock()
analyser.operation._find_pic_path = MagicMock(return_value=str(pic))
analyser.operation.ocr_type = "easyocr"
analyser.operation.ocr = SimpleNamespace(
readtext=MagicMock(
return_value=[
([[700, 10], [740, 10], [740, 35], [700, 35]], "直播", 0.96)
]
)
)
x, y, method = click_live_tab_on_recommend_page(analyser)
assert (x, y, method) == (720, 162, "ocr_live_tab")
analyser.operation.click.assert_called_once_with("D", 720, 162)
def test_click_live_tab_falls_back_when_ocr_misses(tmp_path):
pic = tmp_path / "live_tab.png"
from PIL import Image
Image.new("RGB", (500, 60), (255, 255, 255)).save(pic)
analyser = SimpleNamespace()
analyser.device_id = "D"
analyser.resolution_ratio_x = 1080
analyser.resolution_ratio_y = 2400
analyser.cut_pic = MagicMock()
analyser._log_kv = MagicMock()
analyser.operation = SimpleNamespace()
analyser.operation.click = MagicMock()
analyser.operation._find_pic_path = MagicMock(return_value=str(pic))
analyser.operation.ocr_type = "easyocr"
analyser.operation.ocr = SimpleNamespace(readtext=MagicMock(return_value=[]))
x, y, method = click_live_tab_on_recommend_page(analyser)
assert (x, y, method) == (718, 169, "fixed_fallback")
analyser.operation.click.assert_called_once_with("D", 718, 169)
def test_click_live_tab_uses_joined_row_substring(tmp_path):
pic = tmp_path / "live_tab.png"
from PIL import Image
Image.new("RGB", (1080, 80), (255, 255, 255)).save(pic)
analyser = SimpleNamespace()
analyser.device_id = "D"
analyser.resolution_ratio_x = 1080
analyser.resolution_ratio_y = 2400
analyser.cut_pic = MagicMock()
analyser._log_kv = MagicMock()
analyser.operation = SimpleNamespace()
analyser.operation.click = MagicMock()
analyser.operation._find_pic_path = MagicMock(return_value=str(pic))
analyser.operation.ocr_type = "easyocr"
analyser.operation.ocr = SimpleNamespace(
readtext=MagicMock(
return_value=[
(
[[0, 0], [1080, 0], [1080, 80], [0, 80]],
"商城关注直播推荐",
0.96,
)
]
)
)
x, y, method = click_live_tab_on_recommend_page(analyser)
assert 660 <= x <= 700
assert y == 180
assert method == "ocr_live_tab"
def test_click_live_tab_uses_text_fallback_when_raw_has_no_bbox(tmp_path):
pic = tmp_path / "live_tab.png"
from PIL import Image
Image.new("RGB", (1080, 80), (255, 255, 255)).save(pic)
analyser = SimpleNamespace()
analyser.device_id = "D"
analyser.resolution_ratio_x = 1080
analyser.resolution_ratio_y = 2400
analyser.cut_pic = MagicMock()
analyser._log_kv = MagicMock()
analyser.operation = SimpleNamespace()
analyser.operation.click = MagicMock()
analyser.operation._find_pic_path = MagicMock(return_value=str(pic))
analyser.operation.analyse_pic_word = MagicMock(return_value="=购精选商城关注直播推荐")
analyser.operation.ocr_type = "paddleocr"
analyser.operation.ocr = SimpleNamespace(predict=MagicMock(return_value=[{"rec_texts": []}]))
x, y, method = click_live_tab_on_recommend_page(analyser)
assert x == 718
assert y == 180
assert method == "ocr_live_tab"
analyser.operation.click.assert_called_once_with("D", x, y)
def test_recommend_page_keeps_second_step_entry_mode():
from douyin.core.fudai_modules import run_once_basic_helpers as ronce
s = MagicMock()
s.device_id = "D"
s.resolution_ratio_x = 1080
s.resolution_ratio_y = 2400
s.FOLLOW_TAB_CLICK_DELAY = 0
s.ENTER_LIVE_CLICK_DELAY = 0
s.ENTER_LIVE_FINAL_DELAY = 0
s.RETURN_STEP_DELAY_SECONDS = 0
s.operation.click = MagicMock()
s.operation.delay = MagicMock()
s._log_kv = MagicMock()
def _click_ocr_live_tab(analyser):
analyser.operation.click(analyser.device_id, 720, 162)
return 720, 162, "ocr_live_tab"
with patch(
"douyin.handlers.live_tab_ocr.click_live_tab_on_recommend_page",
side_effect=_click_ocr_live_tab,
), patch(
"douyin.handlers.douyin_swipe_handler.SwipeHandler.resolve_reenter_live_click_point",
return_value=(540, 1200, "screen_center"),
):
ronce.dispatch_stage_branch(s, "RECOMMEND_PAGE", {}, 0.0)
calls = [call.args for call in s.operation.click.call_args_list]
assert calls == [("D", 720, 162), ("D", 540, 1200), ("D", 540, 1200)]