fallow-cli 2.76.0

CLI for fallow, Rust-native codebase intelligence for TypeScript and JavaScript
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
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
use std::process::ExitCode;

use fallow_config::OutputFormat;

use crate::runtime_support::load_config;

pub struct ListOptions<'a> {
    pub root: &'a std::path::Path,
    pub config_path: &'a Option<std::path::PathBuf>,
    pub output: OutputFormat,
    pub threads: usize,
    pub no_cache: bool,
    pub entry_points: bool,
    pub files: bool,
    pub plugins: bool,
    pub boundaries: bool,
    pub production: bool,
}

pub fn run_list(opts: &ListOptions<'_>) -> ExitCode {
    let config = match load_config(
        opts.root,
        opts.config_path,
        OutputFormat::Human,
        opts.no_cache,
        opts.threads,
        opts.production,
        true, // list command doesn't need progress bars
    ) {
        Ok(c) => c,
        Err(code) => return code,
    };

    let show_all = should_show_all(opts);

    // Run plugin detection when plugin output is requested or when entry-point
    // discovery needs plugin-provided entry points.
    let plugin_result = if opts.plugins || opts.entry_points || show_all {
        let disc = fallow_core::discover::discover_files_with_plugin_scopes(&config);
        let file_paths: Vec<std::path::PathBuf> = disc.iter().map(|f| f.path.clone()).collect();
        let registry = fallow_core::plugins::PluginRegistry::new(config.external_plugins.clone());

        let pkg_path = opts.root.join("package.json");
        let mut result = fallow_config::PackageJson::load(&pkg_path).map_or_else(
            |_| fallow_core::plugins::AggregatedPluginResult::default(),
            |pkg| registry.run(&pkg, opts.root, &file_paths),
        );

        // Also run plugins for workspace packages
        let workspaces = fallow_config::discover_workspaces(opts.root);
        for ws in &workspaces {
            let ws_pkg_path = ws.root.join("package.json");
            if let Ok(ws_pkg) = fallow_config::PackageJson::load(&ws_pkg_path) {
                let ws_result = registry.run(&ws_pkg, &ws.root, &file_paths);
                for plugin_name in &ws_result.active_plugins {
                    if !result.active_plugins.contains(plugin_name) {
                        result.active_plugins.push(plugin_name.clone());
                    }
                }
            }
        }
        Some(result)
    } else {
        None
    };

    // Discover files once if needed by files, entry_points, or boundaries
    let need_files = needs_file_discovery(opts.files, show_all, opts.entry_points, opts.boundaries);
    let discovered = if need_files {
        Some(fallow_core::discover::discover_files_with_plugin_scopes(
            &config,
        ))
    } else {
        None
    };

    // Compute entry points once (shared by both JSON and human output branches)
    let all_entry_points = if (opts.entry_points || show_all)
        && let Some(ref disc) = discovered
    {
        let mut entries = fallow_core::discover::discover_entry_points(&config, disc);
        // Add workspace entry points
        let workspaces = fallow_config::discover_workspaces(opts.root);
        for ws in &workspaces {
            let ws_entries =
                fallow_core::discover::discover_workspace_entry_points(&ws.root, &config, disc);
            entries.extend(ws_entries);
        }
        // Add plugin-discovered entry points
        if let Some(ref pr) = plugin_result {
            let plugin_entries =
                fallow_core::discover::discover_plugin_entry_points(pr, &config, disc);
            entries.extend(plugin_entries);
        }
        Some(entries)
    } else {
        None
    };

    // Boundaries are opt-in to keep the default list view focused on files,
    // plugins, and entry points.
    let boundary_data = if opts.boundaries {
        Some(compute_boundary_data(&config, discovered.as_deref()))
    } else {
        None
    };

    match opts.output {
        OutputFormat::Json => print_list_json(
            opts,
            show_all,
            plugin_result.as_ref(),
            discovered.as_deref(),
            all_entry_points.as_deref(),
            boundary_data.as_ref(),
        ),
        _ => {
            print_list_human(
                opts,
                show_all,
                plugin_result.as_ref(),
                discovered.as_deref(),
                all_entry_points.as_deref(),
                boundary_data.as_ref(),
            );
            ExitCode::SUCCESS
        }
    }
}

