probe-code 0.6.0

AI-friendly, fully local, semantic code search tool for large codebases
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
use std::fs;
use tempfile::TempDir;

use probe_code::models::SearchResult;
use probe_code::search::block_merging::merge_ranked_blocks;
use probe_code::search::{perform_probe, SearchOptions};

/// Test merging of blocks with different node types
#[test]
fn test_merge_different_node_types() {
    // Create test blocks with different node types
    let block1 = SearchResult {
        file: "mixed_types.rs".to_string(),
        lines: (1, 5),
        node_type: "function".to_string(),
        code:
            "fn test_function() {\n    let x = 1;\n    let y = 2;\n    println!(\"{}\", x + y);\n}"
                .to_string(),
        matched_by_filename: None,
        rank: Some(1),
        score: Some(0.9),
        tfidf_score: Some(0.8),
        bm25_score: Some(0.85),
        tfidf_rank: Some(1),
        bm25_rank: Some(1),
        new_score: Some(0.87),
        hybrid2_rank: Some(1),
        combined_score_rank: Some(1),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(3),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };
    let block2 = SearchResult {
    file: "mixed_types.rs".to_string(),
    lines: (6, 10),
    node_type: "comment".to_string(),
    code: "// This is a comment block\n// It explains the function above\n// And provides context\n// For the next function\n// Below".to_string(),
    matched_by_filename: None,
    rank: Some(2),
    score: Some(0.8),
    tfidf_score: Some(0.7),
    bm25_score: Some(0.75),
    tfidf_rank: Some(2),
    bm25_rank: Some(2),
    new_score: Some(0.77),
    hybrid2_rank: Some(2),
    combined_score_rank: Some(2),
    file_unique_terms: Some(3),
    file_total_matches: Some(5),
    file_match_rank: Some(1),
    block_unique_terms: Some(2),
    block_total_matches: Some(2),
    parent_file_id: None,
    block_id: None,
    matched_keywords: None,
    tokenized_content: None,
};

    let block3 = SearchResult {
        file: "mixed_types.rs".to_string(),
        lines: (11, 15),
        node_type: "function".to_string(),
        code: "fn another_function() {\n    let z = 3;\n    let result = z * 2;\n    println!(\"{}\", result);\n}".to_string(),
        matched_by_filename: None,
        rank: Some(3),
        score: Some(0.7),
        tfidf_score: Some(0.6),
        bm25_score: Some(0.65),
        tfidf_rank: Some(3),
        bm25_rank: Some(3),
        new_score: Some(0.67),
        hybrid2_rank: Some(3),
        combined_score_rank: Some(3),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(2),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };

    // Create a vector with all blocks
    let blocks = vec![block1, block2, block3];

    // Call the merge_ranked_blocks function
    let merged_blocks = merge_ranked_blocks(blocks, Some(5));

    // Assert that all blocks are merged into one
    assert_eq!(
        merged_blocks.len(),
        1,
        "All adjacent blocks should be merged into one"
    );

    // Check that the merged block has the correct line range
    let merged_block = &merged_blocks[0];
    assert_eq!(
        merged_block.lines,
        (1, 15),
        "Merged block should span from line 1 to 15"
    );

    // Check that the node_type is from the highest-ranked block
    assert_eq!(
        merged_block.node_type, "function",
        "Merged block should have the node_type of the highest-ranked block"
    );

    // Check that the score is the maximum of all blocks
    assert_eq!(
        merged_block.score,
        Some(0.9),
        "Merged score should be the maximum of all blocks"
    );
}

