timebomb-cli 0.5.0

Scan source code for deadline-tagged fuses and fail when they detonate
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
use crate::error::{Error, Result};
use crate::output::OutputFormat;
use crate::scanner::ScanResult;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

// ─── Core types ───────────────────────────────────────────────────────────────

/// A single fuse as stored in the persisted report file.
/// Owned strings — no lifetimes — so it can be deserialized easily.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReportAnnotation {
    pub file: String,
    pub line: usize,
    pub tag: String,
    pub date: String,
    pub owner: Option<String>,
    pub message: String,
    pub status: String,
}

/// The persisted report file format.
#[derive(Debug, Serialize, Deserialize)]
pub struct Report {
    /// RFC 3339 timestamp of when this report was generated.
    pub generated_at: String,
    pub swept_files: usize,
    pub total_fuses: usize,
    pub detonated: Vec<ReportAnnotation>,
    pub ticking: Vec<ReportAnnotation>,
    pub inert: Vec<ReportAnnotation>,
}

/// The result of diffing two reports.
#[derive(Debug)]
pub struct ReportDiff {
    /// Fuses that are detonated in the new report but were not in the old one
    /// (either newly added past their deadline, or crossed the deadline since last report).
    pub newly_detonated: Vec<ReportAnnotation>,
    /// Fuses present in old report but absent in new (cleaned up / deleted).
    pub resolved: Vec<ReportAnnotation>,
    /// Fuses in new report that weren't in old report at all (any status).
    pub new_annotations: Vec<ReportAnnotation>,
    /// Fuses whose date changed between old and new report (snoozed).
    pub snoozed: Vec<(ReportAnnotation, ReportAnnotation)>, // (old, new)
}

// ─── Helper for JSON diff serialization ──────────────────────────────────────

#[derive(Serialize)]
struct SnoozedPair<'a> {
    before: &'a ReportAnnotation,
    after: &'a ReportAnnotation,
}

#[derive(Serialize)]
struct DiffJson<'a> {
    newly_detonated: &'a [ReportAnnotation],
    resolved: &'a [ReportAnnotation],
    new_annotations: &'a [ReportAnnotation],
    snoozed: Vec<SnoozedPair<'a>>,
}

// ─── Key type used for O(1) lookup ───────────────────────────────────────────

type AnnKey = (String, usize, String);

fn ann_key(a: &ReportAnnotation) -> AnnKey {
    (a.file.clone(), a.line, a.tag.clone())
}

fn make_key_map(anns: &[ReportAnnotation]) -> HashMap<AnnKey, &ReportAnnotation> {
    anns.iter().map(|a| (ann_key(a), a)).collect()
}

/// Build a map covering all three status buckets of a report.
fn all_key_map(report: &Report) -> HashMap<AnnKey, &ReportAnnotation> {
    let mut map = HashMap::new();
    for a in &report.detonated {
        map.insert(ann_key(a), a);
    }
    for a in &report.ticking {
        map.insert(ann_key(a), a);
    }
    for a in &report.inert {
        map.insert(ann_key(a), a);
    }
    map
}

// ─── Public functions ─────────────────────────────────────────────────────────

/// Convert a ScanResult into a Report. `generated_at` should be an RFC 3339 string.
/// Accept it as a parameter for testability.
pub fn build_report(result: &ScanResult, generated_at: &str) -> Report {
    let to_report_ann = |a: &crate::annotation::Fuse| ReportAnnotation {
        file: a.file.display().to_string(),
        line: a.line,
        tag: a.tag.clone(),
        date: a.date_str(),
        owner: a.owner.clone(),
        message: a.message.clone(),
        status: a.status.as_str().to_string(),
    };

    // Single pass over fuses — avoids three separate Vec allocations from
    // calling result.detonated() / result.ticking() / result.inert() individually.
    let mut detonated: Vec<ReportAnnotation> = Vec::new();
    let mut ticking: Vec<ReportAnnotation> = Vec::new();
    let mut inert: Vec<ReportAnnotation> = Vec::new();
    for fuse in &result.fuses {
        match fuse.status {
            crate::annotation::Status::Detonated => detonated.push(to_report_ann(fuse)),
            crate::annotation::Status::Ticking => ticking.push(to_report_ann(fuse)),
            crate::annotation::Status::Inert => inert.push(to_report_ann(fuse)),
        }
    }

    let total_fuses = detonated.len() + ticking.len() + inert.len();

    Report {
        generated_at: generated_at.to_string(),
        swept_files: result.swept_files,
        total_fuses,
        detonated,
        ticking,
        inert,
    }
}

