from __future__ import annotations from types import SimpleNamespace from PIL import Image def test_fudai_detail_debug_overview_saves_one_annotated_image(tmp_path, monkeypatch): from douyin.core import path_config from douyin.fudai.core import fudai_debug_utils screenshot = tmp_path / "screenshot.png" Image.new("RGB", (1080, 2400), "white").save(screenshot) saved = [] def fake_save_debug_image(image, **kwargs): saved.append((image.copy(), kwargs)) return str(tmp_path / "overview.png") monkeypatch.setattr(path_config, "get_screenshot_path", lambda: str(screenshot)) monkeypatch.setattr( "douyin.handlers.douyin_screenshot_manager.save_debug_image", fake_save_debug_image, ) analyser = SimpleNamespace( resolution_ratio_x=1080, resolution_ratio_y=2400, _log_kv=lambda *args, **kwargs: None, ) result = fudai_debug_utils.save_fudai_detail_debug_overview( analyser, [ { "key": "content", "name": "内容", "coords": (300, 1400, 1030, 1515), "enabled": True, }, { "key": "participants", "name": "人数", "coords": (520, 1325, 760, 1415), "enabled": False, }, ], ui_version="v2_0", ocr_enabled={"content"}, task_count=1, task_count_source="v2_pixels", ) assert result assert len(saved) == 1 _image, kwargs = saved[0] assert kwargs["picname"] == "福袋详情综合标注" assert kwargs["step"] == "detail_regions" assert kwargs["annotate"] is False assert kwargs["force"] is True assert kwargs["extra_info"]["ui_version"] == "v2_0" def test_fudai_detail_debug_labels_are_inside_region_edges(tmp_path, monkeypatch): from douyin.core import path_config from douyin.fudai.core import fudai_debug_utils screenshot = tmp_path / "screenshot.png" Image.new("RGB", (1080, 2400), "white").save(screenshot) saved = [] def fake_save_debug_image(image, **kwargs): saved.append((image.copy(), kwargs)) return str(tmp_path / "overview.png") monkeypatch.setattr(path_config, "get_screenshot_path", lambda: str(screenshot)) monkeypatch.setattr( "douyin.handlers.douyin_screenshot_manager.save_debug_image", fake_save_debug_image, ) analyser = SimpleNamespace( resolution_ratio_x=1080, resolution_ratio_y=2400, _log_kv=lambda *args, **kwargs: None, ) result = fudai_debug_utils.save_fudai_detail_debug_overview( analyser, [ { "key": "countdown", "name": "倒计时", "coords": (760, 1355, 1035, 1455), "enabled": True, }, { "key": "content", "name": "内容", "coords": (360, 1472, 1035, 1545), "enabled": True, }, ], ui_version="v2_0", ocr_enabled={"countdown", "content"}, task_count=1, task_count_source="v2_pixels", ) assert result assert saved image, _kwargs = saved[0] # Labels are now drawn adjacent to their own boxes, not floating above # another OCR field. assert image.getpixel((763, 1358)) == (0, 0, 0) assert image.getpixel((363, 1475)) == (0, 0, 0) assert image.getpixel((363, 1444)) != (0, 0, 0) def test_fudai_detail_debug_overview_cleans_old_detail_images(tmp_path, monkeypatch): from douyin.core import path_config from douyin.fudai.core import fudai_debug_utils screenshot = tmp_path / "screenshot.png" Image.new("RGB", (1080, 2400), "white").save(screenshot) debug_dir = tmp_path / "debug" debug_dir.mkdir() old_detail = debug_dir / "20260603_ocr_福袋任务数量_v2_识别后.png" old_overview = debug_dir / "20260603_ocr_福袋详情综合标注_detail_regions.png" unrelated = debug_dir / "20260603_像素检测_抽奖结果_检测前.png" old_detail.write_bytes(b"old") old_overview.write_bytes(b"old") unrelated.write_bytes(b"keep") def fake_save_debug_image(image, **kwargs): out = debug_dir / "new_overview.png" image.save(out) return str(out) monkeypatch.setattr(path_config, "get_screenshot_path", lambda: str(screenshot)) monkeypatch.setattr( "douyin.handlers.douyin_screenshot_manager.get_debug_captures_dir", lambda: str(debug_dir), ) monkeypatch.setattr( "douyin.handlers.douyin_screenshot_manager.save_debug_image", fake_save_debug_image, ) analyser = SimpleNamespace( resolution_ratio_x=1080, resolution_ratio_y=2400, _log_kv=lambda *args, **kwargs: None, ) result = fudai_debug_utils.save_fudai_detail_debug_overview( analyser, [], ui_version="v2_0", ocr_enabled=set(), task_count=1, ) assert result assert not old_detail.exists() assert not old_overview.exists() assert unrelated.exists()