/// Test merging of blocks with gaps between them
#[test]
fn test_merge_with_gaps() {
    // Create test blocks with gaps between them
    let block1 = SearchResult {
        file: "gaps.rs".to_string(),
        lines: (1, 5),
        node_type: "function".to_string(),
        code:
            "fn first_function() {\n    let x = 1;\n    let y = 2;\n    println!(\"{}\", x + y);\n}"
                .to_string(),
        matched_by_filename: None,
        rank: Some(1),
        score: Some(0.9),
        tfidf_score: Some(0.8),
        bm25_score: Some(0.85),
        tfidf_rank: Some(1),
        bm25_rank: Some(1),
        new_score: Some(0.87),
        hybrid2_rank: Some(1),
        combined_score_rank: Some(1),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(3),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };

    // Gap of 3 lines between block1 and block2
    let block2 = SearchResult {
        file: "gaps.rs".to_string(),
        lines: (9, 13),
        node_type: "function".to_string(),
        code: "fn second_function() {\n    let z = 3;\n    let result = z * 2;\n    println!(\"{}\", result);\n}".to_string(),
        matched_by_filename: None,
        rank: Some(2),
        score: Some(0.8),
        tfidf_score: Some(0.7),
        bm25_score: Some(0.75),
        tfidf_rank: Some(2),
        bm25_rank: Some(2),
        new_score: Some(0.77),
        hybrid2_rank: Some(2),
        combined_score_rank: Some(2),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(2),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };

    // Gap of 2 lines between block2 and block3
    let block3 = SearchResult {
        file: "gaps.rs".to_string(),
        lines: (16, 20),
        node_type: "function".to_string(),
        code:
            "fn third_function() {\n    let a = 4;\n    let b = 5;\n    println!(\"{}\", a + b);\n}"
                .to_string(),
        matched_by_filename: None,
        rank: Some(3),
        score: Some(0.7),
        tfidf_score: Some(0.6),
        bm25_score: Some(0.65),
        tfidf_rank: Some(3),
        bm25_rank: Some(3),
        new_score: Some(0.67),
        hybrid2_rank: Some(3),
        combined_score_rank: Some(3),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(2),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };

    // Test with default threshold (5)
    {
        let blocks = vec![block1.clone(), block2.clone(), block3.clone()];
        let merged_blocks = merge_ranked_blocks(blocks, Some(5));

        // With threshold 5, all blocks should be merged
        assert_eq!(
            merged_blocks.len(),
            1,
            "With threshold 5, all blocks should be merged"
        );

        // Check that the merged block has the correct line range
        let merged_block = &merged_blocks[0];
        assert_eq!(
            merged_block.lines,
            (1, 20),
            "Merged block should span from line 1 to 20"
        );
    }

    // Test with smaller threshold (2)
    {
        let blocks = vec![block1.clone(), block2.clone(), block3.clone()];
        let merged_blocks = merge_ranked_blocks(blocks, Some(2));

        // With threshold 2, block1 and block2 should not be merged (gap of 3),
        // but block2 and block3 should be merged (gap of 2)
        assert_eq!(
            merged_blocks.len(),
            2,
            "With threshold 2, we should have 2 merged blocks"
        );

        // Find the blocks
        let first_block = merged_blocks.iter().find(|b| b.lines.0 == 1).unwrap();
        let second_block = merged_blocks.iter().find(|b| b.lines.0 == 9).unwrap();

        // Check line ranges
        assert_eq!(
            first_block.lines,
            (1, 5),
            "First block should span from line 1 to 5"
        );
        assert_eq!(
            second_block.lines,
            (9, 20),
            "Second block should span from line 9 to 20"
        );
    }

    // Test with threshold 0 (no merging)
    {
        let blocks = vec![block1.clone(), block2.clone(), block3.clone()];
        let merged_blocks = merge_ranked_blocks(blocks, Some(0));

        // With threshold 0, no blocks should be merged
        assert_eq!(
            merged_blocks.len(),
            3,
            "With threshold 0, no blocks should be merged"
        );
    }
}

