2026-04-04 18:33:42 -07:00
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import tempfile
|
2026-04-12 14:23:34 -07:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from mempalace.config import MempalaceConfig, sanitize_name
|
2026-04-04 18:33:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_default_config():
|
|
|
|
|
cfg = MempalaceConfig(config_dir=tempfile.mkdtemp())
|
|
|
|
|
assert "palace" in cfg.palace_path
|
|
|
|
|
assert cfg.collection_name == "mempalace_drawers"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_from_file():
|
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
|
|
|
with open(os.path.join(tmpdir, "config.json"), "w") as f:
|
|
|
|
|
json.dump({"palace_path": "/custom/palace"}, f)
|
|
|
|
|
cfg = MempalaceConfig(config_dir=tmpdir)
|
|
|
|
|
assert cfg.palace_path == "/custom/palace"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_env_override():
|
|
|
|
|
os.environ["MEMPALACE_PALACE_PATH"] = "/env/palace"
|
|
|
|
|
cfg = MempalaceConfig(config_dir=tempfile.mkdtemp())
|
|
|
|
|
assert cfg.palace_path == "/env/palace"
|
|
|
|
|
del os.environ["MEMPALACE_PALACE_PATH"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_init():
|
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
|
|
|
cfg = MempalaceConfig(config_dir=tmpdir)
|
|
|
|
|
cfg.init()
|
|
|
|
|
assert os.path.exists(os.path.join(tmpdir, "config.json"))
|
2026-04-12 14:23:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- sanitize_name ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_ascii():
|
|
|
|
|
assert sanitize_name("hello") == "hello"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_latvian():
|
|
|
|
|
assert sanitize_name("Jānis") == "Jānis"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_cjk():
|
|
|
|
|
assert sanitize_name("太郎") == "太郎"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_cyrillic():
|
|
|
|
|
assert sanitize_name("Алексей") == "Алексей"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_rejects_leading_underscore():
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
sanitize_name("_foo")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_rejects_path_traversal():
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
sanitize_name("../etc/passwd")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_name_rejects_empty():
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
sanitize_name("")
|