gobby-code 1.3.2

Fast Rust CLI for Gobby's code index — AST-aware search, symbol navigation, and dependency graph
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
use super::support::*;
use super::*;

#[test]
fn clusters_modules_from_graph() {
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec![
            "src/api/handler.rs".to_string(),
            "src/api/inner/router.rs".to_string(),
            "src/domain/service.rs".to_string(),
            "tests/domain/service_test.rs".to_string(),
            "vendor/generated/client.rs".to_string(),
        ],
        graph_edges: vec![
            // Same subsystem root (src/api): clusters to the common module.
            CodewikiGraphEdge::call(
                test_component_id("src/api/handler.rs", "handle", "function"),
                test_component_id("src/api/inner/router.rs", "route", "function"),
            ),
            // Cross-root (src/api -> src/domain): must not collapse the
            // decomposition to the shared `src` container.
            CodewikiGraphEdge::call(
                test_component_id("src/api/handler.rs", "handle", "function"),
                test_component_id("src/domain/service.rs", "Service", "class"),
            ),
        ],
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![
            test_symbol(
                "src/api/handler.rs",
                "handle",
                "function",
                1,
                "pub fn handle()",
            ),
            test_symbol(
                "src/api/inner/router.rs",
                "route",
                "function",
                1,
                "pub fn route()",
            ),
            test_symbol(
                "src/domain/service.rs",
                "Service",
                "class",
                1,
                "pub struct Service;",
            ),
            test_symbol_with_qualified(
                "src/domain/service.rs",
                "new",
                "Service::new",
                "function",
                3,
                "pub fn new() -> Self",
            ),
            test_symbol(
                "tests/domain/service_test.rs",
                "service_test",
                "function",
                1,
                "fn service_test()",
            ),
            test_symbol(
                "vendor/generated/client.rs",
                "GeneratedClient",
                "class",
                1,
                "pub struct GeneratedClient;",
            ),
        ],
    };

    let docs = generate_hierarchical_docs(&input, None);
    let docs_by_path = docs.into_iter().collect::<BTreeMap<_, _>>();

    // The container module lists its children; the cross-root call edge did
    // not pull either file up to `src` directly.
    let container = docs_by_path
        .get("code/modules/src.md")
        .expect("container module is documented");
    assert!(container.contains("[[code/modules/src/api\\|src/api]]"));
    assert!(container.contains("[[code/modules/src/domain\\|src/domain]]"));
    assert!(!container.contains("[[code/files/src/api/handler.rs\\|src/api/handler.rs]]"));

    // Same-root call-connected files cluster to their common module.
    let api = docs_by_path
        .get("code/modules/src/api.md")
        .expect("same-root cluster module is documented");
    assert!(api.contains("[[code/files/src/api/handler.rs\\|src/api/handler.rs]]"));
    assert!(api.contains("[[code/files/src/api/inner/router.rs\\|src/api/inner/router.rs]]"));

    let domain = docs_by_path
        .get("code/modules/src/domain.md")
        .expect("cross-root file keeps its own module");
    assert!(domain.contains("[[code/files/src/domain/service.rs\\|src/domain/service.rs]]"));

    assert!(!docs_by_path.contains_key("code/files/tests/domain/service_test.rs.md"));
    assert!(!docs_by_path.contains_key("code/files/vendor/generated/client.rs.md"));
}

#[test]
fn file_root_detection_breaks_parent_cycles() {
    let mut parents = HashMap::from([
        ("c.rs".to_string(), "b.rs".to_string()),
        ("b.rs".to_string(), "a.rs".to_string()),
        ("a.rs".to_string(), "b.rs".to_string()),
    ]);

    let root = find_file_root(&mut parents, "c.rs");

    assert_eq!(root, "a.rs");
    assert_eq!(parents.get("a.rs").map(String::as_str), Some("a.rs"));
    assert_eq!(parents.get("b.rs").map(String::as_str), Some("a.rs"));
    assert_eq!(parents.get("c.rs").map(String::as_str), Some("a.rs"));
}

