groxide 0.1.0

Query Rust crate documentation from the terminal
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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//! End-to-end CLI integration tests for groxide — happy paths.
//!
//! All tests run the `grox` binary via `assert_cmd` against the fixture crate
//! at `test-fixtures/groxide_test_api/` using `--manifest-path`.
//! Snapshot tests use `insta` for output format stability.

use assert_cmd::Command;

/// Returns the absolute path to the fixture crate's Cargo.toml.
fn fixture_manifest() -> String {
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    format!("{manifest_dir}/test-fixtures/groxide_test_api/Cargo.toml")
}

/// Builds a `grox` command pre-configured with the fixture crate manifest.
fn grox() -> Command {
    let mut cmd = Command::new(assert_cmd::cargo::cargo_bin!("grox"));
    cmd.arg("--manifest-path").arg(fixture_manifest());
    cmd
}

// ── Default output for a struct ──────────────────────────────────────

#[test]
fn default_output_struct() {
    let output = grox()
        .arg("groxide_test_api::GenericStruct")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("struct_default", stdout);
}

// ── Default output for a module ──────────────────────────────────────

#[test]
fn default_output_module() {
    let output = grox()
        .arg("groxide_test_api::containers")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("module_default", stdout);
}

// ── Default output for a function ────────────────────────────────────

#[test]
fn default_output_function() {
    let output = grox()
        .arg("groxide_test_api::add")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("function_default", stdout);
}

// ── Default output for a trait ───────────────────────────────────────

#[test]
fn default_output_trait() {
    let output = grox()
        .arg("groxide_test_api::traits::Stringify")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("trait_default", stdout);
}

// ── Default output for an enum ───────────────────────────────────────

#[test]
fn default_output_enum() {
    let output = grox()
        .arg("groxide_test_api::Direction")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("enum_default", stdout);
}

// ── Default output for crate root ────────────────────────────────────

#[test]
fn default_output_crate_root() {
    let output = grox()
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("crate_root_default", stdout);
}

// ── --json mode ──────────────────────────────────────────────────────

#[test]
fn json_mode() {
    let output = grox()
        .arg("--json")
        .arg("groxide_test_api::GenericStruct")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Verify it's valid JSON
    let parsed: serde_json::Value =
        serde_json::from_str(&stdout).expect("output should be valid JSON");
    assert_eq!(parsed["kind"], "struct");
    assert!(
        parsed["path"]
            .as_str()
            .unwrap_or("")
            .contains("GenericStruct"),
        "JSON path should contain GenericStruct"
    );
    assert!(parsed["methods"].is_array(), "should have methods array");
    assert!(
        parsed["trait_impls"].is_array(),
        "should have trait_impls array"
    );

    insta::assert_snapshot!("json_mode", stdout);
}

// ── --search "query" mode ────────────────────────────────────────────

#[test]
fn search_mode() {
    let output = grox()
        .arg("--search")
        .arg("add")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("results for \"add\""),
        "should have results header: {stdout}"
    );
    assert!(
        stdout.contains("add"),
        "should find the add function: {stdout}"
    );

    insta::assert_snapshot!("search_mode", stdout);
}

// ── --source mode ────────────────────────────────────────────────────

#[test]
fn source_mode() {
    let output = grox()
        .arg("--source")
        .arg("groxide_test_api::add")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("src/lib.rs:"),
        "should have source file location in header: {stdout}"
    );
    assert!(
        stdout.contains("pub fn add"),
        "should contain the function source: {stdout}"
    );

    insta::assert_snapshot!("source_mode", stdout);
}

// ── --impls mode ─────────────────────────────────────────────────────

#[test]
fn impls_mode() {
    let output = grox()
        .arg("groxide_test_api::GenericStruct")
        .arg("--impls")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("trait implementations:"),
        "should show trait implementations: {stdout}"
    );

    insta::assert_snapshot!("impls_mode", stdout);
}

// ── --kind fn filters to functions ───────────────────────────────────

