codesearch 0.1.15

A fast, intelligent CLI tool with multiple search modes (regex, fuzzy, semantic), code analysis, and dead code detection for popular programming languages
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
//! Integration Tests for MCP Symbol Tools
//!
//! Tests the symbol system and MCP symbol tool integration.
//! Run with: cargo test --test mcp_symbol_tests
//! Run MCP tests with: cargo test --test mcp_symbol_tests --features mcp

use std::collections::HashMap;
use std::fs;
use tempfile::tempdir;

use codesearch::symbols::{
    Symbol, SymbolIndex, SymbolKind, SymbolVisibility,
    extractor::{SymbolExtractor, extract_symbols_from_file},
    relationships::RelationshipGraph,
};

// ===== Helper: Create test project with symbols =====

fn create_symbol_test_project() -> tempfile::TempDir {
    let dir = tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Rust file with rich symbols
    fs::write(
        src_dir.join("main.rs"),
        r#"// Main entry point
pub struct Config {
    name: String,
    value: i32,
}

impl Config {
    pub fn new(name: &str) -> Self {
        Config { name: name.to_string(), value: 0 }
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }
}

pub fn process_config(config: &Config) -> String {
    format!("Config: {}", config.get_name())
}

fn main() {
    let config = Config::new("test");
    println!("{}", process_config(&config));
}
"#,
    )
    .unwrap();

    // Python file with classes and functions
    let py_dir = dir.path().join("scripts");
    fs::create_dir_all(&py_dir).unwrap();
    fs::write(
        py_dir.join("processor.py"),
        r#"class DataProcessor:
    def __init__(self, name):
        self.name = name

    def process(self, data):
        return [x * 2 for x in data]

def main():
    processor = DataProcessor("test")
    result = processor.process([1, 2, 3])
    print(result)
"#,
    )
    .unwrap();

    // JavaScript file
    let js_dir = dir.path().join("web");
    fs::create_dir_all(&js_dir).unwrap();
    fs::write(
        js_dir.join("app.js"),
        r#"class ApiHandler {
    constructor(baseUrl) {
        this.baseUrl = baseUrl;
    }

    async get(endpoint) {
        return fetch(`${this.baseUrl}${endpoint}`);
    }
}

function createHandler(url) {
    return new ApiHandler(url);
}
"#,
    )
    .unwrap();

    dir
}

// ===== Symbol Extraction Tests =====

#[test]
fn test_extract_symbols_from_rust() {
    let dir = create_symbol_test_project();
    let file_path = dir.path().join("src/main.rs");

    let symbols = extract_symbols_from_file(&file_path).unwrap();

    assert!(!symbols.is_empty(), "Should extract symbols from Rust file");

    // Should find struct
    let has_struct = symbols.iter().any(|s| s.kind == SymbolKind::Struct);
    assert!(has_struct, "Should find struct definition");

    // Should find functions
    let has_function = symbols.iter().any(|s| s.kind == SymbolKind::Function);
    assert!(has_function, "Should find function definitions");
}

#[test]
fn test_extract_symbols_from_python() {
    let dir = create_symbol_test_project();
    let file_path = dir.path().join("scripts/processor.py");

    let symbols = extract_symbols_from_file(&file_path).unwrap();

    assert!(
        !symbols.is_empty(),
        "Should extract symbols from Python file"
    );

    // Should find class
    let has_class = symbols.iter().any(|s| s.kind == SymbolKind::Class);
    assert!(has_class, "Should find class definition");
}

#[test]
fn test_extract_symbols_from_js() {
    let dir = create_symbol_test_project();
    let file_path = dir.path().join("web/app.js");

    let symbols = extract_symbols_from_file(&file_path).unwrap();

    assert!(!symbols.is_empty(), "Should extract symbols from JS file");

    // Should find class
    let has_class = symbols.iter().any(|s| s.kind == SymbolKind::Class);
    assert!(has_class, "Should find class definition in JS");
}

#[test]
fn test_symbol_metadata() {
    let dir = create_symbol_test_project();
    let file_path = dir.path().join("src/main.rs");

    let symbols = extract_symbols_from_file(&file_path).unwrap();

    for symbol in &symbols {
        assert!(!symbol.id.is_empty(), "Symbol should have an ID");
        assert!(!symbol.name.is_empty(), "Symbol should have a name");
        assert!(
            !symbol.file_path.is_empty(),
            "Symbol should have a file path"
        );
        assert!(symbol.line > 0, "Symbol should have a line number");
    }
}

// ===== Symbol Index Tests =====

