homeboy 0.72.0

CLI for multi-component deployment and development workflow automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! Generic baseline & ratchet primitive for drift detection.
//!
//! Captures a snapshot of "current state" (any set of fingerprintable items)
//! and compares future runs against it. Only NEW items (not in the baseline)
//! trigger a failure — resolved items are celebrated, same-state passes.
//!
//! Zero domain knowledge. The caller decides:
//! - What gets fingerprinted (via [`Fingerprintable`])
//! - What metadata to store (via the `M` type parameter)
//! - What key to use in `homeboy.json` (via [`BaselineConfig`])
//!
//! Baselines are stored in the project's `homeboy.json` under a `baselines`
//! key, keeping all component configuration in a single portable file.
//!
//! # Usage
//!
//! ```ignore
//! use homeboy::utils::baseline::{self, Fingerprintable, BaselineConfig};
//!
//! impl Fingerprintable for MyFinding {
//!     fn fingerprint(&self) -> String {
//!         format!("{}::{}", self.category, self.file)
//!     }
//!     fn description(&self) -> String {
//!         self.message.clone()
//!     }
//!     fn context_label(&self) -> String {
//!         self.category.clone()
//!     }
//! }
//!
//! let config = BaselineConfig::new(source_path, "audit");
//!
//! // First run: save baseline
//! baseline::save(&config, "my-component", &items, my_metadata)?;
//!
//! // Subsequent runs: compare
//! if let Some(saved) = baseline::load::<MyMeta>(&config)? {
//!     let comparison = baseline::compare(&items, &saved);
//!     if comparison.drift_increased {
//!         // CI fails — new findings introduced
//!     }
//! }
//! ```

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::{Error, Result};

// ============================================================================
// Trait: Fingerprintable
// ============================================================================

/// Any item that can produce a stable identity for baseline comparison.
///
/// The fingerprint must be stable across runs for the same logical item.
/// Volatile values (line counts, timestamps) should be excluded.
pub trait Fingerprintable {
    /// Stable identity string for this item.
    ///
    /// Two items with the same fingerprint are considered "the same finding."
    /// Exclude volatile values — if line counts change but the finding is
    /// conceptually the same, the fingerprint must not change.
    fn fingerprint(&self) -> String;

    /// Human-readable description for reporting.
    fn description(&self) -> String;

    /// Context label (e.g., convention name, lint rule, category).
    /// Used in [`NewItem`] for human-readable reports.
    fn context_label(&self) -> String;
}

// ============================================================================
// Config
// ============================================================================

/// Configuration for a baseline: where to store it and under what key.
///
/// Baselines are stored in `homeboy.json` under `baselines.<key>`.
pub struct BaselineConfig {
    /// Root directory containing `homeboy.json`.
    root: PathBuf,
    /// Key within the `baselines` object (e.g., "audit", "lint", "test").
    key: String,
}

const HOMEBOY_JSON: &str = "homeboy.json";
const BASELINES_KEY: &str = "baselines";

impl BaselineConfig {
    /// Create a baseline config.
    ///
    /// - `root`: directory containing `homeboy.json` (component source path)
    /// - `key`: baseline key within `baselines` object (e.g., "audit", "lint")
    pub fn new(root: impl Into<PathBuf>, key: impl Into<String>) -> Self {
        Self {
            root: root.into(),
            key: key.into(),
        }
    }

    /// Path to the `homeboy.json` file.
    pub fn json_path(&self) -> PathBuf {
        self.root.join(HOMEBOY_JSON)
    }

    /// The baseline key.
    pub fn key(&self) -> &str {
        &self.key
    }
}

// ============================================================================
// Stored baseline
// ============================================================================

/// A saved baseline snapshot.
///
/// `M` is caller-defined metadata (e.g., alignment score, coverage percentage).
/// Use `()` if no metadata is needed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Baseline<M: Serialize> {
    /// ISO 8601 timestamp when the baseline was created.
    pub created_at: String,
    /// Identifier for what was baselined (component ID, lint target, etc.).
    pub context_id: String,
    /// Total item count at baseline time.
    pub item_count: usize,
    /// Fingerprints of all known items (the snapshot).
    pub known_fingerprints: Vec<String>,
    /// Domain-specific metadata (alignment score, coverage %, etc.).
    pub metadata: M,
}

// ============================================================================
// Comparison result
// ============================================================================