#[test]
fn kind_filter_fn() {
    let output = grox()
        .arg("--kind")
        .arg("fn")
        .arg("groxide_test_api::add")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("fn groxide_test_api::add"),
        "should match the function: {stdout}"
    );

    insta::assert_snapshot!("kind_filter_fn", stdout);
}

// ── --private includes private items ─────────────────────────────────

#[test]
fn private_flag_accepted() {
    let output = grox()
        .arg("--private")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // The --private flag should be accepted and produce valid output
    assert!(
        stdout.contains("groxide_test_api"),
        "should produce crate root output: {stdout}"
    );

    insta::assert_snapshot!("private_flag", stdout);
}

// ── Additional happy path tests ──────────────────────────────────────

#[test]
fn constant_output() {
    let output = grox()
        .arg("groxide_test_api::MAX_BUFFER_SIZE")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("const"), "should show constant: {stdout}");

    insta::assert_snapshot!("constant_default", stdout);
}

#[test]
fn type_alias_output() {
    let output = grox()
        .arg("groxide_test_api::Result")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("type_alias_default", stdout);
}

#[test]
fn json_mode_module() {
    let output = grox()
        .arg("--json")
        .arg("groxide_test_api::containers")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Module JSON produces JSON Lines (first line = module, rest = children)
    let lines: Vec<&str> = stdout.lines().collect();
    assert!(!lines.is_empty(), "should have at least one line");
    // First line should be valid JSON
    let first: serde_json::Value =
        serde_json::from_str(lines[0]).expect("first line should be valid JSON");
    assert_eq!(first["kind"], "mod");

    insta::assert_snapshot!("json_mode_module", stdout);
}

#[test]
fn search_with_json_mode() {
    let output = grox()
        .arg("--json")
        .arg("--search")
        .arg("stack")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Each line should be valid JSON with a score field
    for line in stdout.lines() {
        let parsed: serde_json::Value =
            serde_json::from_str(line).expect("each line should be valid JSON");
        assert!(
            parsed.get("score").is_some(),
            "should have score field: {line}"
        );
    }

    insta::assert_snapshot!("search_json", stdout);
}

#[test]
fn source_mode_struct() {
    let output = grox()
        .arg("--source")
        .arg("groxide_test_api::GenericStruct")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("src/lib.rs:"),
        "should have source location in header: {stdout}"
    );
    assert!(
        stdout.contains("pub struct GenericStruct"),
        "should contain struct source: {stdout}"
    );

    insta::assert_snapshot!("source_struct", stdout);
}

#[test]
fn source_mode_shows_source_instead_of_docs() {
    let output = grox()
        .arg("--source")
        .arg("groxide_test_api::add")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should NOT include rendered docs (source replaces docs)
    assert!(
        !stdout.contains("Adds two numbers"),
        "should not contain doc text in source mode: {stdout}"
    );
    // Should include source code
    assert!(
        stdout.contains("a + b"),
        "should contain source code: {stdout}"
    );
    // Should include source header
    assert!(
        stdout.contains("src/lib.rs:"),
        "should have source location in header: {stdout}"
    );
}

#[test]
fn impls_on_trait() {
    let output = grox()
        .arg("groxide_test_api::traits::Stringify")
        .arg("--impls")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should show implementors section (may be empty)
    assert!(
        stdout.contains("trait groxide_test_api::traits::Stringify"),
        "should show trait header: {stdout}"
    );

    insta::assert_snapshot!("impls_trait", stdout);
}

#[test]
fn deep_nested_path() {
    let output = grox()
        .arg("groxide_test_api::deeply")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("deeply"),
        "should show deeply module: {stdout}"
    );

    insta::assert_snapshot!("deep_nested", stdout);
}

#[test]
fn method_lookup() {
    let output = grox()
        .arg("groxide_test_api::GenericStruct::new")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("new"),
        "should show the new method: {stdout}"
    );

    insta::assert_snapshot!("method_lookup", stdout);
}