#[test]
fn test_symbol_index_basic() {
    let index = SymbolIndex::new();

    let symbol = Symbol {
        id: "test_id".to_string(),
        name: "test_func".to_string(),
        kind: SymbolKind::Function,
        file_path: "src/main.rs".to_string(),
        line: 10,
        column: 0,
        end_line: 15,
        signature: "()".to_string(),
        documentation: None,
        visibility: SymbolVisibility::Public,
        parent: None,
        type_info: None,
        generics: vec![],
        annotations: vec![],
        attributes: vec![],
        metadata: HashMap::new(),
    };

    index.add_symbol(symbol.clone());

    let found = index.get_symbol("test_id");
    assert!(found.is_some(), "Should find symbol by ID");
    assert_eq!(found.unwrap().name, "test_func");
}

#[test]
fn test_symbol_index_find_by_name() {
    let index = SymbolIndex::new();

    let names = vec!["func_a", "func_b", "func_c"];
    for (i, name) in names.iter().enumerate() {
        let symbol = Symbol {
            id: format!("id_{}", i),
            name: name.to_string(),
            kind: SymbolKind::Function,
            file_path: "src/main.rs".to_string(),
            line: i,
            column: 0,
            end_line: i + 5,
            signature: "()".to_string(),
            documentation: None,
            visibility: SymbolVisibility::Public,
            parent: None,
            type_info: None,
            generics: vec![],
            annotations: vec![],
            attributes: vec![],
            metadata: HashMap::new(),
        };
        index.add_symbol(symbol);
    }

    let found = index.find_by_name("func_b");
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].name, "func_b");
}

#[test]
fn test_symbol_index_search_by_kind() {
    let dir = create_symbol_test_project();
    let file_path = dir.path().join("src/main.rs");

    let symbols = extract_symbols_from_file(&file_path).unwrap();
    let index = SymbolIndex::new();

    for symbol in symbols {
        index.add_symbol(symbol);
    }

    let functions = index.get_symbols_by_kind(&SymbolKind::Function);
    let structs = index.get_symbols_by_kind(&SymbolKind::Struct);

    assert!(!functions.is_empty(), "Should find functions");
    assert!(!structs.is_empty(), "Should find structs");
}

#[test]
fn test_symbol_index_get_symbols_in_file() {
    let dir = create_symbol_test_project();
    let index = SymbolIndex::new();

    let rust_path = dir.path().join("src/main.rs");
    let py_path = dir.path().join("scripts/processor.py");

    let rust_symbols = extract_symbols_from_file(&rust_path).unwrap();
    let py_symbols = extract_symbols_from_file(&py_path).unwrap();

    for s in rust_symbols {
        index.add_symbol(s);
    }
    for s in py_symbols {
        index.add_symbol(s);
    }

    let rust_file_symbols = index.get_symbols_in_file(&rust_path.to_string_lossy().to_string());
    let py_file_symbols = index.get_symbols_in_file(&py_path.to_string_lossy().to_string());

    assert!(
        !rust_file_symbols.is_empty(),
        "Should find symbols in Rust file"
    );
    assert!(
        !py_file_symbols.is_empty(),
        "Should find symbols in Python file"
    );
}

#[test]
fn test_symbol_index_stats() {
    let dir = create_symbol_test_project();
    let index = SymbolIndex::new();

    let rust_path = dir.path().join("src/main.rs");
    let symbols = extract_symbols_from_file(&rust_path).unwrap();

    for s in symbols {
        index.add_symbol(s);
    }

    let stats = index.get_stats();
    assert!(stats.total_symbols > 0, "Should have symbols in stats");
    assert!(stats.total_files > 0, "Should have files in stats");
}

// ===== Relationship Graph Tests =====

#[test]
fn test_relationship_graph_basic() {
    let graph = RelationshipGraph::new();

    let sym_a = Symbol {
        id: "sym_a".to_string(),
        name: "function_a".to_string(),
        kind: SymbolKind::Function,
        file_path: "a.rs".to_string(),
        line: 1,
        column: 0,
        end_line: 5,
        signature: "()".to_string(),
        documentation: None,
        visibility: SymbolVisibility::Public,
        parent: None,
        type_info: None,
        generics: vec![],
        annotations: vec![],
        attributes: vec![],
        metadata: HashMap::new(),
    };

    let sym_b = Symbol {
        id: "sym_b".to_string(),
        name: "function_b".to_string(),
        kind: SymbolKind::Function,
        file_path: "b.rs".to_string(),
        line: 10,
        column: 0,
        end_line: 15,
        signature: "()".to_string(),
        documentation: None,
        visibility: SymbolVisibility::Public,
        parent: None,
        type_info: None,
        generics: vec![],
        annotations: vec![],
        attributes: vec![],
        metadata: HashMap::new(),
    };

    graph.add_symbol(sym_a.clone());
    graph.add_symbol(sym_b.clone());

    graph.add_relationship(
        "sym_a",
        "sym_b",
        codesearch::symbols::SymbolRelationType::Calls,
        1.0,
    );

    let relationships = graph.get_relationships("sym_a");
    assert_eq!(relationships.len(), 1);
    assert_eq!(relationships[0].symbol_id, "sym_b");
}

