Files
douyi_guaji_main/tests/test_read_once_service_recovery.py

230 lines
7.6 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 _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,
)
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_reuses_stage_screenshot_by_default_without_extra_capture(
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_screenshot.png"
_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_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"
_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",
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_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