/// Test merging of blocks with overlapping lines
#[test]
fn test_merge_overlapping_blocks() {
    // Create test blocks with overlapping lines
    let block1 = SearchResult {
        file: "overlap.rs".to_string(),
        lines: (1, 7),
        node_type: "function".to_string(),
        code: "fn first_function() {\n    let x = 1;\n    let y = 2;\n    println!(\"{}\", x + y);\n    // Shared lines\n    let shared = true;\n}".to_string(),
        matched_by_filename: None,
        rank: Some(1),
        score: Some(0.9),
        tfidf_score: Some(0.8),
        bm25_score: Some(0.85),
        tfidf_rank: Some(1),
        bm25_rank: Some(1),
        new_score: Some(0.87),
        hybrid2_rank: Some(1),
        combined_score_rank: Some(1),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(3),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };

    // Overlaps with block1 (lines 5-7 are shared)
    let block2 = SearchResult {
        file: "overlap.rs".to_string(),
        lines: (5, 10),
        node_type: "function".to_string(),
        code: "    // Shared lines\n    let shared = true;\n}\n\nfn second_function() {\n    let z = 3;\n}".to_string(),
        matched_by_filename: None,
        rank: Some(2),
        score: Some(0.8),
        tfidf_score: Some(0.7),
        bm25_score: Some(0.75),
        tfidf_rank: Some(2),
        bm25_rank: Some(2),
        new_score: Some(0.77),
        hybrid2_rank: Some(2),
        combined_score_rank: Some(2),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(2),
        parent_file_id: None,
        block_id: None,
        matched_keywords: None,
        tokenized_content: None,
    };

    // Create a vector with both blocks
    let blocks = vec![block1, block2];

    // Call the merge_ranked_blocks function
    let merged_blocks = merge_ranked_blocks(blocks, Some(5));

    // Assert that blocks are merged into one
    assert_eq!(
        merged_blocks.len(),
        1,
        "Overlapping blocks should be merged into one"
    );

    // Check that the merged block has the correct line range
    let merged_block = &merged_blocks[0];
    assert_eq!(
        merged_block.lines,
        (1, 10),
        "Merged block should span from line 1 to 10"
    );

    // Check that the score is the maximum of both blocks
    assert_eq!(
        merged_block.score,
        Some(0.9),
        "Merged score should be the maximum of both blocks"
    );
}

/// Test merging of blocks with complex file structure
#[test]
fn test_merge_complex_file_structure() {
    // Create a temporary directory for testing
    let temp_dir = TempDir::new().unwrap();
    let temp_path = temp_dir.path();

    // Create a complex test file with multiple functions, classes, and comments
    let complex_file_path = temp_path.join("complex_structure.rs");
    let complex_file_content = r#"
fn first_function() {
    let x = 1;
    let y = 2;
    let result = calculate(x, y);
    println!("{}", result);
}

fn second_function() {
    let z = 3;
    let w = 4;
    let result = calculate(z, w);
    println!("{}", result);
}

// Large gap

fn third_function() {
    let a = 5;
    let b = 6;
    let result = calculate(a, b);
    println!("{}", result);
}
"#;

    // Write file to disk
    fs::write(complex_file_path, complex_file_content).unwrap();

    // Create search queries that exactly match content in the file
    let queries = vec!["result".to_string()]; // This appears in every function and will help us test merging
    let custom_ignores: Vec<String> = vec![];

    // Test with different merge thresholds
    for &threshold in &[2, 5, 10, 20] {
        // Create SearchOptions with the current threshold
        let options = SearchOptions {
            path: temp_path,
            queries: &queries,
            files_only: false,
            custom_ignores: &custom_ignores,
            exclude_filenames: true,
            language: None,
            reranker: "combined",
            frequency_search: false,
            max_results: None,
            max_bytes: None,
            max_tokens: None,
            allow_tests: true,
            no_merge: false,
            merge_threshold: Some(threshold),
            dry_run: false,
            session: None,
            timeout: 30,
            exact: false,
        };

        // Run the search
        let search_results = perform_probe(&options).unwrap();

        // Check that we got results
        assert!(
            !search_results.results.is_empty(),
            "Search should return results"
        );

        // Count results for the complex file
        let complex_file_results: Vec<&SearchResult> = search_results
            .results
            .iter()
            .filter(|r| r.file.ends_with("complex_structure.rs"))
            .collect();

        // Print the number of results for debugging
        println!(
            "With threshold {}, got {} results for complex file",
            threshold,
            complex_file_results.len()
        );

        // With a small threshold, we should have more blocks
        // With a large threshold, we should have fewer blocks
        if threshold <= 5 {
            // With threshold <= 5, first and second functions should be merged,
            // but third function should be separate (due to large gap)
            assert!(
                complex_file_results.len() <= 2,
                "With threshold {}, expected at most 2 blocks, got {}",
                threshold,
                complex_file_results.len()
            );
        } else if threshold >= 20 {
            // With threshold >= 20, all functions should be merged
            assert_eq!(
                complex_file_results.len(),
                1,
                "With threshold {}, expected 1 block, got {}",
                threshold,
                complex_file_results.len()
            );
        }
    }
}

