project-rag 0.1.0

RAG-based codebase indexing and semantic search - dual purpose library and MCP server
Documentation
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
//! Tests for git history searching

use super::*;
use crate::client::RagClient;
use tempfile::TempDir;

// Helper to create test client
async fn create_test_client() -> (RagClient, TempDir) {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("db").to_string_lossy().to_string();
    let cache_path = temp_dir.path().join("cache.json");
    let client = RagClient::new_with_db_path(&db_path, cache_path)
        .await
        .unwrap();
    (client, temp_dir)
}

#[test]
fn test_parse_date_filter_unix_timestamp() {
    let result = parse_date_filter("1704067200").unwrap();
    assert_eq!(result, 1704067200);
}

#[test]
fn test_parse_date_filter_iso8601() {
    let result = parse_date_filter("2024-01-01T00:00:00Z").unwrap();
    assert_eq!(result, 1704067200);
}

#[test]
fn test_parse_date_filter_invalid() {
    let result = parse_date_filter("invalid");
    assert!(result.is_err());
}

#[test]
fn test_parse_author_line() {
    let (name, email) = parse_author_line("Author: John Doe <john@example.com>");
    assert_eq!(name, "John Doe");
    assert_eq!(email, "john@example.com");
}

#[test]
fn test_parse_author_line_no_email() {
    let (name, email) = parse_author_line("Author: John Doe");
    assert_eq!(name, "John Doe");
    assert_eq!(email, "");
}

#[tokio::test]
async fn test_search_git_history_first_time() {
    // First search should index commits
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "test coverage".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let result = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await;

    assert!(result.is_ok(), "Git history search should succeed");
    let response = result.unwrap();

    // Should have indexed commits
    assert!(
        response.commits_indexed > 0,
        "Should have indexed commits on first search"
    );
    assert_eq!(
        response.total_cached_commits, response.commits_indexed,
        "Total cached should match indexed on first search"
    );
}

#[tokio::test]
async fn test_search_git_history_second_time_uses_cache() {
    // Second search should use cache and not re-index
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "indexing".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    // First search
    let response1 = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req.clone(),
    )
    .await
    .unwrap();

    let first_indexed = response1.commits_indexed;
    assert!(first_indexed > 0, "First search should index commits");

    // Second search with same parameters
    let response2 = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await
    .unwrap();

    // Should use cache, not re-index
    assert_eq!(
        response2.commits_indexed, 0,
        "Second search should not re-index (use cache)"
    );
    assert_eq!(
        response2.total_cached_commits, first_indexed,
        "Cache should have commits from first search"
    );
}

#[tokio::test]
async fn test_search_git_history_with_author_filter() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "commit".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: Some(".*".to_string()), // Match all authors (regex)
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let result = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await;

    assert!(result.is_ok(), "Search with author filter should succeed");
}

#[tokio::test]
async fn test_search_git_history_with_file_pattern_filter() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "rust".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: Some(".*\\.rs$".to_string()), // Match .rs files
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let result = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await;

    assert!(
        result.is_ok(),
        "Search with file_pattern filter should succeed"
    );
}

#[tokio::test]
async fn test_search_git_history_with_date_filters() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    // Use a date range that should include recent commits
    let req = SearchGitHistoryRequest {
        query: "update".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: Some("2024-01-01T00:00:00Z".to_string()),
        until: Some("2025-12-31T23:59:59Z".to_string()),
        author: None,
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let result = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await;

    assert!(result.is_ok(), "Search with date filters should succeed");
}

#[tokio::test]
async fn test_search_git_history_with_project_isolation() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "feature".to_string(),
        path: ".".to_string(),
        project: Some("test-project".to_string()),
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 3,
        limit: 5,
        min_score: 0.0,
    };

    let result = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await;

    assert!(
        result.is_ok(),
        "Search with project isolation should succeed"
    );
}

#[tokio::test]
async fn test_search_git_history_incremental_indexing() {
    // Test that requesting more commits triggers incremental indexing
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    // First search with max_commits=2
    let req1 = SearchGitHistoryRequest {
        query: "test".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 2,
        limit: 10,
        min_score: 0.0,
    };

    let response1 = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req1,
    )
    .await
    .unwrap();

    let first_cached = response1.total_cached_commits;
    assert!(first_cached <= 2, "Should cache at most 2 commits");

    // Second search with max_commits=5 (more than cached)
    let req2 = SearchGitHistoryRequest {
        query: "test".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let response2 = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req2,
    )
    .await
    .unwrap();

    // Should have indexed more commits
    assert!(
        response2.commits_indexed > 0,
        "Should index additional commits when max_commits increases"
    );
    assert!(
        response2.total_cached_commits > first_cached,
        "Total cached should increase"
    );
}

#[tokio::test]
async fn test_search_git_history_response_structure() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "refactor".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let response = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await
    .unwrap();

    // Verify response structure
    assert!(response.duration_ms > 0, "Should have non-zero duration");
    assert!(
        response.total_cached_commits > 0,
        "Should have cached commits"
    );

    // Verify result structure if any results found
    for result in &response.results {
        assert!(!result.commit_hash.is_empty(), "Hash should not be empty");
        assert!(result.score >= 0.0, "Score should be non-negative");
    }
}

#[tokio::test]
async fn test_search_git_history_invalid_path() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "test".to_string(),
        path: "/nonexistent/path/to/repo".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 5,
        limit: 10,
        min_score: 0.0,
    };

    let result = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await;

    // Should error for non-existent path
    assert!(result.is_err(), "Should fail for invalid git repository");
}

#[tokio::test]
async fn test_search_git_history_limit_respected() {
    let (client, temp_dir) = create_test_client().await;
    let cache_path = temp_dir.path().join("git_cache.json");

    let req = SearchGitHistoryRequest {
        query: "commit".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        since: None,
        until: None,
        author: None,
        file_pattern: None,
        max_commits: 10,
        limit: 3, // Limit to 3 results
        min_score: 0.0,
    };

    let response = do_search_git_history(
        client.embedding_provider.clone(),
        client.vector_db.clone(),
        client.git_cache.clone(),
        &cache_path,
        req,
    )
    .await
    .unwrap();

    // Results should not exceed limit
    assert!(
        response.results.len() <= 3,
        "Results should respect limit parameter"
    );
}