import pytest from pyra.security.boundaries import VaultAccessError def test_write_creates_file(tmp_pyra_home): from pyra.memory.writer import write_memory path = write_memory("knowledge/test.md", "# Knowledge\n\nSome facts.") assert path.exists() assert path.read_text() == "# Knowledge\n\nSome facts." def test_write_updates_index(tmp_pyra_home): from pyra.memory.writer import write_memory write_memory("user/notes.md", "Notes here.") index = (tmp_pyra_home / "memory" / "MEMORY_INDEX.md").read_text() assert "notes" in index.lower() def test_append_to_existing(tmp_pyra_home): from pyra.memory.writer import write_memory, append_memory write_memory("context/ongoing.md", "First line.") append_memory("context/ongoing.md", "Second line.") from pyra.memory.reader import read_memory content = read_memory("context/ongoing.md") assert "First line." in content assert "Second line." in content def test_append_creates_file_if_missing(tmp_pyra_home): from pyra.memory.writer import append_memory path = append_memory("context/new_file.md", "Created by append.") assert path.exists() def test_write_absolute_path_blocked(tmp_pyra_home): from pyra.memory.writer import write_memory with pytest.raises((ValueError, PermissionError, VaultAccessError)): write_memory("/etc/passwd", "evil") def test_write_home_relative_blocked(tmp_pyra_home): from pyra.memory.writer import write_memory with pytest.raises((ValueError, PermissionError, VaultAccessError)): write_memory("~/secret_file.md", "evil") def test_write_traversal_blocked(tmp_pyra_home): from pyra.memory.writer import write_memory with pytest.raises((VaultAccessError, PermissionError, ValueError)): write_memory("../../vault/secrets/api_keys.json", "evil") def test_file_permissions(tmp_pyra_home): import os from pyra.memory.writer import write_memory path = write_memory("user/perm_test.md", "content") if os.name != "nt": mode = oct(path.stat().st_mode)[-3:] assert mode == "600"