/// Determine whether all listing modes should be shown.
///
/// When none of the specific flags is set, the command defaults to
/// showing everything.
const fn should_show_all(opts: &ListOptions<'_>) -> bool {
    !opts.entry_points && !opts.files && !opts.plugins && !opts.boundaries
}

/// Determine whether file discovery is needed.
///
/// Files must be discovered when showing files, when showing all,
/// when computing entry points, or when computing boundary file counts.
const fn needs_file_discovery(
    files: bool,
    show_all: bool,
    entry_points: bool,
    boundaries: bool,
) -> bool {
    files || show_all || entry_points || boundaries
}

// ── Output helpers ─────────────────────────────────────────────

/// Print list results as JSON and return the appropriate exit code.
fn print_list_json(
    opts: &ListOptions<'_>,
    show_all: bool,
    plugin_result: Option<&fallow_core::plugins::AggregatedPluginResult>,
    discovered: Option<&[fallow_core::discover::DiscoveredFile]>,
    entry_points: Option<&[fallow_core::discover::EntryPoint]>,
    boundary_data: Option<&BoundaryData>,
) -> ExitCode {
    let mut result = serde_json::Map::new();

    if (opts.plugins || show_all)
        && let Some(pr) = plugin_result
    {
        let pl: Vec<serde_json::Value> = pr
            .active_plugins
            .iter()
            .map(|name| serde_json::json!({ "name": name }))
            .collect();
        result.insert("plugins".to_string(), serde_json::json!(pl));
    }

    if (opts.files || show_all)
        && let Some(disc) = discovered
    {
        let paths: Vec<serde_json::Value> = disc
            .iter()
            .map(|f| {
                let relative = f.path.strip_prefix(opts.root).unwrap_or(&f.path);
                serde_json::json!(relative.display().to_string())
            })
            .collect();
        result.insert("file_count".to_string(), serde_json::json!(paths.len()));
        result.insert("files".to_string(), serde_json::json!(paths));
    }

    if let Some(entries) = entry_points {
        let eps: Vec<serde_json::Value> = entries
            .iter()
            .map(|ep| {
                let relative = ep.path.strip_prefix(opts.root).unwrap_or(&ep.path);
                serde_json::json!({
                    "path": relative.display().to_string(),
                    "source": ep.source.to_string(),
                })
            })
            .collect();
        result.insert(
            "entry_point_count".to_string(),
            serde_json::json!(eps.len()),
        );
        result.insert("entry_points".to_string(), serde_json::json!(eps));
    }

    if let Some(bd) = boundary_data {
        result.insert("boundaries".to_string(), boundary_data_to_json(bd));
    }

    match serde_json::to_string_pretty(&serde_json::Value::Object(result)) {
        Ok(json) => {
            println!("{json}");
            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("Error: failed to serialize list output: {e}");
            ExitCode::from(2)
        }
    }
}

/// Print list results in human-readable format.
fn print_list_human(
    opts: &ListOptions<'_>,
    show_all: bool,
    plugin_result: Option<&fallow_core::plugins::AggregatedPluginResult>,
    discovered: Option<&[fallow_core::discover::DiscoveredFile]>,
    entry_points: Option<&[fallow_core::discover::EntryPoint]>,
    boundary_data: Option<&BoundaryData>,
) {
    if (opts.plugins || show_all)
        && let Some(pr) = plugin_result
    {
        eprintln!("Active plugins:");
        for name in &pr.active_plugins {
            eprintln!("  - {name}");
        }
    }

    if (opts.files || show_all)
        && let Some(disc) = discovered
    {
        eprintln!("Discovered {} files", disc.len());
        for file in disc {
            let relative = file.path.strip_prefix(opts.root).unwrap_or(&file.path);
            println!("{}", relative.display());
        }
    }

    if let Some(entries) = entry_points {
        eprintln!("Found {} entry points", entries.len());
        for ep in entries {
            let relative = ep.path.strip_prefix(opts.root).unwrap_or(&ep.path);
            println!("{} ({})", relative.display(), ep.source);
        }
    }

    if let Some(bd) = boundary_data {
        print_boundary_data_human(bd);
    }
}

// ── Boundary listing helpers ───────────────────────────────────

