-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_move_chunks.py
More file actions
482 lines (366 loc) · 16.8 KB
/
Copy pathtest_move_chunks.py
File metadata and controls
482 lines (366 loc) · 16.8 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
"""
Tests for P0 LanceDB meta-patching (move_chunks_metadata + apply_file_move).
Covers:
1. move_chunks_metadata: updates file_path in LanceDB WITHOUT re-embedding
2. apply_file_move: coordinates LanceDB + BM25 + SymbolIndex
3. _infer_module_name / _infer_layer: metadata recalculation
4. Edge cases: empty table, same path, non-existent file, Windows paths
"""
from __future__ import annotations
from types import MethodType
from unittest.mock import MagicMock
import pandas as pd
import pytest
# ── Fixtures ──────────────────────────────────────────────────────────────
@pytest.fixture
def mock_indexer():
"""Creates a mock Indexer for apply_file_move delegation tests.
Attributes:
move_chunks_metadata: mocked to return 5
_symbol_index: real SymbolIndex instance
searcher: MagicMock with _reset_bm25
file_guard: MagicMock
table: MagicMock
"""
from src.core.indexing.indexer import Indexer
from src.core.indexing.symbol_index import SymbolIndex
indexer = MagicMock(spec=Indexer)
# Real SymbolIndex for remap testing
indexer._symbol_index = SymbolIndex()
# Cache state
indexer._cached_total_chunks = 10
indexer._cached_unique_files = {"old/file.py"}
# Sub-component mocks
indexer.searcher = MagicMock()
indexer.file_guard = MagicMock()
indexer.table = MagicMock()
# Stub the high-level API so delegation tests can verify the call
indexer.move_chunks_metadata = MagicMock(return_value=5)
return indexer
@pytest.fixture
def indexer_for_test():
"""Indexer mock with real _infer_module_name / _infer_layer bound.
This fixture is used when we want to call the *real*
move_chunks_metadata implementation while mocking the LanceDB table.
"""
from src.core.indexing.indexer import Indexer
indexer = MagicMock(spec=Indexer)
indexer.table = MagicMock()
indexer._cached_total_chunks = 10
indexer._cached_unique_files = {"old/file.py"}
# Bind the real helper methods so that move_chunks_metadata
# recalculates metadata correctly.
indexer._infer_module_name = MethodType(Indexer._infer_module_name, indexer)
indexer._infer_layer = MethodType(Indexer._infer_layer, indexer)
return indexer
@pytest.fixture
def symbol_index():
"""SymbolIndex seeded with test data for remap_file verification."""
from src.core.indexing.symbol_index import SymbolIndex
si = SymbolIndex()
si.add_definitions("old/file.py", [
{"name": "old_func", "line": 1, "kind": "function"},
{"name": "OldClass", "line": 10, "kind": "class"},
])
si.add_references("main.py", [
{"caller": "main", "callee": "old_func", "line": 5, "file": "main.py"},
])
return si
# ── Tests: move_chunks_metadata ───────────────────────────────────────────
class TestMoveChunksMetadata:
"""Tests for Indexer.move_chunks_metadata()."""
def test_returns_zero_for_nonexistent_path(self, indexer_for_test):
"""move_chunks_metadata returns 0 if old path not in table."""
from src.core.indexing.indexer import Indexer
# Empty DataFrame → no chunks found
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = pd.DataFrame()
result = Indexer.move_chunks_metadata(
indexer_for_test, "old/file.py", "new/file.py",
)
assert result == 0
# Should NOT attempt to delete or re-insert
indexer_for_test.table.delete.assert_not_called()
indexer_for_test.table.add.assert_not_called()
def test_updates_file_path_in_lancedb(self, indexer_for_test):
"""After move, chunks are found under new path, not old."""
from src.core.indexing.indexer import Indexer
df = pd.DataFrame({
"file_path": ["old/file.py", "old/file.py"],
"text": ["chunk1", "chunk2"],
"module_name": ["file", "file"],
"layer": ["root", "root"],
"indexed_at": ["2024-01-01", "2024-01-01"],
})
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = df
result = Indexer.move_chunks_metadata(
indexer_for_test, "old/file.py", "new/file.py",
)
assert result == 2
# Old entries deleted
indexer_for_test.table.delete.assert_called_once_with(
"file_path = 'old/file.py'",
)
# Re-inserted records have new file_path
args, _ = indexer_for_test.table.add.call_args
records = args[0]
assert len(records) == 2
for record in records:
assert record["file_path"] == "new/file.py"
def test_updates_module_name(self, indexer_for_test):
"""module_name is recalculated from new path via _infer_module_name."""
from src.core.indexing.indexer import Indexer
df = pd.DataFrame({
"file_path": ["old/file.py"],
"text": ["chunk"],
"module_name": ["old"],
"layer": ["root"],
"indexed_at": ["2024-01-01"],
})
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = df
Indexer.move_chunks_metadata(
indexer_for_test, "old/file.py", "src/core/foo.py",
)
args, _ = indexer_for_test.table.add.call_args
records = args[0]
assert records[0]["module_name"] == "core.foo"
def test_updates_layer(self, indexer_for_test):
"""layer metadata is recalculated from new path via _infer_layer."""
from src.core.indexing.indexer import Indexer
df = pd.DataFrame({
"file_path": ["old/file.py"],
"text": ["chunk"],
"module_name": ["old"],
"layer": ["root"],
"indexed_at": ["2024-01-01"],
})
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = df
Indexer.move_chunks_metadata(
indexer_for_test, "old/file.py", "src/core/foo.py",
)
args, _ = indexer_for_test.table.add.call_args
records = args[0]
assert records[0]["layer"] == "core"
def test_invalidates_cache(self, indexer_for_test):
"""_cached_total_chunks set to None and old path removed after move."""
from src.core.indexing.indexer import Indexer
df = pd.DataFrame({
"file_path": ["old/file.py"],
"text": ["chunk"],
"module_name": ["old"],
"layer": ["root"],
"indexed_at": ["2024-01-01"],
})
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = df
Indexer.move_chunks_metadata(
indexer_for_test, "old/file.py", "new/file.py",
)
assert indexer_for_test._cached_total_chunks is None
assert "old/file.py" not in indexer_for_test._cached_unique_files
def test_handles_same_path(self, indexer_for_test):
"""Same old and new path → returns 0 (no-op)."""
from src.core.indexing.indexer import Indexer
result = Indexer.move_chunks_metadata(
indexer_for_test, "file.py", "file.py",
)
assert result == 0
# No database interaction should occur
indexer_for_test.table.delete.assert_not_called()
indexer_for_test.table.add.assert_not_called()
def test_handles_windows_backslashes(self, indexer_for_test):
"""Backslashes normalized to forward slashes before SQL filter."""
from src.core.indexing.indexer import Indexer
df = pd.DataFrame({
"file_path": ["old/file.py"],
"text": ["chunk"],
"module_name": ["old"],
"layer": ["root"],
"indexed_at": ["2024-01-01"],
})
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = df
Indexer.move_chunks_metadata(
indexer_for_test, "old\\file.py", "new\\file.py",
)
# WHERE clause must use forward slashes
where_call = indexer_for_test.table.search.return_value.where
where_call.assert_called_once()
where_arg = where_call.call_args[0][0]
assert "old/file.py" in where_arg
assert "old\\file.py" not in where_arg
# DELETE must also use forward slashes
delete_call = indexer_for_test.table.delete
delete_call.assert_called_once()
delete_arg = delete_call.call_args[0][0]
assert "old/file.py" in delete_arg
assert "old\\file.py" not in delete_arg
def test_handles_single_quotes_in_path(self, indexer_for_test):
"""Single quotes in path are properly escaped for SQL safety."""
from src.core.indexing.indexer import Indexer
df = pd.DataFrame({
"file_path": ["john's/file.py"],
"text": ["chunk"],
"module_name": ["file"],
"layer": ["root"],
"indexed_at": ["2024-01-01"],
})
indexer_for_test.table.search.return_value \
.where.return_value \
.limit.return_value \
.to_pandas.return_value = df
Indexer.move_chunks_metadata(
indexer_for_test, "john's/file.py", "new/file.py",
)
# WHERE clause should double the single quote
where_call = indexer_for_test.table.search.return_value.where
where_call.assert_called_once()
where_arg = where_call.call_args[0][0]
assert "john''s" in where_arg
assert "john's" not in where_arg
# DELETE clause also escaped
delete_call = indexer_for_test.table.delete
delete_call.assert_called_once()
delete_arg = delete_call.call_args[0][0]
assert "john''s" in delete_arg
# ── Tests: apply_file_move ────────────────────────────────────────────────
class TestApplyFileMove:
"""Tests for Indexer.apply_file_move()."""
def test_calls_move_chunks_metadata(self, mock_indexer):
"""apply_file_move delegates to move_chunks_metadata."""
from src.core.indexing.indexer import Indexer
result = Indexer.apply_file_move(mock_indexer, "old.py", "new.py")
mock_indexer.move_chunks_metadata.assert_called_once_with(
"old.py", "new.py",
)
assert result["status"] == "ok"
assert result["chunks_moved"] == 5
def test_calls_symbol_index_remap(self, mock_indexer):
"""apply_file_move calls SymbolIndex.remap_file and moves defs."""
from src.core.indexing.indexer import Indexer
# Seed a definition so we can observe the remap
si = mock_indexer._symbol_index
si.add_definitions("old.py", [
{"name": "old_func", "line": 1, "kind": "function"},
])
Indexer.apply_file_move(mock_indexer, "old.py", "new.py")
# The definition should now point to the new file
defs = si.find_definitions("old_func")
assert len(defs) > 0
for d in defs:
assert d.file_path.endswith("/new.py")
def test_invalidates_bm25(self, mock_indexer):
"""apply_file_move resets BM25 cache via searcher._reset_bm25."""
from src.core.indexing.indexer import Indexer
Indexer.apply_file_move(mock_indexer, "old.py", "new.py")
mock_indexer.searcher._reset_bm25.assert_called_once()
def test_returns_chunk_count(self, mock_indexer):
"""Result dict includes chunks_moved and symbol_updates."""
from src.core.indexing.indexer import Indexer
result = Indexer.apply_file_move(mock_indexer, "old.py", "new.py")
assert result["chunks_moved"] == 5
assert result["symbol_updates"] == 0 # empty SymbolIndex → 0
assert result["bm25"] == "invalidated"
def test_handles_missing_searcher(self, mock_indexer):
"""apply_file_move works gracefully when searcher is None."""
mock_indexer.searcher = None
from src.core.indexing.indexer import Indexer
result = Indexer.apply_file_move(mock_indexer, "old.py", "new.py")
assert result["status"] == "ok"
assert result["chunks_moved"] == 5
def test_handles_missing_file_guard_notify(self, mock_indexer):
"""apply_file_move works when file_guard lacks notify_file_renamed."""
# Remove the method so hasattr returns False
del mock_indexer.file_guard.notify_file_renamed
from src.core.indexing.indexer import Indexer
result = Indexer.apply_file_move(mock_indexer, "old.py", "new.py")
assert result["status"] == "ok"
assert result["chunks_moved"] == 5
# ── Tests: _infer_module_name / _infer_layer ──────────────────────────────
class TestInferMetadata:
"""Tests for _infer_module_name and _infer_layer (no self access needed)."""
def test_module_name_src_core(self):
"""src/core/foo.py → 'core.foo'"""
from src.core.indexing.indexer import Indexer
name = Indexer._infer_module_name(None, "src/core/foo.py")
assert name == "core.foo"
def test_module_name_mcp_tools(self):
"""src/mcp/tools/bar.py → 'mcp.tools.bar'"""
from src.core.indexing.indexer import Indexer
name = Indexer._infer_module_name(None, "src/mcp/tools/bar.py")
assert name == "mcp.tools.bar"
def test_module_name_root_level(self):
"""README.md → 'README' (single component, no package prefix)."""
from src.core.indexing.indexer import Indexer
name = Indexer._infer_module_name(None, "README.md")
assert name == "README"
def test_module_name_app_prefix(self):
"""app/models/user.py → 'models.user' (app is a skip_dir)."""
from src.core.indexing.indexer import Indexer
name = Indexer._infer_module_name(None, "app/models/user.py")
assert name == "models.user"
def test_module_name_lib_prefix(self):
"""lib/utils/helpers.py → 'utils.helpers' """
from src.core.indexing.indexer import Indexer
name = Indexer._infer_module_name(None, "lib/utils/helpers.py")
assert name == "utils.helpers"
def test_module_name_without_skip_dir(self):
"""core/indexer.py → 'indexer' (no skip_dir match → first dir skipped)."""
from src.core.indexing.indexer import Indexer
name = Indexer._infer_module_name(None, "core/indexer.py")
assert name == "indexer"
def test_layer_core(self):
"""src/core/foo.py → 'core'"""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "src/core/foo.py")
assert layer == "core"
def test_layer_mcp_tools(self):
"""src/mcp/tools/bar.py → 'mcp_tools'"""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "src/mcp/tools/bar.py")
assert layer == "mcp_tools"
def test_layer_mcp_no_tools(self):
"""src/mcp/handler.py → 'mcp' (tools not in path)."""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "src/mcp/handler.py")
assert layer == "mcp"
def test_layer_tests(self):
"""tests/test_foo.py → 'tests'"""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "tests/test_foo.py")
assert layer == "tests"
def test_layer_utils(self):
"""src/utils/helpers.py → 'utils'"""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "src/utils/helpers.py")
assert layer == "utils"
def test_layer_docs(self):
"""docs/index.md → 'docs'"""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "docs/index.md")
assert layer == "docs"
def test_layer_root(self):
"""README.md → 'root' (no known layer detected)."""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "README.md")
assert layer == "root"
def test_layer_windows_backslashes(self):
"""Windows-style paths are normalised before layer detection."""
from src.core.indexing.indexer import Indexer
layer = Indexer._infer_layer(None, "src\\core\\foo.py")
assert layer == "core"