/// Write a Report to a JSON file at `path`.
pub fn write_report(report: &Report, path: &Path) -> Result<()> {
    // Ensure parent directory exists.
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() {
            std::fs::create_dir_all(parent).map_err(|e| Error::Io {
                source: e,
                path: Some(parent.to_path_buf()),
            })?;
        }
    }

    let json = serde_json::to_string_pretty(report).map_err(|e| Error::Io {
        source: std::io::Error::other(e.to_string()),
        path: Some(path.to_path_buf()),
    })?;

    std::fs::write(path, json).map_err(|e| Error::Io {
        source: e,
        path: Some(path.to_path_buf()),
    })
}

/// Read a Report from a JSON file at `path`.
/// Returns Ok(None) if the file does not exist (first run).
/// Returns Err if the file exists but cannot be parsed.
pub fn read_report(path: &Path) -> Result<Option<Report>> {
    if !path.exists() {
        return Ok(None);
    }

    let content = std::fs::read_to_string(path).map_err(|e| Error::Io {
        source: e,
        path: Some(path.to_path_buf()),
    })?;

    let report: Report = serde_json::from_str(&content).map_err(|e| Error::Io {
        source: std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()),
        path: Some(path.to_path_buf()),
    })?;

    Ok(Some(report))
}

/// Diff two reports. `old` is the previously persisted report, `new` is the freshly built one.
pub fn diff_reports(old: &Report, new: &Report) -> ReportDiff {
    let old_detonated_map = make_key_map(&old.detonated);
    let old_all_map = all_key_map(old);
    let new_all_map = all_key_map(new);

    // newly_detonated: in new.detonated but key not in old.detonated
    let newly_detonated: Vec<ReportAnnotation> = new
        .detonated
        .iter()
        .filter(|a| !old_detonated_map.contains_key(&ann_key(a)))
        .cloned()
        .collect();

    // resolved: key is in old.detonated but not found anywhere in new
    let resolved: Vec<ReportAnnotation> = old
        .detonated
        .iter()
        .filter(|a| !new_all_map.contains_key(&ann_key(a)))
        .cloned()
        .collect();

    // new_annotations: key present in new (any status) but not present anywhere in old
    let new_annotations: Vec<ReportAnnotation> = {
        let mut seen_keys = std::collections::HashSet::new();
        let mut result = Vec::new();
        for bucket in [&new.detonated, &new.ticking, &new.inert] {
            for a in bucket {
                let key = ann_key(a);
                if !old_all_map.contains_key(&key) && seen_keys.insert(key) {
                    result.push(a.clone());
                }
            }
        }
        result
    };

    // snoozed: key present in both old and new, but old.date != new.date
    let snoozed: Vec<(ReportAnnotation, ReportAnnotation)> = {
        let mut result = Vec::new();
        for bucket in [&new.detonated, &new.ticking, &new.inert] {
            for new_ann in bucket {
                let key = ann_key(new_ann);
                if let Some(old_ann) = old_all_map.get(&key) {
                    if old_ann.date != new_ann.date {
                        result.push(((*old_ann).clone(), new_ann.clone()));
                    }
                }
            }
        }
        result
    };

    ReportDiff {
        newly_detonated,
        resolved,
        new_annotations,
        snoozed,
    }
}

/// Whether color output should be enabled (respects NO_COLOR).
fn color_enabled() -> bool {
    std::env::var("NO_COLOR").is_err()
}