#[test]
fn test_relationship_graph_find_callers() {
    let graph = RelationshipGraph::new();

    for i in 0..5 {
        let symbol = Symbol {
            id: format!("sym_{}", i),
            name: format!("func_{}", i),
            kind: SymbolKind::Function,
            file_path: "test.rs".to_string(),
            line: i,
            column: 0,
            end_line: i + 5,
            signature: "()".to_string(),
            documentation: None,
            visibility: SymbolVisibility::Public,
            parent: None,
            type_info: None,
            generics: vec![],
            annotations: vec![],
            attributes: vec![],
            metadata: HashMap::new(),
        };
        graph.add_symbol(symbol);
    }

    // sym_1 and sym_2 call sym_0
    graph.add_relationship(
        "sym_1",
        "sym_0",
        codesearch::symbols::SymbolRelationType::Calls,
        1.0,
    );
    graph.add_relationship(
        "sym_2",
        "sym_0",
        codesearch::symbols::SymbolRelationType::Calls,
        1.0,
    );

    let callers = graph.find_callers("sym_0");
    assert_eq!(callers.len(), 2, "Should find 2 callers");
}

#[test]
fn test_relationship_graph_hierarchy() {
    let graph = RelationshipGraph::new();

    let parent = Symbol {
        id: "parent".to_string(),
        name: "BaseClass".to_string(),
        kind: SymbolKind::Class,
        file_path: "test.rs".to_string(),
        line: 1,
        column: 0,
        end_line: 10,
        signature: "".to_string(),
        documentation: None,
        visibility: SymbolVisibility::Public,
        parent: None,
        type_info: None,
        generics: vec![],
        annotations: vec![],
        attributes: vec![],
        metadata: HashMap::new(),
    };

    let child = Symbol {
        id: "child".to_string(),
        name: "DerivedClass".to_string(),
        kind: SymbolKind::Class,
        file_path: "test.rs".to_string(),
        line: 20,
        column: 0,
        end_line: 30,
        signature: "".to_string(),
        documentation: None,
        visibility: SymbolVisibility::Public,
        parent: None,
        type_info: None,
        generics: vec![],
        annotations: vec![],
        attributes: vec![],
        metadata: HashMap::new(),
    };

    graph.add_symbol(parent);
    graph.add_symbol(child);

    graph.add_relationship(
        "child",
        "parent",
        codesearch::symbols::SymbolRelationType::Inherits,
        1.0,
    );

    let hierarchy = graph.find_hierarchy("parent");
    assert!(
        hierarchy.len() >= 2,
        "Should find hierarchy with parent and child"
    );
}

// ===== Symbol Index Store Tests =====

#[test]
fn test_symbol_index_store_directory_indexing() {
    let dir = create_symbol_test_project();
    let store_path = dir.path().join(".codesearch").join("symbol_index");

    let store = codesearch::symbols::SymbolIndexStore::new(store_path);
    let count = store
        .index_directory(
            dir.path(),
            Some(&["rs".to_string(), "py".to_string(), "js".to_string()]),
            None,
        )
        .unwrap();

    assert!(count > 0, "Should index symbols from directory");

    let stats = store.index().get_stats();
    assert!(stats.total_symbols > 0, "Should have indexed symbols");
    assert!(stats.total_files > 0, "Should have indexed files");
}

// ===== End-to-End Symbol Workflow Tests =====

#[test]
fn test_symbol_end_to_end_workflow() {
    let dir = create_symbol_test_project();
    let index = SymbolIndex::new();

    // Index all source files
    for entry in walkdir::WalkDir::new(dir.path())
        .into_iter()
        .filter_map(|e| e.ok())
    {
        let path = entry.path();
        if path.is_file() {
            if let Some(ext) = path.extension() {
                if ext == "rs" || ext == "py" || ext == "js" {
                    if let Ok(symbols) = extract_symbols_from_file(path) {
                        for symbol in symbols {
                            index.add_symbol(symbol);
                        }
                    }
                }
            }
        }
    }

    // Search for specific symbols
    let config_symbols = index.search_symbols(Some("Config"), None, None, None);
    assert!(!config_symbols.is_empty(), "Should find Config symbols");

    // Get all functions
    let functions = index.get_symbols_by_kind(&SymbolKind::Function);
    assert!(!functions.is_empty(), "Should find functions");

    // Verify stats
    let stats = index.get_stats();
    assert!(stats.total_symbols > 0);
    assert!(!stats.symbols_by_kind.is_empty());
}

