-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_config.py
More file actions
457 lines (355 loc) · 19.4 KB
/
Copy pathtest_config.py
File metadata and controls
457 lines (355 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""Tests for config discovery and resolution logic."""
from pathlib import Path
from java_codebase_rag.config import (
discover_project_root,
YAML_CONFIG_FILENAMES,
resolve_operator_config,
)
class TestDiscoverProjectRoot:
"""Tests for discover_project_root walk-up behavior."""
def test_discover_project_root_finds_config_in_cwd(self, tmp_path):
"""Config in cwd returns cwd."""
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("# test config")
result = discover_project_root(tmp_path)
assert result == tmp_path
def test_discover_project_root_walks_up(self, tmp_path):
"""Config in parent returns parent."""
subdir = tmp_path / "subdir"
subdir.mkdir()
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("# test config")
result = discover_project_root(subdir)
assert result == tmp_path
def test_discover_project_root_stops_at_home_boundary(self, tmp_path, monkeypatch):
"""Config at $HOME itself is found when walking up from subdirectory."""
# Create a fake home under tmp_path
fake_home = tmp_path / "home"
fake_home.mkdir()
project_dir = fake_home / "project"
project_dir.mkdir()
config_file = fake_home / YAML_CONFIG_FILENAMES[0]
config_file.write_text("# test config at home")
# Mock HOME to point to our fake home
monkeypatch.setenv("HOME", str(fake_home))
result = discover_project_root(project_dir)
assert result == fake_home
def test_discover_project_root_not_found_above_home(self, tmp_path, monkeypatch):
"""No config anywhere under $HOME returns None."""
fake_home = tmp_path / "home"
fake_home.mkdir()
project_dir = fake_home / "project"
project_dir.mkdir()
monkeypatch.setenv("HOME", str(fake_home))
result = discover_project_root(project_dir)
assert result is None
def test_discover_project_root_not_found(self, tmp_path):
"""No config anywhere returns None."""
result = discover_project_root(tmp_path)
assert result is None
def test_discover_project_root_first_match_wins(self, tmp_path):
"""Configs at two levels - closest to cwd wins."""
subdir = tmp_path / "subdir"
subdir.mkdir()
subsubdir = subdir / "subsub"
subsubdir.mkdir()
# Config at both levels
parent_config = tmp_path / YAML_CONFIG_FILENAMES[0]
parent_config.write_text("# parent config")
child_config = subdir / YAML_CONFIG_FILENAMES[1] # Use .yaml variant
child_config.write_text("# child config")
result = discover_project_root(subsubdir)
# Should find the closest config (subdir), not the parent (tmp_path)
assert result == subdir
def test_discover_project_root_finds_nonempty_index_dir(self, tmp_path):
"""Non-empty .java-codebase-rag/ directory acts as project anchor."""
subdir = tmp_path / "microservice"
subdir.mkdir()
idx = tmp_path / ".java-codebase-rag"
idx.mkdir()
(idx / "code_graph.lbug").write_bytes(b"\x00" * 16)
result = discover_project_root(subdir)
assert result == tmp_path
def test_discover_project_root_skips_empty_index_dir(self, tmp_path):
"""Empty .java-codebase-rag/ directory does not anchor the project."""
subdir = tmp_path / "microservice"
subdir.mkdir()
# Empty index dir at subdir level
empty_idx = subdir / ".java-codebase-rag"
empty_idx.mkdir()
# Real index at parent level
real_idx = tmp_path / ".java-codebase-rag"
real_idx.mkdir()
(real_idx / "code_graph.lbug").write_bytes(b"\x00" * 16)
result = discover_project_root(subdir)
assert result == tmp_path
def test_discover_project_root_config_wins_over_index_dir(self, tmp_path):
"""Config file takes priority over index dir at the same level."""
subdir = tmp_path / "subdir"
subdir.mkdir()
# Index dir at tmp_path level
idx = tmp_path / ".java-codebase-rag"
idx.mkdir()
(idx / "code_graph.lbug").write_bytes(b"\x00" * 16)
# Config at subdir level
config_file = subdir / YAML_CONFIG_FILENAMES[0]
config_file.write_text("# child config")
deep = subdir / "deep"
deep.mkdir()
result = discover_project_root(deep)
# Config at subdir is closer and wins
assert result == subdir
def test_discover_project_root_both_markers_same_level(self, tmp_path):
"""When both config and index dir exist at same dir, both resolve correctly."""
# Both markers in the same directory
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("# config")
idx = tmp_path / ".java-codebase-rag"
idx.mkdir()
(idx / "code_graph.lbug").write_bytes(b"\x00" * 16)
result = discover_project_root(tmp_path)
assert result == tmp_path
class TestSourceRootFromYaml:
"""Tests for source_root YAML field parsing and resolution."""
def test_source_root_from_yaml_relative(self, tmp_path, monkeypatch):
"""source_root: ../ resolves to parent of config dir."""
# Clean environment from conftest.py session fixture
monkeypatch.delenv("JAVA_CODEBASE_RAG_INDEX_DIR", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("source_root: ../")
# Change cwd to tmp_path so walk-up finds this config
monkeypatch.chdir(tmp_path)
# source_root=None triggers walk-up discovery + YAML parsing
result = resolve_operator_config(source_root=None)
# source_root should be the parent of tmp_path
assert result.source_root == tmp_path.parent
def test_source_root_from_yaml_absolute(self, tmp_path, monkeypatch):
"""source_root: /abs/path resolves to absolute path."""
# Clean environment from conftest.py session fixture
monkeypatch.delenv("JAVA_CODEBASE_RAG_INDEX_DIR", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
absolute_path = "/some/absolute/path"
config_file.write_text(f"source_root: {absolute_path}")
# Change cwd to tmp_path so walk-up finds this config
monkeypatch.chdir(tmp_path)
# source_root=None triggers walk-up discovery + YAML parsing
result = resolve_operator_config(source_root=None)
assert result.source_root == Path(absolute_path)
class TestIndexDirRelativeToConfigDir:
"""YAML ``index_dir`` must resolve against the config file's directory.
``source_root`` already resolves against the config dir (see
``TestSourceRootFromYaml``). ``index_dir`` must use the SAME base so a
user can express both keys relative to the config file — otherwise a
``../`` in ``index_dir`` gets re-applied on top of the already-resolved
``source_root`` and overshoots by one level (the "init indexes ~/"
symptom when the config lives in a subdirectory of the Java tree).
"""
def test_yaml_index_dir_double_dot_resolves_against_config_dir(self, tmp_path, monkeypatch):
"""``index_dir: ../x`` is relative to the config file's directory, not source_root."""
monkeypatch.delenv("JAVA_CODEBASE_RAG_INDEX_DIR", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_dir = tmp_path / "my-project-context"
config_dir.mkdir()
(config_dir / YAML_CONFIG_FILENAMES[0]).write_text(
"source_root: ../\nindex_dir: ../.java-codebase-rag\n"
)
monkeypatch.chdir(config_dir)
result = resolve_operator_config(source_root=None)
# source_root ../ -> tmp_path (one level above the config file)
assert result.source_root == tmp_path
# index_dir ../ -> tmp_path/.java-codebase-rag (one level above the config file),
# NOT tmp_path.parent/.java-codebase-rag (which is what resolving against
# the already-resolved source_root would produce).
assert result.index_dir == (tmp_path / ".java-codebase-rag").resolve()
def test_yaml_index_dir_bare_resolves_against_config_dir(self, tmp_path, monkeypatch):
"""``index_dir: x`` (no ``../``) sits next to the config file."""
monkeypatch.delenv("JAVA_CODEBASE_RAG_INDEX_DIR", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_dir = tmp_path / "my-project-context"
config_dir.mkdir()
(config_dir / YAML_CONFIG_FILENAMES[0]).write_text(
"source_root: ../\nindex_dir: .java-codebase-rag\n"
)
monkeypatch.chdir(config_dir)
result = resolve_operator_config(source_root=None)
assert result.source_root == tmp_path
# Bare path resolves against the config dir, so the index sits beside
# the config file — NOT beside source_root.
assert result.index_dir == (config_dir / ".java-codebase-rag").resolve()
assert result.index_dir_source == "yaml"
class TestSourceRootPrecedence:
"""Tests for source_root precedence chain."""
def test_source_root_precedence_cli_over_yaml(self, tmp_path, monkeypatch):
"""CLI flag wins over YAML source_root."""
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("source_root: /yaml/path")
cli_root = tmp_path / "cli_root"
cli_root.mkdir()
result = resolve_operator_config(source_root=cli_root)
# CLI flag should win
assert result.source_root == cli_root
def test_source_root_precedence_yaml_over_discovery(self, tmp_path, monkeypatch):
"""YAML source_root wins over config dir default."""
# Clean environment from conftest.py session fixture
monkeypatch.delenv("JAVA_CODEBASE_RAG_INDEX_DIR", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("source_root: /yaml/root")
# Change cwd to tmp_path so walk-up finds this config
monkeypatch.chdir(tmp_path)
# source_root=None triggers walk-up discovery
result = resolve_operator_config(source_root=None)
# YAML should override the discovered config dir
assert result.source_root == Path("/yaml/root")
def test_source_root_precedence_env_over_yaml(self, tmp_path, monkeypatch):
"""env var wins over YAML source_root."""
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("source_root: /yaml/path")
env_root = tmp_path / "env_root"
env_root.mkdir()
monkeypatch.setenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", str(env_root))
result = resolve_operator_config(source_root=None)
# Env var should win
assert result.source_root == env_root
def test_existing_behavior_unchanged(self, tmp_path, monkeypatch):
"""No walk-up, cwd = config dir → identical behavior to today."""
# Clean environment from conftest.py session fixture
monkeypatch.delenv("JAVA_CODEBASE_RAG_INDEX_DIR", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
# Create a config at cwd
config_file = tmp_path / YAML_CONFIG_FILENAMES[0]
config_file.write_text("# test config")
# Set cwd to tmp_path
monkeypatch.chdir(tmp_path)
# Call with source_root=tmp_path (old behavior: explicit root)
result = resolve_operator_config(source_root=tmp_path)
assert result.source_root == tmp_path
# Also test that index_dir derives from source_root
assert result.index_dir == tmp_path / ".java-codebase-rag"
class TestEmbeddingModelRelativePath:
"""``embedding.model`` relative paths resolve against a base directory.
Mirrors ``index_dir`` (see ``TestIndexDirRelativeToConfigDir``): a relative
model path in YAML resolves against the config file's directory; a relative
model path from CLI / env resolves against the resolved ``source_root``.
This makes a committed ``.java-codebase-rag.yml`` portable — the model loads
from the same absolute path for the CLI indexer and the MCP reader, instead
of resolving against an unreliable process CWD.
"""
def test_yaml_relative_model_resolves_against_config_dir(self, tmp_path, monkeypatch):
"""``embedding.model: ./models/minilm`` (YAML) -> <config_dir>/models/minilm."""
monkeypatch.delenv("SBERT_MODEL", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_dir = tmp_path / "ctx"
config_dir.mkdir()
(config_dir / YAML_CONFIG_FILENAMES[0]).write_text(
"embedding:\n model: ./models/minilm\n"
)
monkeypatch.chdir(config_dir)
result = resolve_operator_config(source_root=None)
assert result.embedding_model == str((config_dir / "models/minilm").resolve())
assert result.embedding_model_source == "yaml"
def test_yaml_double_dot_model_resolves_against_config_dir(self, tmp_path, monkeypatch):
"""``embedding.model: ../shared/minilm`` (YAML) -> <config_dir>/../shared/minilm."""
monkeypatch.delenv("SBERT_MODEL", raising=False)
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_dir = tmp_path / "ctx"
config_dir.mkdir()
(config_dir / YAML_CONFIG_FILENAMES[0]).write_text(
"embedding:\n model: ../shared/minilm\n"
)
monkeypatch.chdir(config_dir)
result = resolve_operator_config(source_root=None)
assert result.embedding_model == str((tmp_path / "shared/minilm").resolve())
def test_env_relative_model_resolves_against_source_root(self, tmp_path, monkeypatch):
"""``SBERT_MODEL=./models/minilm`` (env) -> <source_root>/models/minilm.
Config sets ``source_root: ../`` so source_root (tmp_path) differs from
config_dir (tmp_path/ctx); the env-sourced model must anchor on
source_root, not config_dir — matching ``index_dir``'s env base.
"""
monkeypatch.delenv("JAVA_CODEBASE_RAG_SOURCE_ROOT", raising=False)
config_dir = tmp_path / "ctx"
config_dir.mkdir()
(config_dir / YAML_CONFIG_FILENAMES[0]).write_text("source_root: ../\n")
monkeypatch.chdir(config_dir)
monkeypatch.setenv("SBERT_MODEL", "./models/minilm")
result = resolve_operator_config(source_root=None)
assert result.source_root == tmp_path
assert result.embedding_model == str((tmp_path / "models/minilm").resolve())
assert result.embedding_model_source == "env"
def test_cli_relative_model_resolves_against_source_root(self, tmp_path, monkeypatch):
"""``--embedding-model ./models/minilm`` (CLI) -> <source_root>/models/minilm."""
monkeypatch.delenv("SBERT_MODEL", raising=False)
result = resolve_operator_config(
source_root=tmp_path, cli_embedding_model="./models/minilm"
)
assert result.embedding_model == str((tmp_path / "models/minilm").resolve())
assert result.embedding_model_source == "cli"
class TestMaybeExpandEmbeddingModelPath:
"""Unit tests pinning the expansion/resolution helper's contract."""
def test_no_base_leaves_relative_unchanged(self):
"""Without a base dir, relative paths are NOT resolved.
``resolved_sbert_model_for_process_env`` (the MCP runtime read of
``SBERT_MODEL``) calls this with no base; it must stay a no-op for
relative values so MCP behavior is unchanged there. The main resolution
path supplies a base, so the absolute path it produces is what reaches
the lazy loader in practice.
"""
from java_codebase_rag.config import maybe_expand_embedding_model_path
assert maybe_expand_embedding_model_path("./models/minilm") == "./models/minilm"
assert maybe_expand_embedding_model_path("../shared/minilm") == "../shared/minilm"
def test_hub_id_passthrough(self):
from java_codebase_rag.config import maybe_expand_embedding_model_path
assert maybe_expand_embedding_model_path("org/name") == "org/name"
assert (
maybe_expand_embedding_model_path("sentence-transformers/all-MiniLM-L6-v2")
== "sentence-transformers/all-MiniLM-L6-v2"
)
def test_absolute_passthrough(self):
from java_codebase_rag.config import maybe_expand_embedding_model_path
assert maybe_expand_embedding_model_path("/opt/models/minilm") == "/opt/models/minilm"
def test_env_var_expansion_preserved(self, monkeypatch):
from java_codebase_rag.config import maybe_expand_embedding_model_path
monkeypatch.setenv("MODEL_DIR", "/opt/models")
assert maybe_expand_embedding_model_path("${MODEL_DIR}/minilm") == "/opt/models/minilm"
assert maybe_expand_embedding_model_path("$MODEL_DIR/minilm") == "/opt/models/minilm"
def test_tilde_expansion_preserved(self, monkeypatch):
from java_codebase_rag.config import maybe_expand_embedding_model_path
monkeypatch.setenv("HOME", "/home/user")
assert maybe_expand_embedding_model_path("~/models/minilm") == "/home/user/models/minilm"
def test_yaml_base_resolves_relative(self, tmp_path):
from java_codebase_rag.config import maybe_expand_embedding_model_path
out = maybe_expand_embedding_model_path(
"./models/minilm", config_dir=tmp_path, source="yaml"
)
assert out == str((tmp_path / "models/minilm").resolve())
def test_cli_env_base_is_source_root(self, tmp_path):
from java_codebase_rag.config import maybe_expand_embedding_model_path
for src in ("cli", "env"):
out = maybe_expand_embedding_model_path(
"./models/minilm", source_root=tmp_path, source=src
)
assert out == str((tmp_path / "models/minilm").resolve())
def test_absolute_after_env_var_not_rebased(self, tmp_path, monkeypatch):
"""An env var that already yields an absolute path is left absolute.
Guards the ``${HUB_ID}`` edge: only ``./`` / ``../``-prefixed results are
re-based, so a var holding ``org/name`` or an absolute path is untouched.
"""
from java_codebase_rag.config import maybe_expand_embedding_model_path
monkeypatch.setenv("MODEL_DIR", "/opt/models")
out = maybe_expand_embedding_model_path(
"${MODEL_DIR}/minilm", config_dir=tmp_path, source="yaml"
)
assert out == "/opt/models/minilm"
def test_cocoindex_subprocess_env_defaults_uses_real_inflight_env_var() -> None:
"""The throttle must use CocoIndex's REAL env var name.
The earlier #293 "fix" set ``COCOINDEX_SOURCE_MAX_INFLIGHT_ROWS``, an env
var CocoIndex never reads (it reads ``COCOINDEX_MAX_INFLIGHT_COMPONENTS``,
default 1024), so it was a no-op and the EMFILE error recurred (#306).
"""
from java_codebase_rag.config import cocoindex_subprocess_env_defaults
defaults = cocoindex_subprocess_env_defaults()
assert defaults["COCOINDEX_MAX_INFLIGHT_COMPONENTS"] == "256"
# The bogus name from the broken #293 fix must NOT leak back in.
assert "COCOINDEX_SOURCE_MAX_INFLIGHT_ROWS" not in defaults