"""Tests for lapis_spec.sections.meta."""

from lapis_spec.sections.meta import convert_meta


class TestConvertMeta:
    def test_basic_meta(self, minimal_openapi):
        meta = convert_meta(minimal_openapi)
        assert meta.api == "Minimal API"
        assert meta.base == "https://api.example.com"
        assert meta.version == "1.0.0"

    def test_description_truncated(self):
        doc = {
            "openapi": "3.0.0",
            "info": {
                "title": "Test",
                "version": "1.0",
                "description": "A" * 200,
            },
            "servers": [{"url": "https://example.com"}],
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.desc is not None
        assert len(meta.desc) <= 80

    def test_no_servers_default(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "No Servers", "version": "1.0"},
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.base == "https://api.example.com"

    def test_auth_bearer(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "Auth", "version": "1.0"},
            "servers": [{"url": "https://api.test.com"}],
            "components": {
                "securitySchemes": {
                    "bearerAuth": {
                        "type": "http",
                        "scheme": "bearer",
                    }
                }
            },
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.auth == "bearer header:Authorization"

    def test_auth_apikey_header(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "Auth", "version": "1.0"},
            "servers": [{"url": "https://api.test.com"}],
            "components": {
                "securitySchemes": {
                    "apiKey": {
                        "type": "apiKey",
                        "in": "header",
                        "name": "X-API-Key",
                    }
                }
            },
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.auth == "apikey header:X-API-Key"

    def test_auth_apikey_query(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "Auth", "version": "1.0"},
            "servers": [{"url": "https://api.test.com"}],
            "components": {
                "securitySchemes": {
                    "apiKey": {
                        "type": "apiKey",
                        "in": "query",
                        "name": "api_key",
                    }
                }
            },
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.auth == "apikey query:api_key"

    def test_auth_basic(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "Auth", "version": "1.0"},
            "servers": [{"url": "https://api.test.com"}],
            "components": {
                "securitySchemes": {
                    "basicAuth": {"type": "http", "scheme": "basic"}
                }
            },
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.auth == "basic"

    def test_auth_oauth2(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "Auth", "version": "1.0"},
            "servers": [{"url": "https://api.test.com"}],
            "components": {
                "securitySchemes": {
                    "oauth": {
                        "type": "oauth2",
                        "flows": {
                            "clientCredentials": {
                                "tokenUrl": "https://auth.test.com/token",
                                "scopes": {},
                            }
                        },
                    }
                }
            },
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.auth == "oauth2 https://auth.test.com/token"

    def test_auth_none(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "No Auth", "version": "1.0"},
            "servers": [{"url": "https://api.test.com"}],
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.auth is None

    def test_trailing_slash_removed(self):
        doc = {
            "openapi": "3.0.0",
            "info": {"title": "Test", "version": "1.0"},
            "servers": [{"url": "https://api.test.com/v2/"}],
            "paths": {},
        }
        meta = convert_meta(doc)
        assert meta.base == "https://api.test.com/v2"
