|
1
|
"""Tests for navegador.config — get_store() env var resolution and init_project().""" |
|
2
|
|
|
3
|
import tempfile |
|
4
|
from pathlib import Path |
|
5
|
from unittest.mock import MagicMock, patch |
|
6
|
|
|
7
|
|
|
8
|
class TestGetStore: |
|
9
|
def test_explicit_path_returns_sqlite(self): |
|
10
|
with patch("navegador.graph.GraphStore") as mock_gs: |
|
11
|
mock_gs.sqlite.return_value = MagicMock() |
|
12
|
from navegador.config import get_store |
|
13
|
|
|
14
|
get_store("/tmp/test.db") |
|
15
|
mock_gs.sqlite.assert_called_once_with("/tmp/test.db") |
|
16
|
|
|
17
|
def test_redis_url_env_returns_redis(self, monkeypatch): |
|
18
|
monkeypatch.setenv("NAVEGADOR_REDIS_URL", "redis://localhost:6379") |
|
19
|
monkeypatch.delenv("NAVEGADOR_DB", raising=False) |
|
20
|
with patch("navegador.graph.GraphStore") as mock_gs: |
|
21
|
mock_gs.redis.return_value = MagicMock() |
|
22
|
# Re-import to pick up env changes |
|
23
|
import importlib |
|
24
|
|
|
25
|
import navegador.config as cfg |
|
26
|
importlib.reload(cfg) |
|
27
|
cfg.get_store() |
|
28
|
mock_gs.redis.assert_called_once_with("redis://localhost:6379") |
|
29
|
|
|
30
|
def test_db_env_returns_sqlite(self, monkeypatch): |
|
31
|
monkeypatch.delenv("NAVEGADOR_REDIS_URL", raising=False) |
|
32
|
monkeypatch.setenv("NAVEGADOR_DB", "/tmp/custom.db") |
|
33
|
with patch("navegador.graph.GraphStore") as mock_gs: |
|
34
|
mock_gs.sqlite.return_value = MagicMock() |
|
35
|
import importlib |
|
36
|
|
|
37
|
import navegador.config as cfg |
|
38
|
importlib.reload(cfg) |
|
39
|
cfg.get_store() |
|
40
|
mock_gs.sqlite.assert_called_once_with("/tmp/custom.db") |
|
41
|
|
|
42
|
def test_default_sqlite_path(self, monkeypatch): |
|
43
|
monkeypatch.delenv("NAVEGADOR_REDIS_URL", raising=False) |
|
44
|
monkeypatch.delenv("NAVEGADOR_DB", raising=False) |
|
45
|
with patch("navegador.graph.GraphStore") as mock_gs: |
|
46
|
mock_gs.sqlite.return_value = MagicMock() |
|
47
|
import importlib |
|
48
|
|
|
49
|
import navegador.config as cfg |
|
50
|
importlib.reload(cfg) |
|
51
|
cfg.get_store() |
|
52
|
mock_gs.sqlite.assert_called_once_with(".navegador/graph.db") |
|
53
|
|
|
54
|
def test_redis_takes_precedence_over_db_env(self, monkeypatch): |
|
55
|
monkeypatch.setenv("NAVEGADOR_REDIS_URL", "redis://myhost:6379") |
|
56
|
monkeypatch.setenv("NAVEGADOR_DB", "/tmp/other.db") |
|
57
|
with patch("navegador.graph.GraphStore") as mock_gs: |
|
58
|
mock_gs.redis.return_value = MagicMock() |
|
59
|
import importlib |
|
60
|
|
|
61
|
import navegador.config as cfg |
|
62
|
importlib.reload(cfg) |
|
63
|
cfg.get_store() |
|
64
|
mock_gs.redis.assert_called_once_with("redis://myhost:6379") |
|
65
|
mock_gs.sqlite.assert_not_called() |
|
66
|
|
|
67
|
|
|
68
|
class TestInitProject: |
|
69
|
def test_creates_navegador_dir(self): |
|
70
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
71
|
from navegador.config import init_project |
|
72
|
nav_dir = init_project(tmpdir) |
|
73
|
assert nav_dir.exists() |
|
74
|
assert nav_dir.name == ".navegador" |
|
75
|
|
|
76
|
def test_creates_env_example(self): |
|
77
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
78
|
from navegador.config import init_project |
|
79
|
nav_dir = init_project(tmpdir) |
|
80
|
env_example = nav_dir / ".env.example" |
|
81
|
assert env_example.exists() |
|
82
|
content = env_example.read_text() |
|
83
|
assert "NAVEGADOR_DB" in content |
|
84
|
assert "NAVEGADOR_REDIS_URL" in content |
|
85
|
|
|
86
|
def test_does_not_overwrite_existing_env_example(self): |
|
87
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
88
|
from navegador.config import init_project |
|
89
|
nav_dir = Path(tmpdir) / ".navegador" |
|
90
|
nav_dir.mkdir() |
|
91
|
env_example = nav_dir / ".env.example" |
|
92
|
env_example.write_text("custom content") |
|
93
|
init_project(tmpdir) |
|
94
|
assert env_example.read_text() == "custom content" |
|
95
|
|
|
96
|
def test_creates_gitignore_if_missing(self): |
|
97
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
98
|
from navegador.config import init_project |
|
99
|
init_project(tmpdir) |
|
100
|
gitignore = Path(tmpdir) / ".gitignore" |
|
101
|
assert gitignore.exists() |
|
102
|
assert ".navegador/" in gitignore.read_text() |
|
103
|
|
|
104
|
def test_appends_to_existing_gitignore(self): |
|
105
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
106
|
gitignore = Path(tmpdir) / ".gitignore" |
|
107
|
gitignore.write_text("*.pyc\n__pycache__/\n") |
|
108
|
from navegador.config import init_project |
|
109
|
init_project(tmpdir) |
|
110
|
content = gitignore.read_text() |
|
111
|
assert "*.pyc" in content |
|
112
|
assert ".navegador/" in content |
|
113
|
|
|
114
|
def test_does_not_duplicate_gitignore_entry(self): |
|
115
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
116
|
gitignore = Path(tmpdir) / ".gitignore" |
|
117
|
gitignore.write_text(".navegador/\n") |
|
118
|
from navegador.config import init_project |
|
119
|
init_project(tmpdir) |
|
120
|
content = gitignore.read_text() |
|
121
|
assert content.count(".navegador/") == 1 |
|
122
|
|
|
123
|
def test_returns_nav_dir_path(self): |
|
124
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
125
|
from navegador.config import init_project |
|
126
|
result = init_project(tmpdir) |
|
127
|
assert isinstance(result, Path) |
|
128
|
assert result == Path(tmpdir).resolve() / ".navegador" |
|
129
|
|
|
130
|
def test_creates_config_toml(self): |
|
131
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
132
|
from navegador.config import init_project |
|
133
|
nav_dir = init_project(tmpdir) |
|
134
|
config = nav_dir / "config.toml" |
|
135
|
assert config.exists() |
|
136
|
content = config.read_text() |
|
137
|
assert "[storage]" in content |
|
138
|
assert "[llm]" in content |
|
139
|
assert "[cluster]" in content |
|
140
|
|
|
141
|
def test_config_toml_sqlite_defaults(self): |
|
142
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
143
|
from navegador.config import init_project |
|
144
|
nav_dir = init_project(tmpdir) |
|
145
|
content = (nav_dir / "config.toml").read_text() |
|
146
|
assert 'backend = "sqlite"' in content |
|
147
|
assert "db_path" in content |
|
148
|
|
|
149
|
def test_config_toml_redis_mode(self): |
|
150
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
151
|
from navegador.config import init_project |
|
152
|
nav_dir = init_project(tmpdir, storage="redis", redis_url="redis://host:6379") |
|
153
|
content = (nav_dir / "config.toml").read_text() |
|
154
|
assert 'backend = "redis"' in content |
|
155
|
assert 'redis_url = "redis://host:6379"' in content |
|
156
|
|
|
157
|
def test_config_toml_llm_settings(self): |
|
158
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
159
|
from navegador.config import init_project |
|
160
|
nav_dir = init_project(tmpdir, llm_provider="anthropic", llm_model="claude-sonnet-4-6") |
|
161
|
content = (nav_dir / "config.toml").read_text() |
|
162
|
assert 'provider = "anthropic"' in content |
|
163
|
assert 'model = "claude-sonnet-4-6"' in content |
|
164
|
|
|
165
|
def test_config_toml_cluster_enabled(self): |
|
166
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
167
|
from navegador.config import init_project |
|
168
|
nav_dir = init_project(tmpdir, cluster=True) |
|
169
|
content = (nav_dir / "config.toml").read_text() |
|
170
|
assert "enabled = true" in content |
|
171
|
|
|
172
|
def test_config_toml_cluster_disabled_by_default(self): |
|
173
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
174
|
from navegador.config import init_project |
|
175
|
nav_dir = init_project(tmpdir) |
|
176
|
content = (nav_dir / "config.toml").read_text() |
|
177
|
assert "enabled = false" in content |
|
178
|
|