codenexus 0.3.4

A queryable code knowledge graph tool built on LadybugDB and tree-sitter
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
// Copyright (c) 2026 Kirky.X. All rights reserved.
// SPDX-License-Identifier: MIT

//! Search command: search for symbols by name or content.

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
#[cfg(any(feature = "cli", feature = "mcp", test))]
use std::str::FromStr;

#[cfg(any(feature = "cli", feature = "mcp", test))]
use crate::kit::{AsyncKit, AsyncReady, QueryModule, StorageModule};
#[cfg(any(feature = "cli", feature = "mcp", test))]
use crate::query::structured::{SearchEngine, SearchMode, SearchParams};
#[cfg(any(feature = "cli", feature = "mcp", test))]
use crate::query::SearchResult;
#[cfg(any(feature = "cli", feature = "mcp", test))]
use crate::service::error::CodeNexusError;
#[cfg(any(feature = "cli", feature = "mcp"))]
use crate::service::error::{kit_not_initialized, to_api_error};
#[cfg(any(feature = "cli", feature = "mcp"))]
use crate::service::runtime::kit;

#[cfg(any(feature = "cli", feature = "mcp"))]
use sdforge::forge;
#[cfg(any(feature = "cli", feature = "mcp"))]
use sdforge::prelude::ApiError;

/// JSON-serializable search result.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SearchOutput {
    pub count: usize,
    pub results: Vec<Value>,
}

/// Converts a [`SearchResult`] into a JSON object for API output.
#[cfg(any(feature = "cli", feature = "mcp", test))]
fn search_result_to_json(r: &SearchResult) -> Value {
    json!({
        "name": r.name,
        "label": r.label,
        "filePath": r.file_path,
        "startLine": r.start_line,
        "qualifiedName": r.qualified_name,
        "score": r.score,
        "matchReason": r.match_reason,
    })
}

/// Runs search against an injected Kit (testable core).
///
/// When `mode` is non-empty, uses [`SearchEngine`] with the corresponding
/// [`SearchMode`] for multi-mode search (exact/regex/fuzzy/graph/multi).
/// When `mode` is empty, falls back to the legacy `QueryModule` search
/// (structured or BM25 full-text based on `fulltext` flag).
#[cfg(any(feature = "cli", feature = "mcp", test))]
pub fn run_search(
    kit: &AsyncKit<AsyncReady>,
    text: &str,
    fulltext: bool,
    limit: u32,
    mode: &str,
    project: &str,
) -> Result<SearchOutput, CodeNexusError> {
    if let Ok(search_mode) = SearchMode::from_str(mode) {
        let storage = kit.require::<StorageModule>()?;
        let engine = SearchEngine::new(&*storage);
        let params = SearchParams {
            query: text.to_string(),
            mode: search_mode,
            limit: limit as usize,
            ..Default::default()
        };
        let results = engine.search(project, &params)?;
        let results: Vec<Value> = results.iter().map(search_result_to_json).collect();
        Ok(SearchOutput {
            count: results.len(),
            results,
        })
    } else {
        let q = kit.require::<QueryModule>()?;
        let results = if fulltext {
            q.fulltext_search(text, None, limit as usize)
        } else {
            q.search(text, None, limit as usize)
        }?;
        let results: Vec<Value> = results.iter().map(search_result_to_json).collect();
        Ok(SearchOutput {
            count: results.len(),
            results,
        })
    }
}

/// CLI wrapper — prints result to stdout as JSON.
#[cfg(feature = "cli")]
#[forge(
    name = "search",
    version = "0.3.3",
    description = "Search for symbols by name (structured) or content (BM25 full-text).",
    cli = true
)]
async fn search(
    text: String,
    fulltext: bool,
    limit: u32,
    mode: String,
    project: String,
) -> Result<(), ApiError> {
    let kit = kit().ok_or_else(kit_not_initialized)?;
    let result = run_search(&kit, &text, fulltext, limit, &mode, &project)
        .map_err(|e| to_api_error(e, "search_error"))?;
    let json = serde_json::to_string(&result)
        .map_err(|e| to_api_error(CodeNexusError::from(e), "search_error"))?;
    println!("{json}");
    Ok(())
}

