from fastapi.testclient import TestClient
from hypothesis import given, settings
from hypothesis import strategies as st

# Import the main FastAPI application instance from your source code
from main import app

# Initialize the TestClient to make requests against your FastAPI app instance
client = TestClient(app)

def test_liveness_ok():
    """
    Test the basic liveness endpoint.
    A liveness probe checks if the container is running and responsive.
    It should always return a 200 OK status and the text 'live'.
    """
    response = client.get("/health/live")
    assert response.status_code == 200
    assert response.text == "live"

@given(st.text(min_size=0, max_size=16))
@settings(max_examples=10)
def test_liveness_resilience_to_query_noise(noise: str):
    """
    Use Hypothesis for property-based testing.
    This test ensures that the liveness endpoint is robust and remains functional
    even when unexpected or garbage query parameters ("noise") are provided in the URL.
    The `given` decorator generates various string inputs for the 'noise' parameter.
    """
    # Pass arbitrary query parameters to the endpoint
    response = client.get("/health/live", params={"noise": noise})
    assert response.status_code == 200
    assert response.text == "live"

def test_readiness_ok():
    """
    Test the basic readiness endpoint.
    A readiness probe checks if the container is ready to accept traffic (e.g., database connection established).
    It should return a 200 OK status and the text 'ready' when healthy.
    """
    response = client.get("/health/ready")
    assert response.status_code == 200
    assert response.text == "ready"

@given(st.text(min_size=0, max_size=16))
@settings(max_examples=10)
def test_readiness_resilience_to_query_noise(noise: str):
    """
    Use Hypothesis for property-based testing.
    This test ensures that the readiness endpoint is robust and remains functional
    even when unexpected or garbage query parameters ("noise") are provided in the URL.
    The `given` decorator generates various string inputs for the 'noise' parameter.
    """
    # Pass arbitrary query parameters to the endpoint
    response = client.get("/health/ready", params={"noise": noise})
    assert response.status_code == 200
    assert response.text == "ready"

def test_detailed_health_pass():
    """
    Test the detailed health check endpoint, often conforming to the
    [IETF health check standard](datatracker.ietf.org).
    It should return a 200 OK status, and the JSON body should have a
    "status" of "pass" and a dictionary of individual "checks".
    """
    response = client.get("/health")
    assert response.status_code == 200
    body = response.json()
    assert body["status"] == "pass"
    assert isinstance(body["checks"], dict)

@given(st.text(min_size=0, max_size=16))
@settings(max_examples=10)
def test_health_resilience_to_query_noise(noise: str):
    """
    Use Hypothesis for property-based testing.
    This test ensures that the health endpoint is robust and remains functional
    even when unexpected or garbage query parameters ("noise") are provided in the URL.
    The `given` decorator generates various string inputs for the 'noise' parameter.
    """
    # Pass arbitrary query parameters to the endpoint
    response = client.get("/health", params={"noise": noise})
    assert response.status_code == 200
    body = response.json()
    assert body["status"] == "pass"
    assert isinstance(body["checks"], dict)
