"""Tests for the adapter registry."""

import pytest

from infrastructure.world.adapters.mock import MockWorldAdapter
from infrastructure.world.registry import AdapterRegistry


class TestAdapterRegistry:
    def test_register_and_get(self):
        reg = AdapterRegistry()
        reg.register("mock", MockWorldAdapter)
        adapter = reg.get("mock")
        assert isinstance(adapter, MockWorldAdapter)

    def test_register_with_kwargs(self):
        reg = AdapterRegistry()
        reg.register("mock", MockWorldAdapter)
        adapter = reg.get("mock", location="Custom Room")
        assert adapter._location == "Custom Room"

    def test_get_unknown_raises(self):
        reg = AdapterRegistry()
        with pytest.raises(KeyError):
            reg.get("nonexistent")

    def test_register_non_subclass_raises(self):
        reg = AdapterRegistry()
        with pytest.raises(TypeError):
            reg.register("bad", dict)

    def test_list_adapters(self):
        reg = AdapterRegistry()
        reg.register("beta", MockWorldAdapter)
        reg.register("alpha", MockWorldAdapter)
        assert reg.list_adapters() == ["alpha", "beta"]

    def test_contains(self):
        reg = AdapterRegistry()
        reg.register("mock", MockWorldAdapter)
        assert "mock" in reg
        assert "other" not in reg

    def test_len(self):
        reg = AdapterRegistry()
        assert len(reg) == 0
        reg.register("mock", MockWorldAdapter)
        assert len(reg) == 1

    def test_overwrite_warns(self, caplog):
        import logging

        reg = AdapterRegistry()
        reg.register("mock", MockWorldAdapter)
        with caplog.at_level(logging.WARNING):
            reg.register("mock", MockWorldAdapter)
        assert "Overwriting" in caplog.text


class TestModuleLevelRegistry:
    """Test the convenience functions in infrastructure.world.__init__."""

    def test_register_and_get(self):
        from infrastructure.world import get_adapter, register_adapter

        register_adapter("test_mock", MockWorldAdapter)
        adapter = get_adapter("test_mock")
        assert isinstance(adapter, MockWorldAdapter)
