"""Integration tests for /engine/trace and /engine/simplify endpoints."""
import json
import cv2
import numpy as np
import pytest
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def _make_test_png(width: int = 100, height: int = 100) -> bytes:
"""Create a simple test PNG with a white rectangle on black background."""
img = np.zeros((height, width, 3), dtype=np.uint8)
cv2.rectangle(img, (20, 20), (80, 80), (255, 255, 255), -1)
ok, buf = cv2.imencode(".png", img)
assert ok
return buf.tobytes()
def _make_test_svg() -> str:
"""Create a simple SVG with a rectangular path for simplify tests."""
return (
'"
)
def _make_complex_svg() -> str:
"""Create an SVG with many intermediate points (suitable for RDP reduction)."""
# A path with extra collinear intermediate points that RDP can remove
points = " ".join(
f"L {x},{10}" for x in range(11, 91)
)
return (
'"
)
@pytest.fixture
def test_png() -> bytes:
return _make_test_png()
@pytest.fixture
def test_svg() -> bytes:
return _make_test_svg().encode("utf-8")
@pytest.fixture
def complex_svg() -> bytes:
return _make_complex_svg().encode("utf-8")
# -----------------------------------------------------------------------
# /engine/trace — SVG output (default)
# -----------------------------------------------------------------------
class TestTraceEndpointSVG:
"""Tests for /engine/trace with SVG output format."""
def test_basic_trace(self, test_png):
resp = client.post(
"/engine/trace",
files={"file": ("test.png", test_png, "image/png")},
data={"mode": "potrace"},
)
assert resp.status_code == 200
body = resp.json()
assert body["format"] == "svg"
assert "