struct BoundaryData {
    zones: Vec<ZoneInfo>,
    rules: Vec<RuleInfo>,
    logical_groups: Vec<LogicalGroupInfo>,
    is_empty: bool,
}

struct ZoneInfo {
    name: String,
    patterns: Vec<String>,
    file_count: usize,
}

struct RuleInfo {
    from: String,
    allow: Vec<String>,
}

/// View-model mirror of [`fallow_config::LogicalGroup`] with the summed
/// `file_count` derived from `zones[]`. The config-layer type stops at
/// "what did the user write?"; this struct adds the analytical view "how
/// many files does the group reach?" so the JSON consumer (Sankey
/// renderer, dashboard, agent tooling) does not have to re-aggregate.
struct LogicalGroupInfo {
    name: String,
    children: Vec<String>,
    auto_discover: Vec<String>,
    authored_rule: Option<fallow_config::AuthoredRule>,
    fallback_zone: Option<String>,
    source_zone_index: usize,
    status: fallow_config::LogicalGroupStatus,
    /// Sum of `file_count` across `children` PLUS the fallback zone's
    /// `file_count` when present. The two halves are kept separately in
    /// [`Self::child_file_count`] and [`Self::fallback_file_count`] so the
    /// human renderer can show the split when a fallback exists.
    file_count: usize,
    /// Subtotal: sum of `file_count` across `children` only. Equals
    /// [`Self::file_count`] when there is no fallback zone.
    child_file_count: usize,
    /// Subtotal: `file_count` of the `fallback_zone`. `0` when there is
    /// no fallback zone.
    fallback_file_count: usize,
    /// Parent zone indices merged into this group when the user declared
    /// the same parent name twice. Mirrors
    /// [`fallow_config::LogicalGroup::merged_from`].
    merged_from: Option<Vec<usize>>,
    /// Parent zone's `root` (subtree scope) as the user authored it.
    /// Mirrors [`fallow_config::LogicalGroup::original_zone_root`].
    original_zone_root: Option<String>,
    /// Parallel to `children`: source path indices. Empty when only one
    /// `auto_discover` path was authored. Mirrors
    /// [`fallow_config::LogicalGroup::child_source_indices`].
    child_source_indices: Vec<usize>,
}

fn compute_boundary_data(
    config: &fallow_config::ResolvedConfig,
    discovered: Option<&[fallow_core::discover::DiscoveredFile]>,
) -> BoundaryData {
    let boundaries = &config.boundaries;

    if boundaries.is_empty() {
        return BoundaryData {
            zones: vec![],
            rules: vec![],
            logical_groups: vec![],
            is_empty: true,
        };
    }

    let zones: Vec<ZoneInfo> = boundaries
        .zones
        .iter()
        .map(|zone| {
            let file_count = discovered.map_or(0, |files| {
                files
                    .iter()
                    .filter(|f| {
                        let rel = f
                            .path
                            .strip_prefix(&config.root)
                            .ok()
                            .map(|p| p.to_string_lossy().replace('\\', "/"));
                        rel.is_some_and(|p| {
                            boundaries.classify_zone(&p) == Some(zone.name.as_str())
                        })
                    })
                    .count()
            });
            ZoneInfo {
                name: zone.name.clone(),
                patterns: zone.matchers.iter().map(|m| m.glob().to_string()).collect(),
                file_count,
            }
        })
        .collect();

    let rules: Vec<RuleInfo> = boundaries
        .rules
        .iter()
        .map(|r| RuleInfo {
            from: r.from_zone.clone(),
            allow: r.allowed_zones.clone(),
        })
        .collect();

    // Index zones by name once for O(1) child file_count lookups; the
    // per-child loop below would otherwise scan the zone list quadratically.
    let zone_count_by_name: rustc_hash::FxHashMap<&str, usize> = zones
        .iter()
        .map(|z| (z.name.as_str(), z.file_count))
        .collect();

    let logical_groups: Vec<LogicalGroupInfo> = boundaries
        .logical_groups
        .iter()
        .map(|g| {
            let child_file_count: usize = g
                .children
                .iter()
                .filter_map(|child| zone_count_by_name.get(child.as_str()).copied())
                .sum();
            let fallback_file_count = g
                .fallback_zone
                .as_deref()
                .and_then(|fb| zone_count_by_name.get(fb).copied())
                .unwrap_or(0);
            LogicalGroupInfo {
                name: g.name.clone(),
                children: g.children.clone(),
                auto_discover: g.auto_discover.clone(),
                authored_rule: g.authored_rule.clone(),
                fallback_zone: g.fallback_zone.clone(),
                source_zone_index: g.source_zone_index,
                status: g.status,
                file_count: child_file_count + fallback_file_count,
                child_file_count,
                fallback_file_count,
                merged_from: g.merged_from.clone(),
                original_zone_root: g.original_zone_root.clone(),
                child_source_indices: g.child_source_indices.clone(),
            }
        })
        .collect();

    BoundaryData {
        zones,
        rules,
        logical_groups,
        is_empty: false,
    }
}

