"""Exercise the service boundary over a disposable loopback HTTP endpoint."""

from __future__ import annotations

import json
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

import pytest

from obliteratus import models_client


pytestmark = pytest.mark.network


def test_bestiary_catalog_http_boundary(monkeypatch):
    catalog = {
        "models": [
            {
                "id": "example/tiny",
                "vendor": "example",
                "open_weight": True,
                "channels": ["huggingface"],
                "capabilities": ["text"],
            }
        ]
    }
    body = json.dumps(catalog).encode()

    class Handler(BaseHTTPRequestHandler):
        def do_GET(self):
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        def log_message(self, _format, *_args):
            return

    server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    try:
        monkeypatch.setenv(
            "BESTIARY_CATALOG", f"http://127.0.0.1:{server.server_port}/catalog.json"
        )
        assert models_client.model_ids(open_weight=True) == ["example/tiny"]
    finally:
        server.shutdown()
        thread.join(timeout=5)
        server.server_close()
