Add save directory helpers for IF sessions

This commit is contained in:
Jared Miller 2026-02-09 16:31:12 -05:00
parent 057a746687
commit 6ea82a8496
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 45 additions and 0 deletions

View file

@ -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."""

View file

@ -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()