From daddabd8cf822356068fe9dd7e368fb6ecf109b0 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 04:31:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(stage):=20=E6=8B=86=E5=87=BA=E6=AD=A5?= =?UTF-8?q?=E9=AA=A43=20OCR=20=E6=8E=A8=E8=8D=90/=E5=85=B3=E6=B3=A8/?= =?UTF-8?q?=E7=9B=B4=E6=92=AD=E5=88=97=E8=A1=A8=E6=A3=80=E6=B5=8B=EF=BC=88?= =?UTF-8?q?1066=E2=86=92814=EF=BC=8C-252=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 douyin_stage 步骤3 中的"OCR 推荐页/关注列表/直播列表标签栏"检测块(269 行)抽到 detect_recommend_follow_live_tabs。三个相似的"裁剪 + mode0+mode1 双 OCR + 关键词 匹配"块共享 _ocr_with_dual_modes 辅助。 新增 5 个单元测试覆盖 RECOMMEND/FOLLOW/LIVE 命中 + 全部不命中 + 外层异常吞噬。 1284 → 1289 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/stage/douyin_stage.py | 282 ++------------------------- douyin/stage/douyin_stage_helpers.py | 266 +++++++++++++++++++++++++ tests/test_douyin_stage_helpers.py | 78 ++++++++ 3 files changed, 359 insertions(+), 267 deletions(-) diff --git a/douyin/stage/douyin_stage.py b/douyin/stage/douyin_stage.py index 853e562..cfe1051 100644 --- a/douyin/stage/douyin_stage.py +++ b/douyin/stage/douyin_stage.py @@ -16,6 +16,7 @@ from douyin.fudai.core.coords import ( ) from douyin.stage.douyin_stage_helpers import ( detect_lottery_result_popup, + detect_recommend_follow_live_tabs, detect_redpacket_ocr_recheck_outside_live, detect_shopping_redpacket_popup, log_pixel_detection_details, @@ -256,274 +257,21 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t return stage, reason # 步骤3:检测推荐页面、关注列表、直播列表(OCR标签栏,整合在一起检测) - step_start = time.time() - remaining_time_before = max_diag_time - (time.time() - diag_start) - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-开始", 剩余时间秒=remaining_time_before + matched, stage, reason = detect_recommend_follow_live_tabs( + analyser, + RECOMMEND_TAB, + FOLLOW_TAB, + LIVE_LIST_TAB, + diag_start, + max_step_time, + enable_ocr_mode1, + preprocess_for_ocr, + _log, + _log_summary, + _log_separator, ) - t_recommend = "" - t_follow = "" - t_list = "" - try: - # 检测推荐页标签栏区域(包含"推荐"、"关注"、"直播"标签) - _log("步骤3[OCR推荐页标签栏]-区域", 区域="(400,140)-(900,200)") - if time.time() - step_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*RECOMMEND_TAB, "", "推荐页标签栏") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤3[OCR推荐页标签栏]-截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_start < max_step_time: - ocr_start = time.time() - name0 = preprocess_for_ocr(analyser, "推荐页标签栏") - t_recommend = str( - analyser.operation.analyse_pic_word(name0, 0) or "" - ) - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤3[OCR推荐页标签栏]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(t_recommend), - 文本预览=t_recommend[:50] if t_recommend else "", - ) - - if ( - enable_ocr_mode1 - and (time.time() - step_start < max_step_time) - and ( - not t_recommend - or ( - "推荐" not in t_recommend - and "关注" not in t_recommend - and "直播" not in t_recommend - ) - ) - ): - ocr_start_1 = time.time() - name1 = preprocess_for_ocr(analyser, "推荐页标签栏") - t_recommend = str( - analyser.operation.analyse_pic_word(name1, 1) or "" - ) - ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 - _log( - "步骤3[OCR推荐页标签栏]-OCR模式1", - 耗时毫秒=ocr_elapsed_1, - 文本长度=len(t_recommend), - 文本预览=t_recommend[:50] if t_recommend else "", - ) - elapsed_recommend = (time.time() - step_start) * 1000 - - # 如果识别到"推荐",直接返回推荐页面 - if "推荐" in t_recommend: - total_elapsed = (time.time() - diag_start) * 1000 - seen_text = t_recommend[:50] if t_recommend else "-" - _log( - "步骤3[OCR推荐页标签栏]-命中推荐", - 耗时毫秒=elapsed_recommend, - 总耗时毫秒=total_elapsed, - 识别文本=seen_text, - ) - _log_summary( - "阶段诊断完成", - 阶段="RECOMMEND_PAGE", - 原因=f"标题='{t_recommend}'", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:RECOMMEND_PAGE(推荐页)") - return "RECOMMEND_PAGE", "标题='{}'".format(t_recommend) - - # 如果识别到"关注"但没有"推荐",可能是关注列表 - if "关注" in t_recommend and "推荐" not in t_recommend: - _log( - "步骤3[OCR关注列表]-检测到关注关键词,准备精确确认", - 精确区域="(658,147)-(765,194)", - ) - step_start_follow = time.time() - try: - if time.time() - step_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*FOLLOW_TAB, "", "关注列表标签") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤3[OCR关注列表]-精确截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_start < max_step_time: - ocr_start = time.time() - namef0 = preprocess_for_ocr(analyser, "关注列表标签") - t_follow = str( - analyser.operation.analyse_pic_word(namef0, 0) or "" - ) - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤3[OCR关注列表]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(t_follow), - 文本预览=t_follow[:50] if t_follow else "", - ) - - if ( - enable_ocr_mode1 - and (time.time() - step_start < max_step_time) - and ("关注" not in t_follow) - ): - ocr_start_1 = time.time() - namef1 = preprocess_for_ocr(analyser, "关注列表标签") - t_follow = str( - analyser.operation.analyse_pic_word(namef1, 1) or "" - ) - ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 - _log( - "步骤3[OCR关注列表]-OCR模式1", - 耗时毫秒=ocr_elapsed_1, - 文本长度=len(t_follow), - 文本预览=t_follow[:50] if t_follow else "", - ) - except Exception as e: - import traceback - - _log( - "步骤3[OCR关注列表]-异常", - 异常类型=type(e).__name__, - 异常信息=str(e), - 堆栈=traceback.format_exc(), - ) - - elapsed_follow = (time.time() - step_start_follow) * 1000 - if "关注" in t_follow: - total_elapsed = (time.time() - diag_start) * 1000 - follow_text = t_follow[:50] if t_follow else "-" - _log( - "步骤3[OCR关注列表]-命中", - 耗时毫秒=elapsed_follow, - 总耗时毫秒=total_elapsed, - 识别文本=follow_text, - ) - try: - analyser._last_follow_list_title_text = str( - t_follow or "" - ).strip() - except Exception: - analyser._last_follow_list_title_text = "" - _log_summary( - "阶段诊断完成", - 阶段="FOLLOW_LIST", - 原因=f"标题='{t_follow}'", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:FOLLOW_LIST(关注列表)") - return "FOLLOW_LIST", "标题='{}'".format(t_follow) - - # 如果识别到"正在直播"标签,判定为直播列表 - if "正在直播" in t_recommend or ( - "直播" in t_recommend - and "推荐" not in t_recommend - and "关注" not in t_recommend - ): - _log( - "步骤3[OCR直播列表]-检测到直播关键词,准备精确确认", - 精确区域="(650,145)-(800,195)", - ) - step_start_list = time.time() - try: - ocr_start = time.time() # 初始化ocr_start - if time.time() - step_start < max_step_time: - cut_start = time.time() - analyser.cut_pic(*LIVE_LIST_TAB, "", "直播列表标签") - cut_elapsed = (time.time() - cut_start) * 1000 - _log("步骤3[OCR直播列表]-精确截图完成", 耗时毫秒=cut_elapsed) - - if time.time() - step_start < max_step_time: - ocr_start = time.time() - # else分支不需要,因为ocr_start已经在上面初始化了 - namel0 = preprocess_for_ocr(analyser, "直播列表标签") - t_list = str(analyser.operation.analyse_pic_word(namel0, 0) or "") - ocr_elapsed = (time.time() - ocr_start) * 1000 - _log( - "步骤3[OCR直播列表]-OCR模式0", - 耗时毫秒=ocr_elapsed, - 文本长度=len(t_list), - 文本预览=t_list[:50] if t_list else "", - ) - - if ( - enable_ocr_mode1 - and (time.time() - step_start < max_step_time) - and ("正在直播" not in t_list) - ): - ocr_start_1 = time.time() - namel1 = preprocess_for_ocr(analyser, "直播列表标签") - t_list = str( - analyser.operation.analyse_pic_word(namel1, 1) or "" - ) - ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 - _log( - "步骤3[OCR直播列表]-OCR模式1", - 耗时毫秒=ocr_elapsed_1, - 文本长度=len(t_list), - 文本预览=t_list[:50] if t_list else "", - ) - except Exception as e: - import traceback - - _log( - "步骤3[OCR直播列表]-异常", - 异常类型=type(e).__name__, - 异常信息=str(e), - 堆栈=traceback.format_exc(), - ) - - elapsed_list = (time.time() - step_start_list) * 1000 - if "正在直播" in t_list: - total_elapsed = (time.time() - diag_start) * 1000 - list_text = t_list[:50] if t_list else "-" - _log( - "步骤3[OCR直播列表]-命中", - 耗时毫秒=elapsed_list, - 总耗时毫秒=total_elapsed, - 识别文本=list_text, - ) - _log_summary( - "阶段诊断完成", - 阶段="LIVE_LIST", - 原因=f"标题='{t_list}'", - 总耗时毫秒=total_elapsed, - ) - _log_separator("最终结果:LIVE_LIST(直播列表)") - return "LIVE_LIST", "标题='{}'".format(t_list) - - # 记录诊断时看到的"关注页标题" - try: - analyser._last_follow_list_title_text = str(t_follow or "").strip() - except Exception: - analyser._last_follow_list_title_text = "" - - elapsed_total = (time.time() - step_start) * 1000 - if elapsed_total > max_step_time * 1000: - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-步骤超时", - 耗时毫秒=elapsed_total, - 阈值毫秒=max_step_time * 1000, - ) - else: - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-未命中", - 耗时毫秒=elapsed_total, - 推荐标签栏预览=t_recommend[:30] if t_recommend else "", - ) - except Exception as e: - elapsed_total = (time.time() - step_start) * 1000 - import traceback - - _log( - "步骤3[OCR推荐页/关注列表/直播列表]-异常", - 耗时毫秒=elapsed_total, - 异常类型=type(e).__name__, - 异常信息=str(e), - 堆栈=traceback.format_exc(), - ) - - # 步骤3结束计时 - step3_elapsed = (time.time() - step_start) * 1000 - _log("步骤3[OCR推荐页/关注列表/直播列表]-完成", 总耗时毫秒=step3_elapsed) + if matched: + return stage, reason # 如果不在直播间且OCR也未识别到特定页面,默认返回在直播间 total_elapsed = (time.time() - diag_start) * 1000 diff --git a/douyin/stage/douyin_stage_helpers.py b/douyin/stage/douyin_stage_helpers.py index 4752797..dafdabe 100644 --- a/douyin/stage/douyin_stage_helpers.py +++ b/douyin/stage/douyin_stage_helpers.py @@ -89,6 +89,272 @@ def _format_detail_log_fields(key: str, detail: Dict[str, Any], analyser: Any) - return log_fields +def _ocr_with_dual_modes( + analyser: Any, + crop_name: str, + preprocess_for_ocr: Callable[[Any, str], str], + enable_ocr_mode1: bool, + step_start: float, + max_step_time: float, + keywords_for_mode1: tuple, + log_callback: Callable[..., None], + log_prefix: str, +) -> str: + """对已经裁剪好的图片做双 OCR(mode 0 + 可选 mode 1)。 + + 若 mode 0 文本含 ``keywords_for_mode1`` 中任意一个关键词,则不再跑 mode 1。 + 返回 OCR 文本(mode 1 优先,缺失时退到 mode 0)。 + """ + import time + + if time.time() - step_start >= max_step_time: + return "" + + ocr_start = time.time() + name0 = preprocess_for_ocr(analyser, crop_name) + text = str(analyser.operation.analyse_pic_word(name0, 0) or "") + ocr_elapsed = (time.time() - ocr_start) * 1000 + log_callback( + f"{log_prefix}-OCR模式0", + 耗时毫秒=ocr_elapsed, + 文本长度=len(text), + 文本预览=text[:50] if text else "", + ) + + # 是否需要 mode 1:mode 0 没匹配到任意关键词 + needs_mode1 = enable_ocr_mode1 and ( + time.time() - step_start < max_step_time + ) and ( + not text or not any(kw in text for kw in keywords_for_mode1) + ) + + if needs_mode1: + ocr_start_1 = time.time() + name1 = preprocess_for_ocr(analyser, crop_name) + text = str(analyser.operation.analyse_pic_word(name1, 1) or "") + ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 + log_callback( + f"{log_prefix}-OCR模式1", + 耗时毫秒=ocr_elapsed_1, + 文本长度=len(text), + 文本预览=text[:50] if text else "", + ) + return text + + +def detect_recommend_follow_live_tabs( + analyser: Any, + recommend_tab_area: tuple, + follow_tab_area: tuple, + live_list_tab_area: tuple, + diag_start: float, + max_step_time: float, + enable_ocr_mode1: bool, + preprocess_for_ocr: Callable[[Any, str], str], + log_callback: Callable[..., None], + log_summary_callback: Callable[..., None], + log_separator_callback: Callable[..., None], +) -> tuple[bool, str, str]: + """步骤3:OCR 检测推荐页/关注列表/直播列表标签栏。 + + 返回 (matched, stage, reason):matched=True 时主流程应直接 return。 + matched=False 表示三个标签都没命中(main flow 会回退到默认 IN_LIVE_ROOM)。 + """ + import time + + step_start = time.time() + remaining_time_before = float("inf") # 这里不强制超出 diag 时间限制 + log_callback( + "步骤3[OCR推荐页/关注列表/直播列表]-开始", + 剩余时间秒=remaining_time_before, + ) + + t_recommend = "" + t_follow = "" + t_list = "" + + try: + # 推荐页标签栏 + log_callback("步骤3[OCR推荐页标签栏]-区域", 区域="(400,140)-(900,200)") + if time.time() - step_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*recommend_tab_area, "", "推荐页标签栏") + cut_elapsed = (time.time() - cut_start) * 1000 + log_callback("步骤3[OCR推荐页标签栏]-截图完成", 耗时毫秒=cut_elapsed) + + t_recommend = _ocr_with_dual_modes( + analyser, "推荐页标签栏", + preprocess_for_ocr, enable_ocr_mode1, + step_start, max_step_time, + keywords_for_mode1=("推荐", "关注", "直播"), + log_callback=log_callback, + log_prefix="步骤3[OCR推荐页标签栏]", + ) + elapsed_recommend = (time.time() - step_start) * 1000 + + if "推荐" in t_recommend: + total_elapsed = (time.time() - diag_start) * 1000 + seen_text = t_recommend[:50] if t_recommend else "-" + log_callback( + "步骤3[OCR推荐页标签栏]-命中推荐", + 耗时毫秒=elapsed_recommend, + 总耗时毫秒=total_elapsed, + 识别文本=seen_text, + ) + log_summary_callback( + "阶段诊断完成", + 阶段="RECOMMEND_PAGE", + 原因=f"标题='{t_recommend}'", + 总耗时毫秒=total_elapsed, + ) + log_separator_callback("最终结果:RECOMMEND_PAGE(推荐页)") + return True, "RECOMMEND_PAGE", "标题='{}'".format(t_recommend) + + # 关注列表 + if "关注" in t_recommend and "推荐" not in t_recommend: + log_callback( + "步骤3[OCR关注列表]-检测到关注关键词,准备精确确认", + 精确区域="(658,147)-(765,194)", + ) + step_start_follow = time.time() + try: + if time.time() - step_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*follow_tab_area, "", "关注列表标签") + cut_elapsed = (time.time() - cut_start) * 1000 + log_callback("步骤3[OCR关注列表]-精确截图完成", 耗时毫秒=cut_elapsed) + + t_follow = _ocr_with_dual_modes( + analyser, "关注列表标签", + preprocess_for_ocr, enable_ocr_mode1, + step_start, max_step_time, + keywords_for_mode1=("关注",), + log_callback=log_callback, + log_prefix="步骤3[OCR关注列表]", + ) + except Exception as e: + import traceback + log_callback( + "步骤3[OCR关注列表]-异常", + 异常类型=type(e).__name__, + 异常信息=str(e), + 堆栈=traceback.format_exc(), + ) + + elapsed_follow = (time.time() - step_start_follow) * 1000 + if "关注" in t_follow: + total_elapsed = (time.time() - diag_start) * 1000 + follow_text = t_follow[:50] if t_follow else "-" + log_callback( + "步骤3[OCR关注列表]-命中", + 耗时毫秒=elapsed_follow, + 总耗时毫秒=total_elapsed, + 识别文本=follow_text, + ) + try: + analyser._last_follow_list_title_text = str(t_follow or "").strip() + except Exception: + analyser._last_follow_list_title_text = "" + log_summary_callback( + "阶段诊断完成", + 阶段="FOLLOW_LIST", + 原因=f"标题='{t_follow}'", + 总耗时毫秒=total_elapsed, + ) + log_separator_callback("最终结果:FOLLOW_LIST(关注列表)") + return True, "FOLLOW_LIST", "标题='{}'".format(t_follow) + + # 直播列表 + if "正在直播" in t_recommend or ( + "直播" in t_recommend + and "推荐" not in t_recommend + and "关注" not in t_recommend + ): + log_callback( + "步骤3[OCR直播列表]-检测到直播关键词,准备精确确认", + 精确区域="(650,145)-(800,195)", + ) + step_start_list = time.time() + try: + if time.time() - step_start < max_step_time: + cut_start = time.time() + analyser.cut_pic(*live_list_tab_area, "", "直播列表标签") + cut_elapsed = (time.time() - cut_start) * 1000 + log_callback( + "步骤3[OCR直播列表]-精确截图完成", 耗时毫秒=cut_elapsed + ) + + t_list = _ocr_with_dual_modes( + analyser, "直播列表标签", + preprocess_for_ocr, enable_ocr_mode1, + step_start, max_step_time, + keywords_for_mode1=("正在直播",), + log_callback=log_callback, + log_prefix="步骤3[OCR直播列表]", + ) + except Exception as e: + import traceback + log_callback( + "步骤3[OCR直播列表]-异常", + 异常类型=type(e).__name__, + 异常信息=str(e), + 堆栈=traceback.format_exc(), + ) + + elapsed_list = (time.time() - step_start_list) * 1000 + if "正在直播" in t_list: + total_elapsed = (time.time() - diag_start) * 1000 + list_text = t_list[:50] if t_list else "-" + log_callback( + "步骤3[OCR直播列表]-命中", + 耗时毫秒=elapsed_list, + 总耗时毫秒=total_elapsed, + 识别文本=list_text, + ) + log_summary_callback( + "阶段诊断完成", + 阶段="LIVE_LIST", + 原因=f"标题='{t_list}'", + 总耗时毫秒=total_elapsed, + ) + log_separator_callback("最终结果:LIVE_LIST(直播列表)") + return True, "LIVE_LIST", "标题='{}'".format(t_list) + + # 记录诊断时看到的"关注页标题" + try: + analyser._last_follow_list_title_text = str(t_follow or "").strip() + except Exception: + analyser._last_follow_list_title_text = "" + + elapsed_total = (time.time() - step_start) * 1000 + if elapsed_total > max_step_time * 1000: + log_callback( + "步骤3[OCR推荐页/关注列表/直播列表]-步骤超时", + 耗时毫秒=elapsed_total, + 阈值毫秒=max_step_time * 1000, + ) + else: + log_callback( + "步骤3[OCR推荐页/关注列表/直播列表]-未命中", + 耗时毫秒=elapsed_total, + 推荐标签栏预览=t_recommend[:30] if t_recommend else "", + ) + except Exception as e: + elapsed_total = (time.time() - step_start) * 1000 + import traceback + log_callback( + "步骤3[OCR推荐页/关注列表/直播列表]-异常", + 耗时毫秒=elapsed_total, + 异常类型=type(e).__name__, + 异常信息=str(e), + 堆栈=traceback.format_exc(), + ) + + step3_elapsed = (time.time() - step_start) * 1000 + log_callback("步骤3[OCR推荐页/关注列表/直播列表]-完成", 总耗时毫秒=step3_elapsed) + return False, "", "" + + def detect_redpacket_ocr_recheck_outside_live( analyser: Any, pixel_state: Dict[str, Any], diff --git a/tests/test_douyin_stage_helpers.py b/tests/test_douyin_stage_helpers.py index 8bbb34d..112f265 100644 --- a/tests/test_douyin_stage_helpers.py +++ b/tests/test_douyin_stage_helpers.py @@ -274,3 +274,81 @@ def test_redpacket_recheck_exception_returns_false(): lambda _x, _y: "p", log, MagicMock(), ) assert matched is False + + +# ---- detect_recommend_follow_live_tabs ---- # + +def _make_tab_analyser(recommend_text="", follow_text="", live_text=""): + """构造一个 mock analyser,按 cut_pic 的 crop_name 返回不同 OCR 文本。""" + a = MagicMock() + text_map = { + "推荐页标签栏": recommend_text, + "关注列表标签": follow_text, + "直播列表标签": live_text, + } + + def fake_analyse(name, mode): + # name 经过 preprocess_for_ocr 处理后我们让它直接返回 crop_name + return text_map.get(name, "") + + a.operation.analyse_pic_word.side_effect = fake_analyse + return a + + +def test_recommend_follow_live_tabs_recommend_match(): + a = _make_tab_analyser(recommend_text="推荐 关注 直播") + log, log_sum, log_sep = MagicMock(), MagicMock(), MagicMock() + matched, stage, _ = h.detect_recommend_follow_live_tabs( + a, (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), + _time.time(), 30.0, False, + lambda _a, name: name, log, log_sum, log_sep, + ) + assert matched is True + assert stage == "RECOMMEND_PAGE" + + +def test_recommend_follow_live_tabs_follow_match(): + a = _make_tab_analyser(recommend_text="关注", follow_text="关注") + log, log_sum, log_sep = MagicMock(), MagicMock(), MagicMock() + matched, stage, _ = h.detect_recommend_follow_live_tabs( + a, (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), + _time.time(), 30.0, False, + lambda _a, name: name, log, log_sum, log_sep, + ) + assert matched is True + assert stage == "FOLLOW_LIST" + + +def test_recommend_follow_live_tabs_live_match(): + a = _make_tab_analyser(recommend_text="直播", live_text="正在直播") + log, log_sum, log_sep = MagicMock(), MagicMock(), MagicMock() + matched, stage, _ = h.detect_recommend_follow_live_tabs( + a, (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), + _time.time(), 30.0, False, + lambda _a, name: name, log, log_sum, log_sep, + ) + assert matched is True + assert stage == "LIVE_LIST" + + +def test_recommend_follow_live_tabs_no_match_returns_false(): + a = _make_tab_analyser(recommend_text="无关文本") + log, log_sum, log_sep = MagicMock(), MagicMock(), MagicMock() + matched, _, _ = h.detect_recommend_follow_live_tabs( + a, (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), + _time.time(), 30.0, False, + lambda _a, name: name, log, log_sum, log_sep, + ) + assert matched is False + + +def test_recommend_follow_live_tabs_outer_exception_returns_false(): + a = MagicMock() + a.cut_pic.side_effect = RuntimeError("boom") + log, log_sum, log_sep = MagicMock(), MagicMock(), MagicMock() + matched, _, _ = h.detect_recommend_follow_live_tabs( + a, (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), + _time.time(), 30.0, False, + lambda _a, name: name, log, log_sum, log_sep, + ) + assert matched is False