claire/tests/test_config.py
autocommit 6d212b7dbe refactor(testing-test): ♻️ Update test imports to use claire instead of clare in package references
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-05-20 19:54:05 -07:00

79 lines
2.5 KiB
Python

from __future__ import annotations
import tomllib
from pathlib import Path
from claire.config import load_or_init
def test_load_or_init_creates_file_with_machine_id(tmp_path: Path) -> None:
cfg_path = tmp_path / "claire.toml"
cfg = load_or_init(cfg_path)
assert cfg_path.exists()
assert len(cfg.machine_id) == 36 # uuid4 string
# Reload returns the same machine_id (stable across runs).
cfg2 = load_or_init(cfg_path)
assert cfg2.machine_id == cfg.machine_id
def test_load_or_init_defaults_web_host_port(tmp_path: Path) -> None:
cfg = load_or_init(tmp_path / "claire.toml")
assert cfg.web.host == "127.0.0.1"
assert cfg.web.port == 8765
def test_load_or_init_migrates_missing_sync_secret(tmp_path: Path) -> None:
cfg_path = tmp_path / "claire.toml"
cfg_path.write_text(
'machine_id = "abc-123"\n'
"\n"
"[web]\n"
'host = "127.0.0.1"\n'
"port = 8765\n",
encoding="utf-8",
)
cfg = load_or_init(cfg_path)
assert cfg.sync_secret is not None
assert len(cfg.sync_secret) > 0
# File on disk now has the secret.
on_disk = tomllib.loads(cfg_path.read_text(encoding="utf-8"))
assert on_disk["sync_secret"] == cfg.sync_secret
# Idempotent: reload returns the same secret (no re-rolling).
cfg2 = load_or_init(cfg_path)
assert cfg2.sync_secret == cfg.sync_secret
def test_load_or_init_migration_preserves_other_fields(tmp_path: Path) -> None:
cfg_path = tmp_path / "claire.toml"
cfg_path.write_text(
'machine_id = "preserved-id"\n'
"\n"
"[web]\n"
'host = "0.0.0.0"\n'
"port = 9999\n"
"\n"
"[[peers]]\n"
'url = "http://peer-a.local"\n'
'secret = "peer-a-secret"\n'
"\n"
"[[peers]]\n"
'url = "http://peer-b.local"\n'
"\n"
"[[groups]]\n"
'name = "docs"\n'
'patterns = ["*.md", "*.txt"]\n',
encoding="utf-8",
)
cfg = load_or_init(cfg_path)
assert cfg.machine_id == "preserved-id"
assert cfg.web.host == "0.0.0.0"
assert cfg.web.port == 9999
assert len(cfg.peers) == 2
assert cfg.peers[0].url == "http://peer-a.local"
assert cfg.peers[0].secret == "peer-a-secret"
assert cfg.peers[1].url == "http://peer-b.local"
assert cfg.peers[1].secret is None
assert len(cfg.groups) == 1
assert cfg.groups[0].name == "docs"
assert cfg.groups[0].patterns == ["*.md", "*.txt"]
assert cfg.sync_secret is not None