213 lines
6.9 KiB
Python
213 lines
6.9 KiB
Python
from __future__ import annotations
|
|
|
|
import sys
|
|
from types import ModuleType, SimpleNamespace
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
def _mk_analyser():
|
|
a = SimpleNamespace()
|
|
a._log_kv = MagicMock(return_value=None)
|
|
a._fudai_detail_task_count_from_stage = 1
|
|
a._screenshot_ready_for_ocr = False
|
|
a.device_id = "MOCK_DEVICE"
|
|
a.enable_streaming_screenshot = True
|
|
a.operation = SimpleNamespace(
|
|
get_screenshot=lambda *_a, **_k: True,
|
|
check_countdown=lambda _text: (30, 1234567890.0),
|
|
)
|
|
a.current_room_title = ""
|
|
a.current_room_title_raw = ""
|
|
return a
|
|
|
|
|
|
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,
|
|
)
|
|
|
|
analyser = _mk_analyser()
|
|
screenshot_path = tmp_path / "screenshot.png"
|
|
screenshot_path.write_bytes(
|
|
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"
|
|
)
|
|
|
|
starts: list[tuple[str, float]] = []
|
|
|
|
class _Svc:
|
|
def is_running(self):
|
|
return True
|
|
|
|
def stop(self):
|
|
return None
|
|
|
|
def start(self, device_id, interval=1.5):
|
|
starts.append((device_id, interval))
|
|
|
|
pil_mod = ModuleType("PIL")
|
|
pil_mod.Image = SimpleNamespace(
|
|
open=lambda _p: type(
|
|
"_Img",
|
|
(),
|
|
{
|
|
"load": lambda self: None,
|
|
"convert": lambda self, _mode: object(),
|
|
"close": lambda self: None,
|
|
},
|
|
)()
|
|
)
|
|
|
|
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(
|
|
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.validate_fudai_detail",
|
|
return_value=(True, "ok"),
|
|
), patch(
|
|
"operations.screenshot_service.ScreenshotService.get_instance",
|
|
return_value=_Svc(),
|
|
):
|
|
monkeypatch.setitem(sys.modules, "PIL", pil_mod)
|
|
status, detail, extra = read_fudai_detail_once(analyser)
|
|
|
|
assert status == "ok"
|
|
assert detail[0] == "内容"
|
|
assert extra["task_count"] == 1
|
|
assert starts == [("MOCK_DEVICE", 1.5)]
|
|
|
|
|
|
def test_read_once_skips_stage_screenshot_reuse_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
|
|
|
|
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)
|
|
|
|
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_called_once_with(
|
|
analyser.device_id, use_service=False
|
|
)
|
|
|
|
|
|
def test_read_once_can_reuse_stage_screenshot_when_explicitly_enabled(
|
|
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.fudai_detail_reuse_stage_screenshot_enabled = True
|
|
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())
|
|
|
|
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),
|
|
):
|
|
monkeypatch.setitem(sys.modules, "PIL", pil_mod)
|
|
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_clears_reuse_flag_when_screenshot_still_unreadable(
|
|
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 / "still_broken.png"
|
|
screenshot_path.write_bytes(b"x" * 2048)
|
|
|
|
pil_mod = ModuleType("PIL")
|
|
pil_mod.Image = SimpleNamespace(open=lambda _p: (_ for _ in ()).throw(OSError("broken png")))
|
|
|
|
with patch(
|
|
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_once.path_config.get_screenshot_path",
|
|
return_value=str(screenshot_path),
|
|
), patch(
|
|
"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 == "error"
|
|
assert extra["reason"] == "screenshot_failed"
|
|
assert analyser._screenshot_ready_for_ocr is False
|