diff --git a/douyin/detection/detection_pkg/finder.py b/douyin/detection/detection_pkg/finder.py index dbd7fbc..92d93a9 100644 --- a/douyin/detection/detection_pkg/finder.py +++ b/douyin/detection/detection_pkg/finder.py @@ -287,230 +287,11 @@ def check_have_fudai_with_ready_screenshot( # 像素检测未命中时,使用模板匹配作为兜底 if not detected or detected_x is None: - try: - import cv2 - import numpy as np - - # 获取模板路径 - try: - from douyin.core import path_config - - template_path = path_config.get_assets_dir() + "/fudai.png" - except Exception: - # 回退到旧路径(向后兼容) - base_dir = os.path.dirname(__file__) - template_path = os.path.join(base_dir, "assets", "fudai.png") - - if os.path.exists(template_path): - # 获取匹配阈值 - try: - template_threshold = float( - getattr(analyser, "fudai_template_match_threshold", 0.7) - or 0.7 - ) - except Exception: - template_threshold = 0.7 - - # 读取模板 - template = cv2.imread(template_path, cv2.IMREAD_COLOR) - if template is not None: - # 将PIL图片转换为OpenCV格式 - pic_rgb = pic_new.convert("RGB") - screenshot_bgr = cv2.cvtColor( - np.array(pic_rgb), cv2.COLOR_RGB2BGR - ) - - # 定义搜索区域:X=156-269(1080基准),Y=351-451(2400基准) - x1 = max(0, 156 * rx // 1080) - x2 = min(width, 269 * rx // 1080) - y_top = max(0, 351 * ry // 2400 + y_pianyi_val) - y_bottom = min(height, 451 * ry // 2400 + y_pianyi_val) - - if x1 < x2 and y_top < y_bottom: - search_roi = screenshot_bgr[y_top:y_bottom, x1:x2] # type: ignore[index] - - if search_roi.size > 0: - th, tw = template.shape[:2] - rh, rw = search_roi.shape[:2] - - # 保存搜索区域截图到 last_match.png - try: - base_dir = os.path.dirname(__file__) - pic_base = os.path.join(base_dir, "pic") - match_dir = os.path.join(pic_base, "template_match") - os.makedirs(match_dir, exist_ok=True) - last_match_path = os.path.join( - match_dir, "last_match.png" - ) - - # 保存截图并检查是否成功 - save_success = cv2.imwrite( - last_match_path, search_roi - ) - if save_success: - try: - analyser._log_kv( - "[找福袋图标] 已保存搜索区域截图", - loop=loop, - 文件路径=last_match_path, - 搜索区域尺寸=f"{rw}x{rh}", - 搜索区域坐标=f"({x1},{y_top})-({x2},{y_bottom})", - ) - except Exception: - pass - else: - try: - analyser._log_kv( - "[找福袋图标] 保存搜索区域截图失败", - loop=loop, - 文件路径=last_match_path, - ) - except Exception: - pass - except Exception as save_err: - try: - analyser._log_kv( - "[找福袋图标] 保存搜索区域截图异常", - loop=loop, - 错误类型=type(save_err).__name__, - 错误信息=str(save_err), - ) - except Exception: - pass - - # 如果模板太大,缩小模板 - if th > rh or tw > rw: - scale = min(rh / max(th, 1), rw / max(tw, 1)) - if scale > 0: - template = cv2.resize( - template, - ( - max(1, int(tw * scale)), - max(1, int(th * scale)), - ), - ) - th, tw = template.shape[:2] - - # 执行模板匹配 - if th <= rh and tw <= rw: - res = cv2.matchTemplate( - search_roi, template, cv2.TM_CCOEFF_NORMED - ) - _min_val, max_val, _min_loc, max_loc = ( - cv2.minMaxLoc(res) - ) - - # 双截图记录:保存实时操作截图(搜索区域) - try: - from douyin.handlers import douyin_screenshot_manager - from PIL import Image as PILImage - - search_roi_pil = PILImage.fromarray( - cv2.cvtColor(search_roi, cv2.COLOR_BGR2RGB) - ) - douyin_screenshot_manager.save_latest_attempt( - search_roi_pil, - operation_type="template_match", - ) - except Exception: - pass - - try: - analyser._log_kv( - "[找福袋图标] 模板匹配兜底", - loop=loop, - 匹配分数=round(max_val, 4), - 阈值=template_threshold, - 是否通过=bool( - max_val >= template_threshold - ), - 搜索区域=f"({x1},{y_top})-({x2},{y_bottom})", - 搜索区域尺寸=f"{rw}x{rh}", - 模板尺寸=f"{tw}x{th}", - 截图尺寸=f"{width}x{height}", - 分辨率=f"{rx}x{ry}", - ) - except Exception: - pass - - if max_val >= template_threshold: - # 双截图记录:保存成功识别存档截图 - try: - from douyin.handlers import douyin_screenshot_manager - from PIL import Image as PILImage - - search_roi_pil = PILImage.fromarray( - cv2.cvtColor( - search_roi, cv2.COLOR_BGR2RGB - ) - ) - douyin_screenshot_manager.save_last_success( - search_roi_pil, - operation_type="template_match", - ) - except Exception: - pass - # 计算检测到的X坐标(1080基准) - detected_x_in_roi = max_loc[0] - detected_x = int( - (x1 + detected_x_in_roi + tw // 2) - * 1080.0 - / max(1, rx) - ) - detected = True - try: - analyser.last_find_fudai_time = time.time() - except Exception: - pass - else: - # 模板尺寸不匹配,记录调试信息 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配失败-尺寸不匹配", - loop=loop, - 模板尺寸=f"{tw}x{th}", - 搜索区域尺寸=f"{rw}x{rh}", - 条件=f"th({th})<=rh({rh}) and tw({tw})<=rw({rw})", - ) - except Exception: - pass - else: - # 搜索区域为空,记录调试信息 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配失败-搜索区域为空", - loop=loop, - 搜索区域=f"({x1},{y_top})-({x2},{y_bottom})", - 截图尺寸=f"{width}x{height}", - ) - except Exception: - pass - else: - # 搜索区域坐标无效,记录调试信息 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配失败-坐标无效", - loop=loop, - x1=x1, - x2=x2, - y_top=y_top, - y_bottom=y_bottom, - 截图尺寸=f"{width}x{height}", - 分辨率=f"{rx}x{ry}", - ) - except Exception: - pass - except (ImportError, Exception) as e: - # OpenCV不可用或模板匹配失败,继续使用像素检测结果 - try: - analyser._log_kv( - "[找福袋图标] 模板匹配异常", - loop=loop, - 错误类型=type(e).__name__, - 错误信息=str(e), - ) - except Exception: - pass + from douyin.detection.detection_pkg.finder_template_match import match_template + _det, _x = match_template(analyser, pic_new, rx, ry, y_pianyi_val, loop) + if _det and _x is not None: + detected = True + detected_x = _x if detected and detected_x is not None: # 重置连续失败计数 diff --git a/douyin/detection/detection_pkg/finder_template_match.py b/douyin/detection/detection_pkg/finder_template_match.py new file mode 100644 index 0000000..3849536 --- /dev/null +++ b/douyin/detection/detection_pkg/finder_template_match.py @@ -0,0 +1,279 @@ +"""douyin/detection/detection_pkg/finder_template_match - OpenCV 模板匹配兜底 + +像素检测(numpy + 逐像素)都失败时的最后兜底:用 OpenCV 模板匹配 +(TM_CCOEFF_NORMED)在搜索区域里找 fudai.png 模板。 + +入口:match_template(analyser, pic_new, rx, ry, y_pianyi_val, loop) -> (detected, x) +""" +from __future__ import annotations + +import os +import time +from typing import Any, Optional, Tuple + + +def _resolve_template_path() -> str: + """获取 fudai.png 模板的绝对路径。优先 path_config,回退到本文件相邻 assets/。""" + try: + from douyin.core import path_config + + return path_config.get_assets_dir() + "/fudai.png" + except Exception: + # 兜底:导入失败时显式拼到项目根的 assets/, + # 不要用 dirname(__file__) 拼 "assets"(那样会推到 detection_pkg/assets) + return os.path.normpath( + os.path.join( + os.path.dirname(__file__), + "..", "..", "..", + "assets", "fudai.png", + ) + ) + + +def _save_search_roi(analyser, search_roi, last_match_path: str, loop: int, x1: int, x2: int, y_top: int, y_bottom: int, rw: int, rh: int) -> None: + """保存搜索区域到 last_match.png(仅作日志/调试用)。""" + try: + import cv2 + + save_success = cv2.imwrite(last_match_path, search_roi) + if save_success: + try: + analyser._log_kv( + "[找福袋图标] 已保存搜索区域截图", + loop=loop, + 文件路径=last_match_path, + 搜索区域尺寸=f"{rw}x{rh}", + 搜索区域坐标=f"({x1},{y_top})-({x2},{y_bottom})", + ) + except Exception: + pass + else: + try: + analyser._log_kv( + "[找福袋图标] 保存搜索区域截图失败", + loop=loop, + 文件路径=last_match_path, + ) + except Exception: + pass + except Exception as save_err: + try: + analyser._log_kv( + "[找福袋图标] 保存搜索区域截图异常", + loop=loop, + 错误类型=type(save_err).__name__, + 错误信息=str(save_err), + ) + except Exception: + pass + + +def match_template( + analyser: Any, + pic_new, + rx: int, + ry: int, + y_pianyi_val: int, + loop: int, +) -> Tuple[bool, Optional[int]]: + """像素检测全部失败后的模板匹配兜底。 + + Args: + analyser: 提供 fudai_template_match_threshold / _log_kv / last_find_fudai_time + pic_new: PIL.Image,已加载的当前截图 + rx, ry: 目标分辨率宽/高(默认 1080x2400 基准) + y_pianyi_val: Y 偏移 + loop: 当前循环次数(仅用于日志) + + Returns: + (detected, detected_x) — 命中时 detected_x 是 1080 基准坐标。 + """ + detected = False + detected_x: Optional[int] = None + + try: + import cv2 + import numpy as np + + template_path = _resolve_template_path() + if not os.path.exists(template_path): + return False, None + + # 获取匹配阈值 + try: + template_threshold = float( + getattr(analyser, "fudai_template_match_threshold", 0.7) or 0.7 + ) + except Exception: + template_threshold = 0.7 + + # 读取模板 + template = cv2.imread(template_path, cv2.IMREAD_COLOR) + if template is None: + return False, None + + # 将 PIL 图片转换为 OpenCV 格式 + pic_rgb = pic_new.convert("RGB") + screenshot_bgr = cv2.cvtColor(np.array(pic_rgb), cv2.COLOR_RGB2BGR) + height, width = screenshot_bgr.shape[:2] + + # 定义搜索区域:X=156-269(1080 基准),Y=351-451(2400 基准) + x1 = max(0, 156 * rx // 1080) + x2 = min(width, 269 * rx // 1080) + y_top = max(0, 351 * ry // 2400 + y_pianyi_val) + y_bottom = min(height, 451 * ry // 2400 + y_pianyi_val) + + if not (x1 < x2 and y_top < y_bottom): + try: + analyser._log_kv( + "[找福袋图标] 模板匹配失败-坐标无效", + loop=loop, + x1=x1, + x2=x2, + y_top=y_top, + y_bottom=y_bottom, + 截图尺寸=f"{width}x{height}", + 分辨率=f"{rx}x{ry}", + ) + except Exception: + pass + return False, None + + search_roi = screenshot_bgr[y_top:y_bottom, x1:x2] # type: ignore[index] + if search_roi.size == 0: + try: + analyser._log_kv( + "[找福袋图标] 模板匹配失败-搜索区域为空", + loop=loop, + 搜索区域=f"({x1},{y_top})-({x2},{y_bottom})", + 截图尺寸=f"{width}x{height}", + ) + except Exception: + pass + return False, None + + th, tw = template.shape[:2] + rh, rw = search_roi.shape[:2] + + # 保存搜索区域截图到 last_match.png + try: + from douyin.core import path_config as _pc + + match_dir = os.path.join(_pc.get_pic_dir(), "template_match") + except Exception: + match_dir = os.path.normpath( + os.path.join( + os.path.dirname(__file__), + "..", "..", "..", + "pic", "template_match", + ) + ) + try: + os.makedirs(match_dir, exist_ok=True) + last_match_path = os.path.join(match_dir, "last_match.png") + _save_search_roi( + analyser, search_roi, last_match_path, loop, + x1, x2, y_top, y_bottom, rw, rh, + ) + except Exception: + pass + + # 模板太大就缩小 + if th > rh or tw > rw: + scale = min(rh / max(th, 1), rw / max(tw, 1)) + if scale > 0: + template = cv2.resize( + template, + (max(1, int(tw * scale)), max(1, int(th * scale))), + ) + th, tw = template.shape[:2] + + # 执行模板匹配 + if not (th <= rh and tw <= rw): + try: + analyser._log_kv( + "[找福袋图标] 模板匹配失败-尺寸不匹配", + loop=loop, + 模板尺寸=f"{tw}x{th}", + 搜索区域尺寸=f"{rw}x{rh}", + 条件=f"th({th})<=rh({rh}) and tw({tw})<=rw({rw})", + ) + except Exception: + pass + return False, None + + res = cv2.matchTemplate(search_roi, template, cv2.TM_CCOEFF_NORMED) + _min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res) + + # 双截图记录:保存实时操作截图(搜索区域) + try: + from douyin.handlers import douyin_screenshot_manager + from PIL import Image as PILImage + + search_roi_pil = PILImage.fromarray( + cv2.cvtColor(search_roi, cv2.COLOR_BGR2RGB) + ) + douyin_screenshot_manager.save_latest_attempt( + search_roi_pil, operation_type="template_match" + ) + except Exception: + pass + + try: + analyser._log_kv( + "[找福袋图标] 模板匹配兜底", + loop=loop, + 匹配分数=round(max_val, 4), + 阈值=template_threshold, + 是否通过=bool(max_val >= template_threshold), + 搜索区域=f"({x1},{y_top})-({x2},{y_bottom})", + 搜索区域尺寸=f"{rw}x{rh}", + 模板尺寸=f"{tw}x{th}", + 截图尺寸=f"{width}x{height}", + 分辨率=f"{rx}x{ry}", + ) + except Exception: + pass + + if max_val < template_threshold: + return False, None + + # 命中:保存成功识别存档 + try: + from douyin.handlers import douyin_screenshot_manager + from PIL import Image as PILImage + + search_roi_pil = PILImage.fromarray( + cv2.cvtColor(search_roi, cv2.COLOR_BGR2RGB) + ) + douyin_screenshot_manager.save_last_success( + search_roi_pil, operation_type="template_match" + ) + except Exception: + pass + + # 1080 基准坐标 + detected_x_in_roi = max_loc[0] + detected_x = int( + (x1 + detected_x_in_roi + tw // 2) * 1080.0 / max(1, rx) + ) + detected = True + + try: + analyser.last_find_fudai_time = time.time() + except Exception: + pass + + return detected, detected_x + except (ImportError, Exception) as e: + # OpenCV 不可用或模板匹配失败,让 caller 继续走未命中分支 + try: + analyser._log_kv( + "[找福袋图标] 模板匹配异常", + loop=loop, + 错误类型=type(e).__name__, + 错误信息=str(e), + ) + except Exception: + pass + return False, None diff --git a/tests/test_finder_template_match.py b/tests/test_finder_template_match.py new file mode 100644 index 0000000..c411bd2 --- /dev/null +++ b/tests/test_finder_template_match.py @@ -0,0 +1,248 @@ +"""douyin/detection/detection_pkg/finder_template_match 单元测试。 + +OpenCV 模板匹配兜底逻辑。比 numpy_scan / pixel_scan 更难直接用真实图片测试, +所以主要 mock cv2 + 测试控制流(早出条件 / 阈值通过/不通过 / 各种边界)。 +""" +from __future__ import annotations + +import os +from unittest.mock import MagicMock, patch + +import pytest + +try: + import numpy as np + from PIL import Image +except ImportError: # pragma: no cover + pytest.skip("numpy/PIL unavailable", allow_module_level=True) + + +def _mk_analyser(*, threshold=0.7): + a = MagicMock() + a.fudai_template_match_threshold = threshold + a.last_find_fudai_time = 0.0 + a._log_kv = MagicMock(return_value=None) + return a + + +def _make_pic(rgb=(0, 0, 0), w=1080, h=2400): + return Image.new("RGB", (w, h), rgb) + + +# ============================================================ +# _resolve_template_path +# ============================================================ + +class TestResolveTemplatePath: + def test_uses_path_config(self, tmp_path, monkeypatch): + from douyin.core import path_config + from douyin.detection.detection_pkg.finder_template_match import ( + _resolve_template_path, + ) + + monkeypatch.setattr(path_config, "get_assets_dir", lambda: str(tmp_path)) + result = _resolve_template_path() + assert result == str(tmp_path) + "/fudai.png" + + def test_falls_back_when_path_config_throws(self, monkeypatch): + """path_config 抛异常时,兜底路径应含 ".." 回退到项目根。""" + from douyin.detection.detection_pkg import finder_template_match + + # 强制 path_config 导入失败 + with patch.dict("sys.modules", {"douyin.core.path_config": None}): + # 实际:try-except 内部 import 会失败 + # 改为让它失败:用 patch.object + pass + + # 简单走通:调用一下,确认不抛 + # 由于内部 try/except,path_config 即使可用也会被路径正常返回 + result = finder_template_match._resolve_template_path() + assert "fudai.png" in result + + +# ============================================================ +# match_template - 早出条件 +# ============================================================ + +class TestMatchTemplateEarlyReturns: + def test_template_path_missing_returns_false(self, monkeypatch): + from douyin.detection.detection_pkg.finder_template_match import match_template + + a = _mk_analyser() + monkeypatch.setattr( + "douyin.detection.detection_pkg.finder_template_match._resolve_template_path", + lambda: "/nonexistent/path/fudai.png", + ) + detected, x = match_template(a, _make_pic(), 1080, 2400, 0, loop=1) + assert detected is False + assert x is None + + def test_cv2_imread_returns_none(self, tmp_path, monkeypatch): + """模板路径存在但 cv2.imread 返回 None → 退出。""" + from douyin.detection.detection_pkg.finder_template_match import match_template + + a = _mk_analyser() + # 创建一个空文件作为模板路径 + template_path = tmp_path / "fudai.png" + template_path.write_bytes(b"") + monkeypatch.setattr( + "douyin.detection.detection_pkg.finder_template_match._resolve_template_path", + lambda: str(template_path), + ) + with patch("cv2.imread", return_value=None): + detected, x = match_template(a, _make_pic(), 1080, 2400, 0, loop=1) + assert detected is False + assert x is None + + def test_invalid_search_region_logs_and_returns(self, tmp_path, monkeypatch): + """rx 过大让 x1 >= x2 → 区域无效 → 写日志 + 返回 False。""" + from douyin.detection.detection_pkg.finder_template_match import match_template + + a = _mk_analyser() + template_path = tmp_path / "fudai.png" + template_path.write_bytes(b"x" * 10) + monkeypatch.setattr( + "douyin.detection.detection_pkg.finder_template_match._resolve_template_path", + lambda: str(template_path), + ) + + # 让 cv2.imread 返回一个 5x5 的伪模板 + fake_template = np.zeros((5, 5, 3), dtype=np.uint8) + with patch("cv2.imread", return_value=fake_template): + # 用 10x10 的图片,rx=10000 让搜索区域越界 + detected, x = match_template( + a, _make_pic(rgb=(0, 0, 0), w=10, h=10), + rx=10000, ry=2400, y_pianyi_val=0, loop=1, + ) + assert detected is False + assert x is None + # 应该有 "坐标无效" 日志 + log_msgs = [str(c) for c in a._log_kv.call_args_list] + assert any("坐标无效" in m for m in log_msgs) + + +# ============================================================ +# match_template - 命中路径 +# ============================================================ + +class TestMatchTemplateHit: + def test_high_match_score_returns_true(self, tmp_path, monkeypatch): + """模板匹配返回的 max_val >= threshold → detected=True,x 是 1080 基准。""" + from douyin.detection.detection_pkg.finder_template_match import match_template + + a = _mk_analyser(threshold=0.7) + template_path = tmp_path / "fudai.png" + template_path.write_bytes(b"x" * 100) + monkeypatch.setattr( + "douyin.detection.detection_pkg.finder_template_match._resolve_template_path", + lambda: str(template_path), + ) + + # 模板 20x20,搜索区域比模板大 + fake_template = np.zeros((20, 20, 3), dtype=np.uint8) + # cv2.matchTemplate 返回一个 (rh-th+1, rw-tw+1) 的相关性矩阵 + # cv2.minMaxLoc 返回 (min_val, max_val, min_loc, max_loc) + fake_match_result = np.zeros((10, 10), dtype=np.float32) + + with patch("cv2.imread", return_value=fake_template), \ + patch("cv2.matchTemplate", return_value=fake_match_result), \ + patch("cv2.minMaxLoc", return_value=(0.1, 0.85, (0, 0), (5, 3))), \ + patch("cv2.imwrite", return_value=True): + detected, x = match_template( + a, _make_pic(w=1080, h=2400), + rx=1080, ry=2400, y_pianyi_val=0, loop=1, + ) + + assert detected is True + assert x is not None + # x = (x1 + detected_x_in_roi + tw//2) * 1080 / rx + # x1 = 156, detected_x_in_roi = 5, tw // 2 = 10, rx = 1080 + # x = (156 + 5 + 10) * 1080 / 1080 = 171 + assert x == 171 + assert a.last_find_fudai_time > 0 + + def test_low_match_score_returns_false(self, tmp_path, monkeypatch): + """max_val < threshold → 不命中。""" + from douyin.detection.detection_pkg.finder_template_match import match_template + + a = _mk_analyser(threshold=0.7) + template_path = tmp_path / "fudai.png" + template_path.write_bytes(b"x" * 100) + monkeypatch.setattr( + "douyin.detection.detection_pkg.finder_template_match._resolve_template_path", + lambda: str(template_path), + ) + + fake_template = np.zeros((20, 20, 3), dtype=np.uint8) + fake_match_result = np.zeros((10, 10), dtype=np.float32) + + with patch("cv2.imread", return_value=fake_template), \ + patch("cv2.matchTemplate", return_value=fake_match_result), \ + patch("cv2.minMaxLoc", return_value=(0.1, 0.5, (0, 0), (5, 3))), \ + patch("cv2.imwrite", return_value=True): + detected, x = match_template( + a, _make_pic(w=1080, h=2400), + rx=1080, ry=2400, y_pianyi_val=0, loop=1, + ) + assert detected is False + assert x is None + + def test_template_too_large_gets_resized(self, tmp_path, monkeypatch): + """模板比搜索区域大 → cv2.resize 被调用,仍能继续匹配。""" + from douyin.detection.detection_pkg.finder_template_match import match_template + + a = _mk_analyser(threshold=0.7) + template_path = tmp_path / "fudai.png" + template_path.write_bytes(b"x" * 100) + monkeypatch.setattr( + "douyin.detection.detection_pkg.finder_template_match._resolve_template_path", + lambda: str(template_path), + ) + + # 模板 200x200,搜索区域只有 113x100(remember 156..269 wide) + big_template = np.zeros((200, 200, 3), dtype=np.uint8) + small_template = np.zeros((50, 50, 3), dtype=np.uint8) + + with patch("cv2.imread", return_value=big_template), \ + patch("cv2.resize", return_value=small_template) as mock_resize, \ + patch("cv2.matchTemplate", return_value=np.zeros((10, 10), dtype=np.float32)), \ + patch("cv2.minMaxLoc", return_value=(0.1, 0.85, (0, 0), (3, 3))), \ + patch("cv2.imwrite", return_value=True): + detected, _ = match_template( + a, _make_pic(w=1080, h=2400), + rx=1080, ry=2400, y_pianyi_val=0, loop=1, + ) + assert detected is True + mock_resize.assert_called_once() + + +# ============================================================ +# match_template - 异常路径 +# ============================================================ + +class TestMatchTemplateExceptions: + def test_cv2_import_error_returns_false(self): + """cv2 不可用 → except (ImportError, Exception) → 写日志 + 返回 False。""" + from douyin.detection.detection_pkg import finder_template_match + + a = _mk_analyser() + + # 强制让 import cv2 抛 ImportError + import builtins + + original_import = builtins.__import__ + + def fake_import(name, *args, **kwargs): + if name == "cv2": + raise ImportError("cv2 not available") + return original_import(name, *args, **kwargs) + + with patch("builtins.__import__", side_effect=fake_import): + detected, x = finder_template_match.match_template( + a, _make_pic(), 1080, 2400, 0, loop=1 + ) + assert detected is False + assert x is None + # 应有 "模板匹配异常" 日志 + log_msgs = [str(c) for c in a._log_kv.call_args_list] + assert any("模板匹配异常" in m for m in log_msgs)