#[test]
fn kind_filter_struct() {
    let output = grox()
        .arg("--kind")
        .arg("struct")
        .arg("groxide_test_api::GenericStruct")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("struct groxide_test_api::GenericStruct"),
        "should match the struct: {stdout}"
    );
}

#[test]
fn enum_with_variants() {
    let output = grox()
        .arg("groxide_test_api::Shape")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("variants:"),
        "should show variants section: {stdout}"
    );
    assert!(
        stdout.contains("Circle"),
        "should list Circle variant: {stdout}"
    );

    insta::assert_snapshot!("enum_with_data_variants", stdout);
}

// ── Re-exports module shows real signatures ──────────────────────────

#[test]
fn reexports_module_shows_real_signatures() {
    let output = grox()
        .arg("groxide_test_api::reexports")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // In-crate re-exports should show real signatures, not "pub use ... as ..." stubs
    assert!(
        !stdout.contains("pub use inner::"),
        "should not show 'pub use' stubs for in-crate re-exports: {stdout}"
    );
    assert!(
        stdout.contains("pub fn inner_fn() -> i32"),
        "should show real function signature: {stdout}"
    );
    assert!(
        stdout.contains("pub fn glob_fn() -> bool"),
        "should show real glob-reexported function signature: {stdout}"
    );

    insta::assert_snapshot!("reexports_module", stdout);
}

#[test]
fn reexported_struct_shows_fields_and_impls() {
    let output = grox()
        .arg("groxide_test_api::reexports::Helper")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("pub struct Helper"),
        "should show real struct signature: {stdout}"
    );
    assert!(stdout.contains("id"), "should show struct fields: {stdout}");

    insta::assert_snapshot!("reexported_struct", stdout);
}

// ── Re-export resolution on not-found path ──────────────────────────

#[test]
fn reexport_resolves_transparently_with_wrong_module_path() {
    // Query Helper via a non-existent module path — the re-export fallback
    // should find it under reexports::Helper since it's re-exported from inner.
    let output = grox()
        .arg("groxide_test_api::wrong_module::Helper")
        .output()
        .expect("command runs");

    assert!(
        output.status.success(),
        "should resolve via re-export fallback, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Helper"), "should show Helper: {stdout}");
}

// ── Cross-crate re-exports ───────────────────────────────────────────
//
// These tests pin the new resolution paths added for B2/B3/B5:
// - Cross-crate wildcard re-exports via `pub use other::*`
// - Cross-crate module re-exports walked through the prefix descent
// - Cross-crate terminal stubs followed via the `follow_by_name` fallback
//
// They build against `groxide_test_inner` (a path-dep added to the fixture)
// so they cover the actual end-to-end pipeline: index → resolve →
// follow → render.

#[test]
fn cross_crate_terminal_stub_resolves_to_canonical() {
    // `cross_reexports` has `pub use groxide_test_inner::CrossStruct;`.
    // The query should follow the terminal stub into the inner crate
    // and render the canonical struct.
    let output = grox()
        .arg("groxide_test_api::cross_reexports::CrossStruct")
        .output()
        .expect("command runs");

    assert!(
        output.status.success(),
        "should resolve cross-crate stub, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("pub struct CrossStruct"),
        "should show canonical struct signature, got: {stdout}"
    );
    assert!(
        stdout.contains("value"),
        "should expose the inner struct's fields: {stdout}"
    );
}

#[test]
fn cross_crate_wildcard_glob_resolves_items_only_reachable_via_glob() {
    // `cross_reexports` has `pub use groxide_test_inner::*;`. CROSS_CONST
    // is only reachable through that glob — it is NOT terminal-stubbed.
    // The query exercises `try_resolve_via_glob_reexport` end-to-end.
    let output = grox()
        .arg("groxide_test_api::cross_reexports::CROSS_CONST")
        .output()
        .expect("command runs");

    assert!(
        output.status.success(),
        "should resolve via glob re-export, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("CROSS_CONST"),
        "glob should expose CROSS_CONST: {stdout}"
    );
}

