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 check --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 check --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 check --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            unresolved_imports: 1,
714            unlisted_dependencies: 0,
715            duplicate_exports: 1,
716            circular_dependencies: 0,
717            re_export_cycles: 0,
718            type_only_dependencies: 0,
719            test_only_dependencies: 0,
720            boundary_violations: 0,
721        };
722        let dupes = DupesCounts {
723            clone_groups: 4,
724            duplication_percentage: 2.5,
725        };
726
727        save_regression_baseline(
728            &path,
729            dir.path(),
730            Some(&counts),
731            Some(&dupes),
732            OutputFormat::Human,
733        )
734        .unwrap();
735        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
736
737        assert_eq!(loaded.schema_version, REGRESSION_SCHEMA_VERSION);
738        let check = loaded.check.unwrap();
739        assert_eq!(check.total_issues, 15);
740        assert_eq!(check.unused_files, 3);
741        assert_eq!(check.unused_exports, 5);
742        assert_eq!(check.unused_types, 2);
743        assert_eq!(check.unused_dependencies, 1);
744        assert_eq!(check.unresolved_imports, 1);
745        assert_eq!(check.duplicate_exports, 1);
746        let dupes = loaded.dupes.unwrap();
747        assert_eq!(dupes.clone_groups, 4);
748        assert!((dupes.duplication_percentage - 2.5).abs() < f64::EPSILON);
749    }
750
751    #[test]
752    fn save_load_roundtrip_check_only() {
753        let dir = tempfile::tempdir().unwrap();
754        let path = dir.path().join("regression-baseline.json");
755        let counts = CheckCounts {
756            total_issues: 5,
757            unused_files: 5,
758            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
759        };
760
761        save_regression_baseline(&path, dir.path(), Some(&counts), None, OutputFormat::Human)
762            .unwrap();
763        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
764
765        assert!(loaded.check.is_some());
766        assert!(loaded.dupes.is_none());
767        assert_eq!(loaded.check.unwrap().unused_files, 5);
768    }
769
770    #[test]
771    fn save_creates_parent_directories() {
772        let dir = tempfile::tempdir().unwrap();
773        let path = dir.path().join("nested").join("dir").join("baseline.json");
774        let counts = CheckCounts {
775            total_issues: 1,
776            unused_files: 1,
777            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
778        };
779
780        save_regression_baseline(&path, dir.path(), Some(&counts), None, OutputFormat::Human)
781            .unwrap();
782        assert!(path.exists());
783    }
784
785    #[test]
786    fn load_nonexistent_file_returns_error() {
787        let result = load_regression_baseline(
788            Path::new("/tmp/nonexistent-baseline-12345.json"),
789            OutputFormat::Human,
790        );
791        assert!(result.is_err());
792    }
793
794    #[test]
795    fn load_invalid_json_returns_error() {
796        let dir = tempfile::tempdir().unwrap();
797        let path = dir.path().join("bad.json");
798        std::fs::write(&path, "not valid json {{{").unwrap();
799        let result = load_regression_baseline(&path, OutputFormat::Human);
800        assert!(result.is_err());
801    }
802
803    #[test]
804    fn save_baseline_to_json_config() {
805        let dir = tempfile::tempdir().unwrap();
806        let config_path = dir.path().join(".fallowrc.json");
807        std::fs::write(&config_path, r#"{"entry": ["src/main.ts"]}"#).unwrap();
808
809        let counts = CheckCounts {
810            total_issues: 7,
811            unused_files: 3,
812            unused_exports: 4,
813            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
814        };
815        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
816
817        let content = std::fs::read_to_string(&config_path).unwrap();
818        assert!(content.contains("\"regression\""));
819        assert!(content.contains("\"totalIssues\": 7"));
820        serde_json::from_str::<serde_json::Value>(&content).unwrap();
821    }
822
823    #[test]
824    fn save_baseline_to_toml_config() {
825        let dir = tempfile::tempdir().unwrap();
826        let config_path = dir.path().join("fallow.toml");
827        std::fs::write(&config_path, "[rules]\nunused-files = \"warn\"\n").unwrap();
828
829        let counts = CheckCounts {
830            total_issues: 7,
831            unused_files: 3,
832            unused_exports: 4,
833            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
834        };
835        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
836
837        let content = std::fs::read_to_string(&config_path).unwrap();
838        assert!(content.contains("[regression.baseline]"));
839        assert!(content.contains("totalIssues = 7"));
840        assert!(content.contains("[rules]"));
841    }
842
843    #[test]
844    fn save_baseline_to_nonexistent_json_config() {
845        let dir = tempfile::tempdir().unwrap();
846        let config_path = dir.path().join(".fallowrc.json");
847
848        let counts = CheckCounts {
849            total_issues: 1,
850            unused_files: 1,
851            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
852        };
853        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
854
855        let content = std::fs::read_to_string(&config_path).unwrap();
856        assert!(content.contains("\"regression\""));
857        serde_json::from_str::<serde_json::Value>(&content).unwrap();
858    }
859
860    #[test]
861    fn save_baseline_to_nonexistent_toml_config() {
862        let dir = tempfile::tempdir().unwrap();
863        let config_path = dir.path().join("fallow.toml");
864
865        let counts = CheckCounts {
866            total_issues: 0,
867            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
868        };
869        save_baseline_to_config(&config_path, &counts, OutputFormat::Human).unwrap();
870
871        let content = std::fs::read_to_string(&config_path).unwrap();
872        assert!(content.contains("[regression.baseline]"));
873        assert!(content.contains("totalIssues = 0"));
874    }
875
876    #[test]
877    fn json_insert_with_trailing_comma() {
878        let config = r#"{
879  "entry": ["src/main.ts"],
880}"#;
881        let result = update_json_regression(config, &sample_baseline()).unwrap();
882        assert!(result.contains("\"regression\""));
883    }
884
885    #[test]
886    fn json_no_closing_brace_returns_error() {
887        let result = update_json_regression("", &sample_baseline());
888        assert!(result.is_err());
889    }
890
891    #[test]
892    fn json_nested_regression_object_replaced_correctly() {
893        let config = r#"{
894  "regression": {
895    "baseline": {
896      "totalIssues": 99,
897      "unusedFiles": 10
898    },
899    "tolerance": "5%"
900  },
901  "entry": ["src/main.ts"]
902}"#;
903        let result = update_json_regression(config, &sample_baseline()).unwrap();
904        assert!(!result.contains("99"));
905        assert!(result.contains("\"totalIssues\": 5"));
906        assert!(result.contains("\"entry\""));
907    }
908
909    #[test]
910    fn toml_content_without_trailing_newline() {
911        let config = "[rules]\nunused-files = \"warn\"";
912        let result = update_toml_regression(config, &sample_baseline());
913        assert!(result.contains("[regression.baseline]"));
914        assert!(result.contains("[rules]"));
915    }
916
917    #[test]
918    fn toml_replace_section_not_at_end() {
919        let config = "[regression.baseline]\ntotalIssues = 99\nunusedFiles = 10\n\n[rules]\nunused-files = \"warn\"\n";
920        let result = update_toml_regression(config, &sample_baseline());
921        assert!(!result.contains("99"));
922        assert!(result.contains("totalIssues = 5"));
923        assert!(result.contains("[rules]"));
924        assert!(result.contains("unused-files = \"warn\""));
925    }
926
927    #[test]
928    fn toml_replace_section_at_end() {
929        let config =
930            "[rules]\nunused-files = \"warn\"\n\n[regression.baseline]\ntotalIssues = 99\n";
931        let result = update_toml_regression(config, &sample_baseline());
932        assert!(!result.contains("99"));
933        assert!(result.contains("totalIssues = 5"));
934        assert!(result.contains("[rules]"));
935    }
936
937    #[test]
938    fn find_json_key_multiple_same_keys() {
939        let content = r#"{"foo": 1, "bar": {"foo": 2}}"#;
940        let pos = find_json_key(content, "foo").unwrap();
941        assert_eq!(pos, 1);
942    }
943
944    #[test]
945    fn find_json_key_in_nested_comment_then_real() {
946        let content = "{\n  // \"entry\": old\n  /* \"entry\": also old */\n  \"entry\": []\n}";
947        let pos = find_json_key(content, "entry").unwrap();
948        assert!(content[pos..].starts_with("\"entry\": []"));
949    }
950
951    fn make_opts(
952        fail: bool,
953        tolerance: Tolerance,
954        scoped: bool,
955        baseline_file: Option<&Path>,
956    ) -> RegressionOpts<'_> {
957        RegressionOpts {
958            fail_on_regression: fail,
959            tolerance,
960            regression_baseline_file: baseline_file,
961            save_target: SaveRegressionTarget::None,
962            scoped,
963            quiet: true,
964            output: OutputFormat::Human,
965        }
966    }
967
968    #[test]
969    fn compare_returns_none_when_disabled() {
970        let results = AnalysisResults::default();
971        let opts = make_opts(false, Tolerance::Absolute(0), false, None);
972        let config_baseline = fallow_config::RegressionBaseline {
973            total_issues: 5,
974            ..Default::default()
975        };
976        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
977        assert!(outcome.is_none());
978    }
979
980    #[test]
981    fn compare_returns_skipped_when_scoped() {
982        let results = AnalysisResults::default();
983        let opts = make_opts(true, Tolerance::Absolute(0), true, None);
984        let config_baseline = fallow_config::RegressionBaseline {
985            total_issues: 5,
986            ..Default::default()
987        };
988        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
989        assert!(matches!(outcome, Some(RegressionOutcome::Skipped { .. })));
990    }
991
992    #[test]
993    fn compare_pass_with_config_baseline() {
994        let results = AnalysisResults::default(); // 0 issues
995        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
996        let config_baseline = fallow_config::RegressionBaseline {
997            total_issues: 0,
998            ..Default::default()
999        };
1000        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1001        match outcome {
1002            Some(RegressionOutcome::Pass {
1003                baseline_total,
1004                current_total,
1005            }) => {
1006                assert_eq!(baseline_total, 0);
1007                assert_eq!(current_total, 0);
1008            }
1009            other => panic!("expected Pass, got {other:?}"),
1010        }
1011    }
1012
1013    #[test]
1014    fn compare_exceeded_with_config_baseline() {
1015        let mut results = AnalysisResults::default();
1016        results
1017            .unused_files
1018            .push(UnusedFileFinding::with_actions(UnusedFile {
1019                path: PathBuf::from("a.ts"),
1020            }));
1021        results
1022            .unused_files
1023            .push(UnusedFileFinding::with_actions(UnusedFile {
1024                path: PathBuf::from("b.ts"),
1025            }));
1026        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1027        let config_baseline = fallow_config::RegressionBaseline {
1028            total_issues: 0,
1029            ..Default::default()
1030        };
1031        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1032        match outcome {
1033            Some(RegressionOutcome::Exceeded {
1034                baseline_total,
1035                current_total,
1036                ..
1037            }) => {
1038                assert_eq!(baseline_total, 0);
1039                assert_eq!(current_total, 2);
1040            }
1041            other => panic!("expected Exceeded, got {other:?}"),
1042        }
1043    }
1044
1045    #[test]
1046    fn compare_pass_within_tolerance() {
1047        let mut results = AnalysisResults::default();
1048        results
1049            .unused_files
1050            .push(UnusedFileFinding::with_actions(UnusedFile {
1051                path: PathBuf::from("a.ts"),
1052            }));
1053        let opts = make_opts(true, Tolerance::Absolute(5), false, None);
1054        let config_baseline = fallow_config::RegressionBaseline {
1055            total_issues: 0,
1056            ..Default::default()
1057        };
1058        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1059        assert!(matches!(outcome, Some(RegressionOutcome::Pass { .. })));
1060    }
1061
1062    #[test]
1063    fn compare_improvement_is_pass() {
1064        let results = AnalysisResults::default(); // 0 issues
1065        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1066        let config_baseline = fallow_config::RegressionBaseline {
1067            total_issues: 10,
1068            unused_files: 5,
1069            unused_exports: 5,
1070            ..Default::default()
1071        };
1072        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1073        match outcome {
1074            Some(RegressionOutcome::Pass {
1075                baseline_total,
1076                current_total,
1077            }) => {
1078                assert_eq!(baseline_total, 10);
1079                assert_eq!(current_total, 0);
1080            }
1081            other => panic!("expected Pass, got {other:?}"),
1082        }
1083    }
1084
1085    #[test]
1086    fn compare_with_file_baseline() {
1087        let dir = tempfile::tempdir().unwrap();
1088        let baseline_path = dir.path().join("baseline.json");
1089
1090        let counts = CheckCounts {
1091            total_issues: 5,
1092            unused_files: 5,
1093            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
1094        };
1095        save_regression_baseline(
1096            &baseline_path,
1097            dir.path(),
1098            Some(&counts),
1099            None,
1100            OutputFormat::Human,
1101        )
1102        .unwrap();
1103
1104        let results = AnalysisResults::default();
1105        let opts = make_opts(true, Tolerance::Absolute(0), false, Some(&baseline_path));
1106        let outcome = compare_check_regression(&results, &opts, None).unwrap();
1107        assert!(matches!(outcome, Some(RegressionOutcome::Pass { .. })));
1108    }
1109
1110    #[test]
1111    fn compare_file_baseline_missing_check_data_returns_error() {
1112        let dir = tempfile::tempdir().unwrap();
1113        let baseline_path = dir.path().join("baseline.json");
1114
1115        save_regression_baseline(
1116            &baseline_path,
1117            dir.path(),
1118            None,
1119            Some(&DupesCounts {
1120                clone_groups: 1,
1121                duplication_percentage: 1.0,
1122            }),
1123            OutputFormat::Human,
1124        )
1125        .unwrap();
1126
1127        let results = AnalysisResults::default();
1128        let opts = make_opts(true, Tolerance::Absolute(0), false, Some(&baseline_path));
1129        let outcome = compare_check_regression(&results, &opts, None);
1130        assert!(outcome.is_err());
1131    }
1132
1133    #[test]
1134    fn compare_no_baseline_source_returns_error() {
1135        let results = AnalysisResults::default();
1136        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1137        let outcome = compare_check_regression(&results, &opts, None);
1138        assert!(outcome.is_err());
1139    }
1140
1141    #[test]
1142    fn compare_exceeded_includes_type_deltas() {
1143        let mut results = AnalysisResults::default();
1144        results
1145            .unused_files
1146            .push(UnusedFileFinding::with_actions(UnusedFile {
1147                path: PathBuf::from("a.ts"),
1148            }));
1149        results
1150            .unused_files
1151            .push(UnusedFileFinding::with_actions(UnusedFile {
1152                path: PathBuf::from("b.ts"),
1153            }));
1154        results
1155            .unused_exports
1156            .push(UnusedExportFinding::with_actions(UnusedExport {
1157                path: PathBuf::from("c.ts"),
1158                export_name: "foo".into(),
1159                is_type_only: false,
1160                line: 1,
1161                col: 0,
1162                span_start: 0,
1163                is_re_export: false,
1164            }));
1165
1166        let opts = make_opts(true, Tolerance::Absolute(0), false, None);
1167        let config_baseline = fallow_config::RegressionBaseline {
1168            total_issues: 0,
1169            ..Default::default()
1170        };
1171        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1172
1173        match outcome {
1174            Some(RegressionOutcome::Exceeded { type_deltas, .. }) => {
1175                assert!(type_deltas.contains(&("unused_files", 2)));
1176                assert!(type_deltas.contains(&("unused_exports", 1)));
1177            }
1178            other => panic!("expected Exceeded, got {other:?}"),
1179        }
1180    }
1181
1182    #[test]
1183    fn compare_with_percentage_tolerance() {
1184        let mut results = AnalysisResults::default();
1185        results
1186            .unused_files
1187            .push(UnusedFileFinding::with_actions(UnusedFile {
1188                path: PathBuf::from("a.ts"),
1189            }));
1190
1191        let opts = make_opts(true, Tolerance::Percentage(50.0), false, None);
1192        let config_baseline = fallow_config::RegressionBaseline {
1193            total_issues: 10,
1194            unused_files: 10,
1195            ..Default::default()
1196        };
1197        let outcome = compare_check_regression(&results, &opts, Some(&config_baseline)).unwrap();
1198        assert!(matches!(outcome, Some(RegressionOutcome::Pass { .. })));
1199    }
1200
1201    fn write_baseline_with_schema_version(dir: &Path, version: u32) -> PathBuf {
1202        let path = dir.join("baseline.json");
1203        let body = format!(
1204            r#"{{
1205  "schema_version": {version},
1206  "fallow_version": "3.0.0",
1207  "timestamp": "2026-05-21T00:00:00Z",
1208  "check": {{
1209    "total_issues": 0,
1210    "unused_files": 0
1211  }}
1212}}"#
1213        );
1214        std::fs::write(&path, body).unwrap();
1215        path
1216    }
1217
1218    #[test]
1219    fn load_rejects_schema_version_too_high() {
1220        let dir = tempfile::tempdir().unwrap();
1221        let path = write_baseline_with_schema_version(dir.path(), REGRESSION_SCHEMA_VERSION + 1);
1222        let result = load_regression_baseline(&path, OutputFormat::Human);
1223        assert!(result.is_err());
1224    }
1225
1226    #[test]
1227    fn load_rejects_schema_version_zero_predates_versioning() {
1228        let dir = tempfile::tempdir().unwrap();
1229        let path = write_baseline_with_schema_version(dir.path(), 0);
1230        let result = load_regression_baseline(&path, OutputFormat::Human);
1231        assert!(result.is_err());
1232    }
1233
1234    #[test]
1235    fn load_accepts_current_schema_version() {
1236        let dir = tempfile::tempdir().unwrap();
1237        let path = write_baseline_with_schema_version(dir.path(), REGRESSION_SCHEMA_VERSION);
1238        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
1239        assert_eq!(loaded.schema_version, REGRESSION_SCHEMA_VERSION);
1240    }
1241
1242    #[test]
1243    fn load_rewrites_missing_schema_version_field_error() {
1244        let dir = tempfile::tempdir().unwrap();
1245        let path = dir.path().join("baseline.json");
1246        std::fs::write(
1247            &path,
1248            r#"{
1249  "fallow_version": "1.0.0",
1250  "timestamp": "2026-05-21T00:00:00Z",
1251  "check": {}
1252}"#,
1253        )
1254        .unwrap();
1255        let result = load_regression_baseline(&path, OutputFormat::Human);
1256        assert!(result.is_err());
1257    }
1258
1259    #[test]
1260    fn format_schema_mismatch_error_too_high() {
1261        let msg =
1262            format_schema_mismatch_error(Path::new("/repo/.fallow-baseline.json"), 1, 99, "3.0.0");
1263        assert!(msg.contains("schema_version 99"));
1264        assert!(msg.contains("expects 1"));
1265        assert!(msg.contains("fallow 3.0.0"));
1266        assert!(
1267            msg.contains("fallow check --save-regression-baseline /repo/.fallow-baseline.json")
1268        );
1269        assert!(!msg.to_lowercase().contains("refresh"));
1270        assert!(msg.contains("schema_version"));
1271    }
1272
1273    #[test]
1274    fn format_schema_mismatch_error_actual_zero_special_case() {
1275        let msg =
1276            format_schema_mismatch_error(Path::new("/repo/.fallow-baseline.json"), 1, 0, "2.0.0");
1277        assert!(msg.contains("predate"));
1278        assert!(msg.contains("fallow 2.0.0"));
1279        assert!(
1280            msg.contains("fallow check --save-regression-baseline /repo/.fallow-baseline.json")
1281        );
1282    }
1283
1284    #[test]
1285    fn format_missing_schema_version_error_includes_regenerate_command() {
1286        let msg = format_missing_schema_version_error(Path::new("/repo/baseline.json"));
1287        assert!(msg.contains("missing the schema_version field"));
1288        assert!(msg.contains("fallow check --save-regression-baseline /repo/baseline.json"));
1289    }
1290
1291    #[test]
1292    fn save_load_preserves_schema_version() {
1293        let dir = tempfile::tempdir().unwrap();
1294        let path = dir.path().join("baseline.json");
1295        let counts = CheckCounts {
1296            total_issues: 1,
1297            unused_files: 1,
1298            ..CheckCounts::from_config_baseline(&fallow_config::RegressionBaseline::default())
1299        };
1300        save_regression_baseline(&path, dir.path(), Some(&counts), None, OutputFormat::Human)
1301            .unwrap();
1302        let loaded = load_regression_baseline(&path, OutputFormat::Human).unwrap();
1303        assert_eq!(loaded.schema_version, REGRESSION_SCHEMA_VERSION);
1304    }
1305}