"""Tests for container template loading and spawning.""" from pathlib import Path from tempfile import TemporaryDirectory from mudlib.container import Container from mudlib.thing import Thing from mudlib.things import load_thing_template, spawn_thing from mudlib.zone import Zone def test_load_container_template_with_capacity(): """load_thing_template recognizes container templates (has capacity field).""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "chest.toml" toml_path.write_text( """ name = "chest" description = "a wooden chest" capacity = 5 """ ) template = load_thing_template(toml_path) assert template.name == "chest" assert template.description == "a wooden chest" assert hasattr(template, "capacity") assert template.capacity == 5 def test_load_container_template_with_closed(): """load_thing_template handles closed field.""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "chest.toml" toml_path.write_text( """ name = "chest" description = "a wooden chest" capacity = 5 closed = true """ ) template = load_thing_template(toml_path) assert template.closed is True def test_load_container_template_with_locked(): """load_thing_template handles locked field.""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "chest.toml" toml_path.write_text( """ name = "chest" description = "a wooden chest" capacity = 5 closed = true locked = true """ ) template = load_thing_template(toml_path) assert template.locked is True def test_load_container_template_defaults(): """Container template fields have sensible defaults.""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "sack.toml" toml_path.write_text( """ name = "sack" description = "a cloth sack" capacity = 3 """ ) template = load_thing_template(toml_path) assert template.capacity == 3 assert template.closed is False # default: open assert template.locked is False # default: unlocked def test_spawn_container_from_template(): """spawn_thing creates a Container when template has capacity.""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "chest.toml" toml_path.write_text( """ name = "chest" description = "a wooden chest" capacity = 5 closed = true """ ) template = load_thing_template(toml_path) terrain = [["." for _ in range(10)] for _ in range(10)] zone = Zone(name="test", width=10, height=10, terrain=terrain) thing = spawn_thing(template, zone, x=5, y=5) assert isinstance(thing, Container) assert thing.name == "chest" assert thing.description == "a wooden chest" assert thing.capacity == 5 assert thing.closed is True assert thing.location is zone def test_spawn_regular_thing_from_template_without_capacity(): """spawn_thing creates a regular Thing when template lacks capacity.""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "rock.toml" toml_path.write_text( """ name = "rock" description = "a smooth grey rock" portable = true """ ) template = load_thing_template(toml_path) terrain = [["." for _ in range(10)] for _ in range(10)] zone = Zone(name="test", width=10, height=10, terrain=terrain) thing = spawn_thing(template, zone, x=5, y=5) assert isinstance(thing, Thing) assert not isinstance(thing, Container) assert thing.name == "rock" assert thing.portable is True def test_spawn_portable_container(): """spawn_thing creates a portable Container (like a sack).""" with TemporaryDirectory() as tmpdir: toml_path = Path(tmpdir) / "sack.toml" toml_path.write_text( """ name = "sack" description = "a cloth sack" capacity = 3 portable = true """ ) template = load_thing_template(toml_path) terrain = [["." for _ in range(10)] for _ in range(10)] zone = Zone(name="test", width=10, height=10, terrain=terrain) thing = spawn_thing(template, zone, x=5, y=5) assert isinstance(thing, Container) assert thing.portable is True assert thing.capacity == 3