#[test]
fn common_module_for_empty_files_is_root() {
    assert_eq!(common_module_for_files(&[]), "");
}

#[test]
fn module_depth_counts_only_non_empty_segments() {
    assert_eq!(module_depth(""), 0);
    assert_eq!(module_depth("/"), 0);
    assert_eq!(module_depth("src"), 1);
    assert_eq!(module_depth("src/commands/"), 2);
}

#[test]
fn core_file_filter_excludes_specs_mocks_and_test_prefixes() {
    for file in [
        "src/test_parser.rs",
        "src/parser_spec.rs",
        "src/parser.spec.rs",
        "src/__mocks__/client.rs",
        "src/mocks/client.rs",
    ] {
        assert!(!is_core_file(file), "{file} should be filtered out");
    }

    assert!(is_core_file("src/parser.rs"));
}

#[test]
fn core_file_filter_excludes_hidden_metadata_paths() {
    for file in [
        "gobby-wiki/code/files/crates/gcode/src/cli.rs.md",
        ".gobby/plans/goal.md",
        ".github/workflows/ci.yml",
        ".claude/settings.json",
        ".gitignore",
    ] {
        assert!(!is_core_file(file), "{file} should be filtered out");
    }

    assert!(is_core_file("docs/guides/codewiki.md"));
}

#[test]
fn import_targets_match_exact_path_or_module_components() {
    let files = vec![
        "src/domain/service.rs".to_string(),
        "src/domain/service_extra.rs".to_string(),
        "src/domain_extra/service.rs".to_string(),
        "crates/app/src/domain/mod.rs".to_string(),
        "crates/app/src/application/use_case.rs".to_string(),
    ];

    assert_eq!(
        files_for_import_target(&files, "domain.service"),
        vec!["src/domain/service.rs"]
    );
    assert_eq!(
        files_for_import_target(&files, "domain"),
        vec![
            "src/domain/service.rs",
            "src/domain/service_extra.rs",
            "crates/app/src/domain/mod.rs"
        ]
    );
    assert!(files_for_import_target(&files, "main.service").is_empty());
}

#[test]
fn graph_queries_use_requested_edge_limit() {
    let (call_query, _) = codewiki_call_edges_query("project-1", 17);
    let (import_query, _) = codewiki_import_edges_query("project-1", 17);

    assert!(call_query.contains("LIMIT 17"));
    assert!(import_query.contains("LIMIT 17"));
}

#[test]
fn import_edges_drop_non_core_source_files() {
    let file_symbols = BTreeMap::from([
        ("src/api.rs".to_string(), vec!["comp-api".to_string()]),
        ("src/domain.rs".to_string(), vec!["comp-domain".to_string()]),
        (
            "tests/api_test.rs".to_string(),
            vec!["comp-test".to_string()],
        ),
    ]);
    let core_files = vec!["src/api.rs".to_string(), "src/domain.rs".to_string()];
    let pairs = vec![
        ("tests/api_test.rs".to_string(), "domain".to_string()),
        ("src/api.rs".to_string(), "domain".to_string()),
    ];

    let edges = import_edges_from_pairs(&pairs, &core_files, &file_symbols);

    assert_eq!(
        edges,
        vec![CodewikiGraphEdge::import("comp-api", "comp-domain")]
    );
}

#[test]
fn graph_queries_stay_small_and_carry_no_id_lists() {
    // Embedding the core symbol-id/file lists in the Cypher text produced
    // ~633KB payloads on this repo, which intermittently failed at the socket
    // layer; core filtering is client-side now.
    let (call_query, _) = codewiki_call_edges_query("project-1", 5000);
    let (import_query, _) = codewiki_import_edges_query("project-1", 5000);

    assert!(!call_query.contains(" IN ["));
    assert!(!import_query.contains(" IN ["));
    assert!(call_query.len() < 1024);
    assert!(import_query.len() < 1024);
}

