"""Tests for the Container class.""" from mudlib.container import Container from mudlib.object import Object from mudlib.thing import Thing from mudlib.zone import Zone # --- construction --- # --- can_accept --- def test_container_accepts_thing_when_open(): """Container accepts Things when open and has capacity.""" c = Container(name="chest") sword = Thing(name="sword") assert c.can_accept(sword) is True def test_container_rejects_when_closed(): """Container rejects Things when closed.""" c = Container(name="chest", closed=True) sword = Thing(name="sword") assert c.can_accept(sword) is False def test_container_rejects_when_at_capacity(): """Container rejects Things when at capacity.""" c = Container(name="pouch", capacity=2) # Add two items Thing(name="rock1", location=c) Thing(name="rock2", location=c) # Third should be rejected rock3 = Thing(name="rock3") assert c.can_accept(rock3) is False def test_container_accepts_when_below_capacity(): """Container accepts Things when below capacity.""" c = Container(name="pouch", capacity=2) Thing(name="rock1", location=c) # Second item should be accepted rock2 = Thing(name="rock2") assert c.can_accept(rock2) is True def test_container_rejects_non_thing(): """Container rejects objects that aren't Things.""" c = Container(name="chest") other = Object(name="abstract") assert c.can_accept(other) is False def test_container_locked_is_just_flag(): """Locked flag is stored but doesn't affect can_accept (used by commands).""" c = Container(name="chest", locked=True, closed=False) sword = Thing(name="sword") # can_accept doesn't check locked (commands will check it separately) # This test documents current behavior — locked is for command layer assert c.locked is True # can_accept only checks closed and capacity assert c.can_accept(sword) is True # --- integration with zones --- def test_container_in_zone(): """Container can be placed in a zone with coordinates.""" terrain = [["." for _ in range(10)] for _ in range(10)] zone = Zone(name="test", width=10, height=10, terrain=terrain) chest = Container(name="chest", location=zone, x=5, y=5) assert chest.location is zone assert chest.x == 5 assert chest.y == 5 assert chest in zone.contents def test_container_with_contents(): """Container can hold Things.""" chest = Container(name="chest") sword = Thing(name="sword", location=chest) gem = Thing(name="gem", location=chest) assert sword in chest.contents assert gem in chest.contents assert len(chest.contents) == 2