"""端点测试 fixtures：用内存替身覆盖依赖，注入有效加密 key（无 DB/无网络）。"""

from __future__ import annotations

import os

import pytest
from cryptography.fernet import Fernet
from fakes_providers import FakeCredentialStore, FakeProviderProbe
from fastapi.testclient import TestClient


@pytest.fixture
def enc_key() -> str:
    return Fernet.generate_key().decode()


@pytest.fixture
def store() -> FakeCredentialStore:
    return FakeCredentialStore()


@pytest.fixture
def probe() -> FakeProviderProbe:
    return FakeProviderProbe()


@pytest.fixture
def client(enc_key: str, store: FakeCredentialStore, probe: FakeProviderProbe) -> TestClient:
    os.environ["CREDENTIAL_ENC_KEY"] = enc_key
    from ww_config import get_settings

    get_settings.cache_clear()  # 让新 env 生效

    from ww_api.main import create_app
    from ww_api.services.provider_deps import (
        get_credential_store,
        get_provider_probe,
    )

    app = create_app()
    app.dependency_overrides[get_credential_store] = lambda: store
    app.dependency_overrides[get_provider_probe] = lambda: probe
    return TestClient(app)
