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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
use super::*;
use crate::client::RagClient;
use tempfile::TempDir;
use tokio_util::sync::CancellationToken;

#[tokio::test]
async fn test_new_creates_server() {
    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;
    assert!(client.is_ok(), "Client creation should succeed");

    let client = client.unwrap();
    assert_eq!(client.embedding_dimension(), 384);

    let client = RagMcpServer::with_client(Arc::new(client));
    assert!(client.is_ok(), "Server creation should succeed");
}

#[tokio::test]
async fn test_get_info() {
    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();
    let client = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let info = client.get_info();

    assert_eq!(info.server_info.name, "project");
    assert!(info.server_info.title.is_some());
    assert!(info.instructions.is_some());
    assert!(info.capabilities.tools.is_some());
    assert!(info.capabilities.prompts.is_some());
}

#[test]
fn test_normalize_path_valid() {
    let temp_dir = TempDir::new().unwrap();
    let path = temp_dir.path().to_string_lossy().to_string();

    let normalized = RagClient::normalize_path(&path);
    assert!(normalized.is_ok());

    let normalized_path = normalized.unwrap();
    assert!(!normalized_path.is_empty());
}

#[test]
fn test_normalize_path_nonexistent() {
    let result = RagClient::normalize_path("/nonexistent/path/12345");
    assert!(result.is_err());
}

#[test]
fn test_normalize_path_current_dir() {
    let result = RagClient::normalize_path(".");
    assert!(result.is_ok());
    let normalized = result.unwrap();
    assert!(!normalized.is_empty());
}

#[tokio::test]
async fn test_do_index_empty_directory() {
    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();

    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();

    let result = crate::client::indexing::do_index(
        &client,
        data_dir.to_string_lossy().to_string(),
        None,
        vec![],
        vec![],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.mode, IndexingMode::Full);
    assert_eq!(response.files_indexed, 0);
    assert!(!response.errors.is_empty());
}

#[tokio::test]
async fn test_do_index_with_files() {
    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();

    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();

    // Create a test file
    let test_file = data_dir.join("test.rs");
    std::fs::write(&test_file, "fn main() { println!(\"test\"); }").unwrap();

    let result = crate::client::indexing::do_index(
        &client,
        data_dir.to_string_lossy().to_string(),
        Some("test-project".to_string()),
        vec![],
        vec![],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.mode, IndexingMode::Full);
    assert_eq!(response.files_indexed, 1);
    assert!(response.chunks_created > 0);
    assert!(response.embeddings_generated > 0);
}

#[tokio::test]
async fn test_do_index_with_exclude_patterns() {
    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();

    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();

    // Create test files
    std::fs::write(data_dir.join("include.rs"), "fn test() {}").unwrap();
    std::fs::write(data_dir.join("exclude.txt"), "exclude this").unwrap();

    let result = crate::client::indexing::do_index(
        &client,
        data_dir.to_string_lossy().to_string(),
        None,
        vec![],
        vec!["**/*.txt".to_string()],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await;

    assert!(result.is_ok());
    let response = result.unwrap();
    // The exclude pattern should filter out .txt files
    // Note: Both files might still be indexed if the pattern doesn't match,
    // but at least we verify the indexing works
    assert!(response.files_indexed >= 1);
}

#[tokio::test]
async fn test_do_incremental_update_no_cache() {
    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();

    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();

    // Create a test file
    std::fs::write(data_dir.join("test.rs"), "fn main() {}").unwrap();

    let result = crate::client::indexing::do_incremental_update(
        &client,
        data_dir.to_string_lossy().to_string(),
        None,
        vec![],
        vec![],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.mode, IndexingMode::Incremental);
}

#[tokio::test]
async fn test_do_index_smart_new_codebase() {
    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();

    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();

    std::fs::write(data_dir.join("test.rs"), "fn main() {}").unwrap();

    let result = crate::client::indexing::do_index_smart(
        &client,
        data_dir.to_string_lossy().to_string(),
        None,
        vec![],
        vec![],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await;

    assert!(result.is_ok());
    let response = result.unwrap();
    // First time should be Full
    assert_eq!(response.mode, IndexingMode::Full);
}

#[tokio::test]
async fn test_server_cloneable() {
    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();

    let _cloned = client.clone();
    // Should compile and run without errors
}

// ===== Tool Handler Tests =====

#[tokio::test]
async fn test_tool_query_codebase_with_empty_index() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let req = QueryRequest {
        query: "test query".to_string(),
        path: None,
        project: None,
        limit: 10,
        min_score: 0.7,
        hybrid: true,
    };

    // This should succeed even with empty index (just return no results)
    let result = server.client().query_codebase(req).await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.results.len(), 0);
}

