"""Tests for lapis_spec.sections.limits."""

from lapis_spec.sections.limits import convert_limits


class TestConvertLimits:
    def test_no_extensions(self):
        doc = {"info": {"title": "Test", "version": "1.0"}, "paths": {}}
        assert convert_limits(doc) is None

    def test_rate_limit_extension(self):
        doc = {
            "info": {"title": "Test", "version": "1.0"},
            "x-rateLimit": {
                "count": 100,
                "window": "m",
                "headers": "X-RateLimit-Limit, X-RateLimit-Remaining",
                "on_exceed": "429 retry_after",
            },
            "paths": {},
        }
        limits = convert_limits(doc)
        assert limits is not None
        assert len(limits.rates) == 1
        assert limits.rates[0].count == 100
        assert limits.rates[0].window == "m"
        assert limits.headers == "X-RateLimit-Limit, X-RateLimit-Remaining"
        assert limits.on_exceed == "429 retry_after"

    def test_rate_limit_list(self):
        doc = {
            "info": {"title": "Test", "version": "1.0"},
            "x-rateLimit": [
                {"count": 60, "window": "m", "scope": "@key"},
                {"count": 10, "window": "m", "scope": "@op:bulk"},
            ],
            "paths": {},
        }
        limits = convert_limits(doc)
        assert limits is not None
        assert len(limits.rates) == 2

    def test_quota_extension(self):
        doc = {
            "info": {"title": "Test", "version": "1.0"},
            "x-quota": [
                {"count": 1000, "period": "mo", "scope": "@key", "description": "monthly"},
            ],
            "paths": {},
        }
        limits = convert_limits(doc)
        assert limits is not None
        assert len(limits.quotas) == 1
        assert limits.quotas[0].count == 1000
        assert limits.quotas[0].description == "monthly"

    def test_plans(self):
        doc = {
            "info": {"title": "Test", "version": "1.0"},
            "x-plans": {
                "free": {
                    "rates": [{"count": 60, "window": "m"}],
                    "quotas": [{"count": 1000, "period": "mo"}],
                    "max_batch": 10,
                },
                "pro": {
                    "rates": [{"count": 600, "window": "m"}],
                    "max_batch": 100,
                },
            },
            "paths": {},
        }
        limits = convert_limits(doc)
        assert limits is not None
        assert len(limits.plans) == 2
        free = next(p for p in limits.plans if p.name == "free")
        assert free.max_batch == 10
        assert len(free.rates) == 1
        assert len(free.quotas) == 1

    def test_max_body_upload(self):
        doc = {
            "info": {"title": "Test", "version": "1.0"},
            "x-rateLimit": {
                "count": 100,
                "window": "m",
                "max_body": "5MB",
                "max_upload": "25MB",
            },
            "paths": {},
        }
        limits = convert_limits(doc)
        assert limits is not None
        assert limits.max_body == "5MB"
        assert limits.max_upload == "25MB"

    def test_info_extension(self):
        doc = {
            "info": {
                "title": "Test",
                "version": "1.0",
                "x-rateLimit": {"count": 50, "window": "h"},
            },
            "paths": {},
        }
        limits = convert_limits(doc)
        assert limits is not None
        assert len(limits.rates) == 1