/// Result of comparing current items against a saved baseline.
#[derive(Debug, Clone, Serialize)]
pub struct Comparison {
    /// Items that are new since the baseline.
    pub new_items: Vec<NewItem>,
    /// Fingerprints that were in the baseline but are now gone (resolved).
    pub resolved_fingerprints: Vec<String>,
    /// Net change in item count (positive = more items, negative = fewer).
    pub delta: i64,
    /// Whether drift increased (true = new items appeared = ratchet failure).
    pub drift_increased: bool,
}

/// An item that wasn't in the baseline.
#[derive(Debug, Clone, Serialize)]
pub struct NewItem {
    /// The item's fingerprint.
    pub fingerprint: String,
    /// Human-readable description.
    pub description: String,
    /// Context label (convention, rule, category).
    pub context_label: String,
}

// ============================================================================
// Core operations
// ============================================================================

/// Save a baseline snapshot into `homeboy.json`.
///
/// Reads the existing `homeboy.json` (or creates one), sets
/// `baselines.<key>` to the new baseline, and writes it back.
pub fn save<M: Serialize>(
    config: &BaselineConfig,
    context_id: &str,
    items: &[impl Fingerprintable],
    metadata: M,
) -> Result<PathBuf> {
    let known_fingerprints: Vec<String> = items.iter().map(|i| i.fingerprint()).collect();

    let baseline = Baseline {
        created_at: utc_now_iso8601(),
        context_id: context_id.to_string(),
        item_count: items.len(),
        known_fingerprints,
        metadata,
    };

    let baseline_value = serde_json::to_value(&baseline).map_err(|e| {
        Error::internal_io(
            format!("Failed to serialize baseline: {}", e),
            Some("baseline.save".to_string()),
        )
    })?;

    // Read existing homeboy.json or start fresh
    let json_path = config.json_path();
    let mut root = read_json_or_empty(&json_path)?;

    // Ensure baselines object exists
    let baselines = root
        .as_object_mut()
        .ok_or_else(|| {
            Error::internal_io(
                "homeboy.json root is not an object".to_string(),
                Some("baseline.save".to_string()),
            )
        })?
        .entry(BASELINES_KEY)
        .or_insert_with(|| Value::Object(serde_json::Map::new()));

    // Set the specific baseline key
    baselines
        .as_object_mut()
        .ok_or_else(|| {
            Error::internal_io(
                "baselines key is not an object".to_string(),
                Some("baseline.save".to_string()),
            )
        })?
        .insert(config.key.clone(), baseline_value);

    // Write back
    write_json(&json_path, &root)?;

    Ok(json_path)
}

/// Load a baseline if one exists in `homeboy.json`.
///
/// Returns `Ok(None)` if:
/// - No `homeboy.json` exists
/// - No `baselines` key exists
/// - No baseline for the configured key exists
///
/// Returns `Err` if the file exists but the baseline data is malformed.
pub fn load<M: for<'de> Deserialize<'de> + Serialize>(
    config: &BaselineConfig,
) -> Result<Option<Baseline<M>>> {
    let json_path = config.json_path();
    if !json_path.exists() {
        return Ok(None);
    }

    let root = read_json_or_empty(&json_path)?;

    let baseline_value = root.get(BASELINES_KEY).and_then(|b| b.get(&config.key));

    let Some(value) = baseline_value else {
        return Ok(None);
    };

    let baseline: Baseline<M> = serde_json::from_value(value.clone()).map_err(|e| {
        Error::internal_io(
            format!(
                "Malformed baseline '{}' in {}: {}",
                config.key,
                json_path.display(),
                e
            ),
            Some("baseline.load".to_string()),
        )
    })?;

    Ok(Some(baseline))
}