#[test]
fn cross_crate_module_reexport_descends_into_inner() {
    // `cross_reexports` re-exports `groxide_test_inner::cross_mod`.
    // Querying an item INSIDE that module is the prefix-descent case:
    // resolve "cross_mod" stub here, then look up "ModItem" in the inner
    // crate's `cross_mod`.
    let output = grox()
        .arg("groxide_test_api::cross_reexports::cross_mod::ModItem")
        .output()
        .expect("command runs");

    assert!(
        output.status.success(),
        "should descend through module re-export, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("ModItem"),
        "should resolve to canonical ModItem: {stdout}"
    );
}

// ── --recursive mode ──────────────────────────────────────────────────

#[test]
fn recursive_lists_all_items() {
    let output = grox()
        .arg("--recursive")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should contain items from nested modules
    assert!(
        stdout.contains("groxide_test_api::containers::Stack"),
        "should list nested struct: {stdout}"
    );
    assert!(
        stdout.contains("groxide_test_api::traits::Stringify"),
        "should list nested trait: {stdout}"
    );

    insta::assert_snapshot!("recursive_crate_root", stdout);
}

#[test]
fn recursive_with_json() {
    let output = grox()
        .arg("--recursive")
        .arg("--json")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Each line should be valid JSON
    for line in stdout.lines() {
        let parsed: serde_json::Value =
            serde_json::from_str(line).expect("each line should be valid JSON");
        assert!(
            parsed.get("path").is_some(),
            "should have path field: {line}"
        );
    }

    insta::assert_snapshot!("recursive_json", stdout);
}

#[test]
fn recursive_with_kind_filter() {
    let output = grox()
        .arg("--recursive")
        .arg("--kind")
        .arg("fn")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should only contain functions
    for line in stdout.lines() {
        if line.ends_with(':') || line.is_empty() {
            continue; // skip section headers and blank lines
        }
        assert!(line.contains("fn"), "all items should be functions: {line}");
    }

    insta::assert_snapshot!("recursive_kind_filter", stdout);
}

// ── --impls with trait filter ────────────────────────────────────────

#[test]
fn impls_filter_by_trait_name() {
    let output = grox()
        .arg("--impls-of")
        .arg("Default")
        .arg("groxide_test_api::containers::Stack")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Default"),
        "should show Default impl: {stdout}"
    );
    // Should NOT show synthetic impls like Send/Sync when filtering
    assert!(
        !stdout.contains("Send"),
        "should not show unrelated impls: {stdout}"
    );

    insta::assert_snapshot!("impls_filter_default", stdout);
}

#[test]
fn impls_filter_no_match() {
    let output = grox()
        .arg("--impls-of")
        .arg("NonexistentTrait")
        .arg("groxide_test_api::containers::Stack")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("No implementation of 'NonexistentTrait' found"),
        "should show no-match message: {stdout}"
    );

    insta::assert_snapshot!("impls_filter_no_match", stdout);
}

#[test]
fn impls_bare_flag_still_works() {
    let output = grox()
        .arg("groxide_test_api::containers::Stack")
        .arg("--impls")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("trait implementations:"),
        "should show all trait implementations: {stdout}"
    );

    insta::assert_snapshot!("impls_bare_flag", stdout);
}

// ── --recursive composes with detail flags ───────────────────────────

#[test]
fn recursive_and_source_composable() {
    let output = grox()
        .args(["-r", "-s", "groxide_test_api"])
        .output()
        .expect("command runs");
    // Should not fail with conflict error (exit 2)
    assert_ne!(output.status.code(), Some(2));
}

#[test]
fn recursive_and_brief_composable() {
    let output = grox()
        .args(["-r", "-b", "groxide_test_api"])
        .output()
        .expect("command runs");
    assert_ne!(output.status.code(), Some(2));
}

