A 5e storytelling engine with an LLM DM
1"""Tests for the DM notification channel."""
2
3from pathlib import Path
4
5import pytest
6
7from storied import notifications
8
9
10@pytest.fixture
11def world(tmp_path: Path) -> str:
12 """Return the world_id with world directory created under the
13 autouse-isolated data home."""
14 world_dir = tmp_path / "worlds" / "test-world"
15 world_dir.mkdir(parents=True)
16 return "test-world"
17
18
19class TestAppendAndDrain:
20 def test_drain_empty(self, world: str):
21 assert notifications.drain(world) == []
22
23 def test_append_then_drain(self, world: str):
24 notifications.append(world, "Something happened")
25
26 messages = notifications.drain(world)
27 assert messages == ["Something happened"]
28
29 def test_drain_clears(self, world: str):
30 notifications.append(world, "First")
31
32 notifications.drain(world)
33 assert notifications.drain(world) == []
34
35 def test_multiple_messages(self, world: str):
36 notifications.append(world, "First thing")
37 notifications.append(world, "Second thing")
38 notifications.append(world, "Third thing")
39
40 messages = notifications.drain(world)
41 assert messages == ["First thing", "Second thing", "Third thing"]
42
43 def test_file_removed_after_drain(self, world: str, tmp_path: Path):
44 notifications.append(world, "Temporary")
45
46 notifications.drain(world)
47 path = tmp_path / "worlds" / "test-world" / "dm_notifications.md"
48 assert not path.exists()
49
50 def test_creates_world_dir_if_missing(self):
51 notifications.append("new-world", "Hello")
52
53 messages = notifications.drain("new-world")
54 assert messages == ["Hello"]
55
56 def test_drain_empty_existing_file_returns_empty(
57 self,
58 world: str,
59 tmp_path: Path,
60 ):
61 """An existing notifications file with only whitespace drains
62 as empty and is removed."""
63 path = tmp_path / "worlds" / "test-world" / "dm_notifications.md"
64 path.write_text(" \n \n")
65
66 assert notifications.drain(world) == []
67 assert not path.exists()