From 6ea82a8496301b8bdb0f26cd4b48500fb495c5e7 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Mon, 9 Feb 2026 16:31:12 -0500 Subject: [PATCH] Add save directory helpers for IF sessions --- src/mudlib/if_session.py | 11 +++++++++++ tests/test_if_session.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/mudlib/if_session.py b/src/mudlib/if_session.py index 0cee9ec..04f5c58 100644 --- a/src/mudlib/if_session.py +++ b/src/mudlib/if_session.py @@ -25,6 +25,17 @@ class IFSession: self.story_path = story_path self.game_name = game_name or Path(story_path).stem self.process: asyncio.subprocess.Process | None = None + # data/ directory is at project root (2 levels up from src/mudlib/) + self._data_dir = Path(__file__).resolve().parents[2] / "data" + + @property + def save_path(self) -> Path: + """Return path to save file for this player/game combo.""" + return self._data_dir / "if_saves" / self.player.name / f"{self.game_name}.qzl" + + def _ensure_save_dir(self) -> None: + """Create save directory if it doesn't exist.""" + self.save_path.parent.mkdir(parents=True, exist_ok=True) async def start(self) -> str: """Spawn dfrotz and return intro text.""" diff --git a/tests/test_if_session.py b/tests/test_if_session.py index a7f0db8..1b13441 100644 --- a/tests/test_if_session.py +++ b/tests/test_if_session.py @@ -290,3 +290,37 @@ async def test_handle_input_empty_string(): # Should still write a newline mock_process.stdin.write.assert_called() + + +def test_save_path_property(tmp_path): + """save_path returns correct path for player/game combo.""" + player = MagicMock() + player.name = "tester" + session = IFSession(player, "/path/to/zork.z5", "zork") + + # Override data_dir for testing + session._data_dir = tmp_path + + save_path = session.save_path + assert save_path == tmp_path / "if_saves" / "tester" / "zork.qzl" + + +def test_ensure_save_dir_creates_directories(tmp_path): + """_ensure_save_dir() creates parent directories.""" + player = MagicMock() + player.name = "alice" + session = IFSession(player, "/path/to/story.z5", "story") + + # Override data_dir for testing + session._data_dir = tmp_path + + # Directory shouldn't exist yet + expected_dir = tmp_path / "if_saves" / "alice" + assert not expected_dir.exists() + + # Call _ensure_save_dir + session._ensure_save_dir() + + # Now it should exist + assert expected_dir.exists() + assert expected_dir.is_dir()