ferrograph 1.4.0

Graph-powered Rust code intelligence
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
//! CLI output tests: help, index, status, query.

use std::process::Command;

fn ferrograph_cmd() -> Command {
    Command::new(env!("CARGO_BIN_EXE_ferrograph"))
}

fn fixture_path(name: &str) -> std::path::PathBuf {
    std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join(name)
}

/// Parse node count from `status` stdout (line containing "Nodes (N total)").
fn parse_node_count(stdout: &str) -> u32 {
    stdout
        .lines()
        .find(|l| l.contains("Nodes ("))
        .and_then(|l| {
            // Extract number from "  Nodes (42 total):"
            let start = l.find('(')? + 1;
            let end = l.find(" total")?;
            l[start..end].trim().parse::<u32>().ok()
        })
        .expect("failed to parse node count from status output")
}

#[test]
fn cli_help() {
    let out = ferrograph_cmd().arg("--help").output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    for sub in ["index", "query", "status", "search", "watch", "mcp"] {
        assert!(
            stdout.contains(sub),
            "help should list subcommand '{sub}', got: {stdout}"
        );
    }
}

#[test]
fn cli_index_help() {
    let out = ferrograph_cmd().args(["index", "--help"]).output().unwrap();
    assert!(out.status.success());
}

#[test]
fn cli_index_and_status_and_query() {
    let fixture = fixture_path("single_crate");
    assert!(
        fixture.exists(),
        "fixture missing: {} (run from repo root)",
        fixture.display()
    );
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join(".ferrograph");

    let out = ferrograph_cmd()
        .args([
            "index",
            "--output",
            db_path.to_str().unwrap(),
            fixture.to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "index failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let out = ferrograph_cmd()
        .args(["status", dir.path().to_str().unwrap()])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "status failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Nodes ("),
        "status should report nodes: {stdout}"
    );
    assert!(
        stdout.contains("Edges ("),
        "status should report edges: {stdout}"
    );
    let node_count = parse_node_count(&stdout);
    assert!(
        node_count > 0,
        "expected node count > 0, got {node_count}; stdout: {stdout}"
    );

    let out = ferrograph_cmd()
        .args([
            "query",
            "--db",
            db_path.to_str().unwrap(),
            "?[id, type, payload] := *nodes[id, type, payload]",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "query failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        !stdout.trim().is_empty(),
        "query should return rows: {stdout}"
    );
    assert!(
        stdout.contains("greet") || stdout.contains("main"),
        "query should return expected node payload: {stdout}"
    );
}

#[test]
fn cli_search_after_index() {
    let fixture = fixture_path("single_crate");
    assert!(fixture.exists(), "fixture missing: {}", fixture.display());
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join(".ferrograph");
    let out = ferrograph_cmd()
        .args([
            "index",
            "--output",
            db_path.to_str().unwrap(),
            fixture.to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "index failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let out = ferrograph_cmd()
        .args(["search", "--db", db_path.to_str().unwrap(), "greet"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "search failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("greet"),
        "search for 'greet' should return matching row: {stdout}"
    );
}

#[test]
fn cli_persistent_reopen() {
    let fixture = fixture_path("single_crate");
    assert!(
        fixture.exists(),
        "fixture missing: {} (run from repo root)",
        fixture.display()
    );
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join(".ferrograph");

    let out = ferrograph_cmd()
        .args([
            "index",
            "--output",
            db_path.to_str().unwrap(),
            fixture.to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "index failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let out1 = ferrograph_cmd()
        .args(["status", db_path.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(out1.status.success(), "status (first open) failed");
    let count1 = parse_node_count(&String::from_utf8_lossy(&out1.stdout));

    let out2 = ferrograph_cmd()
        .args(["status", db_path.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(out2.status.success(), "status (reopen) failed");
    let count2 = parse_node_count(&String::from_utf8_lossy(&out2.stdout));

    assert_eq!(
        count1, count2,
        "node count should be unchanged after reopen"
    );
    assert!(count1 > 0, "expected nodes after index");
}

/// Index the `single_crate` fixture into a temp DB and return `(db_path, tempdir)`.
/// The tempdir is returned so the caller keeps it alive for the test's duration.
fn index_fixture() -> (std::path::PathBuf, tempfile::TempDir) {
    let fixture = fixture_path("single_crate");
    assert!(fixture.exists(), "fixture missing: {}", fixture.display());
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join(".ferrograph");
    let out = ferrograph_cmd()
        .args([
            "index",
            "--output",
            db_path.to_str().unwrap(),
            fixture.to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "index failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    (db_path, dir)
}

#[test]
fn cli_dead() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args(["dead", "--db", db_path.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "dead failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("private_unused"),
        "dead should include private_unused: {stdout}"
    );
    assert!(
        stdout.contains("dead nodes found"),
        "dead should print summary line: {stdout}"
    );
    assert!(
        stdout.contains("dynamic dispatch"),
        "dead should print dynamic dispatch caveat: {stdout}"
    );
}

#[test]
fn cli_dead_json() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args(["--json", "dead", "--db", db_path.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "dead --json failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    let parsed: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("dead --json output is not valid JSON: {e}\n{stdout}"));
    assert!(
        parsed["count"].as_u64().unwrap_or(0) > 0,
        "dead --json should report count > 0: {stdout}"
    );
    assert!(
        parsed["dead_nodes"].is_array(),
        "dead --json should have dead_nodes array: {stdout}"
    );
    assert!(
        parsed["caveat"].is_string(),
        "dead --json should include caveat: {stdout}"
    );
}

#[test]
fn cli_blast() {
    let (db_path, _dir) = index_fixture();
    // use_add calls utils::add, so blast radius from use_add should include something
    let out = ferrograph_cmd()
        .args(["search", "--db", db_path.to_str().unwrap(), "pub::use_add"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    let node_id = stdout.lines().next().unwrap().split('\t').next().unwrap();

    let out = ferrograph_cmd()
        .args(["blast", "--db", db_path.to_str().unwrap(), node_id])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "blast failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("blast radius"),
        "blast should print summary: {stdout}"
    );
}

#[test]
fn cli_info() {
    let (db_path, _dir) = index_fixture();
    // Search for greet function to get a node ID
    let out = ferrograph_cmd()
        .args(["search", "--db", db_path.to_str().unwrap(), "pub::greet"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    let node_id = stdout.lines().next().unwrap().split('\t').next().unwrap();

    let out = ferrograph_cmd()
        .args(["info", "--db", db_path.to_str().unwrap(), node_id])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "info failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("greet"),
        "info should show node payload: {stdout}"
    );
}

#[test]
fn cli_info_not_found() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args(["info", "--db", db_path.to_str().unwrap(), "nonexistent#0:0"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("not found"),
        "info for missing node should say not found: {stdout}"
    );
}

#[test]
fn cli_modules() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args(["modules", "--db", db_path.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "modules failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("containment edges"),
        "modules should print summary: {stdout}"
    );
}

#[test]
fn cli_traits() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args(["traits", "--db", db_path.to_str().unwrap(), "Draw"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "traits failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("implementors of"),
        "traits should print summary: {stdout}"
    );
}

#[test]
fn cli_callers() {
    let (db_path, _dir) = index_fixture();
    // Find the add function (called by use_add)
    let out = ferrograph_cmd()
        .args(["search", "--db", db_path.to_str().unwrap(), "pub::add"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    let node_id = stdout.lines().next().unwrap().split('\t').next().unwrap();

    let out = ferrograph_cmd()
        .args(["callers", "--db", db_path.to_str().unwrap(), node_id])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "callers failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("callers of"),
        "callers should print summary: {stdout}"
    );
}

#[test]
fn cli_status_json() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args([
            "--json",
            "status",
            db_path.parent().unwrap().to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "status --json failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    let parsed: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("status --json output is not valid JSON: {e}\n{stdout}"));
    assert!(
        parsed["node_count"].as_u64().unwrap_or(0) > 0,
        "status --json should report node_count > 0: {stdout}"
    );
    assert!(
        parsed["nodes_by_type"].is_array(),
        "status --json should have nodes_by_type: {stdout}"
    );
    assert!(
        parsed["edges_by_type"].is_array(),
        "status --json should have edges_by_type: {stdout}"
    );
}

#[test]
fn cli_query_json() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args([
            "--json",
            "query",
            "--db",
            db_path.to_str().unwrap(),
            "?[n] := n in [1, 2, 3]",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "query --json failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    let parsed: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("query --json output is not valid JSON: {e}\n{stdout}"));
    let rows = parsed["rows"].as_array().expect("rows should be array");
    assert_eq!(rows.len(), 3, "should have 3 rows");
    // Values should be numeric, not stringified
    assert!(
        rows[0][0].is_number(),
        "query --json should preserve numeric types, got: {}",
        rows[0][0]
    );
}

#[test]
fn cli_search_json() {
    let (db_path, _dir) = index_fixture();
    let out = ferrograph_cmd()
        .args([
            "--json",
            "search",
            "--db",
            db_path.to_str().unwrap(),
            "greet",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "search --json failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    let parsed: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("search --json output is not valid JSON: {e}\n{stdout}"));
    assert!(
        parsed["count"].as_u64().unwrap_or(0) > 0,
        "search --json should find results: {stdout}"
    );
    assert!(
        parsed["results"].is_array(),
        "search --json should have results array: {stdout}"
    );
}

#[test]
fn cli_help_lists_new_subcommands() {
    let out = ferrograph_cmd().arg("--help").output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    for sub in ["dead", "blast", "callers", "info", "modules", "traits"] {
        assert!(
            stdout.contains(sub),
            "help should list new subcommand '{sub}', got: {stdout}"
        );
    }
    assert!(
        stdout.contains("--json"),
        "help should list --json flag: {stdout}"
    );
}

#[test]
fn cli_index_nonexistent_path_fails() {
    let out = ferrograph_cmd()
        .args(["index", "/nonexistent/path/xyz"])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "index of nonexistent path should fail"
    );
}

#[test]
fn cli_query_invalid_datalog_fails() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join(".ferrograph");
    // Create empty db so we open it, then run invalid query
    let store = ferrograph::graph::Store::new_persistent(&db_path).unwrap();
    drop(store);
    let out = ferrograph_cmd()
        .args([
            "query",
            "--db",
            db_path.to_str().unwrap(),
            "?[x] := *nodes[(",
        ])
        .output()
        .unwrap();
    assert!(!out.status.success(), "invalid Datalog should fail");
}