ochna 0.0.4

A structural code graph indexing and analysis CLI using Tree-sitter and SQLite
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
//! CLI subcommand implementations, split by concern:
//! - [`index`] — the `init`/`sync` indexing pipeline.
//! - [`status`] — read-only `status`/`files` inspection.
//! - [`query`] — `search`/`callers`/`node`/`explore` graph queries.

mod index;
mod query;
mod status;

pub use index::run_init;
pub use query::{run_callers, run_explore, run_node, run_search};
pub use status::{run_files, run_status};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db;
    use rusqlite::Connection;
    use std::fs;
    use std::path::PathBuf;

    fn create_temp_dir() -> PathBuf {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::time::{SystemTime, UNIX_EPOCH};
        static COUNTER: AtomicUsize = AtomicUsize::new(0);
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let count = COUNTER.fetch_add(1, Ordering::SeqCst);
        let path = std::env::temp_dir().join(format!("ochna_test_{}_{}", now, count));
        fs::create_dir_all(&path).unwrap();
        path
    }

    #[test]
    fn test_commands_workflow() {
        let temp_workspace = create_temp_dir();

        // 1. Create a sub-directory and some mock files
        let src_dir = temp_workspace.join("src");
        fs::create_dir_all(&src_dir).unwrap();

        let rust_file = src_dir.join("main.rs");
        let rust_code = r#"
            /// A main entry point.
            fn main() {
                helper();
            }

            fn helper() {
                println!("hello");
            }
        "#;
        fs::write(&rust_file, rust_code).unwrap();

        let go_file = temp_workspace.join("main.go");
        let go_code = r#"
            package main
            import "fmt"

            // GoHelper function
            func GoHelper() {
                fmt.Println("go helper")
            }
        "#;
        fs::write(&go_file, go_code).unwrap();

        let c_file = temp_workspace.join("main.c");
        let c_code = r#"
            int c_helper(void) {
                return 1;
            }
        "#;
        fs::write(&c_file, c_code).unwrap();

        let cpp_file = temp_workspace.join("main.cpp");
        let cpp_code = r#"
            int cpp_helper() {
                return 2;
            }
        "#;
        fs::write(&cpp_file, cpp_code).unwrap();

        let zig_file = temp_workspace.join("main.zig");
        let zig_code = r#"
            fn zigHelper() i32 {
                return 3;
            }
        "#;
        fs::write(&zig_file, zig_code).unwrap();

        // Let's create an ignored directory/file to ensure they are skipped
        let ignored_dir = temp_workspace.join(".git");
        fs::create_dir_all(&ignored_dir).unwrap();
        fs::write(ignored_dir.join("config"), "dummy content").unwrap();

        let target_dir = temp_workspace.join("target");
        fs::create_dir_all(&target_dir).unwrap();
        fs::write(target_dir.join("binary.rs"), "dummy rust in target").unwrap();

        // 2. Run run_init
        run_init(&temp_workspace, false).unwrap();

        // Verify .ochna/ochna.db was created
        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        assert!(db_path.exists());

        // Verify status fetches expected data
        let conn = Connection::open(&db_path).unwrap();
        let files_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM files", [], |row| row.get(0))
            .unwrap();
        let nodes_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM nodes", [], |row| row.get(0))
            .unwrap();

        // files should only contain supported source files outside ignored directories.
        assert_eq!(files_count, 5);
        // nodes:
        // rust: "main" (function), "helper" (function) -> 2 nodes
        // go: "GoHelper" (function) -> 1 node
        // c: "c_helper" (function) -> 1 node
        // cpp: "cpp_helper" (function) -> 1 node
        // zig: "zigHelper" (function) -> 1 node
        // Total nodes: 6
        assert_eq!(nodes_count, 6);

        // Run status command and verify it succeeds (text + json)
        run_status(&temp_workspace, false).unwrap();
        run_status(&temp_workspace, true).unwrap();

        // Run files command and verify it succeeds (text + json)
        run_files(&temp_workspace, false).unwrap();
        run_files(&temp_workspace, true).unwrap();

        // Verify new query commands query the SQLite database successfully and print expected output formats
        run_search(&temp_workspace, "helper", false, false).unwrap();
        run_search(&temp_workspace, "helper", true, false).unwrap();
        run_callers(&temp_workspace, "helper", false, false).unwrap();
        run_callers(&temp_workspace, "helper", true, false).unwrap();

        // Test run_node with file (symbols_only = false)
        run_node(
            &temp_workspace,
            Some("src/main.rs".to_string()),
            Some(1),
            Some(10),
            false,
            None,
            false,
            None,
            false,
            false,
        )
        .unwrap();
        // Test run_node with file (symbols_only = true)
        run_node(
            &temp_workspace,
            Some("src/main.rs".to_string()),
            None,
            None,
            true,
            None,
            false,
            None,
            false,
            false,
        )
        .unwrap();
        // Test run_node with symbol (include_code = true)
        run_node(
            &temp_workspace,
            None,
            None,
            None,
            false,
            Some("helper".to_string()),
            true,
            None,
            false,
            false,
        )
        .unwrap();
        // Test run_node with symbol (include_code = true, JSON output)
        run_node(
            &temp_workspace,
            None,
            None,
            None,
            false,
            Some("helper".to_string()),
            true,
            None,
            true,
            false,
        )
        .unwrap();
        // Test run_node with symbol (include_code = true and line filtering)
        run_node(
            &temp_workspace,
            None,
            None,
            None,
            false,
            Some("helper".to_string()),
            true,
            Some(6),
            false,
            false,
        )
        .unwrap();

        // Test run_explore (text + json)
        run_explore(&temp_workspace, "helper", false, false).unwrap();
        run_explore(&temp_workspace, "helper", true, false).unwrap();

        // 3. Modify a file and check that re-indexing works
        let rust_code_modified = r#"
            /// Modified main entry point.
            fn main() {
                // calls deleted helper
            }
        "#;
        fs::write(&rust_file, rust_code_modified).unwrap();

        // FileMetadata updates
        run_init(&temp_workspace, false).unwrap();

        let nodes_count_after: i64 = conn
            .query_row("SELECT COUNT(*) FROM nodes", [], |row| row.get(0))
            .unwrap();
        // Now rust file has 1 node ("main"). Other source files retain one node each.
        assert_eq!(nodes_count_after, 5);

        // Clean up temporary workspace
        fs::remove_dir_all(&temp_workspace).unwrap();
    }

    #[test]
    fn test_cross_file_edges_and_unresolved() {
        let temp_workspace = create_temp_dir();
        let src_dir = temp_workspace.join("src");
        fs::create_dir_all(&src_dir).unwrap();

        // a.rs calls target() (defined in b.rs) and missing() (defined nowhere).
        fs::write(
            src_dir.join("a.rs"),
            "fn caller() {\n    target();\n    missing();\n}\n",
        )
        .unwrap();
        fs::write(src_dir.join("b.rs"), "fn target() {}\n").unwrap();

        run_init(&temp_workspace, false).unwrap();

        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        let conn = Connection::open(&db_path).unwrap();

        // A call edge must cross the file boundary: src/a.rs::caller -> src/b.rs::target.
        let cross_file: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM edges e \
                 JOIN nodes s ON e.source_nid = s.nid \
                 JOIN nodes t ON e.target_nid = t.nid \
                 WHERE s.file_path <> t.file_path",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(cross_file, 1, "expected one cross-file call edge");

        // The call to an unindexed symbol must be recorded as an unresolved reference.
        let unresolved: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM unresolved_refs WHERE specifier = 'missing'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(unresolved, 1, "expected one unresolved reference");

        fs::remove_dir_all(&temp_workspace).unwrap();
    }

    #[test]
    fn test_scope_classification_tags_tests_and_skips_libraries() {
        let temp_workspace = create_temp_dir();
        fs::create_dir_all(temp_workspace.join("src/test/java")).unwrap();
        fs::create_dir_all(temp_workspace.join("tests")).unwrap();
        fs::create_dir_all(temp_workspace.join("vendor")).unwrap();
        fs::create_dir_all(temp_workspace.join("target")).unwrap();

        fs::write(temp_workspace.join("src/main.rs"), "fn app_main() {}\n").unwrap();
        fs::write(
            temp_workspace.join("tests/parser.rs"),
            "fn parser_test() {}\n",
        )
        .unwrap();
        fs::write(
            temp_workspace.join("client_test.go"),
            "package main\nfunc TestClient() {}\n",
        )
        .unwrap();
        fs::write(
            temp_workspace.join("src/test/java/AppTest.java"),
            "public class AppTest { public void runs() {} }\n",
        )
        .unwrap();
        fs::write(temp_workspace.join("vendor/lib.rs"), "fn vendored() {}\n").unwrap();
        fs::write(
            temp_workspace.join("target/generated.rs"),
            "fn generated() {}\n",
        )
        .unwrap();

        run_init(&temp_workspace, false).unwrap();

        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        let conn = Connection::open(&db_path).unwrap();
        let files_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM files", [], |row| row.get(0))
            .unwrap();
        assert_eq!(files_count, 4, "library directories should be skipped");

        let test_files: i64 = conn
            .query_row("SELECT COUNT(*) FROM files WHERE is_test = 1", [], |row| {
                row.get(0)
            })
            .unwrap();
        assert_eq!(test_files, 3);

        let test_nodes: i64 = conn
            .query_row("SELECT COUNT(*) FROM nodes WHERE is_test = 1", [], |row| {
                row.get(0)
            })
            .unwrap();
        assert!(test_nodes >= 3);

        run_init(&temp_workspace, true).unwrap();
        let library_files: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM files WHERE file_path IN ('vendor/lib.rs', 'target/generated.rs')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(
            library_files, 2,
            "--include-library should index library dirs"
        );

        fs::remove_dir_all(&temp_workspace).unwrap();
    }

    #[test]
    fn test_fresh_init_rebuilds_fts_index() {
        let temp_workspace = create_temp_dir();
        let src_dir = temp_workspace.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(
            src_dir.join("main.rs"),
            "/// Performs a searchable calibration.\nfn calibrate() {}\n",
        )
        .unwrap();

        run_init(&temp_workspace, false).unwrap();

        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        let conn = Connection::open(&db_path).unwrap();
        let fts_results = db::search_nodes_fts(&conn, "calibration").unwrap();
        assert_eq!(fts_results.len(), 1);
        assert_eq!(fts_results[0].name, "calibrate");

        fs::remove_dir_all(&temp_workspace).unwrap();
    }

    #[test]
    fn test_incremental_sync_keeps_fts_triggers_after_fresh_rebuild() {
        let temp_workspace = create_temp_dir();
        let src_dir = temp_workspace.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        let rust_file = src_dir.join("main.rs");
        fs::write(
            &rust_file,
            "/// Mentions the original marker.\nfn searchable() {}\n",
        )
        .unwrap();

        run_init(&temp_workspace, false).unwrap();

        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        let conn = Connection::open(&db_path).unwrap();
        assert_eq!(db::search_nodes_fts(&conn, "original").unwrap().len(), 1);

        fs::write(
            &rust_file,
            "/// Mentions the replacement marker.\nfn searchable() {}\n",
        )
        .unwrap();
        run_init(&temp_workspace, false).unwrap();

        assert_eq!(db::search_nodes_fts(&conn, "replacement").unwrap().len(), 1);
        assert!(
            db::search_nodes_fts(&conn, "original").unwrap().is_empty(),
            "updated file should remove stale FTS content"
        );

        fs::remove_file(&rust_file).unwrap();
        run_init(&temp_workspace, false).unwrap();

        assert!(
            db::search_nodes_fts(&conn, "replacement")
                .unwrap()
                .is_empty(),
            "deleted file should remove FTS content"
        );

        fs::remove_dir_all(&temp_workspace).unwrap();
    }

    #[test]
    fn test_incremental_sync_re_resolves_unmodified_incoming_callers() {
        let temp_workspace = create_temp_dir();
        let src_dir = temp_workspace.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        let caller_file = src_dir.join("a.rs");
        let old_target_file = src_dir.join("b.rs");
        let new_target_file = src_dir.join("c.rs");
        fs::write(
            &caller_file,
            "fn caller() {\n    target();\n    local_keep();\n}\nfn local_keep() {}\n",
        )
        .unwrap();
        fs::write(&old_target_file, "fn target() {}\n").unwrap();

        run_init(&temp_workspace, false).unwrap();

        fs::write(&old_target_file, "fn other() {}\n").unwrap();
        fs::write(&new_target_file, "fn target() {}\n").unwrap();
        run_init(&temp_workspace, false).unwrap();

        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        let conn = Connection::open(&db_path).unwrap();
        let moved_edge: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM edges
                 WHERE source_nid = (SELECT nid FROM nodes WHERE id = 'src/a.rs::caller')
                   AND target_nid = (SELECT nid FROM nodes WHERE id = 'src/c.rs::target')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(
            moved_edge, 1,
            "unmodified caller should point at new target"
        );

        let stale_edge: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM edges
                 WHERE source_nid = (SELECT nid FROM nodes WHERE id = 'src/a.rs::caller')
                   AND target_nid = (SELECT nid FROM nodes WHERE id = 'src/b.rs::target')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(stale_edge, 0, "stale target edge should be removed");

        let preserved_edge: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM edges
                 WHERE source_nid = (SELECT nid FROM nodes WHERE id = 'src/a.rs::caller')
                   AND target_nid = (SELECT nid FROM nodes WHERE id = 'src/a.rs::local_keep')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(
            preserved_edge, 1,
            "other edges from the source are reinserted"
        );

        fs::remove_dir_all(&temp_workspace).unwrap();
    }

    #[test]
    fn test_incremental_sync_re_resolves_matching_unresolved_refs() {
        let temp_workspace = create_temp_dir();
        let src_dir = temp_workspace.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(src_dir.join("a.rs"), "fn caller() {\n    missing();\n}\n").unwrap();

        run_init(&temp_workspace, false).unwrap();

        let db_path = temp_workspace.join(".ochna").join("ochna.db");
        let conn = Connection::open(&db_path).unwrap();
        let unresolved_before: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM unresolved_refs WHERE specifier = 'missing'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(unresolved_before, 1);

        fs::write(src_dir.join("b.rs"), "fn missing() {}\n").unwrap();
        run_init(&temp_workspace, false).unwrap();

        let resolved_edge: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM edges
                 WHERE source_nid = (SELECT nid FROM nodes WHERE id = 'src/a.rs::caller')
                   AND target_nid = (SELECT nid FROM nodes WHERE id = 'src/b.rs::missing')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(resolved_edge, 1);

        let unresolved_after: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM unresolved_refs WHERE specifier = 'missing'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(unresolved_after, 0);

        fs::remove_dir_all(&temp_workspace).unwrap();
    }
}