code-graph-cli 3.0.2

Code intelligence engine for TypeScript/JavaScript/Rust/Python/Go — query the dependency graph instead of reading source files.
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/// Integration test suite — dogfoods code-graph's own Rust source as the test fixture.
///
/// All tests invoke the compiled `code-graph` binary via subprocess. The `CARGO_BIN_EXE_code-graph`
/// environment variable is automatically set by Cargo during `cargo test` to point to the compiled
/// binary for the current profile (debug or release).
///
/// MCP coverage strategy:
/// All 6 MCP tools (find_symbol, find_references, get_impact, detect_circular, get_context,
/// get_stats) call the same query functions as the CLI commands tested below. The MCP-specific
/// layer (resolve_graph caching, format_*_to_string serialization, suggest_similar error formatting)
/// is covered implicitly because:
///   - CLI tests exercise the same query functions (find_symbol, find_refs, blast_radius,
///     find_circular, symbol_context, project_stats)
///   - JSON format tests (test_find_json_output, test_index_json_output) exercise the same
///     serialization path that MCP tools use
///   - graph building (build_graph / resolve_graph) is exercised by every CLI test
///
/// Writing a full MCP stdio JSON-RPC integration test would require a mock client and async
/// process management; the CLI tests provide equivalent regression coverage for significantly
/// less complexity.
use std::path::PathBuf;
use std::process::Command;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn binary() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_code-graph"))
}

fn project_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

/// Run a code-graph command and assert it exits successfully.
/// Returns stdout as a String.
fn run_success(args: &[&str]) -> String {
    let out = Command::new(binary())
        .args(args)
        .output()
        .expect("failed to invoke code-graph binary");
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        out.status.success(),
        "command {:?} failed with status {:?}\nstdout: {}\nstderr: {}",
        args,
        out.status,
        stdout,
        stderr
    );
    stdout
}

/// Run a code-graph command and assert it exits with a non-zero status.
/// Returns (stdout, stderr) as Strings.
fn run_failure(args: &[&str]) -> (String, String) {
    let out = Command::new(binary())
        .args(args)
        .output()
        .expect("failed to invoke code-graph binary");
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        !out.status.success(),
        "command {:?} expected to fail but exited successfully\nstdout: {}\nstderr: {}",
        args,
        stdout,
        stderr
    );
    (stdout, stderr)
}

// ---------------------------------------------------------------------------
// Task 1: CLI command integration tests on code-graph's own Rust source
// ---------------------------------------------------------------------------

/// test_index_rust_codebase — index command produces non-empty output with file information.
#[test]
fn test_index_rust_codebase() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["index", path]);
    // Output format: "Indexed N files in X.XXs"
    assert!(
        stdout.contains("files"),
        "index output should contain 'files'\nstdout: {}",
        stdout
    );
}

/// test_index_json_output — index --json produces valid JSON with file_count > 0.
#[test]
fn test_index_json_output() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["index", "--json", path]);
    let parsed: serde_json::Value =
        serde_json::from_str(&stdout).expect("index --json output is not valid JSON");
    let file_count = parsed["file_count"]
        .as_u64()
        .expect("JSON missing 'file_count' field");
    assert!(
        file_count > 0,
        "file_count should be > 0, got {}",
        file_count
    );
    // Rust source project — rust_file_count should be positive
    let rust_file_count = parsed["rust_file_count"]
        .as_u64()
        .expect("JSON missing 'rust_file_count' field");
    assert!(
        rust_file_count > 0,
        "rust_file_count should be > 0 for code-graph own source"
    );
}

/// test_find_rust_symbol — find a known Rust function by exact name.
#[test]
fn test_find_rust_symbol() {
    let root = project_root();
    let path = root.to_str().unwrap();
    // parse_file_parallel is a well-known function in code-graph's parser
    let stdout = run_success(&["find", "parse_file_parallel", path]);
    assert!(
        stdout.contains("def"),
        "find output should contain 'def' prefix\nstdout: {}",
        stdout
    );
    assert!(
        stdout.contains("parse_file_parallel"),
        "find output should contain the symbol name\nstdout: {}",
        stdout
    );
    // At least one result line
    let def_count = stdout.lines().filter(|l| l.contains("def")).count();
    assert!(def_count > 0, "should have at least one 'def' result line");
}