#[test]
fn recursive_and_docs_composable() {
    let output = grox()
        .args(["-r", "-d", "groxide_test_api"])
        .output()
        .expect("command runs");
    assert_ne!(output.status.code(), Some(2));
}

// ── --clear-cache ────────────────────────────────────────────────────

// ── Brief mode ──────────────────────────────────────────────────────

#[test]
fn brief_mode_crate_root() {
    let output = grox()
        .arg("-b")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("brief_crate_root", stdout);

    // Verify no signatures appear
    assert!(!stdout.contains("pub fn"), "should not contain signatures");
    assert!(
        !stdout.contains("pub struct"),
        "should not contain signatures"
    );
}

#[test]
fn brief_mode_module() {
    let output = grox()
        .arg("-b")
        .arg("groxide_test_api::containers")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("brief_module", stdout);

    assert!(!stdout.contains("pub fn"), "should not contain signatures");
    assert!(
        !stdout.contains("pub struct"),
        "should not contain signatures"
    );
}

#[test]
fn brief_recursive_crate_root() {
    let output = grox()
        .arg("-r")
        .arg("-b")
        .arg("groxide_test_api")
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("brief_recursive_crate_root", stdout);

    assert!(!stdout.contains("pub fn"), "should not contain signatures");
    assert!(
        !stdout.contains("pub struct"),
        "should not contain signatures"
    );
}

// ── Docs mode ───────────────────────────────────────────────────────

#[test]
fn docs_mode_function() {
    let output = grox()
        .args(["-d", "groxide_test_api::add"])
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("docs_mode_function", stdout);

    // Should show full docs (same as default for single items)
    assert!(stdout.contains("add"), "should contain function name");
    assert!(stdout.contains("fn"), "should contain kind");
}

#[test]
fn docs_mode_recursive() {
    let output = grox()
        .args(["-r", "-d", "groxide_test_api::containers"])
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("docs_mode_recursive", stdout);

    // Should show full docs, not just one-line summaries
    assert!(
        stdout.contains("pub struct") || stdout.contains("pub fn"),
        "should contain signatures: {stdout}"
    );
}

// ── Recursive + source mode ──────────────────────────────────────────

#[test]
fn recursive_source_module() {
    let output = grox()
        .args(["-r", "-s", "groxide_test_api::containers"])
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    insta::assert_snapshot!("recursive_source_module", stdout);
}

#[test]
fn recursive_source_crate_root() {
    let output = grox()
        .args(["-r", "-s", "groxide_test_api"])
        .output()
        .expect("command runs");

    assert!(output.status.success(), "exit code should be 0");

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should contain source from multiple modules
    assert!(
        stdout.contains("src/"),
        "should contain source locations in headers: {stdout}"
    );
    // Should contain items from different modules
    assert!(
        stdout.contains("containers"),
        "should contain containers module items"
    );
}

// ── --clear-cache ────────────────────────────────────────────────────

#[test]
fn clear_cache_exits_successfully() {
    let output = Command::new(assert_cmd::cargo::cargo_bin!("grox"))
        .arg("--clear-cache")
        .output()
        .expect("command runs");
    assert!(output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("[grox]"),
        "should print status message: {stderr}"
    );
}

// ── Feature hint on zero search results ──────────────────────────────

#[test]
fn search_zero_results_hints_features() {
    // Use --no-default-features to bypass the feature cascade (which tries
    // --all-features by default). This builds without `unstable`, so
    // `unstable_api` won't be in the index, triggering the hint.
    let output = grox()
        .args([
            "--no-default-features",
            "-S",
            "unstable_api",
            "groxide_test_api",
        ])
        .output()
        .expect("command runs");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stdout.contains("0 results"),
        "should show 0 results on stdout: {stdout}"
    );
    assert!(
        stderr.contains("hint:") && stderr.contains("--all-features"),
        "should hint about --all-features on stderr: {stderr}"
    );
}