pyLyricFlow/tests/test_session_store.py
2026-02-24 13:22:10 -06:00

67 lines
2.3 KiB
Python

import json
import os
import tempfile
import unittest
from src.utils.session_store import SessionStore, SessionTabSnapshot
class TestSessionStore(unittest.TestCase):
def _make_store(self, root: str) -> tuple[SessionStore, str]:
storage_path = os.path.join(root, "session_snapshots.json")
return SessionStore(storage_path=storage_path), storage_path
def _sample_snapshot(self) -> SessionTabSnapshot:
return SessionTabSnapshot(
tab_id="untitled::0",
file_path=None,
display_name="Untitled",
content="hello world",
cursor_position=5,
is_dirty=True,
is_untitled=True,
snapshot_mtime=None,
workspace_root=None,
updated_at="2026-02-19T00:00:00+00:00",
)
def test_save_and_load(self):
with tempfile.TemporaryDirectory() as tmp:
store, _ = self._make_store(tmp)
snapshot = self._sample_snapshot()
store.save(None, [snapshot])
loaded = store.load(None)
self.assertEqual([snapshot], loaded)
def test_workspace_scoping(self):
with tempfile.TemporaryDirectory() as tmp:
store, _ = self._make_store(tmp)
s1 = self._sample_snapshot()
s2 = self._sample_snapshot()
s2.tab_id = "untitled::1"
s2.content = "workspace two"
ws1 = os.path.join(tmp, "ws1")
ws2 = os.path.join(tmp, "ws2")
store.save(ws1, [s1])
store.save(ws2, [s2])
self.assertEqual([s1], store.load(ws1))
self.assertEqual([s2], store.load(ws2))
self.assertEqual([], store.load(os.path.join(tmp, "missing")))
def test_schema_version_handling(self):
with tempfile.TemporaryDirectory() as tmp:
store, storage_path = self._make_store(tmp)
snapshot = self._sample_snapshot()
with open(storage_path, "w", encoding="utf-8") as f:
json.dump({"version": 999, "snapshots": [snapshot.to_dict()]}, f, indent=2)
loaded = store.load(None)
self.assertEqual(1, len(loaded))
self.assertEqual(snapshot.content, loaded[0].content)
if __name__ == "__main__":
unittest.main()