/// Test merging of blocks with parent-child relationships
#[test]
fn test_merge_parent_child_blocks() {
    // Create test blocks with parent-child relationships
    let parent_block = SearchResult {
        file: "parent_child.rs".to_string(),
        lines: (1, 10),
        node_type: "class".to_string(),
        code: "struct TestStruct {\n    x: i32,\n    y: i32,\n}\n\nimpl TestStruct {\n    fn new(x: i32, y: i32) -> Self {\n        Self { x, y }\n    }\n}".to_string(),
        matched_by_filename: None,
        rank: Some(2),
        score: Some(0.8),
        tfidf_score: Some(0.7),
        bm25_score: Some(0.75),
        tfidf_rank: Some(2),
        bm25_rank: Some(2),
        new_score: Some(0.77),
        hybrid2_rank: Some(2),
        combined_score_rank: Some(2),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(3),
        parent_file_id: Some("parent_child.rs".to_string()),
        block_id: Some(0),
        matched_keywords: None,
        tokenized_content: None,
    };

    // Child block (method inside the struct)
    let child_block = SearchResult {
        file: "parent_child.rs".to_string(),
        lines: (7, 9),
        node_type: "function".to_string(),
        code: "    fn new(x: i32, y: i32) -> Self {\n        Self { x, y }\n    }".to_string(),
        matched_by_filename: None,
        rank: Some(1),
        score: Some(0.9),
        tfidf_score: Some(0.8),
        bm25_score: Some(0.85),
        tfidf_rank: Some(1),
        bm25_rank: Some(1),
        new_score: Some(0.87),
        hybrid2_rank: Some(1),
        combined_score_rank: Some(1),
        file_unique_terms: Some(3),
        file_total_matches: Some(5),
        file_match_rank: Some(1),
        block_unique_terms: Some(2),
        block_total_matches: Some(2),
        parent_file_id: Some("parent_child.rs".to_string()),
        block_id: Some(1),
        matched_keywords: None,
        tokenized_content: None,
    };

    // Create a vector with both blocks
    let blocks = vec![child_block, parent_block];

    // Call the merge_ranked_blocks function
    let merged_blocks = merge_ranked_blocks(blocks, Some(5));

    // Assert that blocks are merged into one
    assert_eq!(
        merged_blocks.len(),
        1,
        "Parent-child blocks should be merged into one"
    );

    // Check that the merged block has the correct line range
    let merged_block = &merged_blocks[0];
    assert_eq!(
        merged_block.lines,
        (1, 10),
        "Merged block should span from line 1 to 10"
    );

    // Check that the score is the maximum of both blocks
    assert_eq!(
        merged_block.score,
        Some(0.9),
        "Merged score should be the maximum of both blocks"
    );

    // Check that the node_type is from the highest-ranked block
    assert_eq!(
        merged_block.node_type, "function",
        "Merged block should have the node_type of the highest-ranked block"
    );
}