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
use super::Expr;
use std::collections::{HashMap, HashSet};

// Helper functions to create common expressions
fn create_term(keyword: &str) -> Expr {
    // For testing purposes, we'll bypass the tokenization and stemming
    // by directly creating a term with the exact keyword
    Expr::Term {
        keywords: vec![keyword.to_string()],
        field: None,
        required: false,
        excluded: false,
        exact: false,
    }
}

fn create_required_term(keyword: &str) -> Expr {
    // For testing purposes, we'll bypass the tokenization and stemming
    // by directly creating a term with the exact keyword
    Expr::Term {
        keywords: vec![keyword.to_string()],
        field: None,
        required: true,
        excluded: false,
        exact: false,
    }
}

fn create_excluded_term(keyword: &str) -> Expr {
    // For testing purposes, we'll bypass the tokenization and stemming
    // by directly creating a term with the exact keyword
    Expr::Term {
        keywords: vec![keyword.to_string()],
        field: None,
        required: false,
        excluded: true,
        exact: false,
    }
}

fn create_exact_term(keyword: &str) -> Expr {
    // For testing purposes, we'll bypass the tokenization and stemming
    // by directly creating a term with the exact keyword
    Expr::Term {
        keywords: vec![keyword.to_string()],
        field: None,
        required: false,
        excluded: false,
        exact: true,
    }
}

// Helper function to create a term index map
fn create_term_indices(terms: &[&str]) -> HashMap<String, usize> {
    let mut map = HashMap::new();
    for (i, &term) in terms.iter().enumerate() {
        map.insert(term.to_string(), i);
    }
    map
}

// Helper function to create a matched terms set
fn create_matched_terms(indices: &[usize]) -> HashSet<usize> {
    indices.iter().copied().collect()
}

