import os
import shutil
import json
from pathlib import Path
import pytest
from amicus.config import ConfigManager
from amicus.core import find_project_root, get_context_bus_dir
@pytest.fixture
def toml_config_env(tmp_path):
"""Create a temporary environment with TOML config."""
workspace = tmp_path / "workspace"
workspace.mkdir()
amicus_dir = workspace / ".amicus"
amicus_dir.mkdir()
toml_file = amicus_dir / "config.toml"
toml_file.write_text('root_dir = "/tmp/custom"\nlock_timeout = 20.0')
return workspace
def test_load_toml_config(toml_config_env):
"""Test loading configuration from TOML file."""
config = ConfigManager(toml_config_env)
assert config.get("lock_timeout") == 20.0
assert config.get("root_dir") == "/tmp/custom"
def test_find_project_root(tmp_path):
"""Test project root discovery."""
# Structure: /root/subdir/deep
root = tmp_path / "root"
root.mkdir()
subdir = root / "subdir"
subdir.mkdir()
deep = subdir / "deep"
deep.mkdir()
# Case 1: No markers, returns start_path
assert find_project_root(deep) == deep
# Case 2: .amicus in root
(root / ".amicus").mkdir()
assert find_project_root(deep) == root
# Case 3: .git in subdir
(subdir / ".git").mkdir()
# Should find .git in subdir before .amicus in root
assert find_project_root(deep) == subdir
def test_get_context_bus_dir_discovery(tmp_path, monkeypatch):
"""Test context bus dir discovery."""
monkeypatch.delenv("CONTEXT_BUS_DIR", raising=False)
# Create a project structure
project = tmp_path / "project"
project.mkdir()
(project / ".ai").mkdir()
cwd = project / "src"
cwd.mkdir()
# Mock cwd
monkeypatch.setattr(Path, "cwd", lambda: cwd)
bus_dir = get_context_bus_dir()
assert bus_dir == project / ".ai"