import pytest
import requests

_DETECT_TIMEOUT = 60


def test_nft_res_01_loader_outage_after_init(
    warm_engine, image_detect, mock_loader_url, image_small, http_client
):
    requests.post(
        f"{mock_loader_url}/mock/config", json={"mode": "error"}, timeout=10
    ).raise_for_status()
    detections, _ = image_detect(image_small, "r1.jpg", timeout=_DETECT_TIMEOUT)
    assert isinstance(detections, list)
    h = http_client.get("/health")
    assert h.status_code == 200
    hd = h.json()
    assert hd["status"] == "healthy"
    assert hd.get("errorMessage") is None


def test_nft_res_03_transient_loader_first_fail(
    mock_loader_url, image_detect, image_small
):
    requests.post(
        f"{mock_loader_url}/mock/config", json={"mode": "first_fail"}, timeout=10
    ).raise_for_status()
    try:
        image_detect(image_small, "r3a.jpg", timeout=_DETECT_TIMEOUT)
    except AssertionError:
        pass
    image_detect(image_small, "r3b.jpg", timeout=_DETECT_TIMEOUT)