/// Compare current items against a saved baseline.
///
/// This is the ratchet check:
/// - `drift_increased = true` if ANY new items appeared (not in baseline)
/// - Resolved items (in baseline but not in current) are tracked but don't fail
/// - `delta` is the net change in item count
pub fn compare<M: Serialize>(
    current_items: &[impl Fingerprintable],
    baseline: &Baseline<M>,
) -> Comparison {
    let current_fingerprints: HashSet<String> =
        current_items.iter().map(|i| i.fingerprint()).collect();
    let baseline_fingerprints: HashSet<String> =
        baseline.known_fingerprints.iter().cloned().collect();

    // New = in current but not in baseline
    let new_items: Vec<NewItem> = current_items
        .iter()
        .filter(|item| !baseline_fingerprints.contains(&item.fingerprint()))
        .map(|item| NewItem {
            fingerprint: item.fingerprint(),
            description: item.description(),
            context_label: item.context_label(),
        })
        .collect();

    // Resolved = in baseline but not in current
    let resolved_fingerprints: Vec<String> = baseline_fingerprints
        .difference(&current_fingerprints)
        .cloned()
        .collect();

    let delta = current_items.len() as i64 - baseline.item_count as i64;
    let drift_increased = !new_items.is_empty();

    Comparison {
        new_items,
        resolved_fingerprints,
        delta,
        drift_increased,
    }
}

/// Load a baseline from a git ref's `homeboy.json` (e.g., `origin/main`).
///
/// Uses `git show <ref>:homeboy.json` to read the baseline as it existed
/// at a specific commit without checking out that ref. Returns `None` if:
/// - `homeboy.json` doesn't exist at that ref
/// - No `baselines.<key>` entry exists
/// - The ref or file is invalid
///
/// This is the core primitive for differential CI: compare current findings
/// against the base ref's state to detect only newly introduced issues.
pub fn load_from_git_ref<M: for<'de> Deserialize<'de> + Serialize>(
    source_path: &str,
    git_ref: &str,
    key: &str,
) -> Option<Baseline<M>> {
    // Read homeboy.json from the git ref
    let git_spec = format!("{}:{}", git_ref, HOMEBOY_JSON);
    let content = crate::utils::command::run_in_optional(source_path, "git", &["show", &git_spec])?;

    // Parse JSON
    let root: Value = serde_json::from_str(&content).ok()?;

    // Extract baseline
    let value = root.get(BASELINES_KEY)?.get(key)?;
    serde_json::from_value::<Baseline<M>>(value.clone()).ok()
}

// ============================================================================
// JSON helpers
// ============================================================================

/// Read homeboy.json as a serde_json::Value, or return an empty object.
fn read_json_or_empty(path: &Path) -> Result<Value> {
    if !path.exists() {
        return Ok(Value::Object(serde_json::Map::new()));
    }

    let content = std::fs::read_to_string(path).map_err(|e| {
        Error::internal_io(
            format!("Failed to read {}: {}", path.display(), e),
            Some("baseline.read_json".to_string()),
        )
    })?;

    serde_json::from_str(&content).map_err(|e| {
        Error::internal_io(
            format!("Failed to parse {}: {}", path.display(), e),
            Some("baseline.read_json".to_string()),
        )
    })
}

/// Write a serde_json::Value to homeboy.json with pretty formatting.
fn write_json(path: &Path, value: &Value) -> Result<()> {
    let json = serde_json::to_string_pretty(value).map_err(|e| {
        Error::internal_io(
            format!("Failed to serialize JSON: {}", e),
            Some("baseline.write_json".to_string()),
        )
    })?;

    // Ensure trailing newline (git-friendly)
    let content = if json.ends_with('\n') {
        json
    } else {
        format!("{}\n", json)
    };

    std::fs::write(path, content).map_err(|e| {
        Error::internal_io(
            format!("Failed to write {}: {}", path.display(), e),
            Some("baseline.write_json".to_string()),
        )
    })?;

    Ok(())
}

// ============================================================================
// Timestamp helpers
// ============================================================================

/// Get current UTC timestamp as ISO 8601 (no external dependencies).
pub fn utc_now_iso8601() -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let secs_per_day = 86400u64;
    let secs_per_hour = 3600u64;
    let secs_per_min = 60u64;

    let days = now / secs_per_day;
    let remaining = now % secs_per_day;
    let hours = remaining / secs_per_hour;
    let remaining = remaining % secs_per_hour;
    let minutes = remaining / secs_per_min;
    let seconds = remaining % secs_per_min;

    let (year, month, day) = days_to_date(days);

    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hours, minutes, seconds
    )
}

/// Convert days since Unix epoch to (year, month, day).
fn days_to_date(mut days: u64) -> (u64, u64, u64) {
    let mut year = 1970u64;

    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if days < days_in_year {
            break;
        }
        days -= days_in_year;
        year += 1;
    }

    let leap = is_leap_year(year);
    let month_days = [
        31,
        if leap { 29 } else { 28 },
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31,
    ];

    let mut month = 1u64;
    for &md in &month_days {
        if days < md {
            break;
        }
        days -= md;
        month += 1;
    }

    (year, month, days + 1)
}

