"""Tests for config load and schema."""

from pathlib import Path

import pytest

from foremet.config.schema import ADKConfig
from foremet.config.load import load_config, CONFIG_FILENAME


def test_config_schema_valid() -> None:
    config = ADKConfig(
        openapi_url="https://example.com/openapi.json",
        product_name="Test",
        base_url="https://api.example.com",
        auth_env_var="API_KEY",
    )
    assert config.product_name == "Test"
    assert config.get_openapi_source() == "https://example.com/openapi.json"
    assert config.is_openapi_url() is True


def test_config_load_missing_file(tmp_path: Path) -> None:
    with pytest.raises(FileNotFoundError) as exc_info:
        load_config(tmp_path)
    assert "Run 'foremet init' first" in str(exc_info.value) or "adk.config.json" in str(exc_info.value)


def test_config_load_valid_json(tmp_path: Path) -> None:
    (tmp_path / CONFIG_FILENAME).write_text('''{
        "openapi_url": "https://api.example.com/spec.json",
        "product_name": "My API",
        "base_url": "https://api.example.com",
        "auth_env_var": "MY_KEY"
    }''')
    config = load_config(tmp_path)
    assert config.product_name == "My API"
    assert config.auth_env_var == "MY_KEY"