/// Print a ReportDiff to stdout in terminal format.
pub fn print_diff_terminal(diff: &ReportDiff) {
    let use_color = color_enabled();

    println!("REPORT DIFF");
    println!("-----------");

    // newly detonated
    let n = diff.newly_detonated.len();
    println!("{} newly detonated fuse(s):", n);
    for a in &diff.newly_detonated {
        let label = "DETONATED";
        let location = format!("{}:{}", a.file, a.line);
        let tag_date = format!("{}[{}]", a.tag, a.date);
        let line = format!(
            "  {:<9} {:<30} {:<22} {}",
            label, location, tag_date, a.message
        );
        if use_color {
            println!("{}", line.red());
        } else {
            println!("{}", line);
        }
    }

    println!();

    // resolved
    let n = diff.resolved.len();
    println!("{} resolved fuse(s):", n);
    for a in &diff.resolved {
        let label = "REMOVED";
        let location = format!("{}:{}", a.file, a.line);
        let tag_date = format!("{}[{}]", a.tag, a.date);
        let line = format!(
            "  {:<9} {:<30} {:<22} {}",
            label, location, tag_date, a.message
        );
        if use_color {
            println!("{}", line.green());
        } else {
            println!("{}", line);
        }
    }

    println!();

    // new annotations
    let n = diff.new_annotations.len();
    println!("{} new fuse(s) added:", n);
    for a in &diff.new_annotations {
        let label = "NEW";
        let location = format!("{}:{}", a.file, a.line);
        let tag_date = format!("{}[{}]", a.tag, a.date);
        let line = format!(
            "  {:<9} {:<30} {:<22} {}",
            label, location, tag_date, a.message
        );
        if use_color {
            println!("{}", line.yellow());
        } else {
            println!("{}", line);
        }
    }

    println!();

    // snoozed
    let n = diff.snoozed.len();
    println!("{} snoozed fuse(s):", n);
    for (old, new) in &diff.snoozed {
        let label = "SNOOZED";
        let location = format!("{}:{}", new.file, new.line);
        let tag_date = format!("{}[{}{}]", new.tag, old.date, new.date);
        let line = format!(
            "  {:<9} {:<30} {:<22} {}",
            label, location, tag_date, new.message
        );
        if use_color {
            println!("{}", line.cyan());
        } else {
            println!("{}", line);
        }
    }
}

/// Print a ReportDiff as JSON to stdout.
pub fn print_diff_json(diff: &ReportDiff) {
    let snoozed: Vec<SnoozedPair<'_>> = diff
        .snoozed
        .iter()
        .map(|(old, new)| SnoozedPair {
            before: old,
            after: new,
        })
        .collect();

    let payload = DiffJson {
        newly_detonated: &diff.newly_detonated,
        resolved: &diff.resolved,
        new_annotations: &diff.new_annotations,
        snoozed,
    };

    match serde_json::to_string_pretty(&payload) {
        Ok(json) => println!("{}", json),
        Err(e) => eprintln!("error serializing diff: {}", e),
    }
}

