"""TS-20 — integration: the middleware stack on a real FastAPI app + TestClient."""

from __future__ import annotations

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

from mgf.fastapi import REQUEST_ID_HEADER, RequestIdMiddleware

pytestmark = [pytest.mark.integration]


def _app() -> FastAPI:
    app = FastAPI()
    app.add_middleware(RequestIdMiddleware)

    @app.get("/ping")
    def ping() -> dict[str, str]:
        return {"ok": "true"}

    return app


def test_request_id_propagates_end_to_end() -> None:
    with TestClient(_app()) as client:
        # generated when absent
        r = client.get("/ping")
        assert r.status_code == 200
        assert r.headers[REQUEST_ID_HEADER]
        # echoed when supplied
        r2 = client.get("/ping", headers={REQUEST_ID_HEADER: "integ-abc-123"})
        assert r2.headers[REQUEST_ID_HEADER] == "integ-abc-123"
