fix: use ocr live tab and merge stuck room titles

This commit is contained in:
Your Name committed 2026-06-23 02:06:53 +08:00
1 parent e286cd46c8
commit 25f7f9af9e
9 files changed
+582 -16

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
from __future__ import annotations
def test_dashboard_merges_similar_mapping_rows():
from gui.dashboard_app.data import _merge_similar_mapping_rows
rows = [
{
"pos": "3",
"title": "三资堂美妆官方旗舰抖音旗舰店铺回头率超",
"raw_title": "三资堂美妆官方旗舰抖音旗舰店铺回头率超7",
"reserved": "-",
"has_fudai": "-",
"value": "-",
"countdown": "-",
"participants": "-",
"copies": "-",
"button": "-",
"task": "-",
"content": "-",
"updated": "-",
},
{
"pos": "4",
"title": "三资堂美妆官方旗舰抖音旗舰美妆达人好店",
"raw_title": "三资堂美妆官方旗舰抖音旗舰美妆达人好店",
"reserved": "-",
"has_fudai": "-",
"value": "-",
"countdown": "-",
"participants": "-",
"copies": "-",
"button": "-",
"task": "-",
"content": "-",
"updated": "-",
},
]
merged = _merge_similar_mapping_rows(rows)
assert len(merged) == 1
assert merged[0]["pos"] == "3"
+119
View File
@@ -0,0 +1,119 @@
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_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_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_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=[
([[300, 10], [340, 10], [340, 35], [300, 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_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)]
+61
View File
@@ -62,6 +62,33 @@ def test_duplicate_titles_are_merged_to_smaller_pos():
assert merged.get("merged_positions") == [1, 2]
def test_similar_long_titles_are_merged_to_smaller_pos():
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod
a = _mk_analyser()
a.room_pos = 4
a.current_room_title = "三资堂美妆官方旗舰抖音旗舰美妆达人好店"
a.current_room_title_raw = "三资堂美妆官方旗舰抖音旗舰美妆达人好店"
a._room_title_identified_positions = {4}
a._room_pos_to_title_map = {
3: {
"title": "三资堂美妆官方旗舰抖音旗舰店铺回头率超",
"title_raw": "三资堂美妆官方旗舰抖音旗舰店铺回头率超7",
"has_fudai": False,
"fudai_content": "",
"fudai_detail": None,
"timestamp": 1.0,
}
}
with patch.object(mod, "_dump_room_pos_map_to_state", return_value=None):
consumed = mod.record_room_title_and_pos(a)
assert consumed is False
assert 4 not in a._room_pos_to_title_map
assert a._room_pos_to_title_map[3]["merged_positions"] == [3, 4]
def test_merge_room_info_keeps_explicit_no_fudai_state():
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod
@@ -156,6 +183,40 @@ def test_same_room_streak_triggers_reset_and_consumes_swipe():
assert len(a._room_pos_to_title_map) >= 1
def test_similar_same_room_streak_triggers_reset_and_consumes_swipe():
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod
a = _mk_analyser()
a.room_pos = 6
a.current_room_title = "三资堂美妆官方旗舰抖音旗舰小体"
a.current_room_title_raw = "三资堂美妆官方旗舰抖音旗舰小体"
a._room_title_identified_positions = {6}
a._room_pos_to_title_map = {
6: {
"title": "三资堂美妆官方旗舰抖音旗舰小体",
"title_raw": "三资堂美妆官方旗舰抖音旗舰小体",
"has_fudai": False,
"fudai_content": "",
"fudai_detail": None,
"timestamp": 1.0,
}
}
a._record_title_before_swipe_up = True
a._last_room_title_norm_seen = "三资堂美妆官方旗舰抖音旗舰半年百万人搜"
a._same_room_title_streak = 2
with patch.object(mod, "_dump_room_pos_map_to_state", return_value=None):
with patch(
"douyin.reservation.douyin_reservation_handler_pkg.record_title.douyin_swipe_handler.SwipeHandler.reset_to_top_keep_mapping",
return_value=True,
) as mock_reset:
consumed = mod.record_room_title_and_pos(a)
assert consumed is True
mock_reset.assert_called_once()
assert int(getattr(a, "_same_room_title_streak", -1)) == 0
def test_same_room_after_recent_reset_triggers_back_then_next_escape():
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod