"""Tests for the DM notification channel.""" from pathlib import Path import pytest from storied import notifications @pytest.fixture def world(tmp_path: Path) -> str: """Return the world_id with world directory created under the autouse-isolated data home.""" world_dir = tmp_path / "worlds" / "test-world" world_dir.mkdir(parents=True) return "test-world" class TestAppendAndDrain: def test_drain_empty(self, world: str): assert notifications.drain(world) == [] def test_append_then_drain(self, world: str): notifications.append(world, "Something happened") messages = notifications.drain(world) assert messages == ["Something happened"] def test_drain_clears(self, world: str): notifications.append(world, "First") notifications.drain(world) assert notifications.drain(world) == [] def test_multiple_messages(self, world: str): notifications.append(world, "First thing") notifications.append(world, "Second thing") notifications.append(world, "Third thing") messages = notifications.drain(world) assert messages == ["First thing", "Second thing", "Third thing"] def test_file_removed_after_drain(self, world: str, tmp_path: Path): notifications.append(world, "Temporary") notifications.drain(world) path = tmp_path / "worlds" / "test-world" / "dm_notifications.md" assert not path.exists() def test_creates_world_dir_if_missing(self): notifications.append("new-world", "Hello") messages = notifications.drain("new-world") assert messages == ["Hello"] def test_drain_empty_existing_file_returns_empty( self, world: str, tmp_path: Path, ): """An existing notifications file with only whitespace drains as empty and is removed.""" path = tmp_path / "worlds" / "test-world" / "dm_notifications.md" path.write_text(" \n \n") assert notifications.drain(world) == [] assert not path.exists()