"""Opt-in HTTP fixture: real middleware/routes, no ambient startup or database.""" from collections.abc import AsyncIterator, Iterator from contextlib import asynccontextmanager import pytest from fastapi import FastAPI from modelforge_api.db import engine as ambient_engine from modelforge_api.main import app @pytest.fixture def isolated_http_contract(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: # These exercise the real HTTP stack with fake collectors, not application startup. # Do not run reconciliation, schema validation or background polling against .env. @asynccontextmanager async def contract_lifespan(_app: FastAPI) -> AsyncIterator[None]: yield ambient_connections: list[str] = [] def reject_ambient_connection(*args: object, **kwargs: object) -> None: ambient_connections.append("connection attempted") raise AssertionError("HTTP contract tests must not use the ambient database") monkeypatch.setattr(ambient_engine, "connect", reject_ambient_connection) monkeypatch.setattr(ambient_engine, "raw_connection", reject_ambient_connection) monkeypatch.setattr(app.router, "lifespan_context", contract_lifespan) # Restore the original mapping (including other tests' overrides) on teardown. monkeypatch.setattr(app, "dependency_overrides", {}) yield assert not ambient_connections