-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_e2e.py
More file actions
460 lines (352 loc) · 16.4 KB
/
test_e2e.py
File metadata and controls
460 lines (352 loc) · 16.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
458
459
460
"""End-to-end tests exercising the full CLI → daemon → index → search flow.
Each test function represents a complete session: a series of CLI commands
executed in order, verifying compound stateful effects. Tests use a real
daemon subprocess (via COCOINDEX_CODE_DIR env var) and the actual CLI
commands through typer's CliRunner.
"""
from __future__ import annotations
import os
import tempfile
from collections.abc import Iterator
from pathlib import Path
import pytest
from cocoindex.connectors import sqlite as coco_sqlite
from typer.testing import CliRunner
from cocoindex_code.cli import app
from cocoindex_code.client import stop_daemon
from cocoindex_code.settings import find_parent_with_marker
runner = CliRunner()
SAMPLE_MAIN_PY = '''\
"""Main application entry point."""
def calculate_fibonacci(n: int) -> int:
"""Calculate the nth Fibonacci number recursively."""
if n <= 1:
return n
return calculate_fibonacci(n - 1) + calculate_fibonacci(n - 2)
def greet_user(name: str) -> str:
"""Return a personalized greeting message."""
return f"Hello, {name}! Welcome to the application."
if __name__ == "__main__":
print(greet_user("World"))
print(calculate_fibonacci(10))
'''
SAMPLE_UTILS_PY = '''\
"""Utility functions for data processing."""
def parse_csv_line(line: str) -> list[str]:
"""Parse a CSV line into a list of values."""
return line.strip().split(",")
def format_currency(amount: float) -> str:
"""Format a number as USD currency."""
return f"${amount:,.2f}"
def validate_email(email: str) -> bool:
"""Check if an email address is valid."""
return "@" in email and "." in email
'''
SAMPLE_DATABASE_PY = '''\
"""Database connection and query utilities."""
class DatabaseConnection:
"""Manages database connections."""
def __init__(self, host: str, port: int):
self.host = host
self.port = port
self._connected = False
def connect(self) -> None:
"""Establish connection to the database."""
self._connected = True
def execute_query(self, sql: str) -> list[dict]:
"""Execute a SQL query and return results."""
if not self._connected:
raise RuntimeError("Not connected to database")
return []
'''
SAMPLE_APP_JS = """\
/** Express web application server. */
const express = require('express');
const app = express();
function handleRequest(req, res) {
const name = req.query.name || 'World';
res.json({ message: `Hello, ${name}!` });
}
module.exports = { handleRequest };
"""
@pytest.fixture()
def e2e_project() -> Iterator[Path]:
"""Set up a temp project dir with sample files.
Cleans up with ``ccc reset --all -f`` and daemon stop.
"""
base_dir = Path(tempfile.mkdtemp(prefix="ccc_e2e_"))
project_dir = base_dir / "project"
project_dir.mkdir()
(project_dir / "main.py").write_text(SAMPLE_MAIN_PY)
(project_dir / "utils.py").write_text(SAMPLE_UTILS_PY)
lib_dir = project_dir / "lib"
lib_dir.mkdir()
(lib_dir / "database.py").write_text(SAMPLE_DATABASE_PY)
(project_dir / ".git").mkdir()
old_env = os.environ.get("COCOINDEX_CODE_DIR")
os.environ["COCOINDEX_CODE_DIR"] = str(base_dir)
old_cwd = os.getcwd()
os.chdir(project_dir)
try:
yield project_dir
finally:
os.chdir(project_dir)
runner.invoke(app, ["reset", "--all", "-f"])
stop_daemon()
os.chdir(old_cwd)
if old_env is None:
os.environ.pop("COCOINDEX_CODE_DIR", None)
else:
os.environ["COCOINDEX_CODE_DIR"] = old_env
# ---------------------------------------------------------------------------
# Session tests — each function is a complete scenario
# ---------------------------------------------------------------------------
def test_session_happy_path(e2e_project: Path) -> None:
"""Init → init (idempotent) → index → status → search variants → daemon status."""
# Init
result = runner.invoke(app, ["init"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert (e2e_project / ".cocoindex_code" / "settings.yml").exists()
assert "Created project settings" in result.output or "settings" in result.output
# Init again — already initialized
result = runner.invoke(app, ["init"], catch_exceptions=False)
assert result.exit_code == 0
assert "already initialized" in result.output
# Index
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "Chunks:" in result.output
assert "Files:" in result.output
# Status
result = runner.invoke(app, ["status"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "Chunks:" in result.output
# Search — fibonacci
result = runner.invoke(app, ["search", "fibonacci", "calculation"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "main.py" in result.output
# Search — database
result = runner.invoke(app, ["search", "database", "connection"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "database.py" in result.output
# Search — --lang filter
result = runner.invoke(app, ["search", "function", "--lang", "python"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "python" in result.output.lower()
# Search — --path filter
result = runner.invoke(app, ["search", "function", "--path", "lib/*"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "lib/" in result.output
# Search — no results
result = runner.invoke(
app, ["search", "xyzzy_nonexistent_symbol_12345"], catch_exceptions=False
)
assert result.exit_code == 0
# Daemon status
result = runner.invoke(app, ["daemon", "status"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "Daemon version:" in result.output
def test_session_incremental_index(e2e_project: Path) -> None:
"""Init → index → add new file → re-index → search finds new content."""
runner.invoke(app, ["init"], catch_exceptions=False)
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
# Add a new file
(e2e_project / "app.js").write_text(SAMPLE_APP_JS)
# Re-index
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
# Search should find the new file
result = runner.invoke(app, ["search", "handleRequest"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "app.js" in result.output
def test_session_reset_databases(e2e_project: Path) -> None:
"""Init → index → search → reset (dbs only) → re-index → search works again."""
runner.invoke(app, ["init"], catch_exceptions=False)
runner.invoke(app, ["index"], catch_exceptions=False)
# Search works before reset
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0
assert "main.py" in result.output
# Reset databases only
result = runner.invoke(app, ["reset", "-f"], catch_exceptions=False)
assert result.exit_code == 0
assert "Databases deleted" in result.output
# Settings should still exist
assert (e2e_project / ".cocoindex_code" / "settings.yml").exists()
# DB files should be gone
assert not (e2e_project / ".cocoindex_code" / "cocoindex.db").exists()
assert not (e2e_project / ".cocoindex_code" / "target_sqlite.db").exists()
# Restart daemon to fully release LMDB handles.
# On free-threaded Python (3.14t), deferred refcounting in the daemon
# process prevents the Rust LMDB environment from being freed promptly
# after remove_project; restarting is the reliable way to ensure cleanup.
runner.invoke(app, ["daemon", "restart"], catch_exceptions=False)
# Re-index — project is still initialized, just databases gone
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
# Search works again
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0
assert "main.py" in result.output
def test_session_reset_all(e2e_project: Path) -> None:
"""Init → index → reset --all → verify full cleanup → search errors."""
runner.invoke(app, ["init"], catch_exceptions=False)
runner.invoke(app, ["index"], catch_exceptions=False)
# .gitignore should have the entry (project has .git dir)
gitignore = e2e_project / ".gitignore"
assert gitignore.is_file()
assert "/.cocoindex_code/" in gitignore.read_text()
# Reset --all
result = runner.invoke(app, ["reset", "--all", "-f"], catch_exceptions=False)
assert result.exit_code == 0
assert "fully reset" in result.output
# Settings should be gone
assert not (e2e_project / ".cocoindex_code" / "settings.yml").exists()
# .gitignore entry should be removed
assert "/.cocoindex_code/" not in gitignore.read_text()
# Search should fail — not initialized
result = runner.invoke(app, ["search", "fibonacci"])
assert result.exit_code != 0
assert "ccc init" in result.output
def test_session_reset_then_full_reinit(e2e_project: Path) -> None:
"""Init → index → reset --all → re-init → re-index → search works again."""
runner.invoke(app, ["init"], catch_exceptions=False)
runner.invoke(app, ["index"], catch_exceptions=False)
# Reset everything
runner.invoke(app, ["reset", "--all", "-f"], catch_exceptions=False)
# Restart daemon to fully release LMDB handles (see test_session_reset_databases).
runner.invoke(app, ["daemon", "restart"], catch_exceptions=False)
# Re-init from scratch
result = runner.invoke(app, ["init"], catch_exceptions=False)
assert result.exit_code == 0
assert (e2e_project / ".cocoindex_code" / "settings.yml").exists()
# Re-index
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
# Search works again
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0
assert "main.py" in result.output
def test_session_respects_gitignore(e2e_project: Path) -> None:
"""Indexing should skip files ignored by .gitignore while honoring negations."""
gitignore_path = e2e_project / ".gitignore"
gitignore_path.write_text("ignored.py\nignored_dir/\n!important.py\n")
(e2e_project / "ignored.py").write_text("IGNORED_TOKEN = True\n")
ignored_dir = e2e_project / "ignored_dir"
ignored_dir.mkdir()
(ignored_dir / "nested.py").write_text("NESTED_IGNORED = True\n")
(e2e_project / "important.py").write_text("IMPORTANT_TOKEN = True\n")
runner.invoke(app, ["init"], catch_exceptions=False)
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
db_path = e2e_project / ".cocoindex_code" / "target_sqlite.db"
conn = coco_sqlite.connect(str(db_path), load_vec=True)
try:
with conn.readonly() as db:
file_paths = {
row[0] for row in db.execute("SELECT DISTINCT file_path FROM code_chunks_vec")
}
finally:
conn.close()
assert "ignored.py" not in file_paths
assert "ignored_dir/nested.py" not in file_paths
assert "important.py" in file_paths
@pytest.mark.usefixtures("e2e_project")
def test_session_daemon_stop_and_auto_start() -> None:
"""Init → index → daemon stop → index auto-starts daemon → search works."""
runner.invoke(app, ["init"], catch_exceptions=False)
runner.invoke(app, ["index"], catch_exceptions=False)
# Stop daemon
result = runner.invoke(app, ["daemon", "stop"], catch_exceptions=False)
assert result.exit_code == 0
# Index should auto-start daemon via ensure_daemon()
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
# Search should work with the new daemon
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0
assert "main.py" in result.output
@pytest.mark.usefixtures("e2e_project")
def test_session_daemon_restart() -> None:
"""Init → index → daemon restart → re-index → search works."""
runner.invoke(app, ["init"], catch_exceptions=False)
runner.invoke(app, ["index"], catch_exceptions=False)
# Restart daemon
result = runner.invoke(app, ["daemon", "restart"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "restarted" in result.output.lower()
# Re-index in the new daemon
result = runner.invoke(app, ["index"], catch_exceptions=False)
assert result.exit_code == 0, result.output
# Search should work
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0
assert "main.py" in result.output
@pytest.mark.usefixtures("e2e_project")
def test_session_search_refresh() -> None:
"""Init (no explicit index) → search --refresh indexes then searches."""
runner.invoke(app, ["init"], catch_exceptions=False)
# search --refresh without prior explicit index
result = runner.invoke(app, ["search", "--refresh", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "main.py" in result.output
@pytest.mark.usefixtures("e2e_project")
def test_session_index_not_initialized_errors() -> None:
"""Running ``ccc index`` from uninitialized dir should error."""
result = runner.invoke(app, ["index"])
assert result.exit_code != 0
assert "ccc init" in result.output
def test_session_subdirectory_path_default(e2e_project: Path) -> None:
"""Search from a subdirectory defaults path filter to that subdirectory."""
runner.invoke(app, ["init"], catch_exceptions=False)
runner.invoke(app, ["index"], catch_exceptions=False)
# Search from project root — should find main.py
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "main.py" in result.output
# Search from lib/ — default path filter restricts to lib/*
os.chdir(e2e_project / "lib")
result = runner.invoke(app, ["search", "database", "connection"], catch_exceptions=False)
assert result.exit_code == 0, result.output
assert "database.py" in result.output
# From lib/, searching for fibonacci should NOT find main.py (outside lib/)
result = runner.invoke(app, ["search", "fibonacci"], catch_exceptions=False)
assert result.exit_code == 0
assert "main.py" not in result.output
# Back to project root
os.chdir(e2e_project)
def test_session_not_initialized_errors(e2e_project: Path) -> None:
"""Search and status from uninitialized dir should error with guidance."""
standalone = Path(tempfile.mkdtemp(prefix="ccc_standalone_"))
os.chdir(standalone)
result = runner.invoke(app, ["search", "hello"])
assert result.exit_code != 0
assert "ccc init" in result.output
result = runner.invoke(app, ["status"])
assert result.exit_code != 0
assert "ccc init" in result.output
# Return to project dir so fixture cleanup works
os.chdir(e2e_project)
# ---------------------------------------------------------------------------
# Unit tests (not session-based)
# ---------------------------------------------------------------------------
class TestCodebaseRootDiscovery:
"""Tests for find_parent_with_marker helper."""
def test_prefers_cocoindex_code_over_git(self, tmp_path: Path) -> None:
parent = tmp_path / "project"
parent.mkdir()
(parent / ".cocoindex_code").mkdir()
(parent / ".git").mkdir()
subdir = parent / "src" / "lib"
subdir.mkdir(parents=True)
assert find_parent_with_marker(subdir) == parent
def test_finds_git_in_parent_hierarchy(self, tmp_path: Path) -> None:
(tmp_path / ".git").mkdir()
deep_dir = tmp_path / "a" / "b" / "c" / "d" / "e"
deep_dir.mkdir(parents=True)
assert find_parent_with_marker(deep_dir) == tmp_path
def test_falls_back_to_none_when_no_markers(self, tmp_path: Path) -> None:
empty_dir = tmp_path / "standalone"
empty_dir.mkdir()
assert find_parent_with_marker(empty_dir) is None