from pathlib import Path

from fastapi.testclient import TestClient

from backend.main import app
from scripts.generate_sample import make_frame

import cv2


client = TestClient(app)


def test_health_and_methods():
    health = client.get("/api/health")
    assert health.status_code == 200
    assert health.json()["status"] == "ok"
    methods = client.get("/api/methods")
    assert methods.status_code == 200
    assert "fusion" in methods.json()["methods"]


def test_segment_image(tmp_path: Path):
    image_path = tmp_path / "sample.png"
    cv2.imwrite(str(image_path), make_frame(8, 320, 220))
    with image_path.open("rb") as handle:
        response = client.post(
            "/api/segment",
            files={"file": ("sample.png", handle, "image/png")},
            data={"method": "fusion", "sensitivity": "0.68"},
        )
    assert response.status_code == 200
    payload = response.json()
    assert payload["kind"] == "image"
    assert payload["frames"][0]["metrics"]["mask_pixels"] > 0