fn boundary_data_to_json(bd: &BoundaryData) -> serde_json::Value {
    if bd.is_empty {
        // Mirror the configured-branch field set so consumers can read
        // `zone_count` / `rule_count` / `logical_group_count` without
        // first branching on `configured`. Keeps the schema symmetric:
        // the count and the array are always present together.
        return serde_json::json!({
            "configured": false,
            "zone_count": 0,
            "zones": [],
            "rule_count": 0,
            "rules": [],
            "logical_group_count": 0,
            "logical_groups": [],
        });
    }

    let zones: Vec<serde_json::Value> = bd
        .zones
        .iter()
        .map(|z| {
            serde_json::json!({
                "name": z.name,
                "patterns": z.patterns,
                "file_count": z.file_count,
            })
        })
        .collect();

    let rules: Vec<serde_json::Value> = bd
        .rules
        .iter()
        .map(|r| {
            serde_json::json!({
                "from": r.from,
                "allow": r.allow,
            })
        })
        .collect();

    let logical_groups: Vec<serde_json::Value> = bd
        .logical_groups
        .iter()
        .map(logical_group_info_to_json)
        .collect();

    serde_json::json!({
        "configured": true,
        "zone_count": bd.zones.len(),
        "zones": zones,
        "rule_count": bd.rules.len(),
        "rules": rules,
        "logical_group_count": bd.logical_groups.len(),
        "logical_groups": logical_groups,
    })
}

fn logical_group_info_to_json(g: &LogicalGroupInfo) -> serde_json::Value {
    let status = match g.status {
        fallow_config::LogicalGroupStatus::Ok => "ok",
        fallow_config::LogicalGroupStatus::Empty => "empty",
        fallow_config::LogicalGroupStatus::InvalidPath => "invalid_path",
    };
    let mut entry = serde_json::Map::new();
    entry.insert("name".to_string(), serde_json::json!(g.name));
    // `children` and `auto_discover` are always emitted, even when empty:
    // `status` discriminates "empty dir" vs "invalid path", and consumers
    // (error renderers, agent tooling) need the authored paths to surface
    // an actionable hint even when discovery turned up nothing. This
    // intentionally deviates from the project's `skip_serializing_if =
    // "Vec::is_empty"` convention.
    entry.insert("children".to_string(), serde_json::json!(g.children));
    entry.insert(
        "auto_discover".to_string(),
        serde_json::json!(g.auto_discover),
    );
    entry.insert("status".to_string(), serde_json::json!(status));
    entry.insert(
        "source_zone_index".to_string(),
        serde_json::json!(g.source_zone_index),
    );
    entry.insert("file_count".to_string(), serde_json::json!(g.file_count));
    if let Some(rule) = &g.authored_rule {
        let mut rule_obj = serde_json::Map::new();
        rule_obj.insert("allow".to_string(), serde_json::json!(rule.allow));
        if !rule.allow_type_only.is_empty() {
            rule_obj.insert(
                "allow_type_only".to_string(),
                serde_json::json!(rule.allow_type_only),
            );
        }
        entry.insert(
            "authored_rule".to_string(),
            serde_json::Value::Object(rule_obj),
        );
    }
    if let Some(fb) = &g.fallback_zone {
        entry.insert("fallback_zone".to_string(), serde_json::json!(fb));
    }
    if let Some(chain) = &g.merged_from {
        entry.insert("merged_from".to_string(), serde_json::json!(chain));
    }
    if let Some(root) = &g.original_zone_root {
        entry.insert("original_zone_root".to_string(), serde_json::json!(root));
    }
    if !g.child_source_indices.is_empty() {
        entry.insert(
            "child_source_indices".to_string(),
            serde_json::json!(g.child_source_indices),
        );
    }
    serde_json::Value::Object(entry)
}

