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