87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
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"]
|
|
samples = client.get("/api/samples")
|
|
assert samples.status_code == 200
|
|
assert "samples" in samples.json()
|
|
if samples.json()["samples"]:
|
|
sample = samples.json()["samples"][0]
|
|
assert "version" in sample
|
|
assert "?v=" in sample["url"]
|
|
|
|
|
|
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
|
|
assert payload["frames"][0]["source_time"] == 0.0
|
|
assert payload["frames"][0]["result_time"] == 0.0
|
|
|
|
|
|
def test_segment_video_and_compare_frame(tmp_path: Path):
|
|
video_path = tmp_path / "sample.mp4"
|
|
writer = cv2.VideoWriter(str(video_path), cv2.VideoWriter_fourcc(*"mp4v"), 12.0, (320, 220))
|
|
for index in range(18):
|
|
writer.write(make_frame(index, 320, 220))
|
|
writer.release()
|
|
|
|
with video_path.open("rb") as handle:
|
|
response = client.post(
|
|
"/api/segment",
|
|
files={"file": ("sample.mp4", handle, "video/mp4")},
|
|
data={"method": "fusion", "sensitivity": "0.58", "frame_stride": "6", "max_frames": "3"},
|
|
)
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert payload["kind"] == "video"
|
|
assert payload["video_url"].endswith(".mp4")
|
|
assert payload["source_fps"] > 0
|
|
assert payload["result_fps"] == 8.0
|
|
assert len(payload["frames"]) == 3
|
|
first_frame = payload["frames"][0]
|
|
assert first_frame["source_time"] == 0.0
|
|
assert first_frame["result_time"] == 0.0
|
|
assert first_frame["result_index"] == 0
|
|
|
|
with video_path.open("rb") as handle:
|
|
compare = client.post(
|
|
"/api/compare-frame",
|
|
files={"file": ("sample.mp4", handle, "video/mp4")},
|
|
data={"frame_index": str(payload["frames"][1]["frame_index"]), "sensitivity": "0.58"},
|
|
)
|
|
assert compare.status_code == 200
|
|
compare_payload = compare.json()
|
|
assert compare_payload["kind"] == "compare"
|
|
assert {frame["method"] for frame in compare_payload["frames"]} == {
|
|
"hessian_ridge",
|
|
"edge_morphology",
|
|
"temporal_difference",
|
|
"fusion",
|
|
}
|