/// test_find_regex_pattern — find with a regex matching multiple symbols.
#[test]
fn test_find_regex_pattern() {
    let root = project_root();
    let path = root.to_str().unwrap();
    // ".*parse.*" should match many functions in the parser module
    let stdout = run_success(&["find", ".*parse.*", path]);
    let def_count = stdout.lines().filter(|l| l.starts_with("def")).count();
    assert!(
        def_count > 1,
        "regex '.*parse.*' should match multiple symbols, got {} def lines\nstdout: {}",
        def_count,
        stdout
    );
}

/// test_find_nonexistent_symbol — finding a symbol that doesn't exist exits non-zero.
#[test]
fn test_find_nonexistent_symbol() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let (_, stderr) = run_failure(&["find", "zzz_nonexistent_symbol_zzz", path]);
    assert!(
        stderr.contains("no symbols matching") || stderr.contains("No"),
        "stderr should indicate symbol not found\nstderr: {}",
        stderr
    );
}

/// test_refs_rust_symbol — refs on a known symbol produces non-empty output.
#[test]
fn test_refs_rust_symbol() {
    let root = project_root();
    let path = root.to_str().unwrap();
    // parse_file_parallel is imported from multiple modules in code-graph's source,
    // so it reliably has reference edges (RustImport) in the graph.
    let stdout = run_success(&["refs", "parse_file_parallel", path]);
    assert!(
        !stdout.trim().is_empty(),
        "refs output should be non-empty for 'parse_file_parallel'\nstdout: {}",
        stdout
    );
    // Should contain at least one "ref" line
    let ref_count = stdout
        .lines()
        .filter(|l| l.starts_with("ref") || l.contains("ref"))
        .count();
    assert!(
        ref_count > 0,
        "refs output should contain at least one 'ref' line\nstdout: {}",
        stdout
    );
}

/// test_impact_rust_symbol — impact on a known symbol produces non-empty output with "impact" lines.
#[test]
fn test_impact_rust_symbol() {
    let root = project_root();
    let path = root.to_str().unwrap();
    // parse_file_parallel is imported from multiple modules — it has a reliable blast radius.
    let stdout = run_success(&["impact", "parse_file_parallel", path]);
    assert!(
        !stdout.trim().is_empty(),
        "impact output should be non-empty for 'parse_file_parallel'\nstdout: {}",
        stdout
    );
    // Impact output contains "impact" prefix lines or summary
    let has_impact_line = stdout
        .lines()
        .any(|l| l.contains("impact") || l.contains("file"));
    assert!(
        has_impact_line,
        "impact output should contain 'impact' or 'file' lines\nstdout: {}",
        stdout
    );
}

/// test_circular_on_rust_codebase — circular command doesn't crash on code-graph's own source.
/// Output may be "no circular dependencies found" or contain cycle data — both are valid.
#[test]
fn test_circular_on_rust_codebase() {
    let root = project_root();
    let path = root.to_str().unwrap();
    // Just verify it exits 0 and produces some output (not a crash)
    let stdout = run_success(&["circular", path]);
    // Either "no circular dependencies found" or actual cycle data — both acceptable
    // We only care it doesn't crash — exit 0 is checked by run_success
    let _ = stdout;
}

/// test_stats_rust_codebase — stats command produces output mentioning "Rust".
#[test]
fn test_stats_rust_codebase() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["stats", path]);
    assert!(
        stdout.contains("Rust"),
        "stats output should contain 'Rust' section for code-graph own source\nstdout: {}",
        stdout
    );
}

/// test_context_rust_symbol — context command produces non-empty output containing the symbol name.
#[test]
fn test_context_rust_symbol() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["context", "build_graph", path]);
    assert!(
        !stdout.trim().is_empty(),
        "context output should be non-empty\nstdout: {}",
        stdout
    );
    assert!(
        stdout.contains("build_graph"),
        "context output should contain the symbol name\nstdout: {}",
        stdout
    );
}