/// MCP wrapper — returns result for MCP protocol.
#[cfg(feature = "mcp")]
#[forge(
    name = "search",
    version = "0.3.3",
    tool_name = "search",
    description = "Search for symbols by name (structured) or content (BM25 full-text)."
)]
async fn search_mcp(
    text: String,
    fulltext: bool,
    limit: u32,
    mode: String,
    project: String,
) -> Result<SearchOutput, ApiError> {
    let kit = kit().ok_or_else(kit_not_initialized)?;
    run_search(&kit, &text, fulltext, limit, &mode, &project)
        .map_err(|e| to_api_error(e, "search_error"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::kit::{build_kit, KitBootstrapConfig};
    use std::path::PathBuf;
    use tempfile::TempDir;

    fn fresh_db_path() -> (TempDir, PathBuf) {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("svc_search_testdb");
        (dir, path)
    }

    fn build_kit_for_db(db: &std::path::Path) -> AsyncKit<AsyncReady> {
        let config = KitBootstrapConfig::new(db.to_path_buf());
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(build_kit(&config))
            .expect("build_kit")
    }

    #[test]
    fn run_search_returns_empty_on_fresh_db() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output = run_search(&kit, "foo", false, 10, "", "").expect("run should succeed");
        assert_eq!(output.count, 0);
        assert!(output.results.is_empty());
    }

    #[test]
    fn run_search_finds_symbol_by_name() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thing", false, 10, "", "").expect("run should succeed");
        let _ = output;
    }

    #[test]
    fn run_search_empty_text_returns_error() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let err = run_search(&kit, "   ", false, 10, "", "").expect_err("empty text should error");
        assert!(matches!(err, CodeNexusError::Query(_)));
    }

    #[test]
    fn run_search_fulltext_empty_text_returns_error() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let err = run_search(&kit, "", true, 10, "", "").expect_err("empty fulltext should error");
        assert!(matches!(err, CodeNexusError::Query(_)));
    }

    #[test]
    fn search_output_serializes_to_json() {
        let output = SearchOutput {
            count: 1,
            results: vec![json!({
                "name": "foo",
                "label": "Function",
                "filePath": "/src/a.rs",
                "startLine": 1,
                "qualifiedName": "demo.foo",
                "score": 0.95,
            })],
        };
        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("\"count\":1"));
        assert!(json.contains("\"name\":\"foo\""));
        assert!(json.contains("\"label\":\"Function\""));
    }

    #[test]
    fn search_result_to_json_maps_all_fields() {
        let r = SearchResult {
            name: "foo".into(),
            label: "Function".into(),
            file_path: Some("/src/a.rs".into()),
            start_line: Some(42),
            qualified_name: Some("demo.foo".into()),
            score: 0.8,
            match_reason: "exact".into(),
            degree: 0,
        };
        let v = search_result_to_json(&r);
        assert_eq!(v["name"], "foo");
        assert_eq!(v["label"], "Function");
        assert_eq!(v["filePath"], "/src/a.rs");
        assert_eq!(v["startLine"], 42);
        assert_eq!(v["qualifiedName"], "demo.foo");
        let score = v["score"].as_f64().expect("score should be a number");
        assert!(
            (score - 0.8).abs() < 1e-6,
            "score should be ~0.8, got {score}"
        );
    }

    #[test]
    fn search_result_to_json_handles_none_fields() {
        let r = SearchResult {
            name: "bar".into(),
            label: "Class".into(),
            file_path: None,
            start_line: None,
            qualified_name: None,
            score: 0.0,
            match_reason: String::new(),
            degree: 0,
        };
        let v = search_result_to_json(&r);
        assert_eq!(v["name"], "bar");
        assert!(v["filePath"].is_null());
        assert!(v["startLine"].is_null());
        assert!(v["qualifiedName"].is_null());
    }

    // ===== T039: run_search with mode parameter =====

    #[test]
    fn run_search_with_exact_mode_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thing", false, 10, "exact", "demo")
            .expect("exact mode search should succeed");
        assert!(output.count > 0, "should find do_thing in exact mode");
    }

    #[test]
    fn run_search_with_regex_mode_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_.*", false, 10, "regex", "demo")
            .expect("regex mode search should succeed");
        assert!(output.count > 0, "should find do_thing with regex do_.*");
    }

    #[test]
    fn run_search_with_fuzzy_mode_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thng", false, 10, "fuzzy", "demo")
            .expect("fuzzy mode search should succeed");
        // Fuzzy search should find do_thing even with a typo
        assert!(output.count > 0, "should find do_thing with fuzzy do_thng");
    }

    #[test]
    fn run_search_with_invalid_mode_falls_back_to_legacy() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        // Invalid mode → None → legacy path (should succeed on empty DB)
        let output = run_search(&kit, "foo", false, 10, "invalid_mode", "")
            .expect("invalid mode should fall back to legacy");
        assert_eq!(output.count, 0);
    }

    #[test]
    fn run_search_with_empty_mode_uses_legacy_path() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output =
            run_search(&kit, "foo", false, 10, "", "").expect("empty mode should use legacy path");
        assert_eq!(output.count, 0);
    }

    // ===== run_search: graph_enhanced and multi_signal modes =====

    #[test]
    fn run_search_with_graph_enhanced_mode_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thing", false, 10, "graph", "demo")
            .expect("graph_enhanced mode should succeed");
        assert!(output.count > 0, "should find do_thing in graph mode");
    }

    #[test]
    fn run_search_with_graph_enhanced_alias_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thing", false, 10, "graph_enhanced", "demo")
            .expect("graph_enhanced alias should succeed");
        assert!(
            output.count > 0,
            "should find do_thing with graph_enhanced alias"
        );
    }

    #[test]
    fn run_search_with_multi_signal_mode_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thing", false, 10, "multi", "demo")
            .expect("multi_signal mode should succeed");
        assert!(output.count > 0, "should find do_thing in multi mode");
    }

    #[test]
    fn run_search_with_multi_signal_alias_finds_symbol() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'do_thing', qualifiedName: 'demo.do_thing', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "do_thing", false, 10, "multi_signal", "demo")
            .expect("multi_signal alias should succeed");
        assert!(
            output.count > 0,
            "should find do_thing with multi_signal alias"
        );
    }

    #[test]
    fn run_search_with_graph_enhanced_on_empty_db_returns_empty() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output = run_search(&kit, "foo", false, 10, "graph", "demo")
            .expect("graph mode on empty DB should succeed");
        assert_eq!(output.count, 0);
    }

    #[test]
    fn run_search_with_multi_signal_on_empty_db_returns_empty() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output = run_search(&kit, "foo", false, 10, "multi", "demo")
            .expect("multi mode on empty DB should succeed");
        assert_eq!(output.count, 0);
    }

    // ===== run_search: fulltext mode exercises legacy fulltext path =====

    #[test]
    fn run_search_fulltext_on_non_empty_db_succeeds() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let storage = kit.require::<crate::kit::StorageModule>().expect("storage");
        storage.execute("CREATE (:Function {id: 'f1', project: 'demo', name: 'fetch_data', qualifiedName: 'demo.fetch_data', filePath: '/src/a.rs', startLine: 1, endLine: 5, signature: '', returnType: '', isExported: false, docstring: '', content: '', parentQn: ''});").expect("create function");
        let output = run_search(&kit, "fetch_data", true, 10, "", "")
            .expect("fulltext search should succeed");
        let _ = output;
    }

    #[test]
    fn run_search_with_exact_mode_on_empty_db_returns_empty() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output = run_search(&kit, "foo", false, 10, "exact", "demo")
            .expect("exact mode on empty DB should succeed");
        assert_eq!(output.count, 0);
    }

    #[test]
    fn run_search_with_regex_mode_on_empty_db_returns_empty() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output = run_search(&kit, "foo.*", false, 10, "regex", "demo")
            .expect("regex mode on empty DB should succeed");
        assert_eq!(output.count, 0);
    }

    #[test]
    fn run_search_with_fuzzy_mode_on_empty_db_returns_empty() {
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        let output = run_search(&kit, "foo", false, 10, "fuzzy", "demo")
            .expect("fuzzy mode on empty DB should succeed");
        assert_eq!(output.count, 0);
    }

    // ===== #[forge] wrapper tests via init_kit =====

    #[serial_test::serial(kit_init)]
    #[cfg(feature = "cli")]
    #[test]
    fn search_wrapper_succeeds_via_init_kit() {
        use crate::service::runtime::{init_kit, reset_kit_for_testing};

        reset_kit_for_testing();
        let (_dir, db) = fresh_db_path();
        let kit = build_kit_for_db(&db);
        init_kit(kit).expect("init_kit");

        let rt = tokio::runtime::Runtime::new().expect("runtime");
        let result = rt.block_on(search(
            "foo".to_string(),
            false,
            10,
            "".to_string(),
            "demo".to_string(),
        ));
        assert!(result.is_ok(), "wrapper should succeed: {:?}", result.err());

        reset_kit_for_testing();
    }

    #[serial_test::serial(kit_init)]
    #[cfg(feature = "cli")]
    #[test]
    fn search_wrapper_fails_when_kit_not_initialized() {
        use crate::service::runtime::reset_kit_for_testing;

        reset_kit_for_testing();
        let rt = tokio::runtime::Runtime::new().expect("runtime");
        let result = rt.block_on(search(
            "foo".to_string(),
            false,
            10,
            "".to_string(),
            "demo".to_string(),
        ));
        assert!(result.is_err(), "wrapper should fail without kit");
        reset_kit_for_testing();
    }
}