#[test]
fn edge_limit_validation_rejects_zero_and_excessive_limits() {
    assert!(validate_edge_limit(1).is_ok());
    assert!(validate_edge_limit(MAX_EDGE_LIMIT).is_ok());
    assert!(validate_edge_limit(0).is_err());

    let error = validate_edge_limit(MAX_EDGE_LIMIT + 1).expect_err("limit above cap fails");
    assert!(error.to_string().contains("codewiki --edge-limit"));
}

#[test]
fn clusters_without_falkordb() {
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec![
            "src/api/handler.rs".to_string(),
            "src/domain/service.rs".to_string(),
            "tests/domain/service_test.rs".to_string(),
        ],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Unavailable,
        symbols: vec![
            test_symbol(
                "src/api/handler.rs",
                "handle",
                "function",
                1,
                "pub fn handle()",
            ),
            test_symbol(
                "src/domain/service.rs",
                "Service",
                "class",
                1,
                "pub struct Service;",
            ),
            test_symbol_with_qualified(
                "src/domain/service.rs",
                "new",
                "Service::new",
                "function",
                3,
                "pub fn new() -> Self",
            ),
            test_symbol(
                "tests/domain/service_test.rs",
                "service_test",
                "function",
                1,
                "fn service_test()",
            ),
        ],
    };

    let docs = generate_hierarchical_docs(&input, None);
    let docs_by_path = docs.into_iter().collect::<BTreeMap<_, _>>();

    assert!(docs_by_path.contains_key("code/modules/src/api.md"));
    assert!(docs_by_path.contains_key("code/modules/src/domain.md"));
    assert!(!docs_by_path.contains_key("code/files/tests/domain/service_test.rs.md"));
    assert!(
        docs_by_path
            .get("code/files/src/api/handler.rs.md")
            .expect("handler file doc")
            .contains("| `handle` | function |")
    );
    assert!(
        docs_by_path
            .get("code/files/src/domain/service.rs.md")
            .expect("service file doc")
            .contains("| `Service` | class |")
    );
    assert!(
        docs_by_path
            .get("code/files/src/domain/service.rs.md")
            .expect("service file doc")
            .contains("| `Service::new` | function |")
    );
    assert!(
        !docs_by_path
            .get("code/files/src/domain/service.rs.md")
            .expect("service file doc")
            .contains("src/domain/service.rs::Service::new")
    );
}

#[test]
fn module_pages_no_longer_emit_mermaid_diagrams() {
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec![
            "src/api/handler.rs".to_string(),
            "src/domain/service.rs".to_string(),
            "src/storage/repo.rs".to_string(),
            "src/unrelated/tool.rs".to_string(),
        ],
        graph_edges: vec![
            CodewikiGraphEdge::import(
                test_component_id("src/api/handler.rs", "handle", "function"),
                test_component_id("src/domain/service.rs", "Service", "class"),
            ),
            CodewikiGraphEdge::import(
                test_component_id("src/domain/service.rs", "Service", "class"),
                test_component_id("src/storage/repo.rs", "Repo", "class"),
            ),
            CodewikiGraphEdge::import(
                test_component_id("src/unrelated/tool.rs", "Tool", "class"),
                test_component_id("src/storage/repo.rs", "Repo", "class"),
            ),
        ],
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![
            test_symbol(
                "src/api/handler.rs",
                "handle",
                "function",
                1,
                "pub fn handle()",
            ),
            test_symbol(
                "src/domain/service.rs",
                "Service",
                "class",
                1,
                "pub struct Service;",
            ),
            test_symbol(
                "src/storage/repo.rs",
                "Repo",
                "class",
                1,
                "pub struct Repo;",
            ),
            test_symbol(
                "src/unrelated/tool.rs",
                "Tool",
                "class",
                1,
                "pub struct Tool;",
            ),
        ],
    };

    let docs = generate_hierarchical_docs(&input, None);
    let docs_by_path = docs.into_iter().collect::<BTreeMap<_, _>>();
    let rendered = docs_by_path
        .get("code/modules/src/api.md")
        .expect("api module doc");

    // The auto-generated code-graph diagrams are gone; graph availability is
    // informational only and never degrades the page.
    assert!(!rendered.contains("```mermaid"));
    assert!(!rendered.contains("## Dependency Diagram"));
    assert!(!rendered.contains("## Call Diagram"));
    assert!(!rendered.contains("graph-truncated"));
    assert!(!rendered.contains("graph-unavailable"));
}