/// test_language_filter_rust — --language rust restricts results to .rs files only.
#[test]
fn test_language_filter_rust() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["find", ".*", "--language", "rust", path]);
    assert!(
        stdout.contains("def"),
        "filtered find output should contain 'def' lines\nstdout: {}",
        stdout
    );
    // Every def line should reference a .rs file (no .ts/.tsx/.js/.jsx)
    for line in stdout.lines() {
        if line.starts_with("def") {
            assert!(
                line.contains(".rs"),
                "--language rust filter: line should reference a .rs file but got: {}",
                line
            );
            assert!(
                !line.contains(".ts")
                    && !line.contains(".tsx")
                    && !line.contains(".js")
                    && !line.contains(".jsx"),
                "--language rust filter: line should not reference TS/JS files: {}",
                line
            );
        }
    }
}

/// test_language_filter_invalid — an unknown --language value exits non-zero with helpful message.
#[test]
fn test_language_filter_invalid() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let (_, stderr) = run_failure(&["find", ".*", "--language", "python", path]);
    assert!(
        stderr.contains("unknown language") || stderr.contains("python"),
        "stderr should mention 'unknown language' or 'python'\nstderr: {}",
        stderr
    );
}

/// test_stats_language_filter — stats --language rust shows Rust section, not TypeScript.
#[test]
fn test_stats_language_filter() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["stats", "--language", "rust", path]);
    assert!(
        stdout.contains("Rust"),
        "stats --language rust should contain 'Rust'\nstdout: {}",
        stdout
    );
    // code-graph's own source is pure Rust — TypeScript section should not appear
    assert!(
        !stdout.contains("TypeScript"),
        "stats --language rust should not show TypeScript section for pure-Rust project\nstdout: {}",
        stdout
    );
}

/// test_mixed_language_project — create a temp dir with both Rust and TypeScript files,
/// verify unified graph with both languages.
#[test]
fn test_mixed_language_project() {
    use std::fs;
    let tmp = tempfile::TempDir::new().expect("failed to create temp dir");
    let tmp_path = tmp.path();

    // Create minimal Rust crate
    let cargo_toml = r#"[package]
name = "test"
version = "0.1.0"
edition = "2021"
"#;
    fs::write(tmp_path.join("Cargo.toml"), cargo_toml).unwrap();
    fs::create_dir_all(tmp_path.join("src")).unwrap();
    fs::write(tmp_path.join("src").join("lib.rs"), "pub fn hello() {}\n").unwrap();

    // Create minimal TypeScript file (tsconfig.json signals TS project)
    fs::write(tmp_path.join("tsconfig.json"), "{}").unwrap();
    fs::write(
        tmp_path.join("src").join("index.ts"),
        "export function greet() {}\n",
    )
    .unwrap();

    let path = tmp_path.to_str().unwrap();

    // Test 1: index produces output mentioning both Rust and TypeScript files
    let index_stdout = run_success(&["index", path]);
    assert!(
        index_stdout.contains("files"),
        "index should mention 'files'\nstdout: {}",
        index_stdout
    );
    // At minimum, file count > 0
    assert!(
        index_stdout.contains("Indexed"),
        "index should mention 'Indexed'\nstdout: {}",
        index_stdout
    );

    // Test 2: find ".*" finds both hello (Rust) and greet (TypeScript)
    let find_stdout = run_success(&["find", ".*", path]);
    assert!(
        find_stdout.contains("hello"),
        "mixed project find should contain Rust function 'hello'\nstdout: {}",
        find_stdout
    );
    assert!(
        find_stdout.contains("greet"),
        "mixed project find should contain TypeScript function 'greet'\nstdout: {}",
        find_stdout
    );

    // Test 3: stats shows both Rust and TypeScript sections
    let stats_stdout = run_success(&["stats", path]);
    assert!(
        stats_stdout.contains("Rust"),
        "mixed project stats should contain 'Rust' section\nstdout: {}",
        stats_stdout
    );
    assert!(
        stats_stdout.contains("TypeScript"),
        "mixed project stats should contain 'TypeScript' section\nstdout: {}",
        stats_stdout
    );
}

// ---------------------------------------------------------------------------
// Task 2: MCP parity — JSON output format test (closest to MCP output format)
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Task 2 (Plan 11-02): Integration tests for export command — EXPORT-01 through EXPORT-06
// ---------------------------------------------------------------------------

/// Run a code-graph export command and return (stdout, stderr).
/// Asserts that the command exits successfully.
fn run_export(extra_args: &[&str]) -> (String, String) {
    let root = project_root();
    let mut args = vec!["export", root.to_str().unwrap()];
    args.extend_from_slice(extra_args);
    let out = Command::new(binary())
        .args(&args)
        .output()
        .expect("failed to invoke code-graph binary");
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        out.status.success(),
        "export {:?} failed\nstdout: {}\nstderr: {}",
        extra_args,
        stdout,
        stderr
    );
    (stdout, stderr)
}

