Skip to main content

fallow_cli/regression/
baseline.rs

1use std::path::Path;
2use std::process::ExitCode;
3
4use fallow_config::OutputFormat;
5use fallow_core::git_env::clear_ambient_git_env;
6use fallow_core::results::AnalysisResults;
7
8use super::counts::{CheckCounts, DupesCounts, REGRESSION_SCHEMA_VERSION, RegressionBaseline};
9use super::outcome::RegressionOutcome;
10use super::tolerance::Tolerance;
11
12use crate::error::emit_error;
13
14/// Number of seconds in one day.
15const SECS_PER_DAY: u64 = 86_400;
16
17/// Where to save the regression baseline.
18#[derive(Clone, Copy)]
19pub enum SaveRegressionTarget<'a> {
20    /// Don't save.
21    None,
22    /// Save into the config file (.fallowrc.json / .fallowrc.jsonc / fallow.toml / .fallow.toml).
23    Config,
24    /// Save to an explicit file path.
25    File(&'a Path),
26}
27
28/// Options for regression detection.
29#[derive(Clone, Copy)]
30pub struct RegressionOpts<'a> {
31    pub fail_on_regression: bool,
32    pub tolerance: Tolerance,
33    /// Explicit regression baseline file path (overrides config).
34    pub regression_baseline_file: Option<&'a Path>,
35    /// Where to save the regression baseline.
36    pub save_target: SaveRegressionTarget<'a>,
37    /// Whether --changed-since or --workspace is active (makes counts incomparable).
38    pub scoped: bool,
39    pub quiet: bool,
40    /// Output format. Drives whether load errors are emitted as structured JSON on stdout
41    /// (for `--format json` CI consumers) or human text on stderr.
42    pub output: OutputFormat,
43}
44
45/// Check whether a path is likely gitignored by running `git check-ignore`.
46/// Returns `false` if git is unavailable or the check fails (conservative).
47fn is_likely_gitignored(path: &Path, root: &Path) -> bool {
48    let mut command = std::process::Command::new("git");
49    command
50        .args(["check-ignore", "-q"])
51        .arg(path)
52        .current_dir(root);
53    clear_ambient_git_env(&mut command);
54    command.output().ok().is_some_and(|o| o.status.success())
55}
56
57/// Get the current git SHA, if available.
58fn current_git_sha(root: &Path) -> Option<String> {
59    let mut command = std::process::Command::new("git");
60    command.args(["rev-parse", "HEAD"]).current_dir(root);
61    clear_ambient_git_env(&mut command);
62    command
63        .output()
64        .ok()
65        .filter(|o| o.status.success())
66        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
67}
68
69/// Save the current analysis results as a regression baseline.
70///
71/// # Errors
72///
73/// Returns an error if the baseline cannot be serialized or written to disk.
74pub fn save_regression_baseline(
75    path: &Path,
76    root: &Path,
77    check_counts: Option<&CheckCounts>,
78    dupes_counts: Option<&DupesCounts>,
79    output: OutputFormat,
80) -> Result<(), ExitCode> {
81    let baseline = RegressionBaseline {
82        schema_version: REGRESSION_SCHEMA_VERSION,
83        fallow_version: env!("CARGO_PKG_VERSION").to_string(),
84        timestamp: chrono_now(),
85        git_sha: current_git_sha(root),
86        check: check_counts.cloned(),
87        dupes: dupes_counts.cloned(),
88    };
89    let json = serde_json::to_string_pretty(&baseline).map_err(|e| {
90        emit_error(
91            &format!("failed to serialize regression baseline: {e}"),
92            2,
93            output,
94        )
95    })?;
96    if let Some(parent) = path.parent() {
97        let _ = std::fs::create_dir_all(parent);
98    }
99    std::fs::write(path, json).map_err(|e| {
100        emit_error(
101            &format!("failed to save regression baseline: {e}"),
102            2,
103            output,
104        )
105    })?;
106    eprintln!("Regression baseline saved to {}", path.display());
107    if is_likely_gitignored(path, root) {
108        eprintln!(
109            "Warning: '{}' may be gitignored. Commit this file so CI can compare against it.",
110            path.display()
111        );
112    }
113    Ok(())
114}
115
116/// Save regression baseline counts into the project's config file.
117///
118/// Reads the existing config, adds/updates the `regression.baseline` section,
119/// and writes it back. For JSONC files, comments are preserved using a targeted
120/// insertion/replacement strategy.
121///
122/// # Errors
123///
124/// Returns an error if the config file cannot be read, updated, or written back.
125pub fn save_baseline_to_config(
126    config_path: &Path,
127    counts: &CheckCounts,
128    output: OutputFormat,
129) -> Result<(), ExitCode> {
130    let content = match std::fs::read_to_string(config_path) {
131        Ok(c) => c,
132        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
133            let is_toml = config_path.extension().is_some_and(|ext| ext == "toml");
134            if is_toml {
135                String::new()
136            } else {
137                "{}".to_string()
138            }
139        }
140        Err(e) => {
141            return Err(emit_error(
142                &format!(
143                    "failed to read config file '{}': {e}",
144                    config_path.display()
145                ),
146                2,
147                output,
148            ));
149        }
150    };
151
152    let baseline = counts.to_config_baseline();
153    let is_toml = config_path.extension().is_some_and(|ext| ext == "toml");
154
155    let updated = if is_toml {
156        Ok(update_toml_regression(&content, &baseline))
157    } else {
158        update_json_regression(&content, &baseline)
159    }
160    .map_err(|e| {
161        emit_error(
162            &format!(
163                "failed to update config file '{}': {e}",
164                config_path.display()
165            ),
166            2,
167            output,
168        )
169    })?;
170
171    std::fs::write(config_path, updated).map_err(|e| {
172        emit_error(
173            &format!(
174                "failed to write config file '{}': {e}",
175                config_path.display()
176            ),
177            2,
178            output,
179        )
180    })?;
181
182    eprintln!(
183        "Regression baseline saved to {} (regression.baseline section)",
184        config_path.display()
185    );
186    Ok(())
187}
188
189/// Update a JSONC config file with regression baseline, preserving comments.
190/// Find a JSON key in content, skipping `//` line comments and `/* */` block comments.
191/// Returns the byte offset of the opening `"` of the key.
192fn find_json_key(content: &str, key: &str) -> Option<usize> {
193    let needle = format!("\"{key}\"");
194    let mut search_from = 0;
195    while let Some(pos) = content[search_from..].find(&needle) {
196        let abs_pos = search_from + pos;
197        let line_start = content[..abs_pos].rfind('\n').map_or(0, |i| i + 1);
198        let line_prefix = content[line_start..abs_pos].trim_start();
199        if line_prefix.starts_with("//") {
200            search_from = abs_pos + needle.len();
201            continue;
202        }
203        let before = &content[..abs_pos];
204        let last_open = before.rfind("/*");
205        let last_close = before.rfind("*/");
206        if let Some(open_pos) = last_open
207            && last_close.is_none_or(|close_pos| close_pos < open_pos)
208        {
209            search_from = abs_pos + needle.len();
210            continue;
211        }
212        return Some(abs_pos);
213    }
214    None
215}
216
217fn update_json_regression(
218    content: &str,
219    baseline: &fallow_config::RegressionBaseline,
220) -> Result<String, String> {
221    let baseline_json =
222        serde_json::to_string_pretty(baseline).map_err(|e| format!("serialization error: {e}"))?;
223
224    let indented: String = baseline_json
225        .lines()
226        .enumerate()
227        .map(|(i, line)| {
228            if i == 0 {
229                format!("    {line}")
230            } else {
231                format!("\n    {line}")
232            }
233        })
234        .collect();
235
236    let regression_block = format!("  \"regression\": {{\n    \"baseline\": {indented}\n  }}");
237
238    if let Some(start) = find_json_key(content, "regression") {
239        let after_key = &content[start..];
240        if let Some(brace_start) = after_key.find('{') {
241            let abs_brace = start + brace_start;
242            let mut depth = 0;
243            let mut end = abs_brace;
244            let mut found_close = false;
245            for (i, ch) in content[abs_brace..].char_indices() {
246                match ch {
247                    '{' => depth += 1,
248                    '}' => {
249                        depth -= 1;
250                        if depth == 0 {
251                            end = abs_brace + i + 1;
252                            found_close = true;
253                            break;
254                        }
255                    }
256                    _ => {}
257                }
258            }
259            if !found_close {
260                return Err("malformed JSON: unmatched brace in regression object".to_string());
261            }
262            let mut result = String::new();
263            result.push_str(&content[..start]);
264            result.push_str(&regression_block[2..]); // skip leading "  " — reuse original indent
265            result.push_str(&content[end..]);
266            return Ok(result);
267        }
268    }
269
270    if let Some(last_brace) = content.rfind('}') {
271        let before_brace = content[..last_brace].trim_end();
272        let needs_comma = !before_brace.ends_with('{') && !before_brace.ends_with(',');
273
274        let mut result = String::new();
275        result.push_str(before_brace);
276        if needs_comma {
277            result.push(',');
278        }
279        result.push('\n');
280        result.push_str(&regression_block);
281        result.push('\n');
282        result.push_str(&content[last_brace..]);
283        Ok(result)
284    } else {
285        Err("config file has no closing brace".to_string())
286    }
287}
288
289/// Update a TOML config file with regression baseline.
290fn update_toml_regression(content: &str, baseline: &fallow_config::RegressionBaseline) -> String {
291    use std::fmt::Write;
292    let mut section = String::from("[regression.baseline]\n");
293    let _ = writeln!(section, "totalIssues = {}", baseline.total_issues);
294    let _ = writeln!(section, "unusedFiles = {}", baseline.unused_files);
295    let _ = writeln!(section, "unusedExports = {}", baseline.unused_exports);
296    let _ = writeln!(section, "unusedTypes = {}", baseline.unused_types);
297    let _ = writeln!(
298        section,
299        "unusedDependencies = {}",
300        baseline.unused_dependencies
301    );
302    let _ = writeln!(
303        section,
304        "unusedDevDependencies = {}",
305        baseline.unused_dev_dependencies
306    );
307    let _ = writeln!(
308        section,
309        "unusedOptionalDependencies = {}",
310        baseline.unused_optional_dependencies
311    );
312    let _ = writeln!(
313        section,
314        "unusedEnumMembers = {}",
315        baseline.unused_enum_members
316    );
317    let _ = writeln!(
318        section,
319        "unusedClassMembers = {}",
320        baseline.unused_class_members
321    );
322    let _ = writeln!(
323        section,
324        "unresolvedImports = {}",
325        baseline.unresolved_imports
326    );
327    let _ = writeln!(
328        section,
329        "unlistedDependencies = {}",
330        baseline.unlisted_dependencies
331    );
332    let _ = writeln!(section, "duplicateExports = {}", baseline.duplicate_exports);
333    let _ = writeln!(
334        section,
335        "circularDependencies = {}",
336        baseline.circular_dependencies
337    );
338    let _ = writeln!(
339        section,
340        "typeOnlyDependencies = {}",
341        baseline.type_only_dependencies
342    );
343    let _ = writeln!(
344        section,
345        "testOnlyDependencies = {}",
346        baseline.test_only_dependencies
347    );
348
349    if let Some(start) = content.find("[regression.baseline]") {
350        let after = &content[start + "[regression.baseline]".len()..];
351        let end_offset = after.find("\n[").map_or(content.len(), |i| {
352            start + "[regression.baseline]".len() + i + 1
353        });
354
355        let mut result = String::new();
356        result.push_str(&content[..start]);
357        result.push_str(&section);
358        if end_offset < content.len() {
359            result.push_str(&content[end_offset..]);
360        }
361        result
362    } else {
363        let mut result = content.to_string();
364        if !result.ends_with('\n') {
365            result.push('\n');
366        }
367        result.push('\n');
368        result.push_str(&section);
369        result
370    }
371}
372
373/// Build the human-readable schema-version mismatch message. Factored out so
374/// tests can assert on the wording without capturing stderr.
375fn format_schema_mismatch_error(
376    path: &Path,
377    expected: u32,
378    actual: u32,
379    writer_version: &str,
380) -> String {
381    let path_display = path.display();
382    if actual == 0 {
383        format!(
384            "regression baseline '{path_display}' appears to predate schema versioning \
385             (schema_version is 0; this fallow build expects {expected}).\n\
386             The baseline was written by fallow {writer_version}.\n\
387             Regenerate it by running: fallow dead-code --save-regression-baseline {path_display}"
388        )
389    } else {
390        format!(
391            "regression baseline '{path_display}' has schema_version {actual} but this fallow build expects {expected}.\n\
392             The baseline was written by fallow {writer_version}.\n\
393             Regenerate it by running: fallow dead-code --save-regression-baseline {path_display}"
394        )
395    }
396}
397
398/// Build the message for a baseline missing `schema_version` entirely. Pre-versioning
399/// baselines (hand-edited or written by a very old fallow) hit this path; the raw
400/// serde error ("missing field `schema_version`") is unhelpful to a CI user.
401fn format_missing_schema_version_error(path: &Path) -> String {
402    let path_display = path.display();
403    let expected = REGRESSION_SCHEMA_VERSION;
404    format!(
405        "regression baseline '{path_display}' is missing the schema_version field; \
406         this fallow build expects schema_version {expected}.\n\
407         The baseline likely predates schema versioning or was hand-edited.\n\
408         Regenerate it by running: fallow dead-code --save-regression-baseline {path_display}"
409    )
410}
411
412/// Load a regression baseline from disk.
413///
414/// Validates that `schema_version` matches `REGRESSION_SCHEMA_VERSION`. Mismatches
415/// (including baselines missing the field entirely) fail loud with an actionable
416/// regenerate hint rather than silently loading default-zero fields, which would
417/// mask real regressions.
418///
419/// # Errors
420///
421/// Returns an error if the file does not exist, cannot be read, contains invalid
422/// JSON, or has a `schema_version` that does not match the current build's
423/// `REGRESSION_SCHEMA_VERSION`.
424pub fn load_regression_baseline(
425    path: &Path,
426    output: OutputFormat,
427) -> Result<RegressionBaseline, ExitCode> {
428    let content = std::fs::read_to_string(path).map_err(|e| {
429        if e.kind() == std::io::ErrorKind::NotFound {
430            emit_error(
431                &format!(
432                    "no regression baseline found at '{}'.\n\
433                     Run with --save-regression-baseline on your main branch to create one.",
434                    path.display()
435                ),
436                2,
437                output,
438            )
439        } else {
440            emit_error(
441                &format!(
442                    "failed to read regression baseline '{}': {e}",
443                    path.display()
444                ),
445                2,
446                output,
447            )
448        }
449    })?;
450    let baseline: RegressionBaseline = serde_json::from_str(&content).map_err(|e| {
451        let message = if e.to_string().contains("missing field `schema_version`") {
452            format_missing_schema_version_error(path)
453        } else {
454            format!(
455                "failed to parse regression baseline '{}': {e}",
456                path.display()
457            )
458        };
459        emit_error(&message, 2, output)
460    })?;
461    if baseline.schema_version != REGRESSION_SCHEMA_VERSION {
462        let message = format_schema_mismatch_error(
463            path,
464            REGRESSION_SCHEMA_VERSION,
465            baseline.schema_version,
466            &baseline.fallow_version,
467        );
468        return Err(emit_error(&message, 2, output));
469    }
470    Ok(baseline)
471}
472
473/// Compare current check results against a regression baseline.
474///
475/// Resolution order for the baseline:
476/// 1. Explicit file via `--regression-baseline <PATH>`
477/// 2. Config-embedded `regression.baseline` section
478/// 3. Error with actionable message
479///
480/// # Errors
481///
482/// Returns an error if the baseline file cannot be loaded, is missing check data,
483/// or no baseline source is available.
484pub fn compare_check_regression(
485    results: &AnalysisResults,
486    opts: &RegressionOpts<'_>,
487    config_baseline: Option<&fallow_config::RegressionBaseline>,
488) -> Result<Option<RegressionOutcome>, ExitCode> {
489    if !opts.fail_on_regression {
490        return Ok(None);
491    }
492
493    if opts.scoped {
494        let reason = "--changed-since or --workspace is active; regression check skipped \
495                      (counts not comparable to full-project baseline)";
496        if !opts.quiet {
497            eprintln!("Warning: {reason}");
498        }
499        return Ok(Some(RegressionOutcome::Skipped { reason }));
500    }
501
502    let baseline_counts: CheckCounts = if let Some(baseline_path) = opts.regression_baseline_file {
503        let baseline = load_regression_baseline(baseline_path, opts.output)?;
504        let Some(counts) = baseline.check else {
505            return Err(emit_error(
506                &format!(
507                    "regression baseline '{}' has no check data",
508                    baseline_path.display()
509                ),
510                2,
511                opts.output,
512            ));
513        };
514        counts
515    } else if let Some(config_baseline) = config_baseline {
516        CheckCounts::from_config_baseline(config_baseline)
517    } else {
518        return Err(emit_error(
519            "no regression baseline found.\n\
520             Either add a `regression.baseline` section to your config file\n\
521             (run with --save-regression-baseline to generate it),\n\
522             or provide an explicit file via --regression-baseline <PATH>.",
523            2,
524            opts.output,
525        ));
526    };
527
528    let current_total = results.total_issues();
529    let baseline_total = baseline_counts.total_issues;
530
531    if opts.tolerance.exceeded(baseline_total, current_total) {
532        let current_counts = CheckCounts::from_results(results);
533        let type_deltas = baseline_counts.deltas(&current_counts);
534        Ok(Some(RegressionOutcome::Exceeded {
535            baseline_total,
536            current_total,
537            tolerance: opts.tolerance,
538            type_deltas,
539        }))
540    } else {
541        Ok(Some(RegressionOutcome::Pass {
542            baseline_total,
543            current_total,
544        }))
545    }
546}
547
548/// ISO 8601 UTC timestamp without external dependencies.
549fn chrono_now() -> String {
550    let duration = std::time::SystemTime::now()
551        .duration_since(std::time::UNIX_EPOCH)
552        .unwrap_or_default();
553    let secs = duration.as_secs();
554    let days = secs / SECS_PER_DAY;
555    let time_secs = secs % SECS_PER_DAY;
556    let hours = time_secs / 3600;
557    let minutes = (time_secs % 3600) / 60;
558    let seconds = time_secs % 60;
559    let z = days + 719_468;
560    let era = z / 146_097;
561    let doe = z - era * 146_097;
562    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
563    let y = yoe + era * 400;
564    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
565    let mp = (5 * doy + 2) / 153;
566    let d = doy - (153 * mp + 2) / 5 + 1;
567    let m = if mp < 10 { mp + 3 } else { mp - 9 };
568    let y = if m <= 2 { y + 1 } else { y };
569    format!("{y:04}-{m:02}-{d:02}T{hours:02}:{minutes:02}:{seconds:02}Z")
570}
571
572#[cfg(test)]
573mod tests {
574    use super::*;
575    use fallow_core::results::*;
576    use std::path::PathBuf;
577
578    fn sample_baseline() -> fallow_config::RegressionBaseline {
579        fallow_config::RegressionBaseline {
580            total_issues: 5,
581            unused_files: 2,
582            ..Default::default()
583        }
584    }
585
586    #[test]
587    fn json_insert_into_empty_object() {
588        let result = update_json_regression("{}", &sample_baseline()).unwrap();
589        assert!(result.contains("\"regression\""));
590        assert!(result.contains("\"totalIssues\": 5"));
591        serde_json::from_str::<serde_json::Value>(&result).unwrap();
592    }
593
594    #[test]
595    fn json_insert_into_existing_config() {
596        let config = r#"{
597  "entry": ["src/main.ts"],
598  "production": true
599}"#;
600        let result = update_json_regression(config, &sample_baseline()).unwrap();
601        assert!(result.contains("\"regression\""));
602        assert!(result.contains("\"entry\""));
603        serde_json::from_str::<serde_json::Value>(&result).unwrap();
604    }
605
606    #[test]
607    fn json_replace_existing_regression() {
608        let config = r#"{
609  "entry": ["src/main.ts"],
610  "regression": {
611    "baseline": {
612      "totalIssues": 99
613    }
614  }
615}"#;
616        let result = update_json_regression(config, &sample_baseline()).unwrap();
617        assert!(!result.contains("99"));
618        assert!(result.contains("\"totalIssues\": 5"));
619        serde_json::from_str::<serde_json::Value>(&result).unwrap();
620    }
621
622    #[test]
623    fn json_skips_regression_in_comment() {
624        let config = "{\n  // See \"regression\" docs\n  \"entry\": []\n}";
625        let result = update_json_regression(config, &sample_baseline()).unwrap();
626        assert!(result.contains("\"regression\":"));
627        assert!(result.contains("\"entry\""));
628    }
629
630    #[test]
631    fn json_malformed_brace_returns_error() {
632        let config = r#"{ "regression": { "baseline": { "totalIssues": 1 }"#;
633        let result = update_json_regression(config, &sample_baseline());
634        assert!(result.is_err());
635    }
636
637    #[test]
638    fn toml_insert_into_empty() {
639        let result = update_toml_regression("", &sample_baseline());
640        assert!(result.contains("[regression.baseline]"));
641        assert!(result.contains("totalIssues = 5"));
642    }
643
644    #[test]
645    fn toml_insert_after_existing_content() {
646        let config = "[rules]\nunused-files = \"warn\"\n";
647        let result = update_toml_regression(config, &sample_baseline());
648        assert!(result.contains("[rules]"));
649        assert!(result.contains("[regression.baseline]"));
650        assert!(result.contains("totalIssues = 5"));
651    }
652
653    #[test]
654    fn toml_replace_existing_section() {
655        let config =
656            "[regression.baseline]\ntotalIssues = 99\n\n[rules]\nunused-files = \"warn\"\n";
657        let result = update_toml_regression(config, &sample_baseline());
658        assert!(!result.contains("99"));
659        assert!(result.contains("totalIssues = 5"));
660        assert!(result.contains("[rules]"));
661    }
662
663    #[test]
664    fn find_json_key_basic() {
665        assert_eq!(find_json_key(r#"{"foo": 1}"#, "foo"), Some(1));
666    }
667
668    #[test]
669    fn find_json_key_skips_comment() {
670        let content = "{\n  // \"foo\" is important\n  \"bar\": 1\n}";
671        assert_eq!(find_json_key(content, "foo"), None);
672        assert!(find_json_key(content, "bar").is_some());
673    }
674
675    #[test]
676    fn find_json_key_not_found() {
677        assert_eq!(find_json_key("{}", "missing"), None);
678    }
679
680    #[test]
681    fn find_json_key_skips_block_comment() {
682        let content = "{\n  /* \"foo\": old value */\n  \"foo\": 1\n}";
683        let pos = find_json_key(content, "foo").unwrap();
684        assert!(content[pos..].starts_with("\"foo\": 1"));
685    }
686
687    #[test]
688    fn chrono_now_format() {
689        let ts = chrono_now();
690        assert_eq!(ts.len(), 20);
691        assert!(ts.ends_with('Z'));
692        assert_eq!(&ts[4..5], "-");
693        assert_eq!(&ts[7..8], "-");
694        assert_eq!(&ts[10..11], "T");
695        assert_eq!(&ts[13..14], ":");
696        assert_eq!(&ts[16..17], ":");
697    }
698
699    #[test]
700    fn save_load_roundtrip() {
701        let dir = tempfile::tempdir().unwrap();
702        let path = dir.path().join("regression-baseline.json");
703        let counts = CheckCounts {
704            total_issues: 15,
705            unused_files: 3,
706            unused_exports: 5,
707            unused_types: 2,
708            unused_dependencies: 1,
709            unused_dev_dependencies: 1,
710            unused_optional_dependencies: 0,
711            unused_enum_members: 1,
712            unused_class_members: 0,
713            unused_store_members: 0,
714            unprovided_injects: 0,
715            unrendered_components: 0,
716            unused_component_props: 0,
717            unused_component_emits: 0,
718            unused_server_actions: 0,
719            unused_load_data_keys: 0,
720            unresolved_imports: 1,
721            unlisted_dependencies: 0,
722            duplicate_exports: 1,
723            circular_dependencies: 0,
724            re_export_cycles: 0,
725            type_only_dependencies: 0,
726            test_only_dependencies: 0,
727            boundary_violations: 0,
728            boundary_coverage_violations: 0,
729            boundary_call_violations: 0,
730            policy_violations: 0,
731        };
732        let dupes = DupesCounts {
733            clone_groups: 4,
734            duplication_percentage: 2.5,
735        };
736
737        save_regression_baseline(
738            &path,
739            dir.path(),
740            Some(&counts),
741            Some(&dupes),
742            OutputFormat::Human,
743        )
744        .unwrap();
745        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
746
747        assert_eq!(loaded.schema_version, REGRESSION_SCHEMA_VERSION);
748        let check = loaded.check.unwrap();
749        assert_eq!(check.total_issues, 15);
750        assert_eq!(check.unused_files, 3);
751        assert_eq!(check.unused_exports, 5);
752        assert_eq!(check.unused_types, 2);
753        assert_eq!(check.unused_dependencies, 1);
754        assert_eq!(check.unresolved_imports, 1);
755        assert_eq!(check.duplicate_exports, 1);
756        let dupes = loaded.dupes.unwrap();
757        assert_eq!(dupes.clone_groups, 4);
758        assert!((dupes.duplication_percentage - 2.5).abs() < f64::EPSILON);
759    }
760
761    #[test]
762    fn save_load_roundtrip_check_only() {
763        let dir = tempfile::tempdir().unwrap();
764        let path = dir.path().join("regression-baseline.json");
765        let counts = CheckCounts {
766            total_issues: 5,
767            unused_files: 5,
768            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
769        };
770
771        save_regression_baseline(&path, dir.path(), Some(&counts), None, OutputFormat::Human)
772            .unwrap();
773        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
774
775        assert!(loaded.check.is_some());
776        assert!(loaded.dupes.is_none());
777        assert_eq!(loaded.check.unwrap().unused_files, 5);
778    }
779
780    #[test]
781    fn save_creates_parent_directories() {
782        let dir = tempfile::tempdir().unwrap();
783        let path = dir.path().join("nested").join("dir").join("baseline.json");
784        let counts = CheckCounts {
785            total_issues: 1,
786            unused_files: 1,
787            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
788        };
789
790        save_regression_baseline(&path, dir.path(), Some(&counts), None, OutputFormat::Human)
791            .unwrap();
792        assert!(path.exists());
793    }
794
795    #[test]
796    fn load_nonexistent_file_returns_error() {
797        let result = load_regression_baseline(
798            Path::new("/tmp/nonexistent-baseline-12345.json"),
799            OutputFormat::Human,
800        );
801        assert!(result.is_err());
802    }
803
804    #[test]
805    fn load_invalid_json_returns_error() {
806        let dir = tempfile::tempdir().unwrap();
807        let path = dir.path().join("bad.json");
808        std::fs::write(&path, "not valid json {{{").unwrap();
809        let result = load_regression_baseline(&path, OutputFormat::Human);
810        assert!(result.is_err());
811    }
812
813    #[test]
814    fn save_baseline_to_json_config() {
815        let dir = tempfile::tempdir().unwrap();
816        let config_path = dir.path().join(".fallowrc.json");
817        std::fs::write(&config_path, r#"{"entry": ["src/main.ts"]}"#).unwrap();
818
819        let counts = CheckCounts {
820            total_issues: 7,
821            unused_files: 3,
822            unused_exports: 4,
823            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
824        };
825        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
826
827        let content = std::fs::read_to_string(&config_path).unwrap();
828        assert!(content.contains("\"regression\""));
829        assert!(content.contains("\"totalIssues\": 7"));
830        serde_json::from_str::<serde_json::Value>(&content).unwrap();
831    }
832
833    #[test]
834    fn save_baseline_to_toml_config() {
835        let dir = tempfile::tempdir().unwrap();
836        let config_path = dir.path().join("fallow.toml");
837        std::fs::write(&config_path, "[rules]\nunused-files = \"warn\"\n").unwrap();
838
839        let counts = CheckCounts {
840            total_issues: 7,
841            unused_files: 3,
842            unused_exports: 4,
843            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
844        };
845        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
846
847        let content = std::fs::read_to_string(&config_path).unwrap();
848        assert!(content.contains("[regression.baseline]"));
849        assert!(content.contains("totalIssues = 7"));
850        assert!(content.contains("[rules]"));
851    }
852
853    #[test]
854    fn save_baseline_to_nonexistent_json_config() {
855        let dir = tempfile::tempdir().unwrap();
856        let config_path = dir.path().join(".fallowrc.json");
857
858        let counts = CheckCounts {
859            total_issues: 1,
860            unused_files: 1,
861            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
862        };
863        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
864
865        let content = std::fs::read_to_string(&config_path).unwrap();
866        assert!(content.contains("\"regression\""));
867        serde_json::from_str::<serde_json::Value>(&content).unwrap();
868    }
869
870    #[test]
871    fn save_baseline_to_nonexistent_toml_config() {
872        let dir = tempfile::tempdir().unwrap();
873        let config_path = dir.path().join("fallow.toml");
874
875        let counts = CheckCounts {
876            total_issues: 0,
877            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
878        };
879        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
880
881        let content = std::fs::read_to_string(&config_path).unwrap();
882        assert!(content.contains("[regression.baseline]"));
883        assert!(content.contains("totalIssues = 0"));
884    }
885
886    #[test]
887    fn json_insert_with_trailing_comma() {
888        let config = r#"{
889  "entry": ["src/main.ts"],
890}"#;
891        let result = update_json_regression(config, &sample_baseline()).unwrap();
892        assert!(result.contains("\"regression\""));
893    }
894
895    #[test]
896    fn json_no_closing_brace_returns_error() {
897        let result = update_json_regression("", &sample_baseline());
898        assert!(result.is_err());
899    }
900
901    #[test]
902    fn json_nested_regression_object_replaced_correctly() {
903        let config = r#"{
904  "regression": {
905    "baseline": {
906      "totalIssues": 99,
907      "unusedFiles": 10
908    },
909    "tolerance": "5%"
910  },
911  "entry": ["src/main.ts"]
912}"#;
913        let result = update_json_regression(config, &sample_baseline()).unwrap();
914        assert!(!result.contains("99"));
915        assert!(result.contains("\"totalIssues\": 5"));
916        assert!(result.contains("\"entry\""));
917    }
918
919    #[test]
920    fn toml_content_without_trailing_newline() {
921        let config = "[rules]\nunused-files = \"warn\"";
922        let result = update_toml_regression(config, &sample_baseline());
923        assert!(result.contains("[regression.baseline]"));
924        assert!(result.contains("[rules]"));
925    }
926
927    #[test]
928    fn toml_replace_section_not_at_end() {
929        let config = "[regression.baseline]\ntotalIssues = 99\nunusedFiles = 10\n\n[rules]\nunused-files = \"warn\"\n";
930        let result = update_toml_regression(config, &sample_baseline());
931        assert!(!result.contains("99"));
932        assert!(result.contains("totalIssues = 5"));
933        assert!(result.contains("[rules]"));
934        assert!(result.contains("unused-files = \"warn\""));
935    }
936
937    #[test]
938    fn toml_replace_section_at_end() {
939        let config =
940            "[rules]\nunused-files = \"warn\"\n\n[regression.baseline]\ntotalIssues = 99\n";
941        let result = update_toml_regression(config, &sample_baseline());
942        assert!(!result.contains("99"));
943        assert!(result.contains("totalIssues = 5"));
944        assert!(result.contains("[rules]"));
945    }
946
947    #[test]
948    fn find_json_key_multiple_same_keys() {
949        let content = r#"{"foo": 1, "bar": {"foo": 2}}"#;
950        let pos = find_json_key(content, "foo").unwrap();
951        assert_eq!(pos, 1);
952    }
953
954    #[test]
955    fn find_json_key_in_nested_comment_then_real() {
956        let content = "{\n  // \"entry\": old\n  /* \"entry\": also old */\n  \"entry\": []\n}";
957        let pos = find_json_key(content, "entry").unwrap();
958        assert!(content[pos..].starts_with("\"entry\": []"));
959    }
960
961    fn make_opts(
962        fail: bool,
963        tolerance: Tolerance,
964        scoped: bool,
965        baseline_file: Option<&Path>,
966    ) -> RegressionOpts<'_> {
967        RegressionOpts {
968            fail_on_regression: fail,
969            tolerance,
970            regression_baseline_file: baseline_file,
971            save_target: SaveRegressionTarget::None,
972            scoped,
973            quiet: true,
974            output: OutputFormat::Human,
975        }
976    }
977
978    #[test]
979    fn compare_returns_none_when_disabled() {
980        let results = AnalysisResults::default();
981        let opts = make_opts(false, Tolerance::Absolute(0), false, None);
982        let config_baseline = fallow_config::RegressionBaseline {
983            total_issues: 5,
984            ..Default::default()
985        };
986        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
987        assert!(outcome.is_none());
988    }
989
990    #[test]
991    fn compare_returns_skipped_when_scoped() {
992        let results = AnalysisResults::default();
993        let opts = make_opts(true, Tolerance::Absolute(0), true, None);
994        let config_baseline = fallow_config::RegressionBaseline {
995            total_issues: 5,
996            ..Default::default()
997        };
998        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
999        assert!(matches!(outcome, Some(RegressionOutcome::Skipped { .. })));
1000    }
1001
1002    #[test]
1003    fn compare_pass_with_config_baseline() {
1004        let results = AnalysisResults::default(); // 0 issues
1005        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1006        let config_baseline = fallow_config::RegressionBaseline {
1007            total_issues: 0,
1008            ..Default::default()
1009        };
1010        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1011        match outcome {
1012            Some(RegressionOutcome::Pass {
1013                baseline_total,
1014                current_total,
1015            }) => {
1016                assert_eq!(baseline_total, 0);
1017                assert_eq!(current_total, 0);
1018            }
1019            other => panic!("expected Pass, got {other:?}"),
1020        }
1021    }
1022
1023    #[test]
1024    fn compare_exceeded_with_config_baseline() {
1025        let mut results = AnalysisResults::default();
1026        results
1027            .unused_files
1028            .push(UnusedFileFinding::with_actions(UnusedFile {
1029                path: PathBuf::from("a.ts"),
1030            }));
1031        results
1032            .unused_files
1033            .push(UnusedFileFinding::with_actions(UnusedFile {
1034                path: PathBuf::from("b.ts"),
1035            }));
1036        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1037        let config_baseline = fallow_config::RegressionBaseline {
1038            total_issues: 0,
1039            ..Default::default()
1040        };
1041        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1042        match outcome {
1043            Some(RegressionOutcome::Exceeded {
1044                baseline_total,
1045                current_total,
1046                ..
1047            }) => {
1048                assert_eq!(baseline_total, 0);
1049                assert_eq!(current_total, 2);
1050            }
1051            other => panic!("expected Exceeded, got {other:?}"),
1052        }
1053    }
1054
1055    #[test]
1056    fn compare_pass_within_tolerance() {
1057        let mut results = AnalysisResults::default();
1058        results
1059            .unused_files
1060            .push(UnusedFileFinding::with_actions(UnusedFile {
1061                path: PathBuf::from("a.ts"),
1062            }));
1063        let opts = make_opts(true, Tolerance::Absolute(5), false, None);
1064        let config_baseline = fallow_config::RegressionBaseline {
1065            total_issues: 0,
1066            ..Default::default()
1067        };
1068        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1069        assert!(matches!(outcome, Some(RegressionOutcome::Pass { .. })));
1070    }
1071
1072    #[test]
1073    fn compare_improvement_is_pass() {
1074        let results = AnalysisResults::default(); // 0 issues
1075        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1076        let config_baseline = fallow_config::RegressionBaseline {
1077            total_issues: 10,
1078            unused_files: 5,
1079            unused_exports: 5,
1080            ..Default::default()
1081        };
1082        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1083        match outcome {
1084            Some(RegressionOutcome::Pass {
1085                baseline_total,
1086                current_total,
1087            }) => {
1088                assert_eq!(baseline_total, 10);
1089                assert_eq!(current_total, 0);
1090            }
1091            other => panic!("expected Pass, got {other:?}"),
1092        }
1093    }
1094
1095    #[test]
1096    fn compare_with_file_baseline() {
1097        let dir = tempfile::tempdir().unwrap();
1098        let baseline_path = dir.path().join("baseline.json");
1099
1100        let counts = CheckCounts {
1101            total_issues: 5,
1102            unused_files: 5,
1103            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
1104        };
1105        save_regression_baseline(
1106            &baseline_path,
1107            dir.path(),
1108            Some(&counts),
1109            None,
1110            OutputFormat::Human,
1111        )
1112        .unwrap();
1113
1114        let results = AnalysisResults::default();
1115        let opts = make_opts(true, Tolerance::Absolute(0), false, Some(&baseline_path));
1116        let outcome = compare_check_regression(&results, &opts, None).unwrap();
1117        assert!(matches!(outcome, Some(RegressionOutcome::Pass { .. })));
1118    }
1119
1120    #[test]
1121    fn compare_file_baseline_missing_check_data_returns_error() {
1122        let dir = tempfile::tempdir().unwrap();
1123        let baseline_path = dir.path().join("baseline.json");
1124
1125        save_regression_baseline(
1126            &baseline_path,
1127            dir.path(),
1128            None,
1129            Some(&DupesCounts {
1130                clone_groups: 1,
1131                duplication_percentage: 1.0,
1132            }),
1133            OutputFormat::Human,
1134        )
1135        .unwrap();
1136
1137        let results = AnalysisResults::default();
1138        let opts = make_opts(true, Tolerance::Absolute(0), false, Some(&baseline_path));
1139        let outcome = compare_check_regression(&results, &opts, None);
1140        assert!(outcome.is_err());
1141    }
1142
1143    #[test]
1144    fn compare_no_baseline_source_returns_error() {
1145        let results = AnalysisResults::default();
1146        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1147        let outcome = compare_check_regression(&results, &opts, None);
1148        assert!(outcome.is_err());
1149    }
1150
1151    #[test]
1152    fn compare_exceeded_includes_type_deltas() {
1153        let mut results = AnalysisResults::default();
1154        results
1155            .unused_files
1156            .push(UnusedFileFinding::with_actions(UnusedFile {
1157                path: PathBuf::from("a.ts"),
1158            }));
1159        results
1160            .unused_files
1161            .push(UnusedFileFinding::with_actions(UnusedFile {
1162                path: PathBuf::from("b.ts"),
1163            }));
1164        results
1165            .unused_exports
1166            .push(UnusedExportFinding::with_actions(UnusedExport {
1167                path: PathBuf::from("c.ts"),
1168                export_name: "foo".into(),
1169                is_type_only: false,
1170                line: 1,
1171                col: 0,
1172                span_start: 0,
1173                is_re_export: false,
1174            }));
1175
1176        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1177        let config_baseline = fallow_config::RegressionBaseline {
1178            total_issues: 0,
1179            ..Default::default()
1180        };
1181        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1182
1183        match outcome {
1184            Some(RegressionOutcome::Exceeded { type_deltas, .. }) => {
1185                assert!(type_deltas.contains(&("unused_files", 2)));
1186                assert!(type_deltas.contains(&("unused_exports", 1)));
1187            }
1188            other => panic!("expected Exceeded, got {other:?}"),
1189        }
1190    }
1191
1192    #[test]
1193    fn compare_with_percentage_tolerance() {
1194        let mut results = AnalysisResults::default();
1195        results
1196            .unused_files
1197            .push(UnusedFileFinding::with_actions(UnusedFile {
1198                path: PathBuf::from("a.ts"),
1199            }));
1200
1201        let opts = make_opts(true, Tolerance::Percentage(50.0), false, None);
1202        let config_baseline = fallow_config::RegressionBaseline {
1203            total_issues: 10,
1204            unused_files: 10,
1205            ..Default::default()
1206        };
1207        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1208        assert!(matches!(outcome, Some(RegressionOutcome::Pass { .. })));
1209    }
1210
1211    fn write_baseline_with_schema_version(dir: &Path, version: u32) -> PathBuf {
1212        let path = dir.join("baseline.json");
1213        let body = format!(
1214            r#"{{
1215  "schema_version": {version},
1216  "fallow_version": "3.0.0",
1217  "timestamp": "2026-05-21T00:00:00Z",
1218  "check": {{
1219    "total_issues": 0,
1220    "unused_files": 0
1221  }}
1222}}"#
1223        );
1224        std::fs::write(&path, body).unwrap();
1225        path
1226    }
1227
1228    #[test]
1229    fn load_rejects_schema_version_too_high() {
1230        let dir = tempfile::tempdir().unwrap();
1231        let path = write_baseline_with_schema_version(dir.path(), REGRESSION_SCHEMA_VERSION + 1);
1232        let result = load_regression_baseline(&path, OutputFormat::Human);
1233        assert!(result.is_err());
1234    }
1235
1236    #[test]
1237    fn load_rejects_schema_version_zero_predates_versioning() {
1238        let dir = tempfile::tempdir().unwrap();
1239        let path = write_baseline_with_schema_version(dir.path(), 0);
1240        let result = load_regression_baseline(&path, OutputFormat::Human);
1241        assert!(result.is_err());
1242    }
1243
1244    #[test]
1245    fn load_accepts_current_schema_version() {
1246        let dir = tempfile::tempdir().unwrap();
1247        let path = write_baseline_with_schema_version(dir.path(), REGRESSION_SCHEMA_VERSION);
1248        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
1249        assert_eq!(loaded.schema_version, REGRESSION_SCHEMA_VERSION);
1250    }
1251
1252    #[test]
1253    fn load_rewrites_missing_schema_version_field_error() {
1254        let dir = tempfile::tempdir().unwrap();
1255        let path = dir.path().join("baseline.json");
1256        std::fs::write(
1257            &path,
1258            r#"{
1259  "fallow_version": "1.0.0",
1260  "timestamp": "2026-05-21T00:00:00Z",
1261  "check": {}
1262}"#,
1263        )
1264        .unwrap();
1265        let result = load_regression_baseline(&path, OutputFormat::Human);
1266        assert!(result.is_err());
1267    }
1268
1269    #[test]
1270    fn format_schema_mismatch_error_too_high() {
1271        let msg =
1272            format_schema_mismatch_error(Path::new("/repo/.fallow-baseline.json"), 1, 99, "3.0.0");
1273        assert!(msg.contains("schema_version 99"));
1274        assert!(msg.contains("expects 1"));
1275        assert!(msg.contains("fallow 3.0.0"));
1276        assert!(
1277            msg.contains("fallow dead-code --save-regression-baseline /repo/.fallow-baseline.json")
1278        );
1279        assert!(!msg.to_lowercase().contains("refresh"));
1280        assert!(msg.contains("schema_version"));
1281    }
1282
1283    #[test]
1284    fn format_schema_mismatch_error_actual_zero_special_case() {
1285        let msg =
1286            format_schema_mismatch_error(Path::new("/repo/.fallow-baseline.json"), 1, 0, "2.0.0");
1287        assert!(msg.contains("predate"));
1288        assert!(msg.contains("fallow 2.0.0"));
1289        assert!(
1290            msg.contains("fallow dead-code --save-regression-baseline /repo/.fallow-baseline.json")
1291        );
1292    }
1293
1294    #[test]
1295    fn format_missing_schema_version_error_includes_regenerate_command() {
1296        let msg = format_missing_schema_version_error(Path::new("/repo/baseline.json"));
1297        assert!(msg.contains("missing the schema_version field"));
1298        assert!(msg.contains("fallow dead-code --save-regression-baseline /repo/baseline.json"));
1299    }
1300
1301    #[test]
1302    fn save_load_preserves_schema_version() {
1303        let dir = tempfile::tempdir().unwrap();
1304        let path = dir.path().join("baseline.json");
1305        let counts = CheckCounts {
1306            total_issues: 1,
1307            unused_files: 1,
1308            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
1309        };
1310        save_regression_baseline(&path, dir.path(), Some(&counts), None, OutputFormat::Human)
1311            .unwrap();
1312        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
1313        assert_eq!(loaded.schema_version, REGRESSION_SCHEMA_VERSION);
1314    }
1315}