/// Top-level entry point — called from main.rs.
/// - Builds the new report from the scan result.
/// - If `diff` is true and a previous report file exists, compute and print the diff.
/// - Always writes the new report to `out_path`.
/// - Returns exit code: 0 normally, 1 if `fail_on_new` is true and `diff.newly_detonated` is non-empty.
pub fn run_report(
    result: &ScanResult,
    out_path: &Path,
    diff: bool,
    fail_on_new: bool,
    format: &OutputFormat,
    generated_at: &str,
) -> Result<i32> {
    let new_report = build_report(result, generated_at);

    let mut exit_code = 0i32;

    if diff {
        match read_report(out_path)? {
            None => {
                println!(
                    "No previous report found at {} — writing initial report.",
                    out_path.display()
                );
            }
            Some(old_report) => {
                let report_diff = diff_reports(&old_report, &new_report);

                match format {
                    OutputFormat::Json => print_diff_json(&report_diff),
                    _ => print_diff_terminal(&report_diff),
                }

                if fail_on_new && !report_diff.newly_detonated.is_empty() {
                    exit_code = 1;
                }
            }
        }
    }

    write_report(&new_report, out_path)?;
    Ok(exit_code)
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::annotation::{Fuse, Status};
    use crate::scanner::ScanResult;
    use chrono::NaiveDate;
    use std::path::PathBuf;

    // ── Helpers ───────────────────────────────────────────────────────────────

    fn make_fuse(file: &str, line: usize, tag: &str, date: &str, status: Status) -> Fuse {
        Fuse {
            file: PathBuf::from(file),
            line,
            tag: tag.to_string(),
            date: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
            owner: None,
            message: "test".to_string(),
            status,
            blamed_owner: None,
        }
    }

    fn make_scan_result(fuses: Vec<Fuse>) -> ScanResult {
        ScanResult {
            swept_files: 1,
            skipped_files: 0,
            fuses,
        }
    }

    fn make_report_ann(
        file: &str,
        line: usize,
        tag: &str,
        date: &str,
        status: &str,
    ) -> ReportAnnotation {
        ReportAnnotation {
            file: file.to_string(),
            line,
            tag: tag.to_string(),
            date: date.to_string(),
            owner: None,
            message: "test".to_string(),
            status: status.to_string(),
        }
    }

    fn make_report(
        detonated: Vec<ReportAnnotation>,
        ticking: Vec<ReportAnnotation>,
        inert: Vec<ReportAnnotation>,
    ) -> Report {
        let total = detonated.len() + ticking.len() + inert.len();
        Report {
            generated_at: "2025-01-01T00:00:00+00:00".to_string(),
            swept_files: 1,
            total_fuses: total,
            detonated,
            ticking,
            inert,
        }
    }

    // ── build_report ──────────────────────────────────────────────────────────

    #[test]
    fn test_build_report_empty() {
        let result = make_scan_result(vec![]);
        let report = build_report(&result, "2025-01-01T00:00:00+00:00");
        assert_eq!(report.total_fuses, 0);
        assert!(report.detonated.is_empty());
        assert!(report.ticking.is_empty());
        assert!(report.inert.is_empty());
        assert_eq!(report.swept_files, 1);
        assert_eq!(report.generated_at, "2025-01-01T00:00:00+00:00");
    }

    #[test]
    fn test_build_report_counts() {
        let fuses = vec![
            make_fuse("src/a.rs", 1, "TODO", "2020-01-01", Status::Detonated),
            make_fuse("src/b.rs", 2, "FIXME", "2020-06-01", Status::Detonated),
            make_fuse("src/c.rs", 3, "TODO", "2025-06-10", Status::Ticking),
            make_fuse("src/d.rs", 4, "TODO", "2099-01-01", Status::Inert),
        ];
        let result = make_scan_result(fuses);
        let report = build_report(&result, "2025-01-01T00:00:00+00:00");

        assert_eq!(report.detonated.len(), 2);
        assert_eq!(report.ticking.len(), 1);
        assert_eq!(report.inert.len(), 1);
        assert_eq!(report.total_fuses, 4);
    }

    // ── write_report / read_report roundtrip ─────────────────────────────────

    #[test]
    fn test_write_and_read_report_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("report.json");

        let detonated = vec![make_report_ann(
            "src/a.rs",
            1,
            "TODO",
            "2020-01-01",
            "detonated",
        )];
        let report = make_report(detonated, vec![], vec![]);

        write_report(&report, &path).unwrap();
        let loaded = read_report(&path).unwrap().unwrap();

        assert_eq!(loaded.generated_at, report.generated_at);
        assert_eq!(loaded.swept_files, report.swept_files);
        assert_eq!(loaded.total_fuses, report.total_fuses);
        assert_eq!(loaded.detonated.len(), 1);
        assert_eq!(loaded.detonated[0], report.detonated[0]);
        assert!(loaded.ticking.is_empty());
        assert!(loaded.inert.is_empty());
    }

    #[test]
    fn test_write_report_creates_parent_dirs() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nested").join("deep").join("report.json");
        let report = make_report(vec![], vec![], vec![]);
        write_report(&report, &path).unwrap();
        assert!(path.exists());
    }

    // ── read_report edge cases ────────────────────────────────────────────────

    #[test]
    fn test_read_report_nonexistent_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("does_not_exist.json");
        let result = read_report(&path).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_read_report_invalid_json_returns_err() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.json");
        std::fs::write(&path, b"this is not json at all!!!").unwrap();
        let result = read_report(&path);
        assert!(result.is_err());
    }

    // ── diff_reports ──────────────────────────────────────────────────────────

    #[test]
    fn test_diff_no_change() {
        let ann = make_report_ann("src/a.rs", 1, "TODO", "2020-01-01", "detonated");
        let old = make_report(vec![ann.clone()], vec![], vec![]);
        let new = make_report(vec![ann], vec![], vec![]);
        let diff = diff_reports(&old, &new);
        assert!(diff.newly_detonated.is_empty());
        assert!(diff.resolved.is_empty());
        assert!(diff.new_annotations.is_empty());
        assert!(diff.snoozed.is_empty());
    }

    #[test]
    fn test_diff_newly_detonated() {
        // Fuse was inert in old report, now detonated in new report.
        let inert_ann = make_report_ann("src/a.rs", 1, "TODO", "2020-01-01", "inert");
        let det_ann = make_report_ann("src/a.rs", 1, "TODO", "2020-01-01", "detonated");

        let old = make_report(vec![], vec![], vec![inert_ann]);
        let new = make_report(vec![det_ann.clone()], vec![], vec![]);

        let diff = diff_reports(&old, &new);
        assert_eq!(diff.newly_detonated.len(), 1);
        assert_eq!(diff.newly_detonated[0], det_ann);
        assert!(diff.resolved.is_empty());
        assert!(diff.new_annotations.is_empty());
    }

    #[test]
    fn test_diff_newly_detonated_brand_new() {
        // Fuse did not exist at all before, and it's already detonated.
        let det_ann = make_report_ann("src/new.rs", 5, "FIXME", "2020-06-01", "detonated");

        let old = make_report(vec![], vec![], vec![]);
        let new = make_report(vec![det_ann.clone()], vec![], vec![]);

        let diff = diff_reports(&old, &new);
        // Appears in both newly_detonated and new_annotations
        assert_eq!(diff.newly_detonated.len(), 1);
        assert_eq!(diff.new_annotations.len(), 1);
    }

    #[test]
    fn test_diff_resolved() {
        // Fuse was detonated in old, gone entirely from new.
        let det_ann = make_report_ann("src/a.rs", 1, "TODO", "2020-01-01", "detonated");

        let old = make_report(vec![det_ann.clone()], vec![], vec![]);
        let new = make_report(vec![], vec![], vec![]);

        let diff = diff_reports(&old, &new);
        assert_eq!(diff.resolved.len(), 1);
        assert_eq!(diff.resolved[0], det_ann);
        assert!(diff.newly_detonated.is_empty());
    }

    #[test]
    fn test_diff_new_annotation() {
        // Fuse present in new but not old (inert status).
        let inert_ann = make_report_ann("src/brand_new.rs", 10, "TODO", "2099-01-01", "inert");

        let old = make_report(vec![], vec![], vec![]);
        let new = make_report(vec![], vec![], vec![inert_ann.clone()]);

        let diff = diff_reports(&old, &new);
        assert_eq!(diff.new_annotations.len(), 1);
        assert_eq!(diff.new_annotations[0], inert_ann);
        assert!(diff.newly_detonated.is_empty());
        assert!(diff.resolved.is_empty());
    }

    #[test]
    fn test_diff_snoozed() {
        // Same key (file, line, tag), but date changed.
        let old_ann = make_report_ann("src/worker.rs", 88, "TODO", "2025-03-01", "inert");
        let new_ann = make_report_ann("src/worker.rs", 88, "TODO", "2026-03-01", "inert");

        let old = make_report(vec![], vec![], vec![old_ann.clone()]);
        let new = make_report(vec![], vec![], vec![new_ann.clone()]);

        let diff = diff_reports(&old, &new);
        assert_eq!(diff.snoozed.len(), 1);
        let (ref before, ref after) = diff.snoozed[0];
        assert_eq!(before.date, "2025-03-01");
        assert_eq!(after.date, "2026-03-01");
        assert!(diff.new_annotations.is_empty());
    }

    #[test]
    fn test_diff_ticking_to_detonated_is_newly_detonated() {
        let ticking_ann = make_report_ann("src/a.rs", 1, "TODO", "2025-06-10", "ticking");
        let detonated_ann = make_report_ann("src/a.rs", 1, "TODO", "2025-06-10", "detonated");

        let old = make_report(vec![], vec![ticking_ann], vec![]);
        let new = make_report(vec![detonated_ann], vec![], vec![]);

        let diff = diff_reports(&old, &new);
        assert_eq!(diff.newly_detonated.len(), 1);
        assert!(diff.resolved.is_empty());
        assert!(diff.new_annotations.is_empty());
    }

    // ── print functions (smoke tests) ─────────────────────────────────────────

    fn make_nontrivial_diff() -> ReportDiff {
        ReportDiff {
            newly_detonated: vec![make_report_ann(
                "src/auth/login.rs",
                42,
                "TODO",
                "2025-01-15",
                "detonated",
            )],
            resolved: vec![make_report_ann(
                "src/db/old.sql",
                7,
                "TODO",
                "2020-01-01",
                "detonated",
            )],
            new_annotations: vec![make_report_ann(
                "src/api/handler.rs",
                12,
                "TODO",
                "2026-06-01",
                "inert",
            )],
            snoozed: vec![(
                make_report_ann("src/worker.rs", 88, "TODO", "2025-03-01", "inert"),
                make_report_ann("src/worker.rs", 88, "TODO", "2026-03-01", "inert"),
            )],
        }
    }

    #[test]
    fn test_print_diff_terminal_does_not_panic() {
        let diff = make_nontrivial_diff();
        // Should not panic — output goes to stdout, which is fine in tests.
        print_diff_terminal(&diff);
    }

    #[test]
    fn test_print_diff_json_does_not_panic() {
        let diff = make_nontrivial_diff();
        print_diff_json(&diff);
    }

    // ── run_report ────────────────────────────────────────────────────────────

    #[test]
    fn test_run_report_no_previous_no_diff() {
        let dir = tempfile::tempdir().unwrap();
        let out_path = dir.path().join("report.json");

        let result = make_scan_result(vec![]);
        let code = run_report(
            &result,
            &out_path,
            false, // diff
            false, // fail_on_new
            &OutputFormat::Terminal,
            "2025-01-01T00:00:00+00:00",
        )
        .unwrap();

        assert_eq!(code, 0);
        assert!(out_path.exists());

        // Report was written correctly.
        let loaded = read_report(&out_path).unwrap().unwrap();
        assert_eq!(loaded.total_fuses, 0);
    }

    #[test]
    fn test_run_report_diff_no_previous_file() {
        let dir = tempfile::tempdir().unwrap();
        let out_path = dir.path().join("report.json");

        let result = make_scan_result(vec![]);
        let code = run_report(
            &result,
            &out_path,
            true,  // diff = true, but no previous file
            false, // fail_on_new
            &OutputFormat::Terminal,
            "2025-01-01T00:00:00+00:00",
        )
        .unwrap();

        // Should print note and exit 0.
        assert_eq!(code, 0);
        // Should still write the file.
        assert!(out_path.exists());
    }

    #[test]
    fn test_run_report_fail_on_new_exits_one() {
        let dir = tempfile::tempdir().unwrap();
        let out_path = dir.path().join("report.json");

        // Write an initial report with no detonated fuses.
        let old_report = make_report(vec![], vec![], vec![]);
        write_report(&old_report, &out_path).unwrap();

        // New scan finds a detonated fuse.
        let fuses = vec![make_fuse(
            "src/a.rs",
            1,
            "TODO",
            "2020-01-01",
            Status::Detonated,
        )];
        let result = make_scan_result(fuses);

        let code = run_report(
            &result,
            &out_path,
            true, // diff
            true, // fail_on_new
            &OutputFormat::Terminal,
            "2025-06-01T00:00:00+00:00",
        )
        .unwrap();

        assert_eq!(code, 1);
    }

    #[test]
    fn test_run_report_fail_on_new_exits_zero_when_no_new_detonated() {
        let dir = tempfile::tempdir().unwrap();
        let out_path = dir.path().join("report.json");

        // Write an initial report with the same detonated fuse.
        let detonated = vec![make_report_ann(
            "src/a.rs",
            1,
            "TODO",
            "2020-01-01",
            "detonated",
        )];
        let old_report = make_report(detonated, vec![], vec![]);
        write_report(&old_report, &out_path).unwrap();

        // New scan finds the same detonated fuse — not "new".
        let fuses = vec![make_fuse(
            "src/a.rs",
            1,
            "TODO",
            "2020-01-01",
            Status::Detonated,
        )];
        let result = make_scan_result(fuses);

        let code = run_report(
            &result,
            &out_path,
            true, // diff
            true, // fail_on_new
            &OutputFormat::Terminal,
            "2025-06-01T00:00:00+00:00",
        )
        .unwrap();

        assert_eq!(code, 0);
    }

    #[test]
    fn test_run_report_json_format_no_panic() {
        let dir = tempfile::tempdir().unwrap();
        let out_path = dir.path().join("report.json");

        // Seed a previous report.
        let old_report = make_report(vec![], vec![], vec![]);
        write_report(&old_report, &out_path).unwrap();

        let fuses = vec![make_fuse(
            "src/b.rs",
            99,
            "FIXME",
            "2099-12-01",
            Status::Inert,
        )];
        let result = make_scan_result(fuses);

        let code = run_report(
            &result,
            &out_path,
            true,
            false,
            &OutputFormat::Json,
            "2025-06-01T00:00:00+00:00",
        )
        .unwrap();

        assert_eq!(code, 0);
    }
}