"""Tests for zone registry."""

import pytest

from mudlib.zone import Zone
from mudlib.zones import get_zone, register_zone, zone_registry


@pytest.fixture(autouse=True)
def clear_registry():
    """Clear zone registry before each test."""
    zone_registry.clear()
    yield
    zone_registry.clear()


def test_register_zone():
    """Register a zone by name."""
    zone = Zone(name="test_zone", width=10, height=10, terrain=[], toroidal=False)
    register_zone("test_zone", zone)

    assert "test_zone" in zone_registry
    assert zone_registry["test_zone"] is zone


def test_get_zone():
    """Look up a zone by name."""
    zone = Zone(name="test_zone", width=10, height=10, terrain=[], toroidal=False)
    register_zone("test_zone", zone)

    retrieved = get_zone("test_zone")
    assert retrieved is zone


def test_get_zone_unknown():
    """Get None for unknown zone name."""
    result = get_zone("nonexistent")
    assert result is None


def test_register_multiple_zones():
    """Register multiple zones."""
    zone1 = Zone(name="zone1", width=10, height=10, terrain=[], toroidal=False)
    zone2 = Zone(name="zone2", width=20, height=15, terrain=[], toroidal=True)

    register_zone("zone1", zone1)
    register_zone("zone2", zone2)

    assert len(zone_registry) == 2
    assert get_zone("zone1") is zone1
    assert get_zone("zone2") is zone2


def test_overwrite_zone():
    """Registering same name twice overwrites."""
    zone1 = Zone(name="zone", width=10, height=10, terrain=[], toroidal=False)
    zone2 = Zone(name="zone", width=20, height=20, terrain=[], toroidal=False)

    register_zone("zone", zone1)
    register_zone("zone", zone2)

    assert get_zone("zone") is zone2
