"""Shared fixtures for the apikey test suite. The per-AK spend module (``apikey/spend.py``) reads its AI-service substring patterns from the ``ai_spending.service_whitelist`` platform setting — same row the AI Spending dashboard reads. In a real deployment the row is seeded by alembic migration 149; the test harness uses ``Base.metadata.create_all`` instead of running alembic, so the row is absent and ``load_whitelist`` would raise. This conftest seeds the platform_setting row at the start of every apikey test so the spend module sees a sensible default. Tests that want to exercise the empty-whitelist path can override the row inline. """ from __future__ import annotations import pytest_asyncio from sqlalchemy import select from app.integrations.models import PlatformSetting @pytest_asyncio.fixture(autouse=True) async def _seed_ai_spending_whitelist(db_session): """Seed ``platform_settings.ai_spending.service_whitelist`` with a minimal Phase-2-style default before every apikey test runs. Patterns mirror the migration-149 seed: ``Foundry`` + ``Azure OpenAI`` for Azure, ``Bedrock`` for AWS, ``Vertex AI`` + ``Generative AI`` for GCP. Phase 1 tests were written against these exact strings (then hardcoded in spend.py), so seeding them keeps Phase 1 assertions valid without per-test edits. """ key = "ai_spending.service_whitelist" existing = ( await db_session.execute( select(PlatformSetting).where(PlatformSetting.key == key), ) ).scalar_one_or_none() if existing is None: db_session.add(PlatformSetting( key=key, value={ "azure": ["Foundry", "Azure OpenAI"], "aws": ["Bedrock"], "gcp": ["Vertex AI", "Generative AI"], }, )) await db_session.flush() yield