#[test]
fn test_evaluate_simple_terms() {
    // Create term indices
    let term_indices = create_term_indices(&["foo", "bar", "baz"]);
    
    // Test a simple term
    let expr = create_term("foo");
    
    // Match when term is present
    let matched_terms = create_matched_terms(&[0]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when term is absent
    let matched_terms = create_matched_terms(&[1, 2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test required term
    let expr = create_required_term("foo");
    
    // Match when required term is present
    let matched_terms = create_matched_terms(&[0]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when required term is absent
    let matched_terms = create_matched_terms(&[1, 2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test excluded term
    let expr = create_excluded_term("foo");
    
    // Match when excluded term is absent
    let matched_terms = create_matched_terms(&[1, 2]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when excluded term is present
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_and_expressions() {
    // Create term indices
    let term_indices = create_term_indices(&["foo", "bar", "baz"]);
    
    // Test AND expression
    let expr = Expr::And(
        Box::new(create_term("foo")),
        Box::new(create_term("bar"))
    );
    
    // Match when both terms are present
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when only one term is present
    let matched_terms = create_matched_terms(&[0]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    let matched_terms = create_matched_terms(&[1]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when neither term is present
    let matched_terms = create_matched_terms(&[2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_or_expressions() {
    // Create term indices
    let term_indices = create_term_indices(&["foo", "bar", "baz"]);
    
    // Test OR expression
    let expr = Expr::Or(
        Box::new(create_term("foo")),
        Box::new(create_term("bar"))
    );
    
    // Match when both terms are present
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when only one term is present
    let matched_terms = create_matched_terms(&[0]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    let matched_terms = create_matched_terms(&[1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when neither term is present
    let matched_terms = create_matched_terms(&[2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_complex_expressions() {
    // Create term indices
    let term_indices = create_term_indices(&["foo", "bar", "baz", "qux", "zod"]);
    
    // Test complex expression: (foo AND bar) OR baz
    let expr = Expr::Or(
        Box::new(Expr::And(
            Box::new(create_term("foo")),
            Box::new(create_term("bar"))
        )),
        Box::new(create_term("baz"))
    );
    
    // Match when both foo and bar are present
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when baz is present
    let matched_terms = create_matched_terms(&[2]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when all terms are present
    let matched_terms = create_matched_terms(&[0, 1, 2]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when only foo is present
    let matched_terms = create_matched_terms(&[0]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when only bar is present
    let matched_terms = create_matched_terms(&[1]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when neither (foo AND bar) nor baz is present
    let matched_terms = create_matched_terms(&[3, 4]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_required_excluded_terms() {
    // Create term indices
    let term_indices = create_term_indices(&["foo", "bar", "baz", "qux"]);
    
    // Test expression: +foo -bar
    let expr = Expr::And(
        Box::new(create_required_term("foo")),
        Box::new(create_excluded_term("bar"))
    );
    
    // Match when foo is present and bar is absent
    let matched_terms = create_matched_terms(&[0, 2, 3]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when foo is absent
    let matched_terms = create_matched_terms(&[2, 3]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when bar is present
    let matched_terms = create_matched_terms(&[0, 1, 2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_elastic_style_queries() {
    // Create term indices
    let term_indices = create_term_indices(&["keyword1", "keyword2", "keyword3", "keyword4"]);
    
    // Test expression: +(keyword1 OR keyword2) -keyword3
    let expr = Expr::And(
        Box::new(Expr::Or(
            Box::new(create_required_term("keyword1")),
            Box::new(create_required_term("keyword2"))
        )),
        Box::new(create_excluded_term("keyword3"))
    );
    
    // Match when keyword1 is present and keyword3 is absent
    let matched_terms = create_matched_terms(&[0]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when keyword2 is present and keyword3 is absent
    let matched_terms = create_matched_terms(&[1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when both keyword1 and keyword2 are present and keyword3 is absent
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when keyword3 is present, even if keyword1 is present
    let matched_terms = create_matched_terms(&[0, 2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when keyword3 is present, even if keyword2 is present
    let matched_terms = create_matched_terms(&[1, 2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when neither keyword1 nor keyword2 is present
    let matched_terms = create_matched_terms(&[3]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_nested_expressions() {
    // Create term indices
    let term_indices = create_term_indices(&["a", "b", "c", "d", "e"]);
    
    // Test expression: a AND (b OR (c AND (d OR e)))
    let expr = Expr::And(
        Box::new(create_term("a")),
        Box::new(Expr::Or(
            Box::new(create_term("b")),
            Box::new(Expr::And(
                Box::new(create_term("c")),
                Box::new(Expr::Or(
                    Box::new(create_term("d")),
                    Box::new(create_term("e"))
                ))
            ))
        ))
    );
    
    // Match when a and b are present
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when a, c, and d are present
    let matched_terms = create_matched_terms(&[0, 2, 3]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when a, c, and e are present
    let matched_terms = create_matched_terms(&[0, 2, 4]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when all terms are present
    let matched_terms = create_matched_terms(&[0, 1, 2, 3, 4]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when a is absent
    let matched_terms = create_matched_terms(&[1, 2, 3, 4]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when neither b nor (c AND (d OR e)) is satisfied
    let matched_terms = create_matched_terms(&[0, 2]); // a and c, but no d or e
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_with_missing_terms() {
    // Create term indices with some terms missing
    let term_indices = create_term_indices(&["foo", "bar"]);
    
    // Test expression with a term not in the index
    let expr = Expr::And(
        Box::new(create_term("foo")),
        Box::new(create_term("baz")) // Not in the index
    );
    
    // Should not match because baz is not in the index
    let matched_terms = create_matched_terms(&[0]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test excluded term not in the index
    let expr = Expr::And(
        Box::new(create_term("foo")),
        Box::new(create_excluded_term("baz")) // Not in the index
    );
    
    // Should match because baz is not in the index (and thus not matched)
    let matched_terms = create_matched_terms(&[0]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_optional_terms() {
    // Create term indices
    let term_indices = create_term_indices(&["required", "optional", "excluded"]);
    
    // Test expression with mixed term types: +required optional -excluded
    // Note: With the new behavior, space-separated terms are OR, not AND
    let expr = Expr::And(
        Box::new(Expr::Or(
            Box::new(create_required_term("required")),
            Box::new(create_term("optional"))
        )),
        Box::new(create_excluded_term("excluded"))
    );
    
    // Match when required is present but optional is absent and excluded is absent
    // This now matches because we only need either required OR optional
    let matched_terms = create_matched_terms(&[0]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when both required and optional are present and excluded is absent
    let matched_terms = create_matched_terms(&[0, 1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Match when only optional is present and excluded is absent
    // With OR behavior, this should match
    let matched_terms = create_matched_terms(&[1]);
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when excluded is present, even with the OR behavior
    let matched_terms = create_matched_terms(&[0, 2]);
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_evaluate_exact_terms() {
    // Create term indices with original and stemmed terms
    let term_indices = create_term_indices(&["running", "run", "whitelist", "white", "list"]);
    
    // Test exact term
    let expr = create_exact_term("running");
    
    // Match when the exact term is present
    let matched_terms = create_matched_terms(&[0]); // "running"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when only the stemmed term is present
    let matched_terms = create_matched_terms(&[1]); // "run"
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test non-exact term - use the stemmed form directly
    let expr = Expr::Term {
        keywords: vec!["run".to_string()],
        field: None,
        required: false,
        excluded: false,
        exact: false,
    };
    
    // The term is "run", so it won't match "running" directly
    // We need to update the term_indices to reflect the actual relationship
    let mut term_indices_updated = HashMap::new();
    term_indices_updated.insert("run".to_string(), 1); // Map "run" to index 1
    term_indices_updated.insert("running".to_string(), 0); // Map "running" to index 0
    term_indices_updated.insert("whitelist".to_string(), 2);
    term_indices_updated.insert("white".to_string(), 3);
    term_indices_updated.insert("list".to_string(), 4);
    
    // Match when the exact term is present
    let matched_terms = create_matched_terms(&[0, 1]); // Include both "running" and "run"
    assert!(expr.evaluate(&matched_terms, &term_indices_updated, false));
    
    // Match when only the stemmed term is present
    let matched_terms = create_matched_terms(&[1]); // "run"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test compound expression with exact term - simplified approach
    // Create a new expression with the stemmed form directly
    let expr = Expr::And(
        Box::new(Expr::Term {
            keywords: vec!["run".to_string()], // Use stemmed form directly
            field: None,
            required: false,
            excluded: false,
            exact: false,
        }),
        Box::new(create_exact_term("whitelist"))
    );
    
    // Match when both terms are present
    let matched_terms = create_matched_terms(&[1, 2]); // "run", "whitelist"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when the exact term is only present as stemmed parts
    let matched_terms = create_matched_terms(&[0, 3, 4]); // "running", "white", "list"
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}