Skip to main content

fallow_cli/report/
json.rs

1use crate::report::sink::outln;
2use std::collections::BTreeMap;
3use std::path::Path;
4use std::process::ExitCode;
5use std::time::Duration;
6
7use fallow_core::duplicates::DuplicationReport;
8use fallow_core::results::AnalysisResults;
9use fallow_types::envelope::{CheckSummary, ElapsedMs, EntryPoints, SchemaVersion, ToolVersion};
10
11use super::{emit_json, normalize_uri};
12use crate::explain;
13use crate::output_dupes::DupesReportPayload;
14use crate::output_envelope::{
15    CheckGroupedEntry, CheckGroupedOutput, CheckOutput, DupesOutput, FallowOutput, GroupByMode,
16    HealthOutput, serialize_root_output,
17};
18use crate::report::grouping::{OwnershipResolver, ResultGroup};
19
20fn apply_config_fixable_to_duplicate_exports(results: &mut AnalysisResults, config_fixable: bool) {
21    if !config_fixable {
22        return;
23    }
24    for finding in &mut results.duplicate_exports {
25        finding.set_config_fixable(true);
26    }
27}
28
29pub(super) struct PrintJsonInput<'a> {
30    pub(super) results: &'a AnalysisResults,
31    pub(super) root: &'a Path,
32    pub(super) elapsed: Duration,
33    pub(super) explain: bool,
34    pub(super) regression: Option<&'a crate::regression::RegressionOutcome>,
35    pub(super) baseline_matched: Option<(usize, usize)>,
36    pub(super) config_fixable: bool,
37}
38
39pub(super) fn print_json(input: &PrintJsonInput<'_>) -> ExitCode {
40    let results = input.results;
41    let root = input.root;
42    let elapsed = input.elapsed;
43    let explain = input.explain;
44    let regression = input.regression;
45    let baseline_matched = input.baseline_matched;
46    let config_fixable = input.config_fixable;
47    match build_json_with_config_fixable(results, root, elapsed, config_fixable) {
48        Ok(mut output) => {
49            if let Some(outcome) = regression
50                && let serde_json::Value::Object(ref mut map) = output
51            {
52                map.insert("regression".to_string(), outcome.to_json());
53            }
54            if let Some((entries, matched)) = baseline_matched
55                && let serde_json::Value::Object(ref mut map) = output
56            {
57                map.insert(
58                    "baseline".to_string(),
59                    serde_json::json!({
60                        "entries": entries,
61                        "matched": matched,
62                    }),
63                );
64            }
65            if explain {
66                insert_meta(&mut output, explain::check_meta());
67            }
68            emit_json(&output, "JSON")
69        }
70        Err(e) => {
71            eprintln!("Error: failed to serialize results: {e}");
72            ExitCode::from(2)
73        }
74    }
75}
76
77#[must_use]
78pub(super) struct PrintGroupedJsonInput<'a> {
79    pub(super) groups: &'a [ResultGroup],
80    pub(super) original: &'a AnalysisResults,
81    pub(super) root: &'a Path,
82    pub(super) elapsed: Duration,
83    pub(super) explain: bool,
84    pub(super) resolver: &'a OwnershipResolver,
85    pub(super) config_fixable: bool,
86}
87
88pub(super) fn print_grouped_json(input: &PrintGroupedJsonInput<'_>) -> ExitCode {
89    let groups = input.groups;
90    let original = input.original;
91    let root = input.root;
92    let elapsed = input.elapsed;
93    let explain = input.explain;
94    let resolver = input.resolver;
95    let config_fixable = input.config_fixable;
96    let entries: Vec<CheckGroupedEntry> = groups
97        .iter()
98        .map(|group| {
99            let mut results = group.results.clone();
100            apply_config_fixable_to_duplicate_exports(&mut results, config_fixable);
101            CheckGroupedEntry {
102                key: group.key.clone(),
103                owners: group.owners.clone(),
104                total_issues: results.total_issues(),
105                results,
106            }
107        })
108        .collect();
109
110    let envelope = CheckGroupedOutput {
111        schema_version: SchemaVersion(SCHEMA_VERSION),
112        version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
113        elapsed_ms: ElapsedMs(elapsed.as_millis() as u64),
114        grouped_by: group_by_mode_from_label(resolver.mode_label()),
115        total_issues: original.total_issues(),
116        groups: entries,
117        meta: None,
118        next_steps: crate::report::suggestions::build_dead_code_next_steps(
119            original,
120            root,
121            crate::report::suggestions::setup_pointer_applicable(root),
122            crate::report::suggestions::due_impact_digest(root),
123        ),
124    };
125
126    let mut output = match serialize_root_output(FallowOutput::CheckGrouped(envelope)) {
127        Ok(value) => value,
128        Err(e) => {
129            eprintln!("Error: failed to serialize grouped results: {e}");
130            return ExitCode::from(2);
131        }
132    };
133
134    let root_prefix = format!("{}/", root.display());
135    if let Some(arr) = output.get_mut("groups").and_then(|v| v.as_array_mut()) {
136        for entry in arr {
137            strip_root_prefix(entry, &root_prefix);
138            harmonize_multi_kind_suppress_line_actions(entry);
139        }
140    }
141
142    if explain {
143        insert_meta(&mut output, explain::check_meta());
144    }
145
146    emit_json(&output, "JSON")
147}
148
149#[allow(
150    clippy::redundant_pub_crate,
151    reason = "used through report module re-export by combined.rs, audit.rs, flags.rs"
152)]
153pub(crate) const SCHEMA_VERSION: u32 = 7;
154
155#[allow(
156    dead_code,
157    reason = "used by the fallow-cli library target for embedders, but dead in the binary target"
158)]
159pub fn build_json(
160    results: &AnalysisResults,
161    root: &Path,
162    elapsed: Duration,
163) -> Result<serde_json::Value, serde_json::Error> {
164    build_json_with_config_fixable(
165        results,
166        root,
167        elapsed,
168        crate::fix::is_config_fixable(root, None),
169    )
170}
171
172pub fn build_json_with_config_fixable(
173    results: &AnalysisResults,
174    root: &Path,
175    elapsed: Duration,
176    config_fixable: bool,
177) -> Result<serde_json::Value, serde_json::Error> {
178    let mut envelope = build_check_output(results, root, elapsed, config_fixable);
179    envelope.next_steps = crate::report::suggestions::build_dead_code_next_steps(
180        results,
181        root,
182        crate::report::suggestions::setup_pointer_applicable(root),
183        crate::report::suggestions::due_impact_digest(root),
184    );
185    let mut output = serialize_root_output(FallowOutput::Check(envelope))?;
186    postprocess_check_json(&mut output, root);
187    Ok(output)
188}
189
190pub fn build_check_json_payload_with_config_fixable(
191    results: &AnalysisResults,
192    root: &Path,
193    elapsed: Duration,
194    config_fixable: bool,
195) -> Result<serde_json::Value, serde_json::Error> {
196    let envelope = build_check_output(results, root, elapsed, config_fixable);
197    let mut output = serde_json::to_value(&envelope)?;
198    postprocess_check_json(&mut output, root);
199    Ok(output)
200}
201
202fn build_check_output(
203    results: &AnalysisResults,
204    root: &Path,
205    elapsed: Duration,
206    config_fixable: bool,
207) -> CheckOutput {
208    let mut owned_results = results.clone();
209    apply_config_fixable_to_duplicate_exports(&mut owned_results, config_fixable);
210    CheckOutput {
211        schema_version: SchemaVersion(SCHEMA_VERSION),
212        version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
213        elapsed_ms: ElapsedMs(elapsed.as_millis() as u64),
214        total_issues: owned_results.total_issues(),
215        entry_points: owned_results
216            .entry_point_summary
217            .as_ref()
218            .map(|ep| EntryPoints {
219                total: ep.total,
220                sources: ep
221                    .by_source
222                    .iter()
223                    .map(|(k, v)| (k.replace(' ', "_"), *v))
224                    .collect(),
225            }),
226        summary: build_check_summary(&owned_results),
227        results: owned_results,
228        baseline_deltas: None,
229        baseline: None,
230        regression: None,
231        meta: None,
232        workspace_diagnostics: crate::runtime_support::workspace_diagnostics_for(root),
233        // Populated only at the standalone-command entry points; the combined
234        // and audit envelopes reuse this struct as a sub-block and aggregate
235        // their own `next_steps` at the top level, so it stays empty here.
236        next_steps: Vec::new(),
237    }
238}
239
240fn postprocess_check_json(output: &mut serde_json::Value, root: &Path) {
241    let root_prefix = format!("{}/", root.display());
242    strip_root_prefix(output, &root_prefix);
243    harmonize_multi_kind_suppress_line_actions(output);
244}
245
246/// Compute the per-category `CheckSummary` from analysis results.
247fn build_check_summary(results: &AnalysisResults) -> CheckSummary {
248    CheckSummary {
249        total_issues: results.total_issues(),
250        unused_files: results.unused_files.len(),
251        unused_exports: results.unused_exports.len(),
252        unused_types: results.unused_types.len(),
253        private_type_leaks: results.private_type_leaks.len(),
254        unused_dependencies: results.unused_dependencies.len()
255            + results.unused_dev_dependencies.len()
256            + results.unused_optional_dependencies.len(),
257        unused_enum_members: results.unused_enum_members.len(),
258        unused_class_members: results.unused_class_members.len(),
259        unused_store_members: results.unused_store_members.len(),
260        unresolved_imports: results.unresolved_imports.len(),
261        unlisted_dependencies: results.unlisted_dependencies.len(),
262        duplicate_exports: results.duplicate_exports.len(),
263        type_only_dependencies: results.type_only_dependencies.len(),
264        test_only_dependencies: results.test_only_dependencies.len(),
265        circular_dependencies: results.circular_dependencies.len(),
266        re_export_cycles: results.re_export_cycles.len(),
267        boundary_violations: results.boundary_violations.len(),
268        boundary_coverage_violations: results.boundary_coverage_violations.len(),
269        boundary_call_violations: results.boundary_call_violations.len(),
270        policy_violations: results.policy_violations.len(),
271        stale_suppressions: results.stale_suppressions.len(),
272        unused_catalog_entries: results.unused_catalog_entries.len(),
273        empty_catalog_groups: results.empty_catalog_groups.len(),
274        unresolved_catalog_references: results.unresolved_catalog_references.len(),
275        unused_dependency_overrides: results.unused_dependency_overrides.len(),
276        misconfigured_dependency_overrides: results.misconfigured_dependency_overrides.len(),
277        invalid_client_exports: results.invalid_client_exports.len(),
278        mixed_client_server_barrels: results.mixed_client_server_barrels.len(),
279        misplaced_directives: results.misplaced_directives.len(),
280        unprovided_injects: results.unprovided_injects.len(),
281        unrendered_components: results.unrendered_components.len(),
282        unused_component_props: results.unused_component_props.len(),
283        unused_component_emits: results.unused_component_emits.len(),
284        unused_component_inputs: results.unused_component_inputs.len(),
285        unused_component_outputs: results.unused_component_outputs.len(),
286        unused_svelte_events: results.unused_svelte_events.len(),
287        unused_server_actions: results.unused_server_actions.len(),
288        unused_load_data_keys: results.unused_load_data_keys.len(),
289        route_collisions: results.route_collisions.len(),
290        dynamic_segment_name_conflicts: results.dynamic_segment_name_conflicts.len(),
291    }
292}
293
294/// Recursively strip the root prefix from all string values in the JSON tree.
295///
296/// This converts absolute paths (e.g., `/home/runner/work/repo/repo/src/utils.ts`)
297/// to relative paths (`src/utils.ts`) for all output fields.
298pub fn strip_root_prefix(value: &mut serde_json::Value, prefix: &str) {
299    match value {
300        serde_json::Value::String(s) => {
301            if let Some(rest) = s.strip_prefix(prefix) {
302                *s = rest.to_string();
303            } else {
304                let normalized = normalize_uri(s);
305                let normalized_prefix = normalize_uri(prefix);
306                if let Some(rest) = normalized.strip_prefix(&normalized_prefix) {
307                    *s = rest.to_string();
308                } else if let Some(stripped) =
309                    strip_embedded_root_prefixes(&normalized, &normalized_prefix)
310                {
311                    *s = stripped;
312                }
313            }
314        }
315        serde_json::Value::Array(arr) => {
316            for item in arr {
317                strip_root_prefix(item, prefix);
318            }
319        }
320        serde_json::Value::Object(map) => {
321            for (_, v) in map.iter_mut() {
322                strip_root_prefix(v, prefix);
323            }
324        }
325        _ => {}
326    }
327}
328
329fn strip_embedded_root_prefixes(value: &str, prefix: &str) -> Option<String> {
330    let mut output = String::with_capacity(value.len());
331    let mut changed = false;
332    let mut last = 0;
333    let mut search_from = 0;
334
335    while let Some(offset) = value[search_from..].find(prefix) {
336        let index = search_from + offset;
337        let can_strip = index > 0
338            && value[..index]
339                .chars()
340                .next_back()
341                .is_some_and(is_embedded_path_boundary);
342
343        if can_strip {
344            output.push_str(&value[last..index]);
345            last = index + prefix.len();
346            changed = true;
347        }
348
349        search_from = index + prefix.len();
350    }
351
352    if changed {
353        output.push_str(&value[last..]);
354        Some(output)
355    } else {
356        None
357    }
358}
359
360fn is_embedded_path_boundary(c: char) -> bool {
361    c.is_whitespace() || matches!(c, '"' | '\'' | '`' | '(' | '[' | '{' | ':' | '=')
362}
363
364type SuppressAnchor = (String, u64);
365
366#[allow(
367    clippy::redundant_pub_crate,
368    reason = "used through report module re-export by audit.rs"
369)]
370pub(crate) fn harmonize_multi_kind_suppress_line_actions(output: &mut serde_json::Value) {
371    let mut anchors: BTreeMap<SuppressAnchor, Vec<String>> = BTreeMap::new();
372    collect_suppress_line_anchors(output, &mut anchors);
373
374    anchors.retain(|_, kinds| {
375        sort_suppression_kinds(kinds);
376        kinds.dedup();
377        kinds.len() > 1
378    });
379    if anchors.is_empty() {
380        return;
381    }
382
383    rewrite_suppress_line_actions(output, &anchors);
384}
385
386fn collect_suppress_line_anchors(
387    value: &serde_json::Value,
388    anchors: &mut BTreeMap<SuppressAnchor, Vec<String>>,
389) {
390    match value {
391        serde_json::Value::Object(map) => {
392            if let Some(anchor) = suppression_anchor(map)
393                && let Some(actions) = map.get("actions").and_then(serde_json::Value::as_array)
394            {
395                for action in actions {
396                    if let Some(comment) = suppress_line_comment(action) {
397                        for kind in parse_suppress_line_comment(comment) {
398                            let kinds = anchors.entry(anchor.clone()).or_default();
399                            if !kinds.iter().any(|existing| existing == &kind) {
400                                kinds.push(kind);
401                            }
402                        }
403                    }
404                }
405            }
406
407            for child in map.values() {
408                collect_suppress_line_anchors(child, anchors);
409            }
410        }
411        serde_json::Value::Array(items) => {
412            for item in items {
413                collect_suppress_line_anchors(item, anchors);
414            }
415        }
416        _ => {}
417    }
418}
419
420fn rewrite_suppress_line_actions(
421    value: &mut serde_json::Value,
422    anchors: &BTreeMap<SuppressAnchor, Vec<String>>,
423) {
424    match value {
425        serde_json::Value::Object(map) => {
426            if let Some(anchor) = suppression_anchor(map)
427                && let Some(kinds) = anchors.get(&anchor)
428            {
429                let comment = format!("// fallow-ignore-next-line {}", kinds.join(", "));
430                if let Some(actions) = map
431                    .get_mut("actions")
432                    .and_then(serde_json::Value::as_array_mut)
433                {
434                    for action in actions {
435                        if suppress_line_comment(action).is_some()
436                            && let serde_json::Value::Object(action_map) = action
437                        {
438                            action_map.insert("comment".to_string(), serde_json::json!(comment));
439                        }
440                    }
441                }
442            }
443
444            for child in map.values_mut() {
445                rewrite_suppress_line_actions(child, anchors);
446            }
447        }
448        serde_json::Value::Array(items) => {
449            for item in items {
450                rewrite_suppress_line_actions(item, anchors);
451            }
452        }
453        _ => {}
454    }
455}
456
457fn suppression_anchor(map: &serde_json::Map<String, serde_json::Value>) -> Option<SuppressAnchor> {
458    let path = map
459        .get("path")
460        .or_else(|| map.get("from_path"))
461        .and_then(serde_json::Value::as_str)?;
462    let line = map.get("line").and_then(serde_json::Value::as_u64)?;
463    Some((path.to_string(), line))
464}
465
466fn suppress_line_comment(action: &serde_json::Value) -> Option<&str> {
467    (action.get("type").and_then(serde_json::Value::as_str) == Some("suppress-line"))
468        .then_some(())
469        .and_then(|()| action.get("comment").and_then(serde_json::Value::as_str))
470}
471
472fn parse_suppress_line_comment(comment: &str) -> Vec<String> {
473    comment
474        .strip_prefix("// fallow-ignore-next-line ")
475        .map(|rest| {
476            rest.split(|c: char| c == ',' || c.is_whitespace())
477                .filter(|token| !token.is_empty())
478                .map(str::to_string)
479                .collect()
480        })
481        .unwrap_or_default()
482}
483
484fn sort_suppression_kinds(kinds: &mut [String]) {
485    kinds.sort_by_key(|kind| suppression_kind_rank(kind));
486}
487
488fn suppression_kind_rank(kind: &str) -> usize {
489    match kind {
490        "unused-file" => 0,
491        "unused-export" => 1,
492        "unused-type" => 2,
493        "private-type-leak" => 3,
494        "unused-enum-member" => 4,
495        "unused-class-member" => 5,
496        "unused-store-member" => 6,
497        "unresolved-import" => 7,
498        "unlisted-dependency" => 8,
499        "duplicate-export" => 9,
500        "circular-dependency" => 10,
501        "re-export-cycle" => 11,
502        "boundary-violation" => 12,
503        "code-duplication" => 13,
504        "complexity" => 14,
505        "unprovided-inject" => 15,
506        "unrendered-component" => 16,
507        "unused-server-action" => 17,
508        _ => usize::MAX,
509    }
510}
511
512pub fn build_baseline_deltas_json<'a>(
513    total_delta: i64,
514    per_category: impl Iterator<Item = (&'a str, usize, usize, i64)>,
515) -> serde_json::Value {
516    let mut per_cat = serde_json::Map::new();
517    for (cat, current, baseline, delta) in per_category {
518        per_cat.insert(
519            cat.to_string(),
520            serde_json::json!({
521                "current": current,
522                "baseline": baseline,
523                "delta": delta,
524            }),
525        );
526    }
527    serde_json::json!({
528        "total_delta": total_delta,
529        "per_category": per_cat
530    })
531}
532
533/// Insert a `_meta` key into a JSON object value.
534fn insert_meta(output: &mut serde_json::Value, meta: serde_json::Value) {
535    if let serde_json::Value::Object(map) = output {
536        let telemetry = map
537            .get("_meta")
538            .and_then(|existing| existing.get("telemetry"))
539            .cloned();
540        let mut meta = meta;
541        if let (Some(telemetry), Some(meta_map)) = (telemetry, meta.as_object_mut()) {
542            meta_map.insert("telemetry".to_string(), telemetry);
543        }
544        map.insert("_meta".to_string(), meta);
545    }
546}
547
548pub fn build_health_json(
549    report: &crate::health_types::HealthReport,
550    root: &Path,
551    elapsed: Duration,
552    explain: bool,
553) -> Result<serde_json::Value, serde_json::Error> {
554    let envelope = HealthOutput {
555        schema_version: SchemaVersion(SCHEMA_VERSION),
556        version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
557        elapsed_ms: ElapsedMs(elapsed.as_millis() as u64),
558        report: report.clone(),
559        grouped_by: None,
560        groups: None,
561        meta: None,
562        workspace_diagnostics: crate::runtime_support::workspace_diagnostics_for(root),
563        next_steps: crate::report::suggestions::build_health_next_steps(
564            report,
565            root,
566            crate::report::suggestions::setup_pointer_applicable(root),
567            crate::report::suggestions::due_impact_digest(root),
568        ),
569    };
570    let mut output = serialize_root_output(FallowOutput::Health(envelope))?;
571    let root_prefix = format!("{}/", root.display());
572    strip_root_prefix(&mut output, &root_prefix);
573    if explain {
574        insert_meta(&mut output, explain::health_meta());
575    }
576    Ok(output)
577}
578
579pub(super) fn print_health_json(
580    report: &crate::health_types::HealthReport,
581    root: &Path,
582    elapsed: Duration,
583    explain: bool,
584) -> ExitCode {
585    match build_health_json(report, root, elapsed, explain) {
586        Ok(output) => emit_json(&output, "JSON"),
587        Err(e) => {
588            eprintln!("Error: failed to serialize health report: {e}");
589            ExitCode::from(2)
590        }
591    }
592}
593
594pub fn build_grouped_health_json(
595    report: &crate::health_types::HealthReport,
596    grouping: &crate::health_types::HealthGrouping,
597    root: &Path,
598    elapsed: Duration,
599    explain: bool,
600) -> Result<serde_json::Value, serde_json::Error> {
601    let root_prefix = format!("{}/", root.display());
602    let envelope = HealthOutput {
603        schema_version: SchemaVersion(SCHEMA_VERSION),
604        version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
605        elapsed_ms: ElapsedMs(elapsed.as_millis() as u64),
606        report: report.clone(),
607        grouped_by: Some(group_by_mode_from_label(grouping.mode)),
608        groups: None,
609        meta: None,
610        workspace_diagnostics: crate::runtime_support::workspace_diagnostics_for(root),
611        next_steps: crate::report::suggestions::build_health_next_steps(
612            report,
613            root,
614            crate::report::suggestions::setup_pointer_applicable(root),
615            crate::report::suggestions::due_impact_digest(root),
616        ),
617    };
618    let mut output = serialize_root_output(FallowOutput::Health(envelope))?;
619    strip_root_prefix(&mut output, &root_prefix);
620
621    let group_values: Vec<serde_json::Value> = grouping
622        .groups
623        .iter()
624        .map(|g| {
625            let mut value = serde_json::to_value(g)?;
626            strip_root_prefix(&mut value, &root_prefix);
627            Ok(value)
628        })
629        .collect::<Result<_, serde_json::Error>>()?;
630
631    if let serde_json::Value::Object(ref mut map) = output {
632        map.insert("groups".to_string(), serde_json::Value::Array(group_values));
633    }
634
635    if explain {
636        insert_meta(&mut output, explain::health_meta());
637    }
638
639    Ok(output)
640}
641
642pub(super) fn print_grouped_health_json(
643    report: &crate::health_types::HealthReport,
644    grouping: &crate::health_types::HealthGrouping,
645    root: &Path,
646    elapsed: Duration,
647    explain: bool,
648) -> ExitCode {
649    match build_grouped_health_json(report, grouping, root, elapsed, explain) {
650        Ok(output) => emit_json(&output, "JSON"),
651        Err(e) => {
652            eprintln!("Error: failed to serialize grouped health report: {e}");
653            ExitCode::from(2)
654        }
655    }
656}
657
658pub fn build_duplication_json(
659    report: &DuplicationReport,
660    root: &Path,
661    elapsed: Duration,
662    explain: bool,
663) -> Result<serde_json::Value, serde_json::Error> {
664    let payload = DupesReportPayload::from_report(report);
665    let next_steps = crate::report::suggestions::build_dupes_next_steps(
666        &payload,
667        root,
668        crate::report::suggestions::setup_pointer_applicable(root),
669        crate::report::suggestions::due_impact_digest(root),
670    );
671    let envelope = DupesOutput {
672        schema_version: SchemaVersion(SCHEMA_VERSION),
673        version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
674        elapsed_ms: ElapsedMs(elapsed.as_millis() as u64),
675        report: payload,
676        grouped_by: None,
677        total_issues: None,
678        groups: None,
679        meta: None,
680        workspace_diagnostics: crate::runtime_support::workspace_diagnostics_for(root),
681        next_steps,
682    };
683    let mut output = serialize_root_output(FallowOutput::Dupes(envelope))?;
684    let root_prefix = format!("{}/", root.display());
685    strip_root_prefix(&mut output, &root_prefix);
686
687    if explain {
688        insert_meta(&mut output, explain::dupes_meta());
689    }
690
691    Ok(output)
692}
693
694pub(super) fn print_duplication_json(
695    report: &DuplicationReport,
696    root: &Path,
697    elapsed: Duration,
698    explain: bool,
699) -> ExitCode {
700    match build_duplication_json(report, root, elapsed, explain) {
701        Ok(output) => emit_json(&output, "JSON"),
702        Err(e) => {
703            eprintln!("Error: failed to serialize duplication report: {e}");
704            ExitCode::from(2)
705        }
706    }
707}
708
709pub fn build_grouped_duplication_json(
710    report: &DuplicationReport,
711    grouping: &super::dupes_grouping::DuplicationGrouping,
712    root: &Path,
713    elapsed: Duration,
714    explain: bool,
715) -> Result<serde_json::Value, serde_json::Error> {
716    let root_prefix = format!("{}/", root.display());
717    let payload = DupesReportPayload::from_report(report);
718    let next_steps = crate::report::suggestions::build_dupes_next_steps(
719        &payload,
720        root,
721        crate::report::suggestions::setup_pointer_applicable(root),
722        crate::report::suggestions::due_impact_digest(root),
723    );
724    let envelope = DupesOutput {
725        schema_version: SchemaVersion(SCHEMA_VERSION),
726        version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
727        elapsed_ms: ElapsedMs(elapsed.as_millis() as u64),
728        report: payload,
729        grouped_by: Some(group_by_mode_from_label(grouping.mode)),
730        total_issues: Some(report.clone_groups.len()),
731        groups: None,
732        meta: None,
733        workspace_diagnostics: crate::runtime_support::workspace_diagnostics_for(root),
734        next_steps,
735    };
736    let mut output = serialize_root_output(FallowOutput::Dupes(envelope))?;
737    strip_root_prefix(&mut output, &root_prefix);
738
739    let group_values: Vec<serde_json::Value> = grouping
740        .groups
741        .iter()
742        .map(|g| {
743            let mut value = serde_json::to_value(g)?;
744            strip_root_prefix(&mut value, &root_prefix);
745            Ok(value)
746        })
747        .collect::<Result<_, serde_json::Error>>()?;
748
749    if let serde_json::Value::Object(ref mut map) = output {
750        map.insert("groups".to_string(), serde_json::Value::Array(group_values));
751    }
752
753    if explain {
754        insert_meta(&mut output, explain::dupes_meta());
755    }
756
757    Ok(output)
758}
759
760fn group_by_mode_from_label(label: &str) -> GroupByMode {
761    match label {
762        "directory" => GroupByMode::Directory,
763        "package" => GroupByMode::Package,
764        "section" => GroupByMode::Section,
765        _ => GroupByMode::Owner,
766    }
767}
768
769pub(super) fn print_grouped_duplication_json(
770    report: &DuplicationReport,
771    grouping: &super::dupes_grouping::DuplicationGrouping,
772    root: &Path,
773    elapsed: Duration,
774    explain: bool,
775) -> ExitCode {
776    match build_grouped_duplication_json(report, grouping, root, elapsed, explain) {
777        Ok(output) => emit_json(&output, "JSON"),
778        Err(e) => {
779            eprintln!("Error: failed to serialize grouped duplication report: {e}");
780            ExitCode::from(2)
781        }
782    }
783}
784
785pub(super) fn print_trace_json<T: serde::Serialize>(value: &T) {
786    match serde_json::to_string_pretty(value) {
787        Ok(json) => outln!("{json}"),
788        Err(e) => {
789            eprintln!("Error: failed to serialize trace output: {e}");
790            #[expect(
791                clippy::exit,
792                reason = "fatal serialization error requires immediate exit"
793            )]
794            std::process::exit(2);
795        }
796    }
797}
798
799#[cfg(test)]
800mod tests {
801    use super::*;
802    use crate::health_types::{
803        RuntimeCoverageAction, RuntimeCoverageConfidence, RuntimeCoverageDataSource,
804        RuntimeCoverageEvidence, RuntimeCoverageFinding, RuntimeCoverageHotPath,
805        RuntimeCoverageMessage, RuntimeCoverageReport, RuntimeCoverageReportVerdict,
806        RuntimeCoverageSchemaVersion, RuntimeCoverageSummary, RuntimeCoverageVerdict,
807        RuntimeCoverageWatermark,
808    };
809    use crate::report::test_helpers::sample_results;
810    use fallow_core::extract::MemberKind;
811    use fallow_core::results::*;
812    use std::path::PathBuf;
813    use std::time::Duration;
814
815    #[test]
816    fn json_output_has_metadata_fields() {
817        let root = PathBuf::from("/project");
818        let results = AnalysisResults::default();
819        let elapsed = Duration::from_millis(123);
820        let output = build_json(&results, &root, elapsed).expect("should serialize");
821
822        assert_eq!(output["kind"], "dead-code");
823        assert_eq!(output["schema_version"], 7);
824        assert!(output["version"].is_string());
825        assert_eq!(output["elapsed_ms"], 123);
826        assert_eq!(output["total_issues"], 0);
827    }
828
829    #[test]
830    fn json_output_includes_issue_arrays() {
831        let root = PathBuf::from("/project");
832        let results = sample_results(&root);
833        let elapsed = Duration::from_millis(50);
834        let output = build_json(&results, &root, elapsed).expect("should serialize");
835
836        assert_eq!(output["unused_files"].as_array().unwrap().len(), 1);
837        assert_eq!(output["unused_exports"].as_array().unwrap().len(), 1);
838        assert_eq!(output["unused_types"].as_array().unwrap().len(), 1);
839        assert_eq!(output["unused_dependencies"].as_array().unwrap().len(), 1);
840        assert_eq!(
841            output["unused_dev_dependencies"].as_array().unwrap().len(),
842            1
843        );
844        assert_eq!(output["unused_enum_members"].as_array().unwrap().len(), 1);
845        assert_eq!(output["unused_class_members"].as_array().unwrap().len(), 1);
846        assert_eq!(output["unresolved_imports"].as_array().unwrap().len(), 1);
847        assert_eq!(output["unlisted_dependencies"].as_array().unwrap().len(), 1);
848        assert_eq!(output["duplicate_exports"].as_array().unwrap().len(), 1);
849        assert_eq!(
850            output["type_only_dependencies"].as_array().unwrap().len(),
851            1
852        );
853        assert_eq!(output["circular_dependencies"].as_array().unwrap().len(), 1);
854    }
855
856    #[test]
857    fn health_json_includes_runtime_coverage_with_relative_paths_and_actions() {
858        let root = PathBuf::from("/project");
859        let report = crate::health_types::HealthReport {
860            runtime_coverage: Some(RuntimeCoverageReport {
861                schema_version: RuntimeCoverageSchemaVersion::V1,
862                verdict: RuntimeCoverageReportVerdict::ColdCodeDetected,
863                signals: Vec::new(),
864                summary: RuntimeCoverageSummary {
865                    data_source: RuntimeCoverageDataSource::Local,
866                    last_received_at: None,
867                    functions_tracked: 3,
868                    functions_hit: 1,
869                    functions_unhit: 1,
870                    functions_untracked: 1,
871                    coverage_percent: 33.3,
872                    trace_count: 2_847_291,
873                    period_days: 30,
874                    deployments_seen: 14,
875                    capture_quality: Some(crate::health_types::RuntimeCoverageCaptureQuality {
876                        window_seconds: 720,
877                        instances_observed: 1,
878                        lazy_parse_warning: true,
879                        untracked_ratio_percent: 42.5,
880                    }),
881                },
882                findings: vec![RuntimeCoverageFinding {
883                    id: "fallow:prod:deadbeef".to_owned(),
884                    stable_id: None,
885                    path: root.join("src/cold.ts"),
886                    function: "coldPath".to_owned(),
887                    line: 12,
888                    verdict: RuntimeCoverageVerdict::ReviewRequired,
889                    invocations: Some(0),
890                    confidence: RuntimeCoverageConfidence::Medium,
891                    evidence: RuntimeCoverageEvidence {
892                        static_status: "used".to_owned(),
893                        test_coverage: "not_covered".to_owned(),
894                        v8_tracking: "tracked".to_owned(),
895                        untracked_reason: None,
896                        observation_days: 30,
897                        deployments_observed: 14,
898                    },
899                    actions: vec![RuntimeCoverageAction {
900                        kind: "review-deletion".to_owned(),
901                        description: "Tracked in runtime coverage with zero invocations."
902                            .to_owned(),
903                        auto_fixable: false,
904                    }],
905                    source_hash: None,
906                }],
907                hot_paths: vec![RuntimeCoverageHotPath {
908                    id: "fallow:hot:cafebabe".to_owned(),
909                    stable_id: None,
910                    path: root.join("src/hot.ts"),
911                    function: "hotPath".to_owned(),
912                    line: 3,
913                    end_line: 9,
914                    invocations: 250,
915                    percentile: 99,
916                    actions: vec![],
917                }],
918                blast_radius: vec![],
919                importance: vec![],
920                watermark: Some(RuntimeCoverageWatermark::LicenseExpiredGrace),
921                warnings: vec![RuntimeCoverageMessage {
922                    code: "partial-merge".to_owned(),
923                    message: "Merged coverage omitted one chunk.".to_owned(),
924                }],
925            }),
926            ..Default::default()
927        };
928
929        let envelope = HealthOutput {
930            schema_version: SchemaVersion(SCHEMA_VERSION),
931            version: ToolVersion(env!("CARGO_PKG_VERSION").to_string()),
932            elapsed_ms: ElapsedMs(7),
933            report,
934            grouped_by: None,
935            groups: None,
936            meta: None,
937            workspace_diagnostics: Vec::new(),
938            next_steps: Vec::new(),
939        };
940        let mut output = serde_json::to_value(&envelope).expect("should serialize health envelope");
941        strip_root_prefix(&mut output, "/project/");
942
943        assert_eq!(
944            output["runtime_coverage"]["verdict"],
945            serde_json::Value::String("cold-code-detected".to_owned())
946        );
947        assert_eq!(
948            output["runtime_coverage"]["schema_version"],
949            serde_json::Value::String("1".to_owned())
950        );
951        assert_eq!(
952            output["runtime_coverage"]["summary"]["functions_tracked"],
953            serde_json::Value::from(3)
954        );
955        assert_eq!(
956            output["runtime_coverage"]["summary"]["coverage_percent"],
957            serde_json::Value::from(33.3)
958        );
959        let finding = &output["runtime_coverage"]["findings"][0];
960        assert_eq!(finding["path"], "src/cold.ts");
961        assert_eq!(finding["verdict"], "review_required");
962        assert_eq!(finding["id"], "fallow:prod:deadbeef");
963        assert_eq!(finding["actions"][0]["type"], "review-deletion");
964        let hot_path = &output["runtime_coverage"]["hot_paths"][0];
965        assert_eq!(hot_path["path"], "src/hot.ts");
966        assert_eq!(hot_path["function"], "hotPath");
967        assert_eq!(hot_path["percentile"], 99);
968        assert_eq!(
969            output["runtime_coverage"]["watermark"],
970            serde_json::Value::String("license-expired-grace".to_owned())
971        );
972        assert_eq!(
973            output["runtime_coverage"]["warnings"][0]["code"],
974            serde_json::Value::String("partial-merge".to_owned())
975        );
976    }
977
978    #[test]
979    fn json_metadata_fields_appear_first() {
980        let root = PathBuf::from("/project");
981        let results = AnalysisResults::default();
982        let elapsed = Duration::from_millis(0);
983        let output = build_json(&results, &root, elapsed).expect("should serialize");
984        let keys: Vec<&String> = output.as_object().unwrap().keys().collect();
985        assert_eq!(keys[0], "kind");
986        assert_eq!(keys[1], "schema_version");
987        assert_eq!(keys[2], "version");
988        assert_eq!(keys[3], "elapsed_ms");
989        assert_eq!(keys[4], "total_issues");
990    }
991
992    #[test]
993    fn json_total_issues_matches_results() {
994        let root = PathBuf::from("/project");
995        let results = sample_results(&root);
996        let total = results.total_issues();
997        let elapsed = Duration::from_millis(0);
998        let output = build_json(&results, &root, elapsed).expect("should serialize");
999
1000        assert_eq!(output["total_issues"], total);
1001    }
1002
1003    #[test]
1004    fn json_unused_export_contains_expected_fields() {
1005        let root = PathBuf::from("/project");
1006        let mut results = AnalysisResults::default();
1007        results
1008            .unused_exports
1009            .push(UnusedExportFinding::with_actions(UnusedExport {
1010                path: root.join("src/utils.ts"),
1011                export_name: "helperFn".to_string(),
1012                is_type_only: false,
1013                line: 10,
1014                col: 4,
1015                span_start: 120,
1016                is_re_export: false,
1017            }));
1018        let elapsed = Duration::from_millis(0);
1019        let output = build_json(&results, &root, elapsed).expect("should serialize");
1020
1021        let export = &output["unused_exports"][0];
1022        assert_eq!(export["export_name"], "helperFn");
1023        assert_eq!(export["line"], 10);
1024        assert_eq!(export["col"], 4);
1025        assert_eq!(export["is_type_only"], false);
1026        assert_eq!(export["span_start"], 120);
1027        assert_eq!(export["is_re_export"], false);
1028    }
1029
1030    #[test]
1031    fn json_serializes_to_valid_json() {
1032        let root = PathBuf::from("/project");
1033        let results = sample_results(&root);
1034        let elapsed = Duration::from_millis(42);
1035        let output = build_json(&results, &root, elapsed).expect("should serialize");
1036
1037        let json_str = serde_json::to_string_pretty(&output).expect("should stringify");
1038        let reparsed: serde_json::Value =
1039            serde_json::from_str(&json_str).expect("JSON output should be valid JSON");
1040        assert_eq!(reparsed, output);
1041    }
1042
1043    #[test]
1044    fn json_empty_results_produce_valid_structure() {
1045        let root = PathBuf::from("/project");
1046        let results = AnalysisResults::default();
1047        let elapsed = Duration::from_millis(0);
1048        let output = build_json(&results, &root, elapsed).expect("should serialize");
1049
1050        assert_eq!(output["total_issues"], 0);
1051        assert_eq!(output["unused_files"].as_array().unwrap().len(), 0);
1052        assert_eq!(output["unused_exports"].as_array().unwrap().len(), 0);
1053        assert_eq!(output["unused_types"].as_array().unwrap().len(), 0);
1054        assert_eq!(output["unused_dependencies"].as_array().unwrap().len(), 0);
1055        assert_eq!(
1056            output["unused_dev_dependencies"].as_array().unwrap().len(),
1057            0
1058        );
1059        assert_eq!(output["unused_enum_members"].as_array().unwrap().len(), 0);
1060        assert_eq!(output["unused_class_members"].as_array().unwrap().len(), 0);
1061        assert_eq!(output["unresolved_imports"].as_array().unwrap().len(), 0);
1062        assert_eq!(output["unlisted_dependencies"].as_array().unwrap().len(), 0);
1063        assert_eq!(output["duplicate_exports"].as_array().unwrap().len(), 0);
1064        assert_eq!(
1065            output["type_only_dependencies"].as_array().unwrap().len(),
1066            0
1067        );
1068        assert_eq!(output["circular_dependencies"].as_array().unwrap().len(), 0);
1069    }
1070
1071    #[test]
1072    fn json_empty_results_round_trips_through_string() {
1073        let root = PathBuf::from("/project");
1074        let results = AnalysisResults::default();
1075        let elapsed = Duration::from_millis(0);
1076        let output = build_json(&results, &root, elapsed).expect("should serialize");
1077
1078        let json_str = serde_json::to_string(&output).expect("should stringify");
1079        let reparsed: serde_json::Value =
1080            serde_json::from_str(&json_str).expect("should parse back");
1081        assert_eq!(reparsed["total_issues"], 0);
1082    }
1083
1084    #[test]
1085    fn json_paths_are_relative_to_root() {
1086        let root = PathBuf::from("/project");
1087        let mut results = AnalysisResults::default();
1088        results
1089            .unused_files
1090            .push(UnusedFileFinding::with_actions(UnusedFile {
1091                path: root.join("src/deep/nested/file.ts"),
1092            }));
1093        let elapsed = Duration::from_millis(0);
1094        let output = build_json(&results, &root, elapsed).expect("should serialize");
1095
1096        let path = output["unused_files"][0]["path"].as_str().unwrap();
1097        assert_eq!(path, "src/deep/nested/file.ts");
1098        assert!(!path.starts_with("/project"));
1099    }
1100
1101    #[test]
1102    fn json_strips_root_from_nested_locations() {
1103        let root = PathBuf::from("/project");
1104        let mut results = AnalysisResults::default();
1105        results
1106            .unlisted_dependencies
1107            .push(UnlistedDependencyFinding::with_actions(
1108                UnlistedDependency {
1109                    package_name: "chalk".to_string(),
1110                    imported_from: vec![ImportSite {
1111                        path: root.join("src/cli.ts"),
1112                        line: 2,
1113                        col: 0,
1114                    }],
1115                },
1116            ));
1117        let elapsed = Duration::from_millis(0);
1118        let output = build_json(&results, &root, elapsed).expect("should serialize");
1119
1120        let site_path = output["unlisted_dependencies"][0]["imported_from"][0]["path"]
1121            .as_str()
1122            .unwrap();
1123        assert_eq!(site_path, "src/cli.ts");
1124    }
1125
1126    #[test]
1127    fn json_strips_root_from_duplicate_export_locations() {
1128        let root = PathBuf::from("/project");
1129        let mut results = AnalysisResults::default();
1130        results
1131            .duplicate_exports
1132            .push(DuplicateExportFinding::with_actions(DuplicateExport {
1133                export_name: "Config".to_string(),
1134                locations: vec![
1135                    DuplicateLocation {
1136                        path: root.join("src/config.ts"),
1137                        line: 15,
1138                        col: 0,
1139                    },
1140                    DuplicateLocation {
1141                        path: root.join("src/types.ts"),
1142                        line: 30,
1143                        col: 0,
1144                    },
1145                ],
1146            }));
1147        let elapsed = Duration::from_millis(0);
1148        let output = build_json(&results, &root, elapsed).expect("should serialize");
1149
1150        let loc0 = output["duplicate_exports"][0]["locations"][0]["path"]
1151            .as_str()
1152            .unwrap();
1153        let loc1 = output["duplicate_exports"][0]["locations"][1]["path"]
1154            .as_str()
1155            .unwrap();
1156        assert_eq!(loc0, "src/config.ts");
1157        assert_eq!(loc1, "src/types.ts");
1158    }
1159
1160    #[test]
1161    fn json_strips_root_from_circular_dependency_files() {
1162        let root = PathBuf::from("/project");
1163        let mut results = AnalysisResults::default();
1164        results
1165            .circular_dependencies
1166            .push(CircularDependencyFinding::with_actions(
1167                CircularDependency {
1168                    files: vec![root.join("src/a.ts"), root.join("src/b.ts")],
1169                    length: 2,
1170                    line: 1,
1171                    col: 0,
1172                    edges: Vec::new(),
1173                    is_cross_package: false,
1174                },
1175            ));
1176        let elapsed = Duration::from_millis(0);
1177        let output = build_json(&results, &root, elapsed).expect("should serialize");
1178
1179        let files = output["circular_dependencies"][0]["files"]
1180            .as_array()
1181            .unwrap();
1182        assert_eq!(files[0].as_str().unwrap(), "src/a.ts");
1183        assert_eq!(files[1].as_str().unwrap(), "src/b.ts");
1184    }
1185
1186    #[test]
1187    fn json_path_outside_root_not_stripped() {
1188        let root = PathBuf::from("/project");
1189        let mut results = AnalysisResults::default();
1190        results
1191            .unused_files
1192            .push(UnusedFileFinding::with_actions(UnusedFile {
1193                path: PathBuf::from("/other/project/src/file.ts"),
1194            }));
1195        let elapsed = Duration::from_millis(0);
1196        let output = build_json(&results, &root, elapsed).expect("should serialize");
1197
1198        let path = output["unused_files"][0]["path"].as_str().unwrap();
1199        assert!(path.contains("/other/project/"));
1200    }
1201
1202    #[test]
1203    fn json_unused_file_contains_path() {
1204        let root = PathBuf::from("/project");
1205        let mut results = AnalysisResults::default();
1206        results
1207            .unused_files
1208            .push(UnusedFileFinding::with_actions(UnusedFile {
1209                path: root.join("src/orphan.ts"),
1210            }));
1211        let elapsed = Duration::from_millis(0);
1212        let output = build_json(&results, &root, elapsed).expect("should serialize");
1213
1214        let file = &output["unused_files"][0];
1215        assert_eq!(file["path"], "src/orphan.ts");
1216    }
1217
1218    #[test]
1219    fn json_unused_type_contains_expected_fields() {
1220        let root = PathBuf::from("/project");
1221        let mut results = AnalysisResults::default();
1222        results
1223            .unused_types
1224            .push(UnusedTypeFinding::with_actions(UnusedExport {
1225                path: root.join("src/types.ts"),
1226                export_name: "OldInterface".to_string(),
1227                is_type_only: true,
1228                line: 20,
1229                col: 0,
1230                span_start: 300,
1231                is_re_export: false,
1232            }));
1233        let elapsed = Duration::from_millis(0);
1234        let output = build_json(&results, &root, elapsed).expect("should serialize");
1235
1236        let typ = &output["unused_types"][0];
1237        assert_eq!(typ["export_name"], "OldInterface");
1238        assert_eq!(typ["is_type_only"], true);
1239        assert_eq!(typ["line"], 20);
1240        assert_eq!(typ["path"], "src/types.ts");
1241    }
1242
1243    #[test]
1244    fn json_unused_dependency_contains_expected_fields() {
1245        let root = PathBuf::from("/project");
1246        let mut results = AnalysisResults::default();
1247        results
1248            .unused_dependencies
1249            .push(UnusedDependencyFinding::with_actions(UnusedDependency {
1250                package_name: "axios".to_string(),
1251                location: DependencyLocation::Dependencies,
1252                path: root.join("package.json"),
1253                line: 10,
1254                used_in_workspaces: Vec::new(),
1255            }));
1256        let elapsed = Duration::from_millis(0);
1257        let output = build_json(&results, &root, elapsed).expect("should serialize");
1258
1259        let dep = &output["unused_dependencies"][0];
1260        assert_eq!(dep["package_name"], "axios");
1261        assert_eq!(dep["line"], 10);
1262        assert!(dep.get("used_in_workspaces").is_none());
1263    }
1264
1265    #[test]
1266    fn json_unused_dependency_includes_cross_workspace_context() {
1267        let root = PathBuf::from("/project");
1268        let mut results = AnalysisResults::default();
1269        results
1270            .unused_dependencies
1271            .push(UnusedDependencyFinding::with_actions(UnusedDependency {
1272                package_name: "lodash-es".to_string(),
1273                location: DependencyLocation::Dependencies,
1274                path: root.join("packages/shared/package.json"),
1275                line: 6,
1276                used_in_workspaces: vec![root.join("packages/consumer")],
1277            }));
1278        let elapsed = Duration::from_millis(0);
1279        let output = build_json(&results, &root, elapsed).expect("should serialize");
1280
1281        let dep = &output["unused_dependencies"][0];
1282        assert_eq!(
1283            dep["used_in_workspaces"],
1284            serde_json::json!(["packages/consumer"])
1285        );
1286    }
1287
1288    #[test]
1289    fn json_unused_dev_dependency_contains_expected_fields() {
1290        let root = PathBuf::from("/project");
1291        let mut results = AnalysisResults::default();
1292        results
1293            .unused_dev_dependencies
1294            .push(UnusedDevDependencyFinding::with_actions(UnusedDependency {
1295                package_name: "vitest".to_string(),
1296                location: DependencyLocation::DevDependencies,
1297                path: root.join("package.json"),
1298                line: 15,
1299                used_in_workspaces: Vec::new(),
1300            }));
1301        let elapsed = Duration::from_millis(0);
1302        let output = build_json(&results, &root, elapsed).expect("should serialize");
1303
1304        let dep = &output["unused_dev_dependencies"][0];
1305        assert_eq!(dep["package_name"], "vitest");
1306    }
1307
1308    #[test]
1309    fn json_unused_optional_dependency_contains_expected_fields() {
1310        let root = PathBuf::from("/project");
1311        let mut results = AnalysisResults::default();
1312        results
1313            .unused_optional_dependencies
1314            .push(UnusedOptionalDependencyFinding::with_actions(
1315                UnusedDependency {
1316                    package_name: "fsevents".to_string(),
1317                    location: DependencyLocation::OptionalDependencies,
1318                    path: root.join("package.json"),
1319                    line: 12,
1320                    used_in_workspaces: Vec::new(),
1321                },
1322            ));
1323        let elapsed = Duration::from_millis(0);
1324        let output = build_json(&results, &root, elapsed).expect("should serialize");
1325
1326        let dep = &output["unused_optional_dependencies"][0];
1327        assert_eq!(dep["package_name"], "fsevents");
1328        assert_eq!(output["total_issues"], 1);
1329    }
1330
1331    #[test]
1332    fn json_unused_enum_member_contains_expected_fields() {
1333        let root = PathBuf::from("/project");
1334        let mut results = AnalysisResults::default();
1335        results
1336            .unused_enum_members
1337            .push(UnusedEnumMemberFinding::with_actions(UnusedMember {
1338                path: root.join("src/enums.ts"),
1339                parent_name: "Color".to_string(),
1340                member_name: "Purple".to_string(),
1341                kind: MemberKind::EnumMember,
1342                line: 5,
1343                col: 2,
1344            }));
1345        let elapsed = Duration::from_millis(0);
1346        let output = build_json(&results, &root, elapsed).expect("should serialize");
1347
1348        let member = &output["unused_enum_members"][0];
1349        assert_eq!(member["parent_name"], "Color");
1350        assert_eq!(member["member_name"], "Purple");
1351        assert_eq!(member["line"], 5);
1352        assert_eq!(member["path"], "src/enums.ts");
1353    }
1354
1355    #[test]
1356    fn json_unused_class_member_contains_expected_fields() {
1357        let root = PathBuf::from("/project");
1358        let mut results = AnalysisResults::default();
1359        results
1360            .unused_class_members
1361            .push(UnusedClassMemberFinding::with_actions(UnusedMember {
1362                path: root.join("src/api.ts"),
1363                parent_name: "ApiClient".to_string(),
1364                member_name: "deprecatedFetch".to_string(),
1365                kind: MemberKind::ClassMethod,
1366                line: 100,
1367                col: 4,
1368            }));
1369        let elapsed = Duration::from_millis(0);
1370        let output = build_json(&results, &root, elapsed).expect("should serialize");
1371
1372        let member = &output["unused_class_members"][0];
1373        assert_eq!(member["parent_name"], "ApiClient");
1374        assert_eq!(member["member_name"], "deprecatedFetch");
1375        assert_eq!(member["line"], 100);
1376    }
1377
1378    #[test]
1379    fn json_unresolved_import_contains_expected_fields() {
1380        let root = PathBuf::from("/project");
1381        let mut results = AnalysisResults::default();
1382        results
1383            .unresolved_imports
1384            .push(UnresolvedImportFinding::with_actions(UnresolvedImport {
1385                path: root.join("src/app.ts"),
1386                specifier: "@acme/missing-pkg".to_string(),
1387                line: 7,
1388                col: 0,
1389                specifier_col: 0,
1390            }));
1391        let elapsed = Duration::from_millis(0);
1392        let output = build_json(&results, &root, elapsed).expect("should serialize");
1393
1394        let import = &output["unresolved_imports"][0];
1395        assert_eq!(import["specifier"], "@acme/missing-pkg");
1396        assert_eq!(import["line"], 7);
1397        assert_eq!(import["path"], "src/app.ts");
1398    }
1399
1400    #[test]
1401    fn json_unlisted_dependency_contains_import_sites() {
1402        let root = PathBuf::from("/project");
1403        let mut results = AnalysisResults::default();
1404        results
1405            .unlisted_dependencies
1406            .push(UnlistedDependencyFinding::with_actions(
1407                UnlistedDependency {
1408                    package_name: "dotenv".to_string(),
1409                    imported_from: vec![
1410                        ImportSite {
1411                            path: root.join("src/config.ts"),
1412                            line: 1,
1413                            col: 0,
1414                        },
1415                        ImportSite {
1416                            path: root.join("src/server.ts"),
1417                            line: 3,
1418                            col: 0,
1419                        },
1420                    ],
1421                },
1422            ));
1423        let elapsed = Duration::from_millis(0);
1424        let output = build_json(&results, &root, elapsed).expect("should serialize");
1425
1426        let dep = &output["unlisted_dependencies"][0];
1427        assert_eq!(dep["package_name"], "dotenv");
1428        let sites = dep["imported_from"].as_array().unwrap();
1429        assert_eq!(sites.len(), 2);
1430        assert_eq!(sites[0]["path"], "src/config.ts");
1431        assert_eq!(sites[1]["path"], "src/server.ts");
1432    }
1433
1434    #[test]
1435    fn json_duplicate_export_contains_locations() {
1436        let root = PathBuf::from("/project");
1437        let mut results = AnalysisResults::default();
1438        results
1439            .duplicate_exports
1440            .push(DuplicateExportFinding::with_actions(DuplicateExport {
1441                export_name: "Button".to_string(),
1442                locations: vec![
1443                    DuplicateLocation {
1444                        path: root.join("src/ui.ts"),
1445                        line: 10,
1446                        col: 0,
1447                    },
1448                    DuplicateLocation {
1449                        path: root.join("src/components.ts"),
1450                        line: 25,
1451                        col: 0,
1452                    },
1453                ],
1454            }));
1455        let elapsed = Duration::from_millis(0);
1456        let output = build_json(&results, &root, elapsed).expect("should serialize");
1457
1458        let dup = &output["duplicate_exports"][0];
1459        assert_eq!(dup["export_name"], "Button");
1460        let locs = dup["locations"].as_array().unwrap();
1461        assert_eq!(locs.len(), 2);
1462        assert_eq!(locs[0]["line"], 10);
1463        assert_eq!(locs[1]["line"], 25);
1464    }
1465
1466    #[test]
1467    fn duplicate_export_add_to_config_is_auto_fixable_when_config_exists() {
1468        let dir = tempfile::tempdir().unwrap();
1469        let root = dir.path();
1470        std::fs::write(root.join(".fallowrc.json"), "{}\n").unwrap();
1471        let mut results = AnalysisResults::default();
1472        results
1473            .duplicate_exports
1474            .push(DuplicateExportFinding::with_actions(DuplicateExport {
1475                export_name: "Button".to_string(),
1476                locations: vec![
1477                    DuplicateLocation {
1478                        path: root.join("src/ui.ts"),
1479                        line: 10,
1480                        col: 0,
1481                    },
1482                    DuplicateLocation {
1483                        path: root.join("src/components.ts"),
1484                        line: 25,
1485                        col: 0,
1486                    },
1487                ],
1488            }));
1489
1490        let output = build_json(&results, root, Duration::ZERO).unwrap();
1491        let actions = output["duplicate_exports"][0]["actions"]
1492            .as_array()
1493            .unwrap();
1494        assert_eq!(actions[0]["type"], "add-to-config");
1495        assert_eq!(actions[0]["auto_fixable"], true);
1496    }
1497
1498    #[test]
1499    fn duplicate_export_add_to_config_is_auto_fixable_when_create_fallback_allowed() {
1500        let dir = tempfile::tempdir().unwrap();
1501        let root = dir.path();
1502        let mut results = AnalysisResults::default();
1503        results
1504            .duplicate_exports
1505            .push(DuplicateExportFinding::with_actions(DuplicateExport {
1506                export_name: "Button".to_string(),
1507                locations: vec![
1508                    DuplicateLocation {
1509                        path: root.join("src/ui.ts"),
1510                        line: 10,
1511                        col: 0,
1512                    },
1513                    DuplicateLocation {
1514                        path: root.join("src/components.ts"),
1515                        line: 25,
1516                        col: 0,
1517                    },
1518                ],
1519            }));
1520
1521        let output = build_json(&results, root, Duration::ZERO).unwrap();
1522        let actions = output["duplicate_exports"][0]["actions"]
1523            .as_array()
1524            .unwrap();
1525        assert_eq!(actions[0]["type"], "add-to-config");
1526        assert_eq!(actions[0]["auto_fixable"], true);
1527    }
1528
1529    #[test]
1530    fn duplicate_export_add_to_config_is_not_auto_fixable_in_monorepo_subpackage() {
1531        let dir = tempfile::tempdir().unwrap();
1532        let workspace = dir.path();
1533        std::fs::write(
1534            workspace.join("pnpm-workspace.yaml"),
1535            "packages:\n  - 'packages/*'\n",
1536        )
1537        .unwrap();
1538        let sub = workspace.join("packages/ui");
1539        std::fs::create_dir_all(&sub).unwrap();
1540        let mut results = AnalysisResults::default();
1541        results
1542            .duplicate_exports
1543            .push(DuplicateExportFinding::with_actions(DuplicateExport {
1544                export_name: "Button".to_string(),
1545                locations: vec![
1546                    DuplicateLocation {
1547                        path: sub.join("src/ui.ts"),
1548                        line: 10,
1549                        col: 0,
1550                    },
1551                    DuplicateLocation {
1552                        path: sub.join("src/components.ts"),
1553                        line: 25,
1554                        col: 0,
1555                    },
1556                ],
1557            }));
1558
1559        let output = build_json(&results, &sub, Duration::ZERO).unwrap();
1560        let actions = output["duplicate_exports"][0]["actions"]
1561            .as_array()
1562            .unwrap();
1563        assert_eq!(actions[0]["type"], "add-to-config");
1564        assert_eq!(actions[0]["auto_fixable"], false);
1565    }
1566
1567    #[test]
1568    fn json_type_only_dependency_contains_expected_fields() {
1569        let root = PathBuf::from("/project");
1570        let mut results = AnalysisResults::default();
1571        results
1572            .type_only_dependencies
1573            .push(TypeOnlyDependencyFinding::with_actions(
1574                TypeOnlyDependency {
1575                    package_name: "zod".to_string(),
1576                    path: root.join("package.json"),
1577                    line: 8,
1578                },
1579            ));
1580        let elapsed = Duration::from_millis(0);
1581        let output = build_json(&results, &root, elapsed).expect("should serialize");
1582
1583        let dep = &output["type_only_dependencies"][0];
1584        assert_eq!(dep["package_name"], "zod");
1585        assert_eq!(dep["line"], 8);
1586    }
1587
1588    #[test]
1589    fn json_circular_dependency_contains_expected_fields() {
1590        let root = PathBuf::from("/project");
1591        let mut results = AnalysisResults::default();
1592        results
1593            .circular_dependencies
1594            .push(CircularDependencyFinding::with_actions(
1595                CircularDependency {
1596                    files: vec![
1597                        root.join("src/a.ts"),
1598                        root.join("src/b.ts"),
1599                        root.join("src/c.ts"),
1600                    ],
1601                    length: 3,
1602                    line: 5,
1603                    col: 0,
1604                    edges: Vec::new(),
1605                    is_cross_package: false,
1606                },
1607            ));
1608        let elapsed = Duration::from_millis(0);
1609        let output = build_json(&results, &root, elapsed).expect("should serialize");
1610
1611        let cycle = &output["circular_dependencies"][0];
1612        assert_eq!(cycle["length"], 3);
1613        assert_eq!(cycle["line"], 5);
1614        let files = cycle["files"].as_array().unwrap();
1615        assert_eq!(files.len(), 3);
1616    }
1617
1618    #[test]
1619    fn json_re_export_flagged_correctly() {
1620        let root = PathBuf::from("/project");
1621        let mut results = AnalysisResults::default();
1622        results
1623            .unused_exports
1624            .push(UnusedExportFinding::with_actions(UnusedExport {
1625                path: root.join("src/index.ts"),
1626                export_name: "reExported".to_string(),
1627                is_type_only: false,
1628                line: 1,
1629                col: 0,
1630                span_start: 0,
1631                is_re_export: true,
1632            }));
1633        let elapsed = Duration::from_millis(0);
1634        let output = build_json(&results, &root, elapsed).expect("should serialize");
1635
1636        assert_eq!(output["unused_exports"][0]["is_re_export"], true);
1637    }
1638
1639    #[test]
1640    fn json_schema_version_is_pinned() {
1641        let root = PathBuf::from("/project");
1642        let results = AnalysisResults::default();
1643        let elapsed = Duration::from_millis(0);
1644        let output = build_json(&results, &root, elapsed).expect("should serialize");
1645
1646        assert_eq!(output["schema_version"], SCHEMA_VERSION);
1647        assert_eq!(output["schema_version"], 7);
1648    }
1649
1650    #[test]
1651    fn json_version_matches_cargo_pkg_version() {
1652        let root = PathBuf::from("/project");
1653        let results = AnalysisResults::default();
1654        let elapsed = Duration::from_millis(0);
1655        let output = build_json(&results, &root, elapsed).expect("should serialize");
1656
1657        assert_eq!(output["version"], env!("CARGO_PKG_VERSION"));
1658    }
1659
1660    #[test]
1661    fn json_elapsed_ms_zero_duration() {
1662        let root = PathBuf::from("/project");
1663        let results = AnalysisResults::default();
1664        let output = build_json(&results, &root, Duration::ZERO).expect("should serialize");
1665
1666        assert_eq!(output["elapsed_ms"], 0);
1667    }
1668
1669    #[test]
1670    fn json_elapsed_ms_large_duration() {
1671        let root = PathBuf::from("/project");
1672        let results = AnalysisResults::default();
1673        let elapsed = Duration::from_mins(2);
1674        let output = build_json(&results, &root, elapsed).expect("should serialize");
1675
1676        assert_eq!(output["elapsed_ms"], 120_000);
1677    }
1678
1679    #[test]
1680    fn json_elapsed_ms_sub_millisecond_truncated() {
1681        let root = PathBuf::from("/project");
1682        let results = AnalysisResults::default();
1683        let elapsed = Duration::from_micros(500);
1684        let output = build_json(&results, &root, elapsed).expect("should serialize");
1685
1686        assert_eq!(output["elapsed_ms"], 0);
1687    }
1688
1689    #[test]
1690    fn json_multiple_unused_files() {
1691        let root = PathBuf::from("/project");
1692        let mut results = AnalysisResults::default();
1693        results
1694            .unused_files
1695            .push(UnusedFileFinding::with_actions(UnusedFile {
1696                path: root.join("src/a.ts"),
1697            }));
1698        results
1699            .unused_files
1700            .push(UnusedFileFinding::with_actions(UnusedFile {
1701                path: root.join("src/b.ts"),
1702            }));
1703        results
1704            .unused_files
1705            .push(UnusedFileFinding::with_actions(UnusedFile {
1706                path: root.join("src/c.ts"),
1707            }));
1708        let elapsed = Duration::from_millis(0);
1709        let output = build_json(&results, &root, elapsed).expect("should serialize");
1710
1711        assert_eq!(output["unused_files"].as_array().unwrap().len(), 3);
1712        assert_eq!(output["total_issues"], 3);
1713    }
1714
1715    #[test]
1716    fn strip_root_prefix_on_string_value() {
1717        let mut value = serde_json::json!("/project/src/file.ts");
1718        strip_root_prefix(&mut value, "/project/");
1719        assert_eq!(value, "src/file.ts");
1720    }
1721
1722    #[test]
1723    fn strip_root_prefix_leaves_non_matching_string() {
1724        let mut value = serde_json::json!("/other/src/file.ts");
1725        strip_root_prefix(&mut value, "/project/");
1726        assert_eq!(value, "/other/src/file.ts");
1727    }
1728
1729    #[test]
1730    fn strip_root_prefix_recurses_into_arrays() {
1731        let mut value = serde_json::json!(["/project/a.ts", "/project/b.ts", "/other/c.ts"]);
1732        strip_root_prefix(&mut value, "/project/");
1733        assert_eq!(value[0], "a.ts");
1734        assert_eq!(value[1], "b.ts");
1735        assert_eq!(value[2], "/other/c.ts");
1736    }
1737
1738    #[test]
1739    fn strip_root_prefix_recurses_into_nested_objects() {
1740        let mut value = serde_json::json!({
1741            "outer": {
1742                "path": "/project/src/nested.ts"
1743            }
1744        });
1745        strip_root_prefix(&mut value, "/project/");
1746        assert_eq!(value["outer"]["path"], "src/nested.ts");
1747    }
1748
1749    #[test]
1750    fn strip_root_prefix_leaves_numbers_and_booleans() {
1751        let mut value = serde_json::json!({
1752            "line": 42,
1753            "is_type_only": false,
1754            "path": "/project/src/file.ts"
1755        });
1756        strip_root_prefix(&mut value, "/project/");
1757        assert_eq!(value["line"], 42);
1758        assert_eq!(value["is_type_only"], false);
1759        assert_eq!(value["path"], "src/file.ts");
1760    }
1761
1762    #[test]
1763    fn strip_root_prefix_normalizes_windows_separators() {
1764        let mut value = serde_json::json!(r"/project\src\file.ts");
1765        strip_root_prefix(&mut value, "/project/");
1766        assert_eq!(value, "src/file.ts");
1767    }
1768
1769    #[test]
1770    fn strip_root_prefix_rewrites_embedded_path_strings() {
1771        let mut value =
1772            serde_json::json!("Add \"/project/src/file.ts\" to boundaries.coverage.allowUnmatched");
1773        strip_root_prefix(&mut value, "/project/");
1774        assert_eq!(
1775            value,
1776            "Add \"src/file.ts\" to boundaries.coverage.allowUnmatched"
1777        );
1778    }
1779
1780    #[test]
1781    fn strip_root_prefix_handles_empty_string_after_strip() {
1782        let mut value = serde_json::json!("/project/");
1783        strip_root_prefix(&mut value, "/project/");
1784        assert_eq!(value, "");
1785    }
1786
1787    #[test]
1788    fn strip_root_prefix_deeply_nested_array_of_objects() {
1789        let mut value = serde_json::json!({
1790            "groups": [{
1791                "instances": [{
1792                    "file": "/project/src/a.ts"
1793                }, {
1794                    "file": "/project/src/b.ts"
1795                }]
1796            }]
1797        });
1798        strip_root_prefix(&mut value, "/project/");
1799        assert_eq!(value["groups"][0]["instances"][0]["file"], "src/a.ts");
1800        assert_eq!(value["groups"][0]["instances"][1]["file"], "src/b.ts");
1801    }
1802
1803    #[test]
1804    fn json_full_sample_results_total_issues_correct() {
1805        let root = PathBuf::from("/project");
1806        let results = sample_results(&root);
1807        let elapsed = Duration::from_millis(100);
1808        let output = build_json(&results, &root, elapsed).expect("should serialize");
1809
1810        assert_eq!(output["total_issues"], results.total_issues());
1811    }
1812
1813    #[test]
1814    fn json_full_sample_no_absolute_paths_in_output() {
1815        let root = PathBuf::from("/project");
1816        let results = sample_results(&root);
1817        let elapsed = Duration::from_millis(0);
1818        let output = build_json(&results, &root, elapsed).expect("should serialize");
1819
1820        let json_str = serde_json::to_string(&output).expect("should stringify");
1821        assert!(!json_str.contains("/project/src/"));
1822        assert!(!json_str.contains("/project/package.json"));
1823    }
1824
1825    #[test]
1826    fn json_output_is_deterministic() {
1827        let root = PathBuf::from("/project");
1828        let results = sample_results(&root);
1829        let elapsed = Duration::from_millis(50);
1830
1831        let output1 = build_json(&results, &root, elapsed).expect("first build");
1832        let output2 = build_json(&results, &root, elapsed).expect("second build");
1833
1834        assert_eq!(output1, output2);
1835    }
1836
1837    #[test]
1838    fn json_results_fields_do_not_shadow_metadata() {
1839        let root = PathBuf::from("/project");
1840        let results = AnalysisResults::default();
1841        let elapsed = Duration::from_millis(99);
1842        let output = build_json(&results, &root, elapsed).expect("should serialize");
1843
1844        assert_eq!(output["kind"], "dead-code");
1845        assert_eq!(output["schema_version"], 7);
1846        assert_eq!(output["elapsed_ms"], 99);
1847    }
1848
1849    #[test]
1850    fn json_all_issue_type_arrays_present_in_empty_results() {
1851        let root = PathBuf::from("/project");
1852        let results = AnalysisResults::default();
1853        let elapsed = Duration::from_millis(0);
1854        let output = build_json(&results, &root, elapsed).expect("should serialize");
1855
1856        let expected_arrays = [
1857            "unused_files",
1858            "unused_exports",
1859            "unused_types",
1860            "unused_dependencies",
1861            "unused_dev_dependencies",
1862            "unused_optional_dependencies",
1863            "unused_enum_members",
1864            "unused_class_members",
1865            "unresolved_imports",
1866            "unlisted_dependencies",
1867            "duplicate_exports",
1868            "type_only_dependencies",
1869            "test_only_dependencies",
1870            "circular_dependencies",
1871        ];
1872        for key in &expected_arrays {
1873            assert!(
1874                output[key].is_array(),
1875                "expected '{key}' to be an array in JSON output"
1876            );
1877        }
1878    }
1879
1880    #[test]
1881    fn insert_meta_adds_key_to_object() {
1882        let mut output = serde_json::json!({ "foo": 1 });
1883        let meta = serde_json::json!({ "docs": "https://example.com" });
1884        insert_meta(&mut output, meta.clone());
1885        assert_eq!(output["_meta"], meta);
1886    }
1887
1888    #[test]
1889    fn insert_meta_noop_on_non_object() {
1890        let mut output = serde_json::json!([1, 2, 3]);
1891        let meta = serde_json::json!({ "docs": "https://example.com" });
1892        insert_meta(&mut output, meta);
1893        assert!(output.is_array());
1894    }
1895
1896    #[test]
1897    fn insert_meta_overwrites_existing_meta() {
1898        let mut output = serde_json::json!({ "_meta": "old" });
1899        let meta = serde_json::json!({ "new": true });
1900        insert_meta(&mut output, meta.clone());
1901        assert_eq!(output["_meta"], meta);
1902    }
1903
1904    #[test]
1905    fn insert_meta_preserves_existing_telemetry_meta() {
1906        let mut output = serde_json::json!({
1907            "_meta": {
1908                "telemetry": {
1909                    "analysis_run_id": "run_test123"
1910                }
1911            }
1912        });
1913        insert_meta(
1914            &mut output,
1915            serde_json::json!({ "docs": "https://example.com" }),
1916        );
1917
1918        assert_eq!(
1919            output["_meta"]["docs"].as_str(),
1920            Some("https://example.com")
1921        );
1922        assert_eq!(
1923            output["_meta"]["telemetry"]["analysis_run_id"].as_str(),
1924            Some("run_test123")
1925        );
1926    }
1927
1928    #[test]
1929    fn strip_root_prefix_null_unchanged() {
1930        let mut value = serde_json::Value::Null;
1931        strip_root_prefix(&mut value, "/project/");
1932        assert!(value.is_null());
1933    }
1934
1935    #[test]
1936    fn strip_root_prefix_empty_string() {
1937        let mut value = serde_json::json!("");
1938        strip_root_prefix(&mut value, "/project/");
1939        assert_eq!(value, "");
1940    }
1941
1942    #[test]
1943    fn strip_root_prefix_mixed_types() {
1944        let mut value = serde_json::json!({
1945            "path": "/project/src/file.ts",
1946            "line": 42,
1947            "flag": true,
1948            "nested": {
1949                "items": ["/project/a.ts", 99, null, "/project/b.ts"],
1950                "deep": { "path": "/project/c.ts" }
1951            }
1952        });
1953        strip_root_prefix(&mut value, "/project/");
1954        assert_eq!(value["path"], "src/file.ts");
1955        assert_eq!(value["line"], 42);
1956        assert_eq!(value["flag"], true);
1957        assert_eq!(value["nested"]["items"][0], "a.ts");
1958        assert_eq!(value["nested"]["items"][1], 99);
1959        assert!(value["nested"]["items"][2].is_null());
1960        assert_eq!(value["nested"]["items"][3], "b.ts");
1961        assert_eq!(value["nested"]["deep"]["path"], "c.ts");
1962    }
1963
1964    #[test]
1965    fn json_check_meta_integrates_correctly() {
1966        let root = PathBuf::from("/project");
1967        let results = AnalysisResults::default();
1968        let elapsed = Duration::from_millis(0);
1969        let mut output = build_json(&results, &root, elapsed).expect("should serialize");
1970        insert_meta(&mut output, crate::explain::check_meta());
1971
1972        assert!(output["_meta"]["docs"].is_string());
1973        assert!(output["_meta"]["rules"].is_object());
1974    }
1975
1976    #[test]
1977    fn json_unused_member_kind_serialized() {
1978        let root = PathBuf::from("/project");
1979        let mut results = AnalysisResults::default();
1980        results
1981            .unused_enum_members
1982            .push(UnusedEnumMemberFinding::with_actions(UnusedMember {
1983                path: root.join("src/enums.ts"),
1984                parent_name: "Color".to_string(),
1985                member_name: "Red".to_string(),
1986                kind: MemberKind::EnumMember,
1987                line: 3,
1988                col: 2,
1989            }));
1990        results
1991            .unused_class_members
1992            .push(UnusedClassMemberFinding::with_actions(UnusedMember {
1993                path: root.join("src/class.ts"),
1994                parent_name: "Foo".to_string(),
1995                member_name: "bar".to_string(),
1996                kind: MemberKind::ClassMethod,
1997                line: 10,
1998                col: 4,
1999            }));
2000
2001        let elapsed = Duration::from_millis(0);
2002        let output = build_json(&results, &root, elapsed).expect("should serialize");
2003
2004        let enum_member = &output["unused_enum_members"][0];
2005        assert!(enum_member["kind"].is_string());
2006        let class_member = &output["unused_class_members"][0];
2007        assert!(class_member["kind"].is_string());
2008    }
2009
2010    #[test]
2011    fn json_unused_export_has_actions() {
2012        let root = PathBuf::from("/project");
2013        let mut results = AnalysisResults::default();
2014        results
2015            .unused_exports
2016            .push(UnusedExportFinding::with_actions(UnusedExport {
2017                path: root.join("src/utils.ts"),
2018                export_name: "helperFn".to_string(),
2019                is_type_only: false,
2020                line: 10,
2021                col: 4,
2022                span_start: 120,
2023                is_re_export: false,
2024            }));
2025        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2026
2027        let actions = output["unused_exports"][0]["actions"].as_array().unwrap();
2028        assert_eq!(actions.len(), 2);
2029
2030        assert_eq!(actions[0]["type"], "remove-export");
2031        assert_eq!(actions[0]["auto_fixable"], true);
2032        assert!(actions[0].get("note").is_none());
2033
2034        assert_eq!(actions[1]["type"], "suppress-line");
2035        assert_eq!(
2036            actions[1]["comment"],
2037            "// fallow-ignore-next-line unused-export"
2038        );
2039    }
2040
2041    #[test]
2042    fn json_boundary_coverage_action_descriptions_use_relative_paths() {
2043        let root = PathBuf::from("/project");
2044        let mut results = AnalysisResults::default();
2045        results
2046            .boundary_coverage_violations
2047            .push(BoundaryCoverageViolationFinding::with_actions(
2048                BoundaryCoverageViolation {
2049                    path: root.join("src/middleware/error.ts"),
2050                    line: 1,
2051                    col: 0,
2052                },
2053            ));
2054
2055        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2056        let action = &output["boundary_coverage_violations"][0]["actions"][1];
2057
2058        assert_eq!(
2059            output["boundary_coverage_violations"][0]["path"],
2060            "src/middleware/error.ts"
2061        );
2062        assert_eq!(action["value"], "src/middleware/error.ts");
2063        assert_eq!(
2064            action["description"],
2065            "Add \"src/middleware/error.ts\" to boundaries.coverage.allowUnmatched in fallow config"
2066        );
2067    }
2068
2069    #[test]
2070    fn json_same_line_findings_share_multi_kind_suppression_comment() {
2071        let root = PathBuf::from("/project");
2072        let mut results = AnalysisResults::default();
2073        results
2074            .unused_exports
2075            .push(UnusedExportFinding::with_actions(UnusedExport {
2076                path: root.join("src/api.ts"),
2077                export_name: "helperFn".to_string(),
2078                is_type_only: false,
2079                line: 10,
2080                col: 4,
2081                span_start: 120,
2082                is_re_export: false,
2083            }));
2084        results
2085            .unused_types
2086            .push(UnusedTypeFinding::with_actions(UnusedExport {
2087                path: root.join("src/api.ts"),
2088                export_name: "OldType".to_string(),
2089                is_type_only: true,
2090                line: 10,
2091                col: 0,
2092                span_start: 60,
2093                is_re_export: false,
2094            }));
2095        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2096
2097        let export_actions = output["unused_exports"][0]["actions"].as_array().unwrap();
2098        let type_actions = output["unused_types"][0]["actions"].as_array().unwrap();
2099        assert_eq!(
2100            export_actions[1]["comment"],
2101            "// fallow-ignore-next-line unused-export, unused-type"
2102        );
2103        assert_eq!(
2104            type_actions[1]["comment"],
2105            "// fallow-ignore-next-line unused-export, unused-type"
2106        );
2107    }
2108
2109    #[test]
2110    fn audit_like_json_shares_suppression_comment_across_dead_code_and_complexity() {
2111        let mut output = serde_json::json!({
2112            "dead_code": {
2113                "unused_exports": [{
2114                    "path": "src/main.ts",
2115                    "line": 1,
2116                    "actions": [
2117                        { "type": "remove-export", "auto_fixable": true },
2118                        {
2119                            "type": "suppress-line",
2120                            "auto_fixable": false,
2121                            "comment": "// fallow-ignore-next-line unused-export"
2122                        }
2123                    ]
2124                }]
2125            },
2126            "complexity": {
2127                "findings": [{
2128                    "path": "src/main.ts",
2129                    "line": 1,
2130                    "actions": [
2131                        { "type": "refactor-function", "auto_fixable": false },
2132                        {
2133                            "type": "suppress-line",
2134                            "auto_fixable": false,
2135                            "comment": "// fallow-ignore-next-line complexity"
2136                        }
2137                    ]
2138                }]
2139            }
2140        });
2141
2142        harmonize_multi_kind_suppress_line_actions(&mut output);
2143
2144        assert_eq!(
2145            output["dead_code"]["unused_exports"][0]["actions"][1]["comment"],
2146            "// fallow-ignore-next-line unused-export, complexity"
2147        );
2148        assert_eq!(
2149            output["complexity"]["findings"][0]["actions"][1]["comment"],
2150            "// fallow-ignore-next-line unused-export, complexity"
2151        );
2152    }
2153
2154    #[test]
2155    fn json_unused_file_has_file_suppress_and_note() {
2156        let root = PathBuf::from("/project");
2157        let mut results = AnalysisResults::default();
2158        results
2159            .unused_files
2160            .push(UnusedFileFinding::with_actions(UnusedFile {
2161                path: root.join("src/dead.ts"),
2162            }));
2163        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2164
2165        let actions = output["unused_files"][0]["actions"].as_array().unwrap();
2166        assert_eq!(actions[0]["type"], "delete-file");
2167        assert_eq!(actions[0]["auto_fixable"], false);
2168        assert!(actions[0]["note"].is_string());
2169        assert_eq!(actions[1]["type"], "suppress-file");
2170        assert_eq!(actions[1]["comment"], "// fallow-ignore-file unused-file");
2171    }
2172
2173    #[test]
2174    fn json_unused_dependency_has_config_suppress_with_package_name() {
2175        let root = PathBuf::from("/project");
2176        let mut results = AnalysisResults::default();
2177        results
2178            .unused_dependencies
2179            .push(UnusedDependencyFinding::with_actions(UnusedDependency {
2180                package_name: "lodash".to_string(),
2181                location: DependencyLocation::Dependencies,
2182                path: root.join("package.json"),
2183                line: 5,
2184                used_in_workspaces: Vec::new(),
2185            }));
2186        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2187
2188        let actions = output["unused_dependencies"][0]["actions"]
2189            .as_array()
2190            .unwrap();
2191        assert_eq!(actions[0]["type"], "remove-dependency");
2192        assert_eq!(actions[0]["auto_fixable"], true);
2193
2194        assert_eq!(actions[1]["type"], "add-to-config");
2195        assert_eq!(actions[1]["config_key"], "ignoreDependencies");
2196        assert_eq!(actions[1]["value"], "lodash");
2197    }
2198
2199    #[test]
2200    fn json_cross_workspace_dependency_is_not_auto_fixable() {
2201        let root = PathBuf::from("/project");
2202        let mut results = AnalysisResults::default();
2203        results
2204            .unused_dependencies
2205            .push(UnusedDependencyFinding::with_actions(UnusedDependency {
2206                package_name: "lodash-es".to_string(),
2207                location: DependencyLocation::Dependencies,
2208                path: root.join("packages/shared/package.json"),
2209                line: 5,
2210                used_in_workspaces: vec![root.join("packages/consumer")],
2211            }));
2212        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2213
2214        let actions = output["unused_dependencies"][0]["actions"]
2215            .as_array()
2216            .unwrap();
2217        assert_eq!(actions[0]["type"], "move-dependency");
2218        assert_eq!(actions[0]["auto_fixable"], false);
2219        assert!(
2220            actions[0]["note"]
2221                .as_str()
2222                .unwrap()
2223                .contains("will not remove")
2224        );
2225        assert_eq!(actions[1]["type"], "add-to-config");
2226    }
2227
2228    #[test]
2229    fn json_empty_results_have_no_actions_in_empty_arrays() {
2230        let root = PathBuf::from("/project");
2231        let results = AnalysisResults::default();
2232        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2233
2234        assert!(output["unused_exports"].as_array().unwrap().is_empty());
2235        assert!(output["unused_files"].as_array().unwrap().is_empty());
2236    }
2237
2238    #[test]
2239    fn json_all_issue_types_have_actions() {
2240        let root = PathBuf::from("/project");
2241        let results = sample_results(&root);
2242        let output = build_json(&results, &root, Duration::ZERO).unwrap();
2243
2244        let issue_keys = [
2245            "unused_files",
2246            "unused_exports",
2247            "unused_types",
2248            "unused_dependencies",
2249            "unused_dev_dependencies",
2250            "unused_optional_dependencies",
2251            "unused_enum_members",
2252            "unused_class_members",
2253            "unresolved_imports",
2254            "unlisted_dependencies",
2255            "duplicate_exports",
2256            "type_only_dependencies",
2257            "test_only_dependencies",
2258            "circular_dependencies",
2259        ];
2260
2261        for key in &issue_keys {
2262            let arr = output[key].as_array().unwrap();
2263            if !arr.is_empty() {
2264                let actions = arr[0]["actions"].as_array();
2265                assert!(
2266                    actions.is_some() && !actions.unwrap().is_empty(),
2267                    "missing actions for {key}"
2268                );
2269            }
2270        }
2271    }
2272
2273    /// Test helper: deserialize a JSON finding shape into a typed
2274    /// [`ComplexityViolation`], run [`HealthFinding::with_actions`] with
2275    /// the supplied thresholds, and return the resulting `actions` array
2276    /// as `serde_json::Value` so existing JSON-shape assertions keep
2277    /// working after PR B2 of #384 moved finding action selection from
2278    /// the JSON post-pass into the typed wrapper.
2279    fn build_actions_for_finding_json(
2280        finding_json: serde_json::Value,
2281        opts: crate::health_types::HealthActionOptions,
2282        max_cyclomatic_threshold: u16,
2283        max_cognitive_threshold: u16,
2284        max_crap_threshold: f64,
2285    ) -> Vec<serde_json::Value> {
2286        let mut value = finding_json;
2287        if let Some(map) = value.as_object_mut() {
2288            map.entry("col".to_string())
2289                .or_insert(serde_json::Value::from(0_u32));
2290            map.entry("line_count".to_string())
2291                .or_insert(serde_json::Value::from(0_u32));
2292            map.entry("param_count".to_string())
2293                .or_insert(serde_json::Value::from(0_u8));
2294            map.entry("severity".to_string())
2295                .or_insert(serde_json::Value::String("moderate".to_string()));
2296        }
2297        let violation = synthesize_complexity_violation(&value);
2298        let ctx = crate::health_types::HealthActionContext {
2299            opts,
2300            max_cyclomatic_threshold,
2301            max_cognitive_threshold,
2302            max_crap_threshold,
2303            crap_refactor_band: 5,
2304        };
2305        let finding = crate::health_types::HealthFinding::with_actions(violation, &ctx);
2306        let serialized = serde_json::to_value(&finding).expect("serialize HealthFinding");
2307        serialized["actions"]
2308            .as_array()
2309            .cloned()
2310            .unwrap_or_default()
2311    }
2312
2313    /// Reads a JSON object with finding-shape fields and produces a
2314    /// [`ComplexityViolation`]. Test-only: panics on schema mismatches so
2315    /// authors notice when synthetic fixtures drift from the canonical
2316    /// shape.
2317    fn synthesize_complexity_violation(
2318        value: &serde_json::Value,
2319    ) -> crate::health_types::ComplexityViolation {
2320        use crate::health_types::{
2321            CoverageSource, CoverageTier, ExceededThreshold, FindingSeverity,
2322        };
2323        let exceeded = match value["exceeded"].as_str().unwrap_or("crap") {
2324            "cyclomatic" => ExceededThreshold::Cyclomatic,
2325            "cognitive" => ExceededThreshold::Cognitive,
2326            "both" => ExceededThreshold::Both,
2327            "crap" => ExceededThreshold::Crap,
2328            "cyclomatic_crap" => ExceededThreshold::CyclomaticCrap,
2329            "cognitive_crap" => ExceededThreshold::CognitiveCrap,
2330            "all" => ExceededThreshold::All,
2331            other => panic!("unknown exceeded label: {other}"),
2332        };
2333        let severity = match value["severity"].as_str().unwrap_or("moderate") {
2334            "moderate" => FindingSeverity::Moderate,
2335            "high" => FindingSeverity::High,
2336            "critical" => FindingSeverity::Critical,
2337            other => panic!("unknown severity label: {other}"),
2338        };
2339        let coverage_tier = value
2340            .get("coverage_tier")
2341            .and_then(|v| v.as_str())
2342            .map(|t| match t {
2343                "none" => CoverageTier::None,
2344                "partial" => CoverageTier::Partial,
2345                "high" => CoverageTier::High,
2346                other => panic!("unknown coverage_tier label: {other}"),
2347            });
2348        let coverage_source =
2349            value
2350                .get("coverage_source")
2351                .and_then(|v| v.as_str())
2352                .map(|s| match s {
2353                    "istanbul" => CoverageSource::Istanbul,
2354                    "estimated" => CoverageSource::Estimated,
2355                    "estimated_component_inherited" => CoverageSource::EstimatedComponentInherited,
2356                    other => panic!("unknown coverage_source label: {other}"),
2357                });
2358        crate::health_types::ComplexityViolation {
2359            path: std::path::PathBuf::from(value["path"].as_str().unwrap_or("src/x.ts")),
2360            name: value["name"].as_str().unwrap_or("fn").to_string(),
2361            line: u32::try_from(value["line"].as_u64().unwrap_or(0)).unwrap_or(0),
2362            col: u32::try_from(value["col"].as_u64().unwrap_or(0)).unwrap_or(0),
2363            cyclomatic: u16::try_from(value["cyclomatic"].as_u64().unwrap_or(0)).unwrap_or(0),
2364            cognitive: u16::try_from(value["cognitive"].as_u64().unwrap_or(0)).unwrap_or(0),
2365            line_count: u32::try_from(value["line_count"].as_u64().unwrap_or(0)).unwrap_or(0),
2366            param_count: u8::try_from(value["param_count"].as_u64().unwrap_or(0)).unwrap_or(0),
2367            react_hook_count: u16::try_from(value["react_hook_count"].as_u64().unwrap_or(0))
2368                .unwrap_or(0),
2369            react_jsx_max_depth: u16::try_from(value["react_jsx_max_depth"].as_u64().unwrap_or(0))
2370                .unwrap_or(0),
2371            react_prop_count: u16::try_from(value["react_prop_count"].as_u64().unwrap_or(0))
2372                .unwrap_or(0),
2373            react_hook_profile: value.get("react_hook_profile").map(|p| {
2374                let read_u16 = |key: &str| {
2375                    u16::try_from(p.get(key).and_then(serde_json::Value::as_u64).unwrap_or(0))
2376                        .unwrap_or(0)
2377                };
2378                crate::health_types::ReactHookProfile {
2379                    state: read_u16("state"),
2380                    effect: read_u16("effect"),
2381                    memo: read_u16("memo"),
2382                    callback: read_u16("callback"),
2383                    custom: read_u16("custom"),
2384                    max_effect_dep_arity: p
2385                        .get("max_effect_dep_arity")
2386                        .and_then(serde_json::Value::as_u64)
2387                        .and_then(|v| u32::try_from(v).ok()),
2388                }
2389            }),
2390            exceeded,
2391            severity,
2392            crap: value.get("crap").and_then(|v| v.as_f64()),
2393            coverage_pct: value.get("coverage_pct").and_then(|v| v.as_f64()),
2394            coverage_tier,
2395            coverage_source,
2396            inherited_from: value
2397                .get("inherited_from")
2398                .and_then(|v| v.as_str())
2399                .map(std::path::PathBuf::from),
2400            component_rollup: value.get("component_rollup").and_then(|v| {
2401                let map = v.as_object()?;
2402                Some(crate::health_types::ComponentRollup {
2403                    component: map.get("component")?.as_str()?.to_string(),
2404                    class_worst_function: map.get("class_worst_function")?.as_str()?.to_string(),
2405                    class_cyclomatic: u16::try_from(map.get("class_cyclomatic")?.as_u64()?).ok()?,
2406                    class_cognitive: u16::try_from(map.get("class_cognitive")?.as_u64()?).ok()?,
2407                    template_path: std::path::PathBuf::from(map.get("template_path")?.as_str()?),
2408                    template_cyclomatic: u16::try_from(map.get("template_cyclomatic")?.as_u64()?)
2409                        .ok()?,
2410                    template_cognitive: u16::try_from(map.get("template_cognitive")?.as_u64()?)
2411                        .ok()?,
2412                })
2413            }),
2414            contributions: Vec::new(),
2415            effective_thresholds: None,
2416            threshold_source: None,
2417        }
2418    }
2419
2420    #[test]
2421    fn health_finding_has_actions() {
2422        let actions = build_actions_for_finding_json(
2423            serde_json::json!({
2424                "path": "src/utils.ts",
2425                "name": "processData",
2426                "line": 10,
2427                "col": 0,
2428                "cyclomatic": 25,
2429                "cognitive": 30,
2430                "line_count": 150,
2431                "exceeded": "both"
2432            }),
2433            crate::health_types::HealthActionOptions::default(),
2434            20,
2435            15,
2436            30.0,
2437        );
2438
2439        assert_eq!(actions.len(), 2);
2440        assert_eq!(actions[0]["type"], "refactor-function");
2441        assert_eq!(actions[0]["auto_fixable"], false);
2442        assert!(
2443            actions[0]["description"]
2444                .as_str()
2445                .unwrap()
2446                .contains("processData")
2447        );
2448        assert_eq!(actions[1]["type"], "suppress-line");
2449        assert_eq!(
2450            actions[1]["comment"],
2451            "// fallow-ignore-next-line complexity"
2452        );
2453    }
2454
2455    #[test]
2456    fn health_finding_suppress_has_placement() {
2457        let actions = build_actions_for_finding_json(
2458            serde_json::json!({
2459                "path": "src/utils.ts",
2460                "name": "processData",
2461                "line": 10,
2462                "col": 0,
2463                "cyclomatic": 25,
2464                "cognitive": 30,
2465                "line_count": 150,
2466                "exceeded": "both"
2467            }),
2468            crate::health_types::HealthActionOptions::default(),
2469            20,
2470            15,
2471            30.0,
2472        );
2473
2474        assert_eq!(actions[1]["placement"], "above-function-declaration");
2475    }
2476
2477    #[test]
2478    fn html_template_health_finding_uses_html_suppression() {
2479        let actions = build_actions_for_finding_json(
2480            serde_json::json!({
2481                "path": "src/app.component.html",
2482                "name": "<template>",
2483                "line": 1,
2484                "col": 0,
2485                "cyclomatic": 25,
2486                "cognitive": 30,
2487                "line_count": 40,
2488                "exceeded": "both"
2489            }),
2490            crate::health_types::HealthActionOptions::default(),
2491            20,
2492            15,
2493            30.0,
2494        );
2495
2496        let suppress = &actions[1];
2497        assert_eq!(suppress["type"], "suppress-file");
2498        assert_eq!(
2499            suppress["comment"],
2500            "<!-- fallow-ignore-file complexity -->"
2501        );
2502        assert_eq!(suppress["placement"], "top-of-template");
2503    }
2504
2505    #[test]
2506    fn inline_template_health_finding_uses_decorator_suppression() {
2507        let actions = build_actions_for_finding_json(
2508            serde_json::json!({
2509                "path": "src/app.component.ts",
2510                "name": "<template>",
2511                "line": 5,
2512                "col": 0,
2513                "cyclomatic": 25,
2514                "cognitive": 30,
2515                "line_count": 40,
2516                "exceeded": "both"
2517            }),
2518            crate::health_types::HealthActionOptions::default(),
2519            20,
2520            15,
2521            30.0,
2522        );
2523
2524        let refactor = &actions[0];
2525        assert_eq!(refactor["type"], "refactor-function");
2526        assert!(
2527            refactor["description"]
2528                .as_str()
2529                .unwrap()
2530                .contains("template complexity")
2531        );
2532        let suppress = &actions[1];
2533        assert_eq!(suppress["type"], "suppress-line");
2534        assert_eq!(
2535            suppress["description"],
2536            "Suppress with an inline comment above the Angular decorator"
2537        );
2538        assert_eq!(suppress["placement"], "above-angular-decorator");
2539    }
2540
2541    /// Helper: build a health JSON envelope with a single CRAP-only finding.
2542    /// Default cognitive complexity is 12 (above the cognitive floor at the
2543    /// default `max_cognitive_threshold / 2 = 7.5`); use
2544    /// `crap_only_finding_envelope_with_cognitive` to exercise low-cog cases
2545    /// (flat dispatchers, JSX render maps) where the cognitive floor should
2546    /// suppress the secondary refactor.
2547    fn crap_only_finding_envelope(
2548        coverage_tier: Option<&str>,
2549        cyclomatic: u16,
2550        max_cyclomatic_threshold: u16,
2551    ) -> serde_json::Value {
2552        crap_only_finding_envelope_with_max_crap(
2553            coverage_tier,
2554            cyclomatic,
2555            12,
2556            max_cyclomatic_threshold,
2557            15,
2558            30.0,
2559        )
2560    }
2561
2562    fn crap_only_finding_envelope_with_cognitive(
2563        coverage_tier: Option<&str>,
2564        cyclomatic: u16,
2565        cognitive: u16,
2566        max_cyclomatic_threshold: u16,
2567    ) -> serde_json::Value {
2568        crap_only_finding_envelope_with_max_crap(
2569            coverage_tier,
2570            cyclomatic,
2571            cognitive,
2572            max_cyclomatic_threshold,
2573            15,
2574            30.0,
2575        )
2576    }
2577
2578    /// Build a synthetic health JSON envelope around a single typed
2579    /// [`HealthFinding`] so the existing JSON-shaped assertions in this
2580    /// module keep working after PR B2 of #384 moved action selection from
2581    /// the JSON post-pass into [`HealthFinding::with_actions`]. Defaults to
2582    /// the un-suppressed action context; callers that want to exercise the
2583    /// `omit_suppress_line` path should go through
2584    /// [`build_finding_envelope_with_ctx`].
2585    fn crap_only_finding_envelope_with_max_crap(
2586        coverage_tier: Option<&str>,
2587        cyclomatic: u16,
2588        cognitive: u16,
2589        max_cyclomatic_threshold: u16,
2590        max_cognitive_threshold: u16,
2591        max_crap_threshold: f64,
2592    ) -> serde_json::Value {
2593        build_finding_envelope_with_ctx(
2594            coverage_tier,
2595            cyclomatic,
2596            cognitive,
2597            max_cyclomatic_threshold,
2598            max_cognitive_threshold,
2599            max_crap_threshold,
2600            crate::health_types::HealthActionOptions::default(),
2601        )
2602    }
2603
2604    /// Build a single-finding health JSON envelope with the supplied action
2605    /// context. Used by the suppress-line gating tests to exercise the
2606    /// `baseline-active` / `config-disabled` reasons.
2607    fn build_finding_envelope_with_ctx(
2608        coverage_tier: Option<&str>,
2609        cyclomatic: u16,
2610        cognitive: u16,
2611        max_cyclomatic_threshold: u16,
2612        max_cognitive_threshold: u16,
2613        max_crap_threshold: f64,
2614        action_opts: crate::health_types::HealthActionOptions,
2615    ) -> serde_json::Value {
2616        let tier = coverage_tier.map(|t| match t {
2617            "none" => crate::health_types::CoverageTier::None,
2618            "partial" => crate::health_types::CoverageTier::Partial,
2619            "high" => crate::health_types::CoverageTier::High,
2620            other => panic!("unknown coverage tier label: {other}"),
2621        });
2622        let violation = crate::health_types::ComplexityViolation {
2623            path: std::path::PathBuf::from("src/risk.ts"),
2624            name: "computeScore".to_string(),
2625            line: 12,
2626            col: 0,
2627            cyclomatic,
2628            cognitive,
2629            line_count: 40,
2630            param_count: 0,
2631            react_hook_count: 0,
2632            react_jsx_max_depth: 0,
2633            react_prop_count: 0,
2634            react_hook_profile: None,
2635            exceeded: crate::health_types::ExceededThreshold::Crap,
2636            severity: crate::health_types::FindingSeverity::Moderate,
2637            crap: Some(35.5),
2638            coverage_pct: None,
2639            coverage_tier: tier,
2640            coverage_source: None,
2641            inherited_from: None,
2642            component_rollup: None,
2643            contributions: Vec::new(),
2644            effective_thresholds: None,
2645            threshold_source: None,
2646        };
2647        let ctx = crate::health_types::HealthActionContext {
2648            opts: action_opts,
2649            max_cyclomatic_threshold,
2650            max_cognitive_threshold,
2651            max_crap_threshold,
2652            crap_refactor_band: 5,
2653        };
2654        let finding = crate::health_types::HealthFinding::with_actions(violation, &ctx);
2655        let actions_meta = if action_opts.omit_suppress_line {
2656            Some(serde_json::json!({
2657                "suppression_hints_omitted": true,
2658                "reason": action_opts.omit_reason.unwrap_or("unspecified"),
2659                "scope": "health-findings",
2660            }))
2661        } else {
2662            None
2663        };
2664        let mut envelope = serde_json::json!({
2665            "findings": [serde_json::to_value(&finding).unwrap()],
2666            "summary": {
2667                "max_cyclomatic_threshold": max_cyclomatic_threshold,
2668                "max_cognitive_threshold": max_cognitive_threshold,
2669                "max_crap_threshold": max_crap_threshold,
2670            },
2671        });
2672        if let Some(meta) = actions_meta
2673            && let Some(map) = envelope.as_object_mut()
2674        {
2675            map.insert("actions_meta".to_string(), meta);
2676        }
2677        envelope
2678    }
2679
2680    #[test]
2681    fn crap_only_tier_none_emits_add_tests() {
2682        let output = crap_only_finding_envelope(Some("none"), 6, 20);
2683        let actions = output["findings"][0]["actions"].as_array().unwrap();
2684        assert!(
2685            actions.iter().any(|a| a["type"] == "add-tests"),
2686            "tier=none crap-only must emit add-tests, got {actions:?}"
2687        );
2688        assert!(
2689            !actions.iter().any(|a| a["type"] == "increase-coverage"),
2690            "tier=none must not emit increase-coverage"
2691        );
2692    }
2693
2694    #[test]
2695    fn crap_only_tier_partial_emits_increase_coverage() {
2696        let output = crap_only_finding_envelope(Some("partial"), 6, 20);
2697        let actions = output["findings"][0]["actions"].as_array().unwrap();
2698        assert!(
2699            actions.iter().any(|a| a["type"] == "increase-coverage"),
2700            "tier=partial crap-only must emit increase-coverage, got {actions:?}"
2701        );
2702        assert!(
2703            !actions.iter().any(|a| a["type"] == "add-tests"),
2704            "tier=partial must not emit add-tests"
2705        );
2706    }
2707
2708    #[test]
2709    fn crap_only_tier_high_emits_increase_coverage_when_full_coverage_can_clear_crap() {
2710        let output = crap_only_finding_envelope(Some("high"), 20, 30);
2711        let actions = output["findings"][0]["actions"].as_array().unwrap();
2712        assert!(
2713            actions.iter().any(|a| a["type"] == "increase-coverage"),
2714            "tier=high crap-only must still emit increase-coverage when full coverage can clear CRAP, got {actions:?}"
2715        );
2716        assert!(
2717            !actions.iter().any(|a| a["type"] == "refactor-function"),
2718            "coverage-remediable crap-only findings should not get refactor-function unless near the cyclomatic threshold"
2719        );
2720        assert!(
2721            !actions.iter().any(|a| a["type"] == "add-tests"),
2722            "tier=high must not emit add-tests"
2723        );
2724    }
2725
2726    #[test]
2727    fn crap_only_emits_refactor_when_full_coverage_cannot_clear_crap() {
2728        let output = crap_only_finding_envelope_with_max_crap(Some("high"), 35, 12, 50, 15, 30.0);
2729        let actions = output["findings"][0]["actions"].as_array().unwrap();
2730        assert!(
2731            actions.iter().any(|a| a["type"] == "refactor-function"),
2732            "full-coverage-impossible CRAP-only finding must emit refactor-function, got {actions:?}"
2733        );
2734        assert!(
2735            !actions.iter().any(|a| a["type"] == "increase-coverage"),
2736            "must not emit increase-coverage when even 100% coverage cannot clear CRAP"
2737        );
2738        assert!(
2739            !actions.iter().any(|a| a["type"] == "add-tests"),
2740            "must not emit add-tests when even 100% coverage cannot clear CRAP"
2741        );
2742    }
2743
2744    #[test]
2745    fn crap_only_high_cc_appends_secondary_refactor() {
2746        let output = crap_only_finding_envelope(Some("none"), 16, 20);
2747        let actions = output["findings"][0]["actions"].as_array().unwrap();
2748        assert!(
2749            actions.iter().any(|a| a["type"] == "add-tests"),
2750            "near-threshold crap-only still emits the primary tier action"
2751        );
2752        assert!(
2753            actions.iter().any(|a| a["type"] == "refactor-function"),
2754            "near-threshold crap-only must also emit secondary refactor-function"
2755        );
2756    }
2757
2758    #[test]
2759    fn crap_only_far_below_threshold_no_secondary_refactor() {
2760        let output = crap_only_finding_envelope(Some("none"), 6, 20);
2761        let actions = output["findings"][0]["actions"].as_array().unwrap();
2762        assert!(
2763            !actions.iter().any(|a| a["type"] == "refactor-function"),
2764            "low-CC crap-only should not get a secondary refactor-function"
2765        );
2766    }
2767
2768    #[test]
2769    fn crap_only_near_threshold_low_cognitive_no_secondary_refactor() {
2770        let output = crap_only_finding_envelope_with_cognitive(Some("none"), 17, 2, 20);
2771        let actions = output["findings"][0]["actions"].as_array().unwrap();
2772        assert!(
2773            actions.iter().any(|a| a["type"] == "add-tests"),
2774            "primary tier action still emits"
2775        );
2776        assert!(
2777            !actions.iter().any(|a| a["type"] == "refactor-function"),
2778            "near-threshold CC with cognitive below floor must NOT emit secondary refactor (got {actions:?})"
2779        );
2780    }
2781
2782    #[test]
2783    fn crap_only_near_threshold_high_cognitive_emits_secondary_refactor() {
2784        let output = crap_only_finding_envelope_with_cognitive(Some("none"), 16, 10, 20);
2785        let actions = output["findings"][0]["actions"].as_array().unwrap();
2786        assert!(
2787            actions.iter().any(|a| a["type"] == "add-tests"),
2788            "primary tier action still emits"
2789        );
2790        assert!(
2791            actions.iter().any(|a| a["type"] == "refactor-function"),
2792            "near-threshold CC with cognitive above floor must emit secondary refactor (got {actions:?})"
2793        );
2794    }
2795
2796    #[test]
2797    fn crap_only_secondary_refactor_respects_configured_band() {
2798        let violation = crate::health_types::ComplexityViolation {
2799            path: std::path::PathBuf::from("src/risk.ts"),
2800            name: "computeScore".to_string(),
2801            line: 12,
2802            col: 0,
2803            cyclomatic: 14,
2804            cognitive: 10,
2805            line_count: 40,
2806            param_count: 0,
2807            react_hook_count: 0,
2808            react_jsx_max_depth: 0,
2809            react_prop_count: 0,
2810            react_hook_profile: None,
2811            exceeded: crate::health_types::ExceededThreshold::Crap,
2812            severity: crate::health_types::FindingSeverity::Moderate,
2813            crap: Some(35.5),
2814            coverage_pct: None,
2815            coverage_tier: Some(crate::health_types::CoverageTier::None),
2816            coverage_source: None,
2817            inherited_from: None,
2818            component_rollup: None,
2819            contributions: Vec::new(),
2820            effective_thresholds: None,
2821            threshold_source: None,
2822        };
2823        let narrow_ctx = crate::health_types::HealthActionContext {
2824            opts: crate::health_types::HealthActionOptions::default(),
2825            max_cyclomatic_threshold: 20,
2826            max_cognitive_threshold: 15,
2827            max_crap_threshold: 30.0,
2828            crap_refactor_band: 5,
2829        };
2830        let wide_ctx = crate::health_types::HealthActionContext {
2831            crap_refactor_band: 6,
2832            ..narrow_ctx
2833        };
2834
2835        let narrow_actions =
2836            crate::health_types::build_health_finding_actions(&violation, &narrow_ctx);
2837        let wide_actions = crate::health_types::build_health_finding_actions(&violation, &wide_ctx);
2838
2839        assert!(
2840            !narrow_actions.iter().any(|a| {
2841                matches!(
2842                    a.kind,
2843                    fallow_types::output_health::HealthFindingActionType::RefactorFunction
2844                )
2845            }),
2846            "default band should not refactor a CRAP-only finding 6 below max cyclomatic"
2847        );
2848        assert!(
2849            wide_actions.iter().any(|a| {
2850                matches!(
2851                    a.kind,
2852                    fallow_types::output_health::HealthFindingActionType::RefactorFunction
2853                )
2854            }),
2855            "configured wider band should emit the secondary refactor action"
2856        );
2857    }
2858
2859    #[test]
2860    fn cyclomatic_only_emits_only_refactor_function() {
2861        let actions = build_actions_for_finding_json(
2862            serde_json::json!({
2863                "path": "src/cyclo.ts",
2864                "name": "branchy",
2865                "line": 5,
2866                "col": 0,
2867                "cyclomatic": 25,
2868                "cognitive": 10,
2869                "line_count": 80,
2870                "exceeded": "cyclomatic",
2871            }),
2872            crate::health_types::HealthActionOptions::default(),
2873            20,
2874            15,
2875            30.0,
2876        );
2877        assert!(
2878            actions.iter().any(|a| a["type"] == "refactor-function"),
2879            "non-CRAP findings emit refactor-function"
2880        );
2881        assert!(
2882            !actions.iter().any(|a| a["type"] == "add-tests"),
2883            "non-CRAP findings must not emit add-tests"
2884        );
2885        assert!(
2886            !actions.iter().any(|a| a["type"] == "increase-coverage"),
2887            "non-CRAP findings must not emit increase-coverage"
2888        );
2889    }
2890
2891    #[test]
2892    fn suppress_line_omitted_when_baseline_active() {
2893        let output = build_finding_envelope_with_ctx(
2894            Some("none"),
2895            6,
2896            12,
2897            20,
2898            15,
2899            30.0,
2900            crate::health_types::HealthActionOptions {
2901                omit_suppress_line: true,
2902                omit_reason: Some("baseline-active"),
2903            },
2904        );
2905        let actions = output["findings"][0]["actions"].as_array().unwrap();
2906        assert!(
2907            !actions.iter().any(|a| a["type"] == "suppress-line"),
2908            "baseline-active must not emit suppress-line, got {actions:?}"
2909        );
2910        assert_eq!(
2911            output["actions_meta"]["suppression_hints_omitted"],
2912            serde_json::Value::Bool(true)
2913        );
2914        assert_eq!(output["actions_meta"]["reason"], "baseline-active");
2915        assert_eq!(output["actions_meta"]["scope"], "health-findings");
2916    }
2917
2918    #[test]
2919    fn suppress_line_omitted_when_config_disabled() {
2920        let output = build_finding_envelope_with_ctx(
2921            Some("none"),
2922            6,
2923            12,
2924            20,
2925            15,
2926            30.0,
2927            crate::health_types::HealthActionOptions {
2928                omit_suppress_line: true,
2929                omit_reason: Some("config-disabled"),
2930            },
2931        );
2932        assert_eq!(output["actions_meta"]["reason"], "config-disabled");
2933    }
2934
2935    #[test]
2936    fn suppress_line_emitted_by_default() {
2937        let output = crap_only_finding_envelope(Some("none"), 6, 20);
2938        let actions = output["findings"][0]["actions"].as_array().unwrap();
2939        assert!(
2940            actions.iter().any(|a| a["type"] == "suppress-line"),
2941            "default opts must emit suppress-line"
2942        );
2943        assert!(
2944            output.get("actions_meta").is_none(),
2945            "actions_meta must be absent when no omission occurred"
2946        );
2947    }
2948
2949    /// Drift guard: every action `type` value emitted by the action builder
2950    /// must appear in `docs/output-schema.json`'s `HealthFindingAction.type`
2951    /// enum. Previously the schema listed only `[refactor-function,
2952    /// suppress-line]` while the code emitted `add-tests` for CRAP findings,
2953    /// silently producing schema-invalid output for any consumer using the
2954    /// schema for validation.
2955    #[test]
2956    fn every_emitted_health_action_type_is_in_schema_enum() {
2957        let cases = [
2958            ("crap", Some("none"), 6_u16, 20_u16),
2959            ("crap", Some("partial"), 6, 20),
2960            ("crap", Some("high"), 12, 20),
2961            ("crap", Some("none"), 16, 20), // near threshold => secondary refactor
2962            ("cyclomatic", None, 25, 20),
2963            ("cognitive_crap", Some("partial"), 6, 20),
2964            ("all", Some("none"), 25, 20),
2965        ];
2966
2967        let mut emitted: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
2968        for (exceeded, tier, cc, max) in cases {
2969            let mut finding = serde_json::json!({
2970                "path": "src/x.ts",
2971                "name": "fn",
2972                "line": 1,
2973                "col": 0,
2974                "cyclomatic": cc,
2975                "cognitive": 5,
2976                "line_count": 10,
2977                "exceeded": exceeded,
2978                "crap": 35.0,
2979            });
2980            if let Some(t) = tier {
2981                finding["coverage_tier"] = serde_json::Value::String(t.to_owned());
2982            }
2983            let actions = build_actions_for_finding_json(
2984                finding,
2985                crate::health_types::HealthActionOptions::default(),
2986                max,
2987                15,
2988                30.0,
2989            );
2990            for action in &actions {
2991                if let Some(ty) = action["type"].as_str() {
2992                    emitted.insert(ty.to_owned());
2993                }
2994            }
2995        }
2996
2997        let schema_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
2998            .join("..")
2999            .join("..")
3000            .join("docs")
3001            .join("output-schema.json");
3002        let raw = std::fs::read_to_string(&schema_path)
3003            .expect("docs/output-schema.json must be readable for the drift-guard test");
3004        let schema: serde_json::Value = serde_json::from_str(&raw).expect("schema parses");
3005        let type_field = &schema["definitions"]["HealthFindingAction"]["properties"]["type"];
3006        let type_def = if let Some(reference) = type_field.get("$ref").and_then(|r| r.as_str()) {
3007            let name = reference
3008                .strip_prefix("#/definitions/")
3009                .expect("HealthFindingAction.type $ref points into #/definitions/");
3010            &schema["definitions"][name]
3011        } else {
3012            type_field
3013        };
3014        let mut enum_values: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
3015        if let Some(arr) = type_def.get("enum").and_then(|e| e.as_array()) {
3016            for v in arr {
3017                if let Some(s) = v.as_str() {
3018                    enum_values.insert(s.to_owned());
3019                }
3020            }
3021        }
3022        if let Some(arr) = type_def.get("oneOf").and_then(|e| e.as_array()) {
3023            for branch in arr {
3024                if let Some(s) = branch.get("const").and_then(|c| c.as_str()) {
3025                    enum_values.insert(s.to_owned());
3026                }
3027            }
3028        }
3029        assert!(
3030            !enum_values.is_empty(),
3031            "could not extract HealthFindingActionType variants from schema (neither `enum` nor `oneOf` with `const` branches)"
3032        );
3033
3034        for ty in &emitted {
3035            assert!(
3036                enum_values.contains(ty),
3037                "build_health_finding_actions emitted action type `{ty}` but \
3038                 docs/output-schema.json HealthFindingAction.type enum does \
3039                 not list it. Add it to the schema (and any downstream \
3040                 typed consumers) when introducing a new action type."
3041            );
3042        }
3043    }
3044
3045    /// Regression for issue #412: prevent reintroduction of the legacy
3046    /// `inject_*` / `augment_*` post-pass pattern in this file. Every
3047    /// JSON `actions[]` array on every finding type should flow from a
3048    /// typed `serde(flatten)` envelope, not from a post-construction
3049    /// mutation of a `serde_json::Value` tree.
3050    ///
3051    /// The allow-list mirrors the `HAND_MAINTAINED_ALLOW_LIST` pattern
3052    /// in `crates/cli/src/bin/schema_emit.rs`: each entry pairs a name
3053    /// with the issue that retires it. It is empty today; any addition
3054    /// needs an issue reference in the same commit. The gate also
3055    /// asserts no STALE entries, so removing a function without
3056    /// removing its allow-list entry fails the test and forces the
3057    /// cleanup commit.
3058    #[test]
3059    fn no_new_post_pass_helpers_in_json_rs() {
3060        const POST_PASS_ALLOW_LIST: &[(&str, &str)] = &[];
3061        let source_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
3062            .join("src")
3063            .join("report")
3064            .join("json.rs");
3065        let source = std::fs::read_to_string(&source_path).expect(
3066            "crates/cli/src/report/json.rs must be readable for the post-pass drift-guard test",
3067        );
3068        let mut found: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
3069        for line in source.lines() {
3070            if let Some(name) = extract_post_pass_fn_name(line) {
3071                found.insert(name.to_owned());
3072            }
3073        }
3074        let allow: std::collections::BTreeSet<&'static str> =
3075            POST_PASS_ALLOW_LIST.iter().map(|(name, _)| *name).collect();
3076        let unexpected: Vec<&str> = found
3077            .iter()
3078            .filter(|name| !allow.contains(name.as_str()))
3079            .map(String::as_str)
3080            .collect();
3081        let stale: Vec<&str> = allow
3082            .iter()
3083            .filter(|name| !found.contains(**name))
3084            .copied()
3085            .collect();
3086        assert!(
3087            unexpected.is_empty(),
3088            "new post-pass helper(s) defined in crates/cli/src/report/json.rs are not in \
3089             POST_PASS_ALLOW_LIST: {unexpected:?}.\n\
3090             The typed `serde(flatten)` envelope is the source of truth for `actions[]` on \
3091             every finding. If a new post-pass is genuinely needed, file a tracking issue, \
3092             add the entry to POST_PASS_ALLOW_LIST with the issue link as the reason, and \
3093             reference the issue in the PR body. See issue #412 for context."
3094        );
3095        assert!(
3096            stale.is_empty(),
3097            "stale entries in POST_PASS_ALLOW_LIST (function no longer defined in \
3098             crates/cli/src/report/json.rs): {stale:?}.\n\
3099             Remove them in the same commit that retired the function."
3100        );
3101    }
3102
3103    /// Extracts an `inject_<name>` or `augment_<name>` identifier from a
3104    /// Rust function-definition line, handling `pub`, `pub(...)`,
3105    /// `async`, `const`, and `unsafe` modifiers. Returns `None` for
3106    /// non-definition lines (comments, call sites, doc strings).
3107    fn extract_post_pass_fn_name(line: &str) -> Option<&str> {
3108        let trimmed = line.trim_start();
3109        if trimmed.starts_with("//") {
3110            return None;
3111        }
3112        let mut rest = trimmed;
3113        if let Some(after) = rest.strip_prefix("pub") {
3114            let after = after.trim_start();
3115            rest = if let Some(after) = after.strip_prefix('(') {
3116                let close = after.find(')')?;
3117                after[close + 1..].trim_start()
3118            } else {
3119                after
3120            };
3121        }
3122        for prefix in ["async ", "const ", "unsafe "] {
3123            if let Some(after) = rest.strip_prefix(prefix) {
3124                rest = after.trim_start();
3125            }
3126        }
3127        let after_fn = rest.strip_prefix("fn ")?;
3128        let name_end = after_fn
3129            .find(|c: char| !c.is_alphanumeric() && c != '_')
3130            .unwrap_or(after_fn.len());
3131        let name = &after_fn[..name_end];
3132        if name.starts_with("inject_") || name.starts_with("augment_") {
3133            Some(name)
3134        } else {
3135            None
3136        }
3137    }
3138}