fn print_boundary_data_human(bd: &BoundaryData) {
    if bd.is_empty {
        eprintln!("Boundaries: not configured");
        return;
    }

    let mut header_parts = vec![
        format!("{} {}", bd.zones.len(), pluralize("zone", bd.zones.len())),
        format!("{} {}", bd.rules.len(), pluralize("rule", bd.rules.len())),
    ];
    if !bd.logical_groups.is_empty() {
        header_parts.push(format!(
            "{} logical {}",
            bd.logical_groups.len(),
            pluralize("group", bd.logical_groups.len())
        ));
    }
    eprintln!("Boundaries: {}", header_parts.join(", "));

    // Guard each section symmetrically: a leading header with an empty
    // body reads as "fallow ran but the data is mysteriously absent". A
    // missing section reads as "this category is not configured".
    if !bd.zones.is_empty() {
        eprintln!("\nZones:");
        for zone in &bd.zones {
            eprintln!(
                "  {:<20} {} {}  {}",
                zone.name,
                zone.file_count,
                pluralize("file", zone.file_count),
                zone.patterns.join(", ")
            );
        }
    }

    if !bd.rules.is_empty() {
        eprintln!("\nRules:");
        for rule in &bd.rules {
            if rule.allow.is_empty() {
                eprintln!("  {:<20} (isolated, no imports allowed)", rule.from);
            } else {
                eprintln!("  {:<20} → {}", rule.from, rule.allow.join(", "));
            }
        }
    }

    if !bd.logical_groups.is_empty() {
        eprintln!("\nLogical groups:");
        // Render non-`ok` groups first so misconfigured autoDiscover paths
        // surface at the top of the section where they cannot be missed.
        // JSON output stays in user-declaration order; only the human
        // render reorders. Stable-sort preserves declaration order within
        // each status bucket.
        let mut ordered: Vec<&LogicalGroupInfo> = bd.logical_groups.iter().collect();
        ordered.sort_by_key(|g| match g.status {
            fallow_config::LogicalGroupStatus::InvalidPath => 0,
            fallow_config::LogicalGroupStatus::Empty => 1,
            fallow_config::LogicalGroupStatus::Ok => 2,
        });
        for g in ordered {
            let status_suffix = match g.status {
                fallow_config::LogicalGroupStatus::Ok => String::new(),
                fallow_config::LogicalGroupStatus::Empty => " (empty)".to_owned(),
                fallow_config::LogicalGroupStatus::InvalidPath => " (invalid path)".to_owned(),
            };
            // When a fallback zone exists the total `file_count` packs two
            // numbers ("children + fallback"). Split them inline so the
            // human reader can see the breakdown without cross-referencing
            // `zones[]`. The JSON keeps only the aggregate per the
            // single-edge-weight Sankey-renderer requirement.
            let file_count_render = if g.fallback_zone.is_some() {
                format!(
                    "{} {} ({} children + {} fallback)",
                    g.file_count,
                    pluralize("file", g.file_count),
                    g.child_file_count,
                    g.fallback_file_count
                )
            } else {
                format!("{} {}", g.file_count, pluralize("file", g.file_count))
            };
            eprintln!(
                "  {:<20} {}  autoDiscover: {}{}",
                g.name,
                file_count_render,
                g.auto_discover.join(", "),
                status_suffix
            );
            if !g.children.is_empty() {
                eprintln!("    children: {}", g.children.join(", "));
            }
        }
    }
}