/// test_export_dot — EXPORT-01: DOT format output contains required header and graph structure.
#[test]
fn test_export_dot() {
    let (stdout, _stderr) = run_export(&["--format", "dot", "--stdout"]);
    // DOT header
    assert!(
        stdout.contains("digraph code_graph"),
        "DOT output should contain 'digraph code_graph'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
    assert!(
        stdout.contains("rankdir=TB"),
        "DOT output should contain 'rankdir=TB'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
    // Node structure: at least one labeled node
    assert!(
        stdout.contains("[label="),
        "DOT output should contain at least one labeled node\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
    // Edge structure: at least one edge
    assert!(
        stdout.contains("->"),
        "DOT output should contain at least one edge '->'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
}

/// test_export_mermaid — EXPORT-02: Mermaid format output contains required header and structure.
#[test]
fn test_export_mermaid() {
    let (stdout, _stderr) = run_export(&["--format", "mermaid", "--stdout"]);
    // Mermaid header
    assert!(
        stdout.contains("flowchart TB"),
        "Mermaid output should contain 'flowchart TB'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
    // Node structure: Mermaid nodes use ["..."] syntax
    assert!(
        stdout.contains("[\""),
        "Mermaid output should contain node syntax '[\"'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
    // Edge structure: Mermaid edges use -->
    assert!(
        stdout.contains("-->"),
        "Mermaid output should contain at least one edge '-->'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
}

/// test_export_granularity — EXPORT-03: granularity flag changes output content.
///
/// symbol granularity includes kind annotations like "(fn)", "(struct)", "(enum)";
/// file granularity shows file paths only.
#[test]
fn test_export_granularity() {
    let (file_stdout, _) = run_export(&["--granularity", "file", "--stdout"]);
    let (symbol_stdout, _) = run_export(&["--granularity", "symbol", "--stdout"]);

    // Outputs must differ (symbol granularity expands each file into individual symbols)
    assert_ne!(
        file_stdout, symbol_stdout,
        "file and symbol granularity should produce different output"
    );

    // File granularity: nodes are file paths — should NOT contain "(fn)" or "(struct)"
    assert!(
        !file_stdout.contains("(fn)"),
        "file granularity output should not contain symbol-level '(fn)' annotations\n{}",
        &file_stdout[..file_stdout.len().min(500)]
    );
    assert!(
        !file_stdout.contains("(struct)"),
        "file granularity output should not contain symbol-level '(struct)' annotations\n{}",
        &file_stdout[..file_stdout.len().min(500)]
    );

    // Symbol granularity: nodes include kind annotations like "(fn)" or "(struct)"
    let has_kind_annotation = symbol_stdout.contains("(fn)")
        || symbol_stdout.contains("(struct)")
        || symbol_stdout.contains("(enum)");
    assert!(
        has_kind_annotation,
        "symbol granularity output should contain kind annotations like '(fn)', '(struct)', '(enum)'\n{}",
        &symbol_stdout[..symbol_stdout.len().min(500)]
    );
}

/// test_export_dot_package_clusters — EXPORT-04: DOT package granularity uses subgraph cluster_ blocks.
#[test]
fn test_export_dot_package_clusters() {
    let (stdout, _stderr) = run_export(&["--granularity", "package", "--stdout"]);

    // Package granularity uses DOT subgraph cluster_ blocks
    assert!(
        stdout.contains("subgraph cluster_"),
        "package granularity DOT output should contain 'subgraph cluster_'\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );

    // code-graph's own source has multiple packages (tests + code_graph_cli at minimum)
    let cluster_count = stdout.matches("subgraph cluster_").count();
    assert!(
        cluster_count >= 2,
        "package granularity DOT should have at least 2 cluster subgraphs, found {}\nstdout: {}",
        cluster_count,
        &stdout[..stdout.len().min(800)]
    );
}

/// test_export_mermaid_edge_limit_warning — EXPORT-05: scale guard warning behavior.
///
/// code-graph's own source has >200 symbols at symbol granularity (505 nodes measured),
/// so the node-count warning MUST appear. The Mermaid edge-limit warning (>500 edges)
/// may or may not appear depending on the current edge count.
///
/// Also verifies that DOT format does NOT produce the Mermaid-specific edge limit warning.
#[test]
fn test_export_mermaid_edge_limit_warning() {
    // Run symbol granularity — project has >200 symbols, triggering the node count warning.
    let root = project_root();
    let out = Command::new(binary())
        .args([
            "export",
            root.to_str().unwrap(),
            "--granularity",
            "symbol",
            "--stdout",
        ])
        .output()
        .expect("failed to invoke code-graph binary");
    assert!(out.status.success(), "export symbol --stdout should exit 0");
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();

    // The project has 505 symbols — the node-count scale guard must fire.
    assert!(
        stderr.contains("Warning:"),
        "symbol granularity should produce a Warning on stderr for >200 nodes\nstderr: {}",
        stderr
    );
    // Warning message should mention the node count or suggest alternatives.
    let has_relevant_warning =
        stderr.contains("nodes") || stderr.contains("granularity") || stderr.contains("edges");
    assert!(
        has_relevant_warning,
        "Warning should mention 'nodes', 'granularity', or 'edges'\nstderr: {}",
        stderr
    );

    // DOT format at symbol granularity: node-count warning fires (same source),
    // but NOT the Mermaid-specific edge limit warning.
    let dot_out = Command::new(binary())
        .args([
            "export",
            root.to_str().unwrap(),
            "--format",
            "dot",
            "--granularity",
            "symbol",
            "--stdout",
        ])
        .output()
        .expect("failed to invoke code-graph binary");
    let dot_stderr = String::from_utf8_lossy(&dot_out.stderr).to_string();
    assert!(
        !dot_stderr.contains("Mermaid"),
        "DOT export should not produce a Mermaid-specific warning\nstderr: {}",
        dot_stderr
    );
}

/// test_export_mcp_tool_registered — EXPORT-06: MCP export_graph tool is registered.
///
/// MCP tool registration is verified at compile time by the tool_router macro.
/// If the #[tool] attribute is missing or the method signature is wrong, the project
/// will not compile and cargo test itself would not run.
///
/// This test validates that the same export_graph() pipeline called by the MCP tool
/// works end-to-end: build graph, apply params, render output.
#[test]
fn test_export_mcp_tool_registered() {
    // If we reach this test, cargo compiled successfully — MCP tool_router macro accepted
    // the export_graph method, meaning it is registered as an MCP tool.
    //
    // Additionally, exercise the exact same export_graph() code path that the MCP tool calls:
    let (stdout, _stderr) = run_export(&["--format", "dot", "--granularity", "file", "--stdout"]);

    // The pipeline must produce a valid DOT graph
    assert!(
        stdout.contains("digraph code_graph"),
        "export_graph() pipeline should produce valid DOT output\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );

    // Must include at least one node (the project is non-empty)
    let node_count = stdout.matches("[label=").count();
    assert!(
        node_count > 0,
        "export_graph() should produce at least one node\nstdout: {}",
        &stdout[..stdout.len().min(500)]
    );
}

/// test_find_json_output — find --format json produces valid JSON array with expected keys.
///
/// This tests the same serialization path used by the MCP find_symbol tool
/// (format_find_to_string, which is called by the MCP server).
#[test]
fn test_find_json_output() {
    let root = project_root();
    let path = root.to_str().unwrap();
    let stdout = run_success(&["find", "build_graph", "--format", "json", path]);
    let parsed: serde_json::Value =
        serde_json::from_str(&stdout).expect("find --format json output is not valid JSON");
    let arr = parsed
        .as_array()
        .expect("find --format json should return a JSON array");
    assert!(
        !arr.is_empty(),
        "JSON array should have at least one result"
    );
    // Verify expected keys in the first element
    let first = &arr[0];
    assert!(
        first.get("name").is_some(),
        "JSON result should have 'name' key\ngot: {}",
        first
    );
    assert!(
        first.get("file").is_some(),
        "JSON result should have 'file' key\ngot: {}",
        first
    );
    assert!(
        first.get("kind").is_some(),
        "JSON result should have 'kind' key\ngot: {}",
        first
    );
    assert!(
        first.get("line").is_some(),
        "JSON result should have 'line' key\ngot: {}",
        first
    );
}