from pathlib import Path import cv2 import numpy as np from backend.app.ai_render_crop import detect_main_content_box, process_ai_render_crop WORKDIR = Path("/Users/icemilk/Workspace/zld_POC") AI_FILE = WORKDIR / "【2026-04-09】端午 - 背标 - 天问.ai" OUTPUT_DIR = WORKDIR / ".tmp_test_render_crop" def test_detect_main_content_box_finds_centered_content() -> None: image = np.full((400, 600, 3), 255, dtype=np.uint8) cv2.rectangle(image, (120, 90), (520, 310), (10, 10, 10), 3) cv2.putText(image, "MAIN CONTENT", (150, 210), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (20, 20, 20), 3, cv2.LINE_AA) x0, y0, x1, y1 = detect_main_content_box(image) assert x0 < 120 assert y0 < 90 assert x1 > 520 assert y1 > 310 def test_process_ai_render_crop_outputs_full_and_cropped_images() -> None: result = process_ai_render_crop(AI_FILE, OUTPUT_DIR) assert result["fullImage"]["url"].endswith(".png") assert result["croppedImage"]["url"].endswith(".png") assert result["cropBox"]["x0"] >= 0 assert result["cropBox"]["y0"] >= 0 assert result["cropBox"]["x1"] > result["cropBox"]["x0"] assert result["cropBox"]["y1"] > result["cropBox"]["y0"] full_path = OUTPUT_DIR / Path(result["fullImage"]["url"]).name cropped_path = OUTPUT_DIR / Path(result["croppedImage"]["url"]).name assert full_path.exists() assert cropped_path.exists() full_image = cv2.imread(str(full_path)) cropped_image = cv2.imread(str(cropped_path)) assert full_image is not None assert cropped_image is not None assert cropped_image.shape[1] < full_image.shape[1] assert cropped_image.shape[0] < full_image.shape[0]