"""
Tests for the FastAPI plugin integration.
"""

import sys
from pathlib import Path

import pytest
from fastapi.testclient import TestClient

BACKEND_DIR = Path(__file__).parent.parent
sys.path.insert(0, str(BACKEND_DIR))

from fastapi_integration import create_app


@pytest.fixture
def client(tmp_path: Path) -> TestClient:
    """Build a FastAPI app with a temporary plugin directory."""
    app = create_app(plugin_dir=tmp_path)
    return TestClient(app)


def test_list_plugins_endpoint(client: TestClient) -> None:
    response = client.get("/plugins")
    assert response.status_code == 200
    data = response.json()
    assert "plugins" in data
    assert "routes" in data


def test_health_check_plugin_registers_route(tmp_path: Path) -> None:
    """Copy the real health check plugin into a temp dir and verify routing."""
    plugin_source = BACKEND_DIR / "plugins" / "health_check_plugin.py"
    plugin_dest = tmp_path / "health_check_plugin.py"
    plugin_dest.write_text(plugin_source.read_text())

    app = create_app(plugin_dir=tmp_path)
    test_client = TestClient(app)

    response = test_client.get("/plugin/health")
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "ok"
    assert data["plugin"] == "health_check"
    assert "started_at" in data