#[tokio::test]
async fn test_tool_query_codebase_validation_failure() {
    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();
    let _server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    // Empty query should fail validation
    let req = QueryRequest {
        query: "   ".to_string(), // Whitespace only
        path: None,
        project: None,
        limit: 10,
        min_score: 0.7,
        hybrid: true,
    };

    let result = req.validate();
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("cannot be empty"));
}

#[tokio::test]
async fn test_tool_get_statistics_empty_index() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let result = server.client().get_statistics().await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.total_files, 0);
    assert_eq!(response.total_chunks, 0);
    assert_eq!(response.total_embeddings, 0);
}

#[tokio::test]
async fn test_tool_get_statistics_with_data() {
    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();

    // Index some data first
    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();
    std::fs::write(data_dir.join("test.rs"), "fn main() {}").unwrap();

    let _index_result = crate::client::indexing::do_index(
        &client,
        data_dir.to_string_lossy().to_string(),
        None,
        vec![],
        vec![],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await
    .unwrap();

    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();
    let result = server.client().get_statistics().await;

    assert!(result.is_ok());
    let response = result.unwrap();
    assert!(response.total_files > 0);
    assert!(response.total_chunks > 0);
    assert!(response.total_embeddings > 0);
}

#[tokio::test]
async fn test_tool_clear_index() {
    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();

    // Index some data first
    let data_dir = temp_dir.path().join("data");
    std::fs::create_dir(&data_dir).unwrap();
    std::fs::write(data_dir.join("test.rs"), "fn main() {}").unwrap();

    let _index_result = crate::client::indexing::do_index(
        &client,
        data_dir.to_string_lossy().to_string(),
        None,
        vec![],
        vec![],
        1024 * 1024,
        None,
        None,
        CancellationToken::new(),
    )
    .await
    .unwrap();

    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    // Clear the index
    let result = server.client().clear_index().await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert!(response.success);

    // Verify index is empty
    let stats = server.client().get_statistics().await.unwrap();
    assert_eq!(stats.total_files, 0);
    assert_eq!(stats.total_chunks, 0);
}

#[tokio::test]
async fn test_tool_search_by_filters_validation_failure() {
    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();
    let _server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    // Empty file extension should fail validation
    let req = AdvancedSearchRequest {
        query: "test".to_string(),
        path: None,
        project: None,
        limit: 10,
        min_score: 0.7,
        file_extensions: vec!["".to_string()],
        languages: vec![],
        path_patterns: vec![],
    };

    let result = req.validate();
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .contains("file extension cannot be empty")
    );
}

#[tokio::test]
async fn test_tool_search_by_filters_valid_request() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let req = AdvancedSearchRequest {
        query: "test".to_string(),
        path: None,
        project: None,
        limit: 10,
        min_score: 0.7,
        file_extensions: vec!["rs".to_string()],
        languages: vec!["Rust".to_string()],
        path_patterns: vec!["src/**".to_string()],
    };

    // Should succeed even with empty index
    let result = server.client().search_with_filters(req).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_tool_search_git_history_validation_failure() {
    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();
    let _server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    // Empty query should fail validation
    let req = SearchGitHistoryRequest {
        query: "  ".to_string(),
        path: ".".to_string(),
        project: None,
        branch: None,
        max_commits: 10,
        limit: 10,
        min_score: 0.7,
        author: None,
        since: None,
        until: None,
        file_pattern: None,
    };

    let result = req.validate();
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("cannot be empty"));
}

#[tokio::test]
async fn test_tool_search_git_history_nonexistent_path() {
    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();
    let _server = RagMcpServer::with_client(Arc::new(client)).unwrap();

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

    let result = req.validate();
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("does not exist"));
}