fn is_leap_year(year: u64) -> bool {
    (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}

// ============================================================================
// Tests
// ============================================================================

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

    // -- Test implementation of Fingerprintable -------------------------------

    #[derive(Clone)]
    struct TestItem {
        category: String,
        file: String,
        message: String,
    }

    impl Fingerprintable for TestItem {
        fn fingerprint(&self) -> String {
            format!("{}::{}", self.category, self.file)
        }
        fn description(&self) -> String {
            self.message.clone()
        }
        fn context_label(&self) -> String {
            self.category.clone()
        }
    }

    fn item(category: &str, file: &str, message: &str) -> TestItem {
        TestItem {
            category: category.to_string(),
            file: file.to_string(),
            message: message.to_string(),
        }
    }

    // -- Save & Load ----------------------------------------------------------

    #[test]
    fn save_creates_homeboy_json() {
        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "audit");
        let items = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "b.rs", "dead code"),
        ];

        let path = save(&config, "test-component", &items, ()).unwrap();
        assert!(path.exists());
        assert!(path.to_string_lossy().ends_with("homeboy.json"));
    }

    #[test]
    fn load_returns_none_when_no_file() {
        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "audit");
        let result = load::<()>(&config).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn load_returns_none_when_no_baselines_key() {
        let dir = TempDir::new().unwrap();
        // Write a homeboy.json without baselines
        std::fs::write(
            dir.path().join("homeboy.json"),
            r#"{"remote_path": "wp-content/plugins/test"}"#,
        )
        .unwrap();

        let config = BaselineConfig::new(dir.path(), "audit");
        let result = load::<()>(&config).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn load_returns_none_when_key_missing() {
        let dir = TempDir::new().unwrap();
        // Write homeboy.json with baselines but different key
        std::fs::write(
            dir.path().join("homeboy.json"),
            r#"{"baselines": {"lint": {"created_at": "x", "context_id": "y", "item_count": 0, "known_fingerprints": [], "metadata": null}}}"#,
        )
        .unwrap();

        let config = BaselineConfig::new(dir.path(), "audit");
        let result = load::<()>(&config).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn save_then_load_roundtrips() {
        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "audit");
        let items = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "b.rs", "dead code"),
        ];

        save(&config, "my-component", &items, ()).unwrap();
        let loaded = load::<()>(&config).unwrap().unwrap();

        assert_eq!(loaded.context_id, "my-component");
        assert_eq!(loaded.item_count, 2);
        assert_eq!(loaded.known_fingerprints.len(), 2);
        assert!(loaded
            .known_fingerprints
            .contains(&"lint::a.rs".to_string()));
        assert!(loaded
            .known_fingerprints
            .contains(&"lint::b.rs".to_string()));
    }

    #[test]
    fn save_preserves_existing_config() {
        let dir = TempDir::new().unwrap();

        // Pre-existing homeboy.json with component config
        std::fs::write(
            dir.path().join("homeboy.json"),
            r#"{
  "remote_path": "wp-content/plugins/data-machine",
  "extensions": { "wordpress": {} }
}"#,
        )
        .unwrap();

        let config = BaselineConfig::new(dir.path(), "audit");
        let items = vec![item("conv", "a.php", "missing method")];
        save(&config, "data-machine", &items, ()).unwrap();

        // Re-read the full file and verify existing keys are preserved
        let content = std::fs::read_to_string(dir.path().join("homeboy.json")).unwrap();
        let root: Value = serde_json::from_str(&content).unwrap();

        assert_eq!(
            root.get("remote_path").and_then(|v| v.as_str()),
            Some("wp-content/plugins/data-machine")
        );
        assert!(root.get("extensions").is_some());
        assert!(root.get("baselines").is_some());
        assert!(root.get("baselines").unwrap().get("audit").is_some());
    }

    #[test]
    fn save_preserves_other_baselines() {
        let dir = TempDir::new().unwrap();
        let audit_config = BaselineConfig::new(dir.path(), "audit");
        let lint_config = BaselineConfig::new(dir.path(), "lint");

        // Save audit baseline
        let audit_items = vec![item("conv", "a.php", "missing method")];
        save(&audit_config, "test", &audit_items, ()).unwrap();

        // Save lint baseline
        let lint_items = vec![item("psr12", "b.php", "wrong indent")];
        save(&lint_config, "test", &lint_items, ()).unwrap();

        // Both should exist
        let audit = load::<()>(&audit_config).unwrap().unwrap();
        let lint = load::<()>(&lint_config).unwrap().unwrap();

        assert_eq!(audit.item_count, 1);
        assert_eq!(lint.item_count, 1);
        assert!(audit
            .known_fingerprints
            .contains(&"conv::a.php".to_string()));
        assert!(lint
            .known_fingerprints
            .contains(&"psr12::b.php".to_string()));
    }

    #[test]
    fn save_with_typed_metadata_roundtrips() {
        #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
        struct AuditMeta {
            alignment_score: f32,
            outlier_count: usize,
        }

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "audit");
        let items = vec![item("conv", "a.php", "missing method")];
        let meta = AuditMeta {
            alignment_score: 0.85,
            outlier_count: 3,
        };

        save(&config, "data-machine", &items, meta.clone()).unwrap();
        let loaded = load::<AuditMeta>(&config).unwrap().unwrap();

        assert_eq!(loaded.metadata, meta);
    }

    // -- Compare: no drift ----------------------------------------------------

    #[test]
    fn compare_identical_items_shows_no_drift() {
        let items = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "b.rs", "dead code"),
        ];

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "test");
        save(&config, "test", &items, ()).unwrap();
        let baseline = load::<()>(&config).unwrap().unwrap();

        let comparison = compare(&items, &baseline);
        assert!(!comparison.drift_increased);
        assert!(comparison.new_items.is_empty());
        assert!(comparison.resolved_fingerprints.is_empty());
        assert_eq!(comparison.delta, 0);
    }

    // -- Compare: new drift ---------------------------------------------------

    #[test]
    fn compare_detects_new_items() {
        let original = vec![item("lint", "a.rs", "unused import")];

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "test");
        save(&config, "test", &original, ()).unwrap();
        let baseline = load::<()>(&config).unwrap().unwrap();

        let current = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "c.rs", "missing docs"),
        ];

        let comparison = compare(&current, &baseline);
        assert!(comparison.drift_increased);
        assert_eq!(comparison.new_items.len(), 1);
        assert_eq!(comparison.new_items[0].fingerprint, "lint::c.rs");
        assert_eq!(comparison.delta, 1);
    }

    // -- Compare: resolved items ----------------------------------------------

    #[test]
    fn compare_detects_resolved_items() {
        let original = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "b.rs", "dead code"),
        ];

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "test");
        save(&config, "test", &original, ()).unwrap();
        let baseline = load::<()>(&config).unwrap().unwrap();

        let current = vec![item("lint", "a.rs", "unused import")];

        let comparison = compare(&current, &baseline);
        assert!(!comparison.drift_increased);
        assert!(comparison.new_items.is_empty());
        assert_eq!(comparison.resolved_fingerprints.len(), 1);
        assert_eq!(comparison.delta, -1);
    }

    // -- Compare: new AND resolved simultaneously -----------------------------

    #[test]
    fn compare_new_and_resolved_simultaneously() {
        let original = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "b.rs", "dead code"),
        ];

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "test");
        save(&config, "test", &original, ()).unwrap();
        let baseline = load::<()>(&config).unwrap().unwrap();

        // b.rs resolved, c.rs introduced
        let current = vec![
            item("lint", "a.rs", "unused import"),
            item("lint", "c.rs", "missing docs"),
        ];

        let comparison = compare(&current, &baseline);
        assert!(comparison.drift_increased); // new item = fail
        assert_eq!(comparison.new_items.len(), 1);
        assert_eq!(comparison.resolved_fingerprints.len(), 1);
        assert_eq!(comparison.delta, 0); // net zero, but still fails
    }

    // -- Fingerprint stability ------------------------------------------------

    #[test]
    fn fingerprint_ignores_description_changes() {
        let item1 = item("structural", "deploy.rs", "File has 2484 lines");
        let item2 = item("structural", "deploy.rs", "File has 2645 lines");
        assert_eq!(item1.fingerprint(), item2.fingerprint());
    }

    // -- Timestamp ------------------------------------------------------------

    #[test]
    fn utc_now_produces_valid_iso8601() {
        let now = utc_now_iso8601();
        assert_eq!(
            now.len(),
            20,
            "Expected 20 chars, got {}: {}",
            now.len(),
            now
        );
        assert!(now.ends_with('Z'));
        assert!(now.contains('T'));
    }

    // -- BaselineConfig -------------------------------------------------------

    #[test]
    fn config_json_path() {
        let config = BaselineConfig::new("/project/root", "audit");
        assert_eq!(
            config.json_path(),
            PathBuf::from("/project/root/homeboy.json")
        );
    }

    // -- Edge cases -----------------------------------------------------------

    #[test]
    fn save_overwrites_same_key() {
        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "audit");

        let items_v1 = vec![item("lint", "a.rs", "v1")];
        save(&config, "test", &items_v1, ()).unwrap();

        let items_v2 = vec![item("lint", "a.rs", "v2"), item("lint", "b.rs", "new")];
        save(&config, "test", &items_v2, ()).unwrap();

        let loaded = load::<()>(&config).unwrap().unwrap();
        assert_eq!(loaded.item_count, 2);
    }

    #[test]
    fn compare_empty_current_against_populated_baseline() {
        let original = vec![item("lint", "a.rs", "unused"), item("lint", "b.rs", "dead")];

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "test");
        save(&config, "test", &original, ()).unwrap();
        let baseline = load::<()>(&config).unwrap().unwrap();

        let current: Vec<TestItem> = vec![];
        let comparison = compare(&current, &baseline);
        assert!(!comparison.drift_increased);
        assert_eq!(comparison.resolved_fingerprints.len(), 2);
        assert_eq!(comparison.delta, -2);
    }

    #[test]
    fn compare_populated_current_against_empty_baseline() {
        let original: Vec<TestItem> = vec![];

        let dir = TempDir::new().unwrap();
        let config = BaselineConfig::new(dir.path(), "test");
        save(&config, "test", &original, ()).unwrap();
        let baseline = load::<()>(&config).unwrap().unwrap();

        let current = vec![item("lint", "a.rs", "new finding")];
        let comparison = compare(&current, &baseline);
        assert!(comparison.drift_increased);
        assert_eq!(comparison.new_items.len(), 1);
        assert_eq!(comparison.delta, 1);
    }

    #[test]
    fn load_returns_error_for_malformed_baseline() {
        let dir = TempDir::new().unwrap();
        std::fs::write(
            dir.path().join("homeboy.json"),
            r#"{"baselines": {"audit": "not a valid baseline object"}}"#,
        )
        .unwrap();

        let config = BaselineConfig::new(dir.path(), "audit");
        let result = load::<()>(&config);
        assert!(result.is_err());
    }

    #[test]
    fn load_returns_error_for_malformed_json() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join("homeboy.json"), "not valid json {{{").unwrap();

        let config = BaselineConfig::new(dir.path(), "audit");
        let result = load::<()>(&config);
        assert!(result.is_err());
    }

    #[test]
    fn load_from_git_ref_returns_none_for_nonexistent_ref() {
        // Non-git directory should return None (not panic)
        let dir = TempDir::new().unwrap();
        let result = load_from_git_ref::<()>(dir.path().to_str().unwrap(), "origin/main", "audit");
        assert!(result.is_none());
    }

    #[test]
    fn load_from_git_ref_returns_none_for_missing_baseline_key() {
        // Git repo with homeboy.json but no baselines key should return None
        let dir = TempDir::new().unwrap();
        let path = dir.path().to_str().unwrap();

        // Create a git repo with a homeboy.json that has no baselines
        let _ = crate::utils::command::run_in(path, "git", &["init"], "git init");
        let _ = crate::utils::command::run_in(
            path,
            "git",
            &["config", "user.email", "test@test.com"],
            "git config email",
        );
        let _ = crate::utils::command::run_in(
            path,
            "git",
            &["config", "user.name", "Test"],
            "git config name",
        );
        std::fs::write(dir.path().join("homeboy.json"), r#"{"id": "test"}"#).unwrap();
        let _ = crate::utils::command::run_in(path, "git", &["add", "."], "git add");
        let _ = crate::utils::command::run_in(path, "git", &["commit", "-m", "init"], "git commit");

        let result = load_from_git_ref::<()>(path, "HEAD", "audit");
        assert!(result.is_none());
    }
}