#[test]
fn module_page_does_not_degrade_without_falkordb() {
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/api/handler.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Unavailable,
        symbols: vec![test_symbol(
            "src/api/handler.rs",
            "handle",
            "function",
            1,
            "pub fn handle()",
        )],
    };

    let docs = generate_hierarchical_docs(&input, None);
    let docs_by_path = docs.into_iter().collect::<BTreeMap<_, _>>();
    let module = docs_by_path
        .get("code/modules/src/api.md")
        .expect("module doc still renders");
    let file = docs_by_path
        .get("code/files/src/api/handler.rs.md")
        .expect("file doc still renders");

    // Graph unavailability is informational only: it never marks a module page
    // degraded and never emits a diagram section.
    assert!(!module.contains("degraded: graph-unavailable"));
    assert!(!module.contains("graph-unavailable"));
    assert!(!module.contains("```mermaid"));
    // File pages render a human Reference table keyed by symbol name, not a
    // UUID component-id dump (#871).
    assert!(file.contains("## Reference"));
    assert!(file.contains("| `handle` | function |"));
    assert!(!file.contains(&test_component_id(
        "src/api/handler.rs",
        "handle",
        "function"
    )));
}

#[test]
fn empty_available_graph_does_not_emit_degradation_marker() {
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec!["src/api/handler.rs".to_string()],
        graph_edges: Vec::new(),
        graph_availability: CodewikiGraphAvailability::Available,
        symbols: vec![test_symbol(
            "src/api/handler.rs",
            "handle",
            "function",
            1,
            "pub fn handle()",
        )],
    };

    let docs = generate_hierarchical_docs(&input, None);
    let docs_by_path = docs.into_iter().collect::<BTreeMap<_, _>>();
    let module = docs_by_path
        .get("code/modules/src/api.md")
        .expect("module doc still renders");

    assert!(!module.contains("degraded: graph-unavailable"));
}

#[test]
fn truncated_graph_does_not_degrade_module_or_emit_diagram() {
    let input = CodewikiInput {
        leading_chunks: std::collections::BTreeMap::new(),
        files: vec![
            "src/api/handler.rs".to_string(),
            "src/domain/service.rs".to_string(),
        ],
        graph_edges: vec![CodewikiGraphEdge::import(
            test_component_id("src/api/handler.rs", "handle", "function"),
            test_component_id("src/domain/service.rs", "Service", "class"),
        )],
        graph_availability: CodewikiGraphAvailability::Truncated,
        symbols: vec![
            test_symbol(
                "src/api/handler.rs",
                "handle",
                "function",
                1,
                "pub fn handle()",
            ),
            test_symbol(
                "src/domain/service.rs",
                "Service",
                "class",
                1,
                "pub struct Service;",
            ),
        ],
    };

    let docs = generate_hierarchical_docs(&input, None);
    let docs_by_path = docs.into_iter().collect::<BTreeMap<_, _>>();
    let module = docs_by_path
        .get("code/modules/src/api.md")
        .expect("module doc still renders");

    // A truncated graph is informational only: the page is not degraded and no
    // diagram (or "simplified diagram" note) is emitted.
    assert!(!module.contains("graph-truncated"));
    assert!(!module.contains("Simplified diagram"));
    assert!(!module.contains("```mermaid"));
    assert!(!module.contains("## Dependency Diagram"));
}