// ===== MCP Tool Parameter Tests =====
#[cfg(feature = "mcp")]
mod mcp_tool_tests {
    use super::*;
    use codesearch::mcp::symbols_tools::*;

    #[test]
    fn test_search_symbols_params_serialization() {
        let params = SearchSymbolsParams {
            pattern: Some("test".to_string()),
            path: Some("/project".to_string()),
            extensions: Some(vec!["rs".to_string()]),
            exclude: Some(vec!["target".to_string()]),
            kind: Some(SymbolKind::Function),
            file_path: None,
            visibility: Some(SymbolVisibility::Public),
            context_lines: Some(3),
            include_related: Some(true),
            limit: Some(10),
        };

        let json = serde_json::to_string(&params).unwrap();
        let deserialized: SearchSymbolsParams = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.pattern, params.pattern);
        assert_eq!(deserialized.extensions, params.extensions);
        assert_eq!(deserialized.limit, params.limit);
    }

    #[test]
    fn test_get_symbol_details_params_serialization() {
        let params = GetSymbolDetailsParams {
            id: Some("sym_123".to_string()),
            name: Some("my_func".to_string()),
            file_path: Some("src/main.rs".to_string()),
        };

        let json = serde_json::to_string(&params).unwrap();
        let deserialized: GetSymbolDetailsParams = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.id, params.id);
        assert_eq!(deserialized.name, params.name);
    }

    #[test]
    fn test_build_symbol_index_params_serialization() {
        let params = BuildSymbolIndexParams {
            path: Some("/project".to_string()),
            extensions: Some(vec!["rs".to_string(), "py".to_string()]),
            exclude: Some(vec!["target".to_string()]),
        };

        let json = serde_json::to_string(&params).unwrap();
        let deserialized: BuildSymbolIndexParams = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.path, params.path);
        assert_eq!(deserialized.extensions, params.extensions);
    }

    #[test]
    fn test_find_symbol_relationships_params_serialization() {
        let params = FindSymbolRelationshipsParams {
            symbol_id: Some("sym_1".to_string()),
            symbol_name: None,
            relation_types: Some(vec![
                codesearch::symbols::SymbolRelationType::Calls,
                codesearch::symbols::SymbolRelationType::Inherits,
            ]),
        };

        let json = serde_json::to_string(&params).unwrap();
        let deserialized: FindSymbolRelationshipsParams = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.symbol_id, params.symbol_id);
        assert!(deserialized.relation_types.is_some());
    }

    #[test]
    fn test_find_symbol_hierarchy_params_serialization() {
        let params = FindSymbolHierarchyParams {
            symbol_id: Some("class_1".to_string()),
            symbol_name: Some("MyClass".to_string()),
        };

        let json = serde_json::to_string(&params).unwrap();
        let deserialized: FindSymbolHierarchyParams = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.symbol_id, params.symbol_id);
        assert_eq!(deserialized.symbol_name, params.symbol_name);
    }

    #[tokio::test]
    async fn test_build_symbol_index_tool() {
        let dir = super::create_symbol_test_project();

        let params = BuildSymbolIndexParams {
            path: Some(dir.path().to_string_lossy().to_string()),
            extensions: Some(vec!["rs".to_string()]),
            exclude: None,
        };

        let result =
            build_symbol_index_tool(rmcp::handler::server::wrapper::Parameters(params)).await;
        let value = result.0;

        assert!(value.get("success").unwrap().as_bool().unwrap());
        assert!(value.get("indexed_symbols").unwrap().as_u64().unwrap() > 0);
    }

    #[tokio::test]
    async fn test_get_index_stats_tool() {
        let dir = super::create_symbol_test_project();

        // First build the index
        let build_params = BuildSymbolIndexParams {
            path: Some(dir.path().to_string_lossy().to_string()),
            extensions: Some(vec!["rs".to_string(), "py".to_string()]),
            exclude: None,
        };
        let _ =
            build_symbol_index_tool(rmcp::handler::server::wrapper::Parameters(build_params)).await;

        // Then get stats
        let stats_params = GetIndexStatsParams {};
        let result =
            get_index_stats_tool(rmcp::handler::server::wrapper::Parameters(stats_params)).await;
        let value = result.0;

        assert!(value.get("total_symbols").unwrap().as_u64().unwrap() > 0);
        assert!(value.get("total_files").unwrap().as_u64().unwrap() > 0);
    }
}