// ===== Prompt Handler Tests =====

#[tokio::test]
async fn test_prompt_index_with_path() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({
        "path": "/test/path"
    });

    let result = server.index_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let prompt_result = result.unwrap();
    assert!(prompt_result.description.is_some());
    assert!(!prompt_result.messages.is_empty());
    // Verify the message contains the path (using debug format as proxy)
    let debug_str = format!("{:?}", prompt_result.messages[0].content);
    assert!(debug_str.contains("/test/path"));
}

#[tokio::test]
async fn test_prompt_index_default_path() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({});

    let result = server.index_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let prompt_result = result.unwrap();
    assert!(prompt_result.description.is_some());
    assert!(!prompt_result.messages.is_empty());
    // Should default to "."
    let debug_str = format!("{:?}", prompt_result.messages[0].content);
    assert!(debug_str.contains("'.'"));
}

#[tokio::test]
async fn test_prompt_query_with_query() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({
        "query": "test query"
    });

    let result = server.query_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let messages = result.unwrap();
    assert!(!messages.is_empty());
    let debug_str = format!("{:?}", messages[0].content);
    assert!(debug_str.contains("test query"));
}

#[tokio::test]
async fn test_prompt_query_default() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({});

    let result = server.query_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let messages = result.unwrap();
    assert!(!messages.is_empty());
}

#[tokio::test]
async fn test_prompt_stats() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let result = server.stats_prompt().await;
    assert!(!result.is_empty());
    let debug_str = format!("{:?}", result[0].content);
    assert!(debug_str.contains("statistics"));
}

#[tokio::test]
async fn test_prompt_clear() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let result = server.clear_prompt().await;
    assert!(!result.is_empty());
    let debug_str = format!("{:?}", result[0].content);
    assert!(debug_str.contains("clear"));
}

#[tokio::test]
async fn test_prompt_search_with_query() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({
        "query": "advanced search"
    });

    let result = server.search_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let messages = result.unwrap();
    assert!(!messages.is_empty());
    let debug_str = format!("{:?}", messages[0].content);
    assert!(debug_str.contains("advanced search"));
}

#[tokio::test]
async fn test_prompt_git_search_with_query_and_path() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({
        "query": "git search",
        "path": "/repo/path"
    });

    let result = server.git_search_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let messages = result.unwrap();
    assert!(!messages.is_empty());
    let debug_str = format!("{:?}", messages[0].content);
    assert!(debug_str.contains("git search"));
    assert!(debug_str.contains("/repo/path"));
}

#[tokio::test]
async fn test_prompt_git_search_default_path() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let args = serde_json::json!({
        "query": "git search"
    });

    let result = server.git_search_prompt(Parameters(args)).await;
    assert!(result.is_ok());

    let messages = result.unwrap();
    assert!(!messages.is_empty());
    let debug_str = format!("{:?}", messages[0].content);
    assert!(debug_str.contains("git search"));
    assert!(debug_str.contains("'.'"));
}

// ===== ServerHandler Tests =====

#[tokio::test]
async fn test_server_info_completeness() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let info = server.get_info();

    // Verify server info details
    assert_eq!(info.server_info.name, "project");
    assert!(info.server_info.title.is_some());
    assert_eq!(
        info.server_info.title.as_deref().unwrap(),
        "Project RAG - Code Understanding with Semantic Search"
    );
    assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));

    // Verify capabilities
    assert!(info.capabilities.tools.is_some());
    assert!(info.capabilities.prompts.is_some());

    // Verify instructions
    assert!(info.instructions.is_some());
    let instructions = info.instructions.as_deref().unwrap();
    assert!(instructions.contains("RAG-based"));
    assert!(instructions.contains("index_codebase"));
    assert!(instructions.contains("query_codebase"));
    assert!(instructions.contains("search_by_filters"));
}

// ===== Client API Tests =====

#[tokio::test]
async fn test_client_accessor() {
    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();
    let server = RagMcpServer::with_client(Arc::new(client)).unwrap();

    let client_ref = server.client();
    assert_eq!(client_ref.embedding_dimension(), 384);
}