/// Naive English pluralizer: `(noun, 1)` -> `noun`, otherwise `noun + "s"`.
/// Covers `zone`, `rule`, `group`, `file`; intentionally NOT general-purpose
/// (would need irregulars `boundary`/`boundaries` if used more broadly).
fn pluralize(noun: &str, count: usize) -> String {
    if count == 1 {
        noun.to_owned()
    } else {
        format!("{noun}s")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── should_show_all ─────────────────────────────────────────

    fn make_opts(
        entry_points: bool,
        files: bool,
        plugins: bool,
        boundaries: bool,
    ) -> ListOptions<'static> {
        ListOptions {
            root: std::path::Path::new("/project"),
            config_path: &None,
            output: OutputFormat::Human,
            threads: 4,
            no_cache: false,
            entry_points,
            files,
            plugins,
            boundaries,
            production: false,
        }
    }

    #[test]
    fn show_all_when_no_flags_set() {
        assert!(should_show_all(&make_opts(false, false, false, false)));
    }

    #[test]
    fn not_show_all_when_entry_points_set() {
        assert!(!should_show_all(&make_opts(true, false, false, false)));
    }

    #[test]
    fn not_show_all_when_files_set() {
        assert!(!should_show_all(&make_opts(false, true, false, false)));
    }

    #[test]
    fn not_show_all_when_plugins_set() {
        assert!(!should_show_all(&make_opts(false, false, true, false)));
    }

    #[test]
    fn not_show_all_when_boundaries_set() {
        assert!(!should_show_all(&make_opts(false, false, false, true)));
    }

    #[test]
    fn not_show_all_when_all_flags_set() {
        assert!(!should_show_all(&make_opts(true, true, true, true)));
    }

    #[test]
    fn not_show_all_when_two_flags_set() {
        assert!(!should_show_all(&make_opts(true, true, false, false)));
        assert!(!should_show_all(&make_opts(true, false, true, false)));
        assert!(!should_show_all(&make_opts(false, true, true, false)));
    }

    // ── needs_file_discovery ────────────────────────────────────

    #[test]
    fn needs_discovery_when_files_requested() {
        assert!(needs_file_discovery(true, false, false, false));
    }

    #[test]
    fn needs_discovery_when_show_all() {
        assert!(needs_file_discovery(false, true, false, false));
    }

    #[test]
    fn needs_discovery_when_entry_points_requested() {
        assert!(needs_file_discovery(false, false, true, false));
    }

    #[test]
    fn needs_discovery_when_boundaries_requested() {
        assert!(needs_file_discovery(false, false, false, true));
    }

    #[test]
    fn no_discovery_when_only_plugins() {
        // plugins=true but show_all=false, files=false, entry_points=false, boundaries=false
        assert!(!needs_file_discovery(false, false, false, false));
    }

    // ── ListOptions construction ────────────────────────────────

    #[test]
    fn list_options_default_flags() {
        let opts = make_opts(false, false, false, false);
        assert!(should_show_all(&opts));
    }

    #[test]
    fn list_options_single_flag() {
        let opts = make_opts(true, false, false, false);
        assert!(!should_show_all(&opts));
        assert!(needs_file_discovery(
            opts.files,
            should_show_all(&opts),
            opts.entry_points,
            opts.boundaries,
        ));
    }

    // ── boundary_data_to_json (issue #373) ──────────────────────

    fn empty_boundary_data() -> BoundaryData {
        BoundaryData {
            zones: vec![],
            rules: vec![],
            logical_groups: vec![],
            is_empty: true,
        }
    }

    #[test]
    fn boundary_json_empty_includes_logical_groups_key() {
        let json = boundary_data_to_json(&empty_boundary_data());
        assert_eq!(json["configured"], false);
        // Consumers grepping for the key must see it even when boundaries are
        // not configured; otherwise the absence-of-key vs absence-of-groups
        // distinction is ambiguous.
        assert!(json["logical_groups"].is_array());
        assert_eq!(json["logical_groups"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn boundary_json_empty_branch_includes_all_count_fields() {
        // Regression: previously the empty branch emitted arrays without
        // their matching `*_count` siblings, so consumers had to first
        // branch on `configured` before reading `zone_count`. Issue #373
        // reviewer feedback: keep schema symmetric across both branches.
        let json = boundary_data_to_json(&empty_boundary_data());
        assert_eq!(json["zone_count"], 0);
        assert_eq!(json["rule_count"], 0);
        assert_eq!(json["logical_group_count"], 0);
    }

    #[test]
    fn pluralize_singular_plural() {
        assert_eq!(pluralize("file", 0), "files");
        assert_eq!(pluralize("file", 1), "file");
        assert_eq!(pluralize("file", 2), "files");
        assert_eq!(pluralize("zone", 1), "zone");
        assert_eq!(pluralize("group", 1), "group");
    }

    #[test]
    fn boundary_json_logical_group_carries_all_fields() {
        let bd = BoundaryData {
            zones: vec![
                ZoneInfo {
                    name: "features/auth".to_string(),
                    patterns: vec!["src/features/auth/**".to_string()],
                    file_count: 3,
                },
                ZoneInfo {
                    name: "features/billing".to_string(),
                    patterns: vec!["src/features/billing/**".to_string()],
                    file_count: 5,
                },
            ],
            rules: vec![],
            logical_groups: vec![LogicalGroupInfo {
                name: "features".to_string(),
                children: vec!["features/auth".to_string(), "features/billing".to_string()],
                auto_discover: vec!["./src/features/".to_string()],
                authored_rule: Some(fallow_config::AuthoredRule {
                    allow: vec!["shared".to_string()],
                    allow_type_only: vec!["types".to_string()],
                }),
                fallback_zone: None,
                source_zone_index: 1,
                status: fallow_config::LogicalGroupStatus::Ok,
                file_count: 8,
                child_file_count: 8,
                fallback_file_count: 0,
                merged_from: None,
                original_zone_root: None,
                child_source_indices: vec![],
            }],
            is_empty: false,
        };
        let json = boundary_data_to_json(&bd);

        assert_eq!(json["logical_group_count"], 1);
        let groups = json["logical_groups"].as_array().unwrap();
        assert_eq!(groups.len(), 1);
        let g = &groups[0];
        assert_eq!(g["name"], "features");
        assert_eq!(g["children"][0], "features/auth");
        assert_eq!(g["children"][1], "features/billing");
        // Verbatim string preserved through the JSON layer.
        assert_eq!(g["auto_discover"][0], "./src/features/");
        assert_eq!(g["status"], "ok");
        assert_eq!(g["source_zone_index"], 1);
        assert_eq!(g["file_count"], 8);
        assert_eq!(g["authored_rule"]["allow"][0], "shared");
        assert_eq!(g["authored_rule"]["allow_type_only"][0], "types");
        // fallback_zone omitted via skip_serializing_if when None.
        assert!(g.get("fallback_zone").is_none());
        // Optional follow-up fields omitted on the common single-path case.
        assert!(g.get("merged_from").is_none());
        assert!(g.get("original_zone_root").is_none());
        assert!(g.get("child_source_indices").is_none());
    }

    #[test]
    fn boundary_json_logical_group_status_serializations() {
        for (status, expected) in [
            (fallow_config::LogicalGroupStatus::Ok, "ok"),
            (fallow_config::LogicalGroupStatus::Empty, "empty"),
            (
                fallow_config::LogicalGroupStatus::InvalidPath,
                "invalid_path",
            ),
        ] {
            let bd = BoundaryData {
                zones: vec![],
                rules: vec![],
                logical_groups: vec![LogicalGroupInfo {
                    name: "features".to_string(),
                    children: vec![],
                    auto_discover: vec!["src/features".to_string()],
                    authored_rule: None,
                    fallback_zone: None,
                    source_zone_index: 0,
                    status,
                    file_count: 0,
                    child_file_count: 0,
                    fallback_file_count: 0,
                    merged_from: None,
                    original_zone_root: None,
                    child_source_indices: vec![],
                }],
                is_empty: false,
            };
            let json = boundary_data_to_json(&bd);
            assert_eq!(json["logical_groups"][0]["status"], expected);
        }
    }

    #[test]
    fn boundary_json_logical_group_fallback_zone_round_trip() {
        let bd = BoundaryData {
            zones: vec![ZoneInfo {
                name: "features".to_string(),
                patterns: vec!["src/features/**".to_string()],
                file_count: 2,
            }],
            rules: vec![],
            logical_groups: vec![LogicalGroupInfo {
                name: "features".to_string(),
                children: vec![],
                auto_discover: vec!["src/features".to_string()],
                authored_rule: None,
                fallback_zone: Some("features".to_string()),
                source_zone_index: 0,
                status: fallow_config::LogicalGroupStatus::Empty,
                file_count: 2,
                child_file_count: 0,
                fallback_file_count: 2,
                merged_from: None,
                original_zone_root: None,
                child_source_indices: vec![],
            }],
            is_empty: false,
        };
        let json = boundary_data_to_json(&bd);
        // Bulletproof shape: the fallback zone cross-reference is present
        // when the parent has both `patterns` and `autoDiscover`.
        assert_eq!(json["logical_groups"][0]["fallback_zone"], "features");
    }

    #[test]
    fn boundary_json_logical_group_authored_rule_omits_empty_allow_type_only() {
        let bd = BoundaryData {
            zones: vec![],
            rules: vec![],
            logical_groups: vec![LogicalGroupInfo {
                name: "features".to_string(),
                children: vec![],
                auto_discover: vec!["src/features".to_string()],
                authored_rule: Some(fallow_config::AuthoredRule {
                    allow: vec!["shared".to_string()],
                    allow_type_only: vec![],
                }),
                fallback_zone: None,
                source_zone_index: 0,
                status: fallow_config::LogicalGroupStatus::Empty,
                file_count: 0,
                child_file_count: 0,
                fallback_file_count: 0,
                merged_from: None,
                original_zone_root: None,
                child_source_indices: vec![],
            }],
            is_empty: false,
        };
        let json = boundary_data_to_json(&bd);
        let rule = &json["logical_groups"][0]["authored_rule"];
        assert_eq!(rule["allow"][0], "shared");
        assert!(rule.get("allow_type_only").is_none());
    }

    // ── follow-up field tests (panel post-impl pass) ────────────

    #[test]
    fn boundary_json_logical_group_merged_from_when_duplicates() {
        let bd = BoundaryData {
            zones: vec![],
            rules: vec![],
            logical_groups: vec![LogicalGroupInfo {
                name: "features".to_string(),
                children: vec![],
                auto_discover: vec!["src/features".to_string(), "src/modules".to_string()],
                authored_rule: None,
                fallback_zone: None,
                source_zone_index: 0,
                status: fallow_config::LogicalGroupStatus::Ok,
                file_count: 0,
                child_file_count: 0,
                fallback_file_count: 0,
                merged_from: Some(vec![0, 3]),
                original_zone_root: None,
                child_source_indices: vec![],
            }],
            is_empty: false,
        };
        let json = boundary_data_to_json(&bd);
        let g = &json["logical_groups"][0];
        // The JSON surfaces the duplicate-merge that tracing::warn! would
        // otherwise hide from --format json consumers.
        assert_eq!(g["merged_from"][0], 0);
        assert_eq!(g["merged_from"][1], 3);
    }

    #[test]
    fn boundary_json_logical_group_original_zone_root_emitted() {
        let bd = BoundaryData {
            zones: vec![],
            rules: vec![],
            logical_groups: vec![LogicalGroupInfo {
                name: "features".to_string(),
                children: vec![],
                auto_discover: vec!["src/features".to_string()],
                authored_rule: None,
                fallback_zone: None,
                source_zone_index: 0,
                status: fallow_config::LogicalGroupStatus::Ok,
                file_count: 0,
                child_file_count: 0,
                fallback_file_count: 0,
                merged_from: None,
                original_zone_root: Some("packages/app/".to_string()),
                child_source_indices: vec![],
            }],
            is_empty: false,
        };
        let json = boundary_data_to_json(&bd);
        assert_eq!(
            json["logical_groups"][0]["original_zone_root"],
            "packages/app/"
        );
    }

    #[test]
    fn boundary_json_logical_group_child_source_indices_emitted_for_multi_path() {
        let bd = BoundaryData {
            zones: vec![],
            rules: vec![],
            logical_groups: vec![LogicalGroupInfo {
                name: "features".to_string(),
                children: vec!["features/auth".to_string(), "features/billing".to_string()],
                auto_discover: vec!["src/features".to_string(), "src/modules".to_string()],
                authored_rule: None,
                fallback_zone: None,
                source_zone_index: 0,
                status: fallow_config::LogicalGroupStatus::Ok,
                file_count: 0,
                child_file_count: 0,
                fallback_file_count: 0,
                merged_from: None,
                original_zone_root: None,
                child_source_indices: vec![0, 1],
            }],
            is_empty: false,
        };
        let json = boundary_data_to_json(&bd);
        assert_eq!(json["logical_groups"][0]["child_source_indices"][0], 0);
        assert_eq!(json["logical_groups"][0]["child_source_indices"][1], 1);
    }
}