Fix fudai wrong-target retry and detail OCR delay
This commit is contained in:
1 parent
207748a901
commit
aa3690fecf
9 files changed
+359
-70
No files matched your search
@@ -57,6 +57,50 @@ def _detail_probe_after_click(analyser: Any) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _close_wrong_fudai_target(analyser: Any, device_id: str, *, stage: str, reason: str) -> None:
|
||||
"""Close a non-detail page opened by a risky fudai candidate click."""
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"find_and_open_fudai_detail:关闭疑似误入口,准备同直播间重试福袋",
|
||||
stage=str(stage or ""),
|
||||
reason=str(reason or ""),
|
||||
decision="close_wrong_target_before_retry",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser.operation.click_back(device_id)
|
||||
except Exception:
|
||||
try:
|
||||
analyser.operation.back(device_id)
|
||||
except Exception:
|
||||
try:
|
||||
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
|
||||
analyser.operation.click(
|
||||
device_id,
|
||||
int(520 * rx / 1080),
|
||||
int(505 * ry / 2400),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser.operation.delay(0.7)
|
||||
except Exception:
|
||||
time.sleep(0.7)
|
||||
for attr, value in (
|
||||
("last_fudai_x", 0),
|
||||
("_last_fudai_confirm_failure", None),
|
||||
("_screenshot_ready_for_ocr", False),
|
||||
("_fudai_detail_task_count_from_stage", 0),
|
||||
):
|
||||
try:
|
||||
setattr(analyser, attr, value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def find_and_open_fudai_detail(
|
||||
analyser: Any,
|
||||
stage: Any = "",
|
||||
@@ -109,7 +153,6 @@ def find_and_open_fudai_detail(
|
||||
|
||||
start_time = time.time()
|
||||
x = False
|
||||
|
||||
while (time.time() - start_time) < max_wait_seconds:
|
||||
# 在调用检测函数之前,检查是否还有足够的时间
|
||||
elapsed = time.time() - start_time
|
||||
@@ -593,6 +636,50 @@ def find_and_open_fudai_detail(
|
||||
click_y=click_y,
|
||||
**probe,
|
||||
)
|
||||
try:
|
||||
candidate_warning = getattr(analyser, "_last_fudai_candidate_warning", {})
|
||||
except Exception:
|
||||
candidate_warning = {}
|
||||
try:
|
||||
retry_depth = int(getattr(analyser, "_fudai_wrong_target_retry_depth", 0) or 0)
|
||||
except Exception:
|
||||
retry_depth = 0
|
||||
if retry_depth < 1:
|
||||
_log_detail_open_verdict(
|
||||
analyser,
|
||||
"wrong_target_retry_same_room",
|
||||
stage=stage,
|
||||
reason=str(reason or ""),
|
||||
click_x=click_x,
|
||||
click_y=click_y,
|
||||
candidate_warning=candidate_warning,
|
||||
**probe,
|
||||
)
|
||||
_close_wrong_fudai_target(
|
||||
analyser,
|
||||
device_id,
|
||||
stage=str(stage or ""),
|
||||
reason=str(reason or ""),
|
||||
)
|
||||
try:
|
||||
setattr(analyser, "_fudai_wrong_target_retry_depth", retry_depth + 1)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
return bool(
|
||||
find_and_open_fudai_detail(
|
||||
analyser,
|
||||
stage=stage,
|
||||
seen=seen,
|
||||
func_start_time=func_start_time,
|
||||
)
|
||||
)
|
||||
finally:
|
||||
try:
|
||||
setattr(analyser, "_fudai_wrong_target_retry_depth", retry_depth)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
|
||||
@@ -34,6 +34,7 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
service_stop_ms: Optional[float] = None
|
||||
service_resume_ms: Optional[float] = None
|
||||
countdown_parse_ms: Optional[float] = None
|
||||
ocr_pic_path: Optional[str] = None
|
||||
|
||||
def _log_perf(tag: str) -> None:
|
||||
try:
|
||||
@@ -175,12 +176,12 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
# 步骤2:获取截图(仅用于后续OCR识别内容/倒计时/价值,不是为了获取任务数量)
|
||||
# 注意:此时后台截图服务已被停止,直接调用底层截图避免递归
|
||||
|
||||
# 检查是否可以复用阶段诊断时的截图。默认关闭:阶段诊断截图通常拍得更早,
|
||||
# 用它做正式 OCR 容易读到未渲染完整的详情页,导致首轮 OCR 为空并触发重试。
|
||||
# 检查是否可以复用点击后阶段诊断截图。阶段检测已确认在详情页时,
|
||||
# 复用同一张图可省掉一次 ADB 截图,避免详情页额外停留数秒。
|
||||
screenshot_reused = False
|
||||
try:
|
||||
allow_stage_screenshot_reuse = bool(
|
||||
getattr(analyser, "fudai_detail_reuse_stage_screenshot_enabled", False)
|
||||
getattr(analyser, "fudai_detail_reuse_stage_screenshot_enabled", True)
|
||||
)
|
||||
if getattr(analyser, "_screenshot_ready_for_ocr", False) and not allow_stage_screenshot_reuse:
|
||||
analyser._screenshot_ready_for_ocr = False
|
||||
@@ -222,6 +223,7 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
_reuse_probe.close()
|
||||
analyser._screenshot_ready_for_ocr = False # 清除标志,避免重复使用
|
||||
screenshot_reused = True
|
||||
ocr_pic_path = pic_path
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
@@ -234,6 +236,10 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
pass
|
||||
except Exception as e:
|
||||
screenshot_reused = False
|
||||
try:
|
||||
analyser._screenshot_ready_for_ocr = False
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
@@ -245,6 +251,10 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
analyser._screenshot_ready_for_ocr = False
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
@@ -294,6 +304,7 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
|
||||
screenshot_end = time.time()
|
||||
wait_file_ms = round((screenshot_end - wait_start) * 1000, 1)
|
||||
ocr_pic_path = pic_path
|
||||
|
||||
_log_perf("after_screenshot") # 移除:避免过多性能日志
|
||||
|
||||
@@ -302,15 +313,15 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
pass
|
||||
|
||||
# 使用统一的截图路径配置
|
||||
from douyin.core import path_config
|
||||
pic_path = path_config.get_screenshot_path()
|
||||
if not ocr_pic_path:
|
||||
ocr_pic_path = path_config.get_screenshot_path()
|
||||
pic_rgb = None
|
||||
try:
|
||||
if os.path.exists(pic_path):
|
||||
if os.path.exists(ocr_pic_path):
|
||||
from PIL import Image
|
||||
|
||||
_img0 = time.time()
|
||||
pic = Image.open(pic_path)
|
||||
pic = Image.open(ocr_pic_path)
|
||||
try:
|
||||
pic.load()
|
||||
pic_rgb = pic.convert("RGB")
|
||||
@@ -403,8 +414,6 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]:
|
||||
except Exception: # pyright: ignore[reportTooBroadExceptionType]
|
||||
# fallback: 将 traceback 写入统一错误日志
|
||||
try:
|
||||
from douyin.core import path_config
|
||||
|
||||
err_log = path_config.get_error_log_path()
|
||||
with open(err_log, "a", encoding="utf-8") as ef:
|
||||
ef.write(
|
||||
|
||||
@@ -1020,6 +1020,9 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
value_raw,
|
||||
task_list_raw,
|
||||
button_text_raw,
|
||||
allow_task_list_heavy=bool(
|
||||
getattr(analyser, "fudai_detail_heavy_task_list_enabled", False)
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
heavy_fields = set()
|
||||
|
||||
@@ -72,6 +72,7 @@ def should_use_heavy(
|
||||
value_text: Any,
|
||||
task_text: Any,
|
||||
button_text: Any,
|
||||
allow_task_list_heavy: bool = False,
|
||||
) -> set[str]:
|
||||
"""根据轻重模式与轻 OCR 结果决定是否触发重 OCR。"""
|
||||
fields: set[str] = set()
|
||||
@@ -83,7 +84,7 @@ def should_use_heavy(
|
||||
fields.update({"countdown_mm", "countdown_ss"})
|
||||
if force_heavy or is_blank_text(value_text):
|
||||
fields.add("value")
|
||||
if force_heavy or is_blank_text(task_text):
|
||||
if allow_task_list_heavy and (force_heavy or is_blank_text(task_text)):
|
||||
fields.add("task_list")
|
||||
if force_heavy or is_blank_text(button_text):
|
||||
fields.add("button")
|
||||
|
||||
@@ -858,30 +858,35 @@ class FudaiDetector:
|
||||
redpacket_neighbor, redpacket_neighbor_info = self._has_redpacket_neighbor_widget(
|
||||
pic, detected_x
|
||||
)
|
||||
passed = (
|
||||
not commercial_overlay
|
||||
and not redpacket_widget
|
||||
and not redpacket_neighbor
|
||||
and 20 <= total <= 120
|
||||
and 6 <= valid_cols <= 45
|
||||
and 2 <= max_col <= 10
|
||||
)
|
||||
core_shape_ok = 20 <= total <= 120 and 6 <= valid_cols <= 45 and 2 <= max_col <= 10
|
||||
commercial_warning = commercial_overlay or redpacket_widget or redpacket_neighbor
|
||||
passed = core_shape_ok
|
||||
shape_info = {
|
||||
"x": detected_x,
|
||||
"passed": bool(passed),
|
||||
"target_pixels": total,
|
||||
"valid_cols": valid_cols,
|
||||
"max_col": max_col,
|
||||
"core_shape_ok": core_shape_ok,
|
||||
"commercial_overlay": commercial_overlay,
|
||||
"commercial_info": commercial_info,
|
||||
"redpacket_widget": redpacket_widget,
|
||||
"redpacket_info": redpacket_info,
|
||||
"redpacket_neighbor": redpacket_neighbor,
|
||||
"redpacket_neighbor_info": redpacket_neighbor_info,
|
||||
"commercial_warning": commercial_warning,
|
||||
"commercial_blocked": False,
|
||||
"window": (x1, y1, x2, y2),
|
||||
}
|
||||
try:
|
||||
setattr(self.analyser, "_last_fudai_shape_confirm_info", shape_info)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"[福袋查找] 形态确认",
|
||||
"当前候选像素形态确认完成",
|
||||
x=detected_x,
|
||||
passed=passed,
|
||||
target_pixels=total,
|
||||
valid_cols=valid_cols,
|
||||
max_col=max_col,
|
||||
commercial_overlay=commercial_overlay,
|
||||
commercial_info=commercial_info,
|
||||
redpacket_widget=redpacket_widget,
|
||||
redpacket_info=redpacket_info,
|
||||
redpacket_neighbor=redpacket_neighbor,
|
||||
redpacket_neighbor_info=redpacket_neighbor_info,
|
||||
window=(x1, y1, x2, y2),
|
||||
**shape_info,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -1038,7 +1043,53 @@ class FudaiDetector:
|
||||
if detected_x is not None:
|
||||
template_ok = self.confirm_current_icon_template(pic, detected_x)
|
||||
shape_ok = self.confirm_pixel_candidate_shape(pic, detected_x)
|
||||
try:
|
||||
shape_info = getattr(
|
||||
self.analyser, "_last_fudai_shape_confirm_info", {}
|
||||
)
|
||||
setattr(
|
||||
self.analyser,
|
||||
"_last_fudai_candidate_warning",
|
||||
{
|
||||
"region": region["name"],
|
||||
"x": detected_x,
|
||||
"template_ok": bool(template_ok),
|
||||
"shape_ok": bool(shape_ok),
|
||||
"commercial_warning": bool(
|
||||
isinstance(shape_info, dict)
|
||||
and shape_info.get("commercial_warning")
|
||||
),
|
||||
"shape_info": shape_info if isinstance(shape_info, dict) else {},
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if not (template_ok or shape_ok):
|
||||
try:
|
||||
shape_info = getattr(
|
||||
self.analyser, "_last_fudai_shape_confirm_info", {}
|
||||
)
|
||||
except Exception:
|
||||
shape_info = {}
|
||||
try:
|
||||
setattr(
|
||||
self.analyser,
|
||||
"_last_fudai_confirm_failure",
|
||||
{
|
||||
"ts": time.time(),
|
||||
"region": region["name"],
|
||||
"x": detected_x,
|
||||
"template_ok": template_ok,
|
||||
"shape_ok": shape_ok,
|
||||
"shape_info": shape_info,
|
||||
"commercial_blocked": bool(
|
||||
isinstance(shape_info, dict)
|
||||
and shape_info.get("commercial_blocked")
|
||||
),
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if douyin_screenshot_manager is not None:
|
||||
detected_x_scaled = int(detected_x * self.rx / 1080)
|
||||
@@ -1060,8 +1111,9 @@ class FudaiDetector:
|
||||
"x": detected_x,
|
||||
"template_ok": template_ok,
|
||||
"shape_ok": shape_ok,
|
||||
"shape_info": shape_info,
|
||||
},
|
||||
background=True,
|
||||
background=False,
|
||||
force=True,
|
||||
)
|
||||
except Exception:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
def test_detail_open_verdict_marker_logs():
|
||||
@@ -26,3 +26,65 @@ def test_detail_data_verdict_marker_logs():
|
||||
args, kwargs = analyser._log_kv.call_args
|
||||
assert args[1] == "[DETAIL_DATA_VERDICT] validation_failed"
|
||||
assert kwargs["verdict"] == "validation_failed"
|
||||
|
||||
|
||||
def test_find_open_retries_same_room_after_wrong_target(tmp_path):
|
||||
from douyin.core.douyin_flow_control_pkg.find_open import find_and_open_fudai_detail
|
||||
|
||||
screenshot = tmp_path / "screenshot.png"
|
||||
screenshot.write_bytes(b"x" * 2048)
|
||||
analyser = SimpleNamespace(
|
||||
_log_kv=MagicMock(return_value=None),
|
||||
device_id="MOCK",
|
||||
room_pos=1,
|
||||
fudai_icon_max_wait_seconds=2.0,
|
||||
fudai_icon_retry_delay_seconds=0.1,
|
||||
fudai_click_wait_seconds=0.0,
|
||||
resolution_ratio_x=1080,
|
||||
resolution_ratio_y=2400,
|
||||
y_pianyi=0,
|
||||
_waiting_for_lottery_result=False,
|
||||
_screenshot_ready_for_ocr=False,
|
||||
_fudai_detail_task_count_from_stage=0,
|
||||
_last_fudai_candidate_warning={"commercial_warning": True, "x": 162},
|
||||
operation=SimpleNamespace(
|
||||
click=MagicMock(return_value=None),
|
||||
click_back=MagicMock(return_value=None),
|
||||
delay=MagicMock(return_value=None),
|
||||
get_screenshot=MagicMock(return_value=True),
|
||||
),
|
||||
)
|
||||
|
||||
stage_results = [
|
||||
("IN_LIVE_ROOM", "clicked_coupon_or_redpacket"),
|
||||
("FUDai_DETAIL", "detail_ok"),
|
||||
]
|
||||
|
||||
def _stage(_analyser, trigger_reason=""):
|
||||
stage, reason = stage_results.pop(0)
|
||||
if stage == "FUDai_DETAIL":
|
||||
analyser._fudai_detail_task_count_from_stage = 1
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
return stage, reason
|
||||
|
||||
with patch(
|
||||
"douyin.core.douyin_flow_control_pkg.find_open.check_have_fudai_with_ready_screenshot",
|
||||
side_effect=[162, 190],
|
||||
), patch(
|
||||
"douyin.core.path_config.get_screenshot_path",
|
||||
return_value=str(screenshot),
|
||||
), patch(
|
||||
"douyin.stage.douyin_stage.diagnose_stage_from_current_screen",
|
||||
side_effect=_stage,
|
||||
):
|
||||
assert find_and_open_fudai_detail(analyser) is True
|
||||
|
||||
assert analyser.operation.click.call_count == 2
|
||||
analyser.operation.click_back.assert_called_once_with("MOCK")
|
||||
verdicts = [
|
||||
kwargs.get("verdict")
|
||||
for args, kwargs in analyser._log_kv.call_args_list
|
||||
if args and args[0] == "详情验证"
|
||||
]
|
||||
assert "wrong_target_retry_same_room" in verdicts
|
||||
assert "detail_recognized" in verdicts
|
||||
@@ -39,10 +39,10 @@ ICON_REGRESSION_CASES = [
|
||||
("real_light10_x150_220.png", True, 150, 220),
|
||||
("false_banner_edge_x292.png", False, None, None),
|
||||
("false_banner_text_x269.png", False, None, None),
|
||||
("false_left_coupon_x162.png", False, None, None),
|
||||
("false_left_coupon_x162.png", "risky_probe", 140, 190),
|
||||
("false_no_fudai_light10.png", False, None, None),
|
||||
("false_right_coupon_strip_x366.png", False, None, None),
|
||||
("false_shopping_redpacket_left_x168.png", False, None, None),
|
||||
("false_shopping_redpacket_left_x168.png", "risky_probe", 140, 190),
|
||||
]
|
||||
|
||||
|
||||
@@ -128,10 +128,16 @@ class TestFudaiDetector:
|
||||
d = FudaiDetector(analyser)
|
||||
detected_x, method = d.detect(Image.open(sample).convert("RGB"))
|
||||
|
||||
if should_detect:
|
||||
if should_detect is True:
|
||||
assert detected_x is not None
|
||||
assert min_x <= detected_x <= max_x
|
||||
assert method in {"pixel", "icon_fallback_template"}
|
||||
elif should_detect == "risky_probe":
|
||||
assert detected_x is not None
|
||||
assert min_x <= detected_x <= max_x
|
||||
assert method == "pixel"
|
||||
warning = getattr(analyser, "_last_fudai_candidate_warning", {})
|
||||
assert warning.get("commercial_warning") is True
|
||||
else:
|
||||
assert detected_x is None
|
||||
assert method == ""
|
||||
@@ -192,6 +198,25 @@ class TestFudaiDetector:
|
||||
assert info["white_ratio"] >= 0.35
|
||||
assert info["pink_ratio"] >= 0.06
|
||||
|
||||
def test_real_fudai_is_not_rejected_by_nearby_commercial_widgets(self):
|
||||
sample = Path(
|
||||
"pic/debug_captures/2026-06-19/"
|
||||
"20260619_124446_593474_px_fdicon_ok_x132y373w60h60_"
|
||||
"src_flow_on_detect_click_x_162_click_y_403_detected_x_scaled_162.png"
|
||||
)
|
||||
if not sample.exists():
|
||||
pytest.skip("live debug sample unavailable")
|
||||
|
||||
analyser = FakeAnalyser()
|
||||
analyser.last_fudai_x = 162
|
||||
d = FudaiDetector(analyser)
|
||||
|
||||
detected_x, method = d.detect(Image.open(sample).convert("RGB"))
|
||||
|
||||
assert detected_x is not None
|
||||
assert 140 <= detected_x <= 230
|
||||
assert method in {"pixel", "icon_fallback_template"}
|
||||
|
||||
def test_current_icon_template_confirms_clean_countdown_sample(self):
|
||||
sample = Path(
|
||||
"artifacts/live_fudai_flow_20260618_0530_valid/snapshots/"
|
||||
|
||||
@@ -17,6 +17,7 @@ from douyin.fudai.douyin_fudai_core_pkg.heavy_ocr import (
|
||||
_read_disable_flag,
|
||||
run_heavy_ocr,
|
||||
)
|
||||
from douyin.fudai.douyin_fudai_core_pkg.contain_helpers import should_use_heavy
|
||||
|
||||
|
||||
def _mk_analyser():
|
||||
@@ -214,3 +215,35 @@ class TestRunHeavyOcr:
|
||||
assert "value" in result
|
||||
assert "button" in result
|
||||
assert "_duration_ms" in result
|
||||
|
||||
|
||||
class TestHeavyFieldSelection:
|
||||
def test_task_list_heavy_is_off_by_default(self):
|
||||
fields = should_use_heavy(
|
||||
False,
|
||||
True,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
)
|
||||
|
||||
assert "task_list" not in fields
|
||||
assert {"content", "countdown_mm", "countdown_ss", "value", "button"} <= fields
|
||||
|
||||
def test_task_list_heavy_can_be_enabled_explicitly(self):
|
||||
fields = should_use_heavy(
|
||||
False,
|
||||
True,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
allow_task_list_heavy=True,
|
||||
)
|
||||
|
||||
assert "task_list" in fields
|
||||
@@ -21,6 +21,21 @@ def _mk_analyser():
|
||||
return a
|
||||
|
||||
|
||||
def _write_valid_large_png(path):
|
||||
from PIL import Image
|
||||
|
||||
img = Image.new("RGB", (240, 240))
|
||||
img.putdata(
|
||||
[
|
||||
((x * 7 + y * 3) % 256, (x * 5 + y * 11) % 256, (x * 13 + y) % 256)
|
||||
for y in range(240)
|
||||
for x in range(240)
|
||||
]
|
||||
)
|
||||
img.save(path)
|
||||
assert path.stat().st_size >= 1024
|
||||
|
||||
|
||||
def test_read_once_restarts_screenshot_service_after_ocr(tmp_path, monkeypatch):
|
||||
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once import (
|
||||
read_fudai_detail_once,
|
||||
@@ -84,7 +99,7 @@ def test_read_once_restarts_screenshot_service_after_ocr(tmp_path, monkeypatch):
|
||||
assert starts == [("MOCK_DEVICE", 1.5)]
|
||||
|
||||
|
||||
def test_read_once_skips_stage_screenshot_reuse_by_default(
|
||||
def test_read_once_reuses_stage_screenshot_by_default_without_extra_capture(
|
||||
tmp_path, monkeypatch
|
||||
):
|
||||
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once import (
|
||||
@@ -93,23 +108,10 @@ def test_read_once_skips_stage_screenshot_reuse_by_default(
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
analyser.operation.get_screenshot = MagicMock(return_value=True)
|
||||
|
||||
screenshot_path = tmp_path / "stage_screenshot.png"
|
||||
screenshot_path.write_bytes(b"x" * 2048)
|
||||
good_png = (
|
||||
b"\x89PNG\r\n\x1a\n"
|
||||
b"\x00\x00\x00\rIHDR"
|
||||
b"\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00"
|
||||
b"\x90wS\xde"
|
||||
b"\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef"
|
||||
b"\x00\x00\x00\x00IEND\xaeB`\x82"
|
||||
)
|
||||
|
||||
def _fresh_screenshot(*_args, **_kwargs):
|
||||
screenshot_path.write_bytes(good_png)
|
||||
return True
|
||||
|
||||
analyser.operation.get_screenshot = MagicMock(side_effect=_fresh_screenshot)
|
||||
_write_valid_large_png(screenshot_path)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.path_config.get_screenshot_path",
|
||||
@@ -127,9 +129,7 @@ def test_read_once_skips_stage_screenshot_reuse_by_default(
|
||||
assert detail[0] == "内容"
|
||||
assert extra["task_count"] == 1
|
||||
assert analyser._screenshot_ready_for_ocr is False
|
||||
analyser.operation.get_screenshot.assert_called_once_with(
|
||||
analyser.device_id, use_service=False
|
||||
)
|
||||
analyser.operation.get_screenshot.assert_not_called()
|
||||
|
||||
|
||||
def test_read_once_can_reuse_stage_screenshot_when_explicitly_enabled(
|
||||
@@ -145,20 +145,38 @@ def test_read_once_can_reuse_stage_screenshot_when_explicitly_enabled(
|
||||
analyser.operation.get_screenshot = MagicMock(return_value=True)
|
||||
|
||||
screenshot_path = tmp_path / "stage_reuse.png"
|
||||
screenshot_path.write_bytes(b"x" * 2048)
|
||||
|
||||
class _ImgObj:
|
||||
def load(self):
|
||||
return None
|
||||
|
||||
def convert(self, _mode):
|
||||
return object()
|
||||
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
pil_mod = ModuleType("PIL")
|
||||
pil_mod.Image = SimpleNamespace(open=lambda _path: _ImgObj())
|
||||
_write_valid_large_png(screenshot_path)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.path_config.get_screenshot_path",
|
||||
return_value=str(screenshot_path),
|
||||
), patch(
|
||||
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.get_fudai_contain",
|
||||
return_value=("内容", "00:30", 1, 1, 10.0),
|
||||
), patch(
|
||||
"operations.screenshot_service.ScreenshotService.get_instance",
|
||||
return_value=SimpleNamespace(is_running=lambda: False),
|
||||
):
|
||||
status, detail, extra = read_fudai_detail_once(analyser)
|
||||
|
||||
assert status == "ok"
|
||||
assert detail[0] == "内容"
|
||||
assert extra["task_count"] == 1
|
||||
assert analyser._screenshot_ready_for_ocr is False
|
||||
analyser.operation.get_screenshot.assert_not_called()
|
||||
|
||||
|
||||
def test_read_once_reuses_stage_screenshot_by_default(tmp_path, monkeypatch):
|
||||
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once import (
|
||||
read_fudai_detail_once,
|
||||
)
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser._screenshot_ready_for_ocr = True
|
||||
analyser.operation.get_screenshot = MagicMock(return_value=True)
|
||||
|
||||
screenshot_path = tmp_path / "stage_reuse_default.png"
|
||||
_write_valid_large_png(screenshot_path)
|
||||
|
||||
with patch(
|
||||
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.path_config.get_screenshot_path",
|
||||
@@ -170,7 +188,6 @@ def test_read_once_can_reuse_stage_screenshot_when_explicitly_enabled(
|
||||
"operations.screenshot_service.ScreenshotService.get_instance",
|
||||
return_value=SimpleNamespace(is_running=lambda: False),
|
||||
):
|
||||
monkeypatch.setitem(sys.modules, "PIL", pil_mod)
|
||||
status, detail, extra = read_fudai_detail_once(analyser)
|
||||
|
||||
assert status == "ok"
|
||||
|
||||
Reference in new issue
Block a user