git-insights 0.0.4

A CLI tool to generate Git repo stats and insights.
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
use crate::git::run_command;
use std::time::{SystemTime, UNIX_EPOCH};

/// Collect commit timestamps (unix epoch seconds) in reverse chronological order.
/// Uses clean git invocation with --no-pager and no merges.
pub fn collect_commit_timestamps() -> Result<Vec<u64>, String> {
    let out = run_command(&["--no-pager", "log", "--no-merges", "--format=%ct"])?;
    let mut ts: Vec<u64> = Vec::new();
    for line in out.lines() {
        if let Ok(v) = line.trim().parse::<u64>() {
            ts.push(v);
        }
    }
    Ok(ts)
}

/// Bucket commit timestamps into week bins (7-day windows) ending at `now`.
/// Returns `weeks` bins, oldest -> newest (counts.len() == weeks).
pub fn compute_timeline_weeks(timestamps: &[u64], weeks: usize, now: u64) -> Vec<usize> {
    let mut counts = vec![0usize; weeks];
    if weeks == 0 {
        return counts;
    }
    const WEEK: u64 = 7 * 24 * 60 * 60; // 604800

    // Align to the end of the current epoch-week so bins are week-aligned, not relative to "now".
    // Current week is [start_of_week .. start_of_week+WEEK-1]; use that end boundary.
    let start_of_week = now - (now % WEEK);
    let aligned_end = start_of_week.saturating_add(WEEK - 1);

    for &t in timestamps {
        if t > aligned_end {
            continue;
        }
        let diff = aligned_end - t;
        let bin = (diff / WEEK) as usize;
        if bin < weeks {
            // newest bin is at the end
            let idx = weeks - 1 - bin;
            counts[idx] += 1;
        }
    }
    counts
}

/// Compute a 7x24 (weekday x hour) heatmap in UTC (kept for internal/tests).
/// Weekday index: 0=Sun,1=Mon,...,6=Sat
pub fn compute_heatmap_utc(timestamps: &[u64]) -> [[usize; 24]; 7] {
    let mut grid = [[0usize; 24]; 7];
    for &t in timestamps {
        let day = t / 86_400;
        // 1970-01-01 was a Thursday. With 0=Sun..6=Sat, Thursday corresponds to 4.
        let weekday = ((day + 4) % 7) as usize;
        let hour = ((t / 3_600) % 24) as usize;
        grid[weekday][hour] += 1;
    }
    grid
}

/// Compute a GitHub-style calendar heatmap (weekday x week-column).
/// Returns grid[7][weeks] as Vec<Vec<usize>> with rows=Sun..Sat, cols=old->new (weeks).
pub fn compute_calendar_heatmap(timestamps: &[u64], weeks: usize, now: u64) -> Vec<Vec<usize>> {
    let mut grid = vec![vec![0usize; weeks]; 7];
    if weeks == 0 {
        return grid;
    }
    const DAY: u64 = 86_400;
    const WEEK: u64 = DAY * 7;

    // Align to end of current week
    let start_of_week = now - (now % WEEK);
    let aligned_end = start_of_week.saturating_add(WEEK - 1);
    let span = (weeks as u64).saturating_mul(WEEK);
    let min_ts = aligned_end.saturating_sub(span.saturating_sub(1));

    for &t in timestamps {
        if t > aligned_end || t < min_ts {
            continue;
        }
        let day_index = (aligned_end - t) / DAY;      // 0.. spanning days
        let week_off = (day_index / 7) as usize;      // 0 = current week
        if week_off >= weeks {
            continue;
        }
        let col = weeks - 1 - week_off;               // oldest..newest left->right
        let day = t / DAY;
        let weekday = ((day + 4) % 7) as usize;       // 0=Sun..6=Sat
        grid[weekday][col] += 1;
    }
    grid
}

/// Render a compact single-line timeline using an ASCII ramp per bin.
/// Uses a small 10-char ramp to visualize relative intensity within the provided counts.
pub fn render_timeline_bars(counts: &[usize]) {
    let ramp: &[u8] = b" .:-=+*#%@"; // 10 levels
    let max = counts.iter().copied().max().unwrap_or(0);
    if max == 0 {
        println!("(no commits in selected window)");
        return;
    }
    let mut line = String::with_capacity(counts.len());
    for &c in counts {
        let idx = (c.saturating_mul(ramp.len() - 1)) / max;
        line.push(ramp[idx] as char);
    }
    println!("{}", line);
}

/// Render a 7x24 heatmap using an ASCII ramp. 0=Sun ... 6=Sat as rows.
/// Header shows hours 00..23; each cell is a character denoting relative intensity.
pub fn render_heatmap_ascii(grid: [[usize; 24]; 7]) {
    let ramp: &[u8] = b" .:-=+*#%@"; // 10 levels
    // Find global max for scaling
    let mut max = 0usize;
    for r in 0..7 {
        for h in 0..24 {
            if grid[r][h] > max {
                max = grid[r][h];
            }
        }
    }
    println!("     00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23");
    let labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    for (r, lbl) in labels.iter().enumerate() {
        print!("{:<3} ", lbl);
        for h in 0..24 {
            let c = grid[r][h];
            let ch = if max == 0 {
                ' '
            } else {
                let idx = (c.saturating_mul(ramp.len() - 1)) / max;
                ramp[idx] as char
            };
            print!(" {}", ch);
        }
        println!();
    }
    // Bottom hour axis for reference
    println!("     00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23");
}

/// Render GitHub-style calendar heatmap (ASCII ramp)
pub fn render_calendar_heatmap_ascii(grid: &[Vec<usize>]) {
    let ramp: &[u8] = b" .:-=+*#%@"; // 10 levels
    // global max
    let mut max = 0usize;
    for r in 0..7 {
        for c in 0..grid[0].len() {
            if grid[r][c] > max {
                max = grid[r][c];
            }
        }
    }
    let labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    for r in 0..7 {
        print!("{:<3} ", labels[r]);
        for c in 0..grid[0].len() {
            let v = grid[r][c];
            let ch = if max == 0 {
                ' '
            } else {
                let idx = (v.saturating_mul(ramp.len() - 1)) / max;
                ramp[idx] as char
            };
            print!(" {}", ch);
        }
        println!();
    }
    // bottom reference: week columns count
    print!("     ");
    for _ in 0..grid[0].len() {
        print!("^");
    }
    println!();
}

fn color_for_level(level: usize) -> &'static str {
    // Simple 6-step ANSI 8-color ramp (foreground)
    match level {
        0 => "\x1b[90m", // bright black / low intensity
        1 => "\x1b[94m", // blue
        2 => "\x1b[96m", // cyan
        3 => "\x1b[92m", // green
        4 => "\x1b[93m", // yellow
        _ => "\x1b[91m", // red (highest)
    }
}
const ANSI_RESET: &str = "\x1b[0m";

/// Print a color/ASCII legend showing low→high intensity and the meaning of blank.
fn print_ramp_legend(color: bool, unit: &str) {
    if color {
        // Levels 1..5 colored blocks; blank = 0
        print!("\x1b[90mLegend (low→high, blank=0 {}):\x1b[0m ", unit);
        for lvl in 1..=5 {
            print!(" {}{}", color_for_level(lvl), ANSI_RESET);
        }
        println!();
    } else {
        let ramp = " .:-=+*#%@";
        println!(
            "Legend (low→high, blank=' ' 0 {}): {}",
            unit, ramp
        );
    }
}

/// Render timeline as Unicode bars with optional color.
/// Uses unicode ramp " ▁▂▃▄▅▆▇█" (9 levels) + color ramp.
pub fn render_timeline_bars_colored(counts: &[usize], color: bool) {
    if !color {
        render_timeline_bars(counts);
        return;
    }
    let ramp: &[char] = &[' ', '', '', '', '', '', '', '', '']; // 9 levels
    let max = counts.iter().copied().max().unwrap_or(0);
    if max == 0 {
        println!("(no commits in selected window)");
        return;
    }
    let mut out = String::with_capacity(counts.len() * 6);
    for &c in counts {
        let idx = (c.saturating_mul(ramp.len() - 1)) / max; // 0..=8
        // map intensity 0..=8 to 0..=5 color levels
        let color_level = if idx == 0 { 0 } else { ((idx - 1) * 5) / (ramp.len() - 2) };
        out.push_str(color_for_level(color_level));
        out.push(ramp[idx]);
    }
    out.push_str(ANSI_RESET);
    println!("{}", out);
}

/// Render timeline as multi-row bars with optional color.
/// height must be >= 1. Uses '█' for color mode and '#' for ASCII.
pub fn render_timeline_multiline(counts: &[usize], height: usize, color: bool) {
    let h = height.max(1);
    let max = counts.iter().copied().max().unwrap_or(0);
    if max == 0 || counts.is_empty() {
        println!("(no commits in selected window)");
        return;
    }

    // Y-axis reference: show labels at top (max), middle (~max/2), and bottom (0)
    let top_label = max;
    let mid_label = (max + 1) / 2;
    let bottom_label = 0usize;
    let label_width = top_label.to_string().len().max(3);
    let axis_char = if color { '' } else { '|' };
    let dim_start = if color { "\x1b[90m" } else { "" };
    let dim_end = if color { "\x1b[0m" } else { "" };

    for row in (1..=h).rev() {
        // Determine label for this row
        let label_val = if row == h {
            Some(top_label)
        } else if row == ((h + 1) / 2) {
            Some(mid_label)
        } else if row == 1 {
            Some(bottom_label)
        } else {
            None
        };

        // Build left y-axis prefix " 123 |"
        let mut line = String::with_capacity(label_width + 2);
        match label_val {
            Some(v) => {
                if color {
                    line.push_str(dim_start);
                }
                line.push_str(&format!("{:>width$} {}", v, axis_char, width = label_width));
                if color {
                    line.push_str(dim_end);
                }
            }
            None => {
                if color {
                    line.push_str(dim_start);
                }
                line.push_str(&format!("{:>width$} {}", "", axis_char, width = label_width));
                if color {
                    line.push_str(dim_end);
                }
            }
        }

        // Build bars for this row
        let mut bars = String::with_capacity(counts.len() * 6);
        for &c in counts {
            let filled = ((c as usize) * h + max - 1) / max; // ceil to 1..=h
            if filled >= row {
                if color {
                    // map count to 0..=5 color level
                    let idx = if c == 0 { 0 } else { ((c - 1) as usize * 5) / max + 1 };
                    bars.push_str(color_for_level(idx));
                    bars.push('');
                } else {
                    bars.push('#');
                }
            } else {
                bars.push(' ');
            }
        }
        if color {
            bars.push_str(ANSI_RESET);
        }

        // Print y-axis + bars
        println!("{}{}", line, bars);
    }
}

/// Render a compact reference axis below the timeline:
/// - Minor ticks every 4 weeks
/// - Major ticks every 12 weeks (labeled with remaining weeks from newest: 48,36,24,12,0)
fn render_timeline_axis(weeks: usize, color: bool) {
    if weeks == 0 {
        return;
    }
    // Ticks line
    let mut ticks = vec![' '; weeks];
    for col in 0..weeks {
        // rel=0 at newest (rightmost), rel=weeks-1 at oldest (leftmost)
        let rel = weeks - 1 - col;
        if rel % 12 == 0 {
            ticks[col] = if color { '' } else { '+' };
        } else if rel % 4 == 0 {
            ticks[col] = if color { '' } else { '|' };
        }
    }
    if color {
        print!("\x1b[90m"); // dim
    }
    println!("{}", ticks.iter().collect::<String>());
    // Labels line (major ticks only). Place numbers without overlaps.
    let mut labels = vec![' '; weeks];
    let label_color_start = if color { "\x1b[90m" } else { "" };
    let label_color_end = if color { "\x1b[0m" } else { "" };

    let mut occupied = vec![false; weeks];
    for col in 0..weeks {
        let rel = weeks - 1 - col;
        if rel % 12 == 0 {
            let s = rel.to_string();
            // avoid overlap: ensure s fits starting at `col`
            if col + s.len() <= weeks
                && (col..col + s.len()).all(|i| !occupied[i])
            {
                // write digits
                for (i, ch) in s.chars().enumerate() {
                    labels[col + i] = ch;
                    occupied[col + i] = true;
                }
            }
        }
    }
    print!("{}", label_color_start);
    println!("{}", labels.iter().collect::<String>());
    if color {
        print!("{}", label_color_end);
    }
}

/// Render heatmap with optional color using '█' blocks (space for zero).
pub fn render_heatmap_ascii_colored(grid: [[usize; 24]; 7], color: bool) {
    if !color {
        render_heatmap_ascii(grid);
        return;
    }
    // global max for scaling
    let mut max = 0usize;
    for r in 0..7 {
        for h in 0..24 {
            if grid[r][h] > max {
                max = grid[r][h];
            }
        }
    }
    println!("     00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23");
    let labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    for (r, lbl) in labels.iter().enumerate() {
        print!("{:<3} ", lbl);
        for h in 0..24 {
            let c = grid[r][h];
            if max == 0 || c == 0 {
                print!("  ");
            } else {
                // build 6 buckets for color
                let idx = ((c - 1) * 5) / max + 1; // 1..=5 approx
                let code = color_for_level(idx);
                print!(" {}{}", code, ANSI_RESET);
            }
        }
        println!();
    }
    // Bottom hour axis for reference
    println!("     00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23");
}

/// Render GitHub-style calendar heatmap (colored)
pub fn render_calendar_heatmap_colored(grid: &[Vec<usize>]) {
    // global max
    let mut max = 0usize;
    for r in 0..7 {
        for c in 0..grid[0].len() {
            if grid[r][c] > max {
                max = grid[r][c];
            }
        }
    }
    let labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    for r in 0..7 {
        print!("{:<3} ", labels[r]);
        for c in 0..grid[0].len() {
            let v = grid[r][c];
            if max == 0 || v == 0 {
                print!("  ");
            } else {
                let idx = ((v - 1) * 5) / max + 1; // 1..=5 approx
                let code = color_for_level(idx);
                print!(" {}{}", code, ANSI_RESET);
            }
        }
        println!();
    }
    // bottom week columns
    print!("     ");
    for _ in 0..grid[0].len() {
        print!("^");
    }
    println!();
}

/// Run the timeline visualization with options.
pub fn run_timeline_with_options(weeks: usize, color: bool) -> Result<(), String> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| format!("clock error: {e}"))?
        .as_secs();
    let ts = collect_commit_timestamps()?;
    let counts = compute_timeline_weeks(&ts, weeks, now);
    println!("Weekly commits (old -> new), weeks={weeks}:");
    // Print Y-axis unit/scale reference
    let max = counts.iter().copied().max().unwrap_or(0);
    let mid = (max + 1) / 2;
    if color { print!("\x1b[90m"); }
    println!("Y-axis: commits/week (max={}, mid≈{})", max, mid);
    if color { print!("\x1b[0m"); }
    print_ramp_legend(color, "commits/week");
    // Default to a 7-line tall chart for better readability without flooding the screen
    render_timeline_multiline(&counts, 7, color);
    // Add axis reference (minor tick=4 weeks, major tick=12 weeks)
    render_timeline_axis(weeks, color);
    Ok(())
}

/// Run the timeline visualization end-to-end with default `weeks` if needed.
pub fn run_timeline(weeks: usize) -> Result<(), String> {
    run_timeline_with_options(weeks, false)
}


/// Run the heatmap visualization with options.
pub fn run_heatmap_with_options(weeks: Option<usize>, color: bool) -> Result<(), String> {
    let ts_all = collect_commit_timestamps()?;
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| format!("clock error: {e}"))?
        .as_secs();

    // Default to 52 weeks if not specified to keep a reasonable width like GitHub
    let w = weeks.unwrap_or(52);
    let grid = compute_calendar_heatmap(&ts_all, w, now);

    // Unit and window line
    let mut max = 0usize;
    for r in 0..7 {
        for c in 0..grid[0].len() {
            if grid[r][c] > max {
                max = grid[r][c];
            }
        }
    }
    if color { print!("\x1b[90m"); }
    println!("Calendar heatmap (UTC) — rows: Sun..Sat, cols: weeks (old→new), unit: commits/day, window: last {} weeks, max={}", w, max);
    if color { print!("\x1b[0m"); }
    print_ramp_legend(color, "commits/day");

    if color {
        render_calendar_heatmap_colored(&grid);
    } else {
        render_calendar_heatmap_ascii(&grid);
    }
    Ok(())
}

/// Run the heatmap visualization end-to-end.
pub fn run_heatmap() -> Result<(), String> {
    run_heatmap_with_options(None, false)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::fs;
    use std::io::Write;
    use std::path::PathBuf;
    use std::process::{Command, Stdio};
    use std::time::{SystemTime, UNIX_EPOCH};
    use std::sync::{Mutex, OnceLock, MutexGuard};

    static TEST_DIR_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

    // Simple temp repo that lives under OS temp dir and is cleaned up on Drop.
    struct TempRepo {
        _guard: MutexGuard<'static, ()>,
        old_dir: PathBuf,
        path: PathBuf,
    }

    impl TempRepo {
        fn new(prefix: &str) -> Self {
            // Serialize temp repo creation and chdir to avoid races across parallel tests
            let guard = TEST_DIR_LOCK
                .get_or_init(|| Mutex::new(()))
                .lock()
                .unwrap_or_else(|e| e.into_inner());

            let old_dir = env::current_dir().unwrap();
            let base = env::temp_dir();
            let ts = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos();
            let path = base.join(format!("{}-{}", prefix, ts));
            fs::create_dir_all(&path).unwrap();
            env::set_current_dir(&path).unwrap();

            assert!(
                Command::new("git")
                    .args(["--no-pager", "init", "-q"])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .unwrap()
                    .success()
            );
            // Keep commands clean
            assert!(
                Command::new("git")
                    .args(["config", "commit.gpgsign", "false"])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .unwrap()
                    .success()
            );
            assert!(
                Command::new("git")
                    .args(["config", "core.hooksPath", "/dev/null"])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .unwrap()
                    .success()
            );
            assert!(
                Command::new("git")
                    .args(["config", "user.name", "Test"])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .unwrap()
                    .success()
            );
            assert!(
                Command::new("git")
                    .args(["config", "user.email", "test@example.com"])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .unwrap()
                    .success()
            );

            // Initial file and commit (for a valid repo)
            fs::write("INIT", "init\n").unwrap();
            let _ = Command::new("git")
                .args(["--no-pager", "add", "."])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status();

            let mut c = Command::new("git");
            c.args(["-c", "commit.gpgsign=false"])
                .arg("--no-pager")
                .arg("commit")
                .arg("--no-verify")
                .arg("-q")
                .arg("-m")
                .arg("chore: init");
            c.env("GIT_AUTHOR_NAME", "Init");
            c.env("GIT_AUTHOR_EMAIL", "init@example.com");
            c.env("GIT_COMMITTER_NAME", "Init");
            c.env("GIT_COMMITTER_EMAIL", "init@example.com");
            c.stdout(Stdio::null()).stderr(Stdio::null());
            assert!(c.status().unwrap().success());

            Self { _guard: guard, old_dir, path }
        }

        fn commit_with_epoch(&self, name: &str, email: &str, file: &str, content: &str, ts: u64) {
            // write/append file
            let mut f = fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(file)
                .unwrap();
            f.write_all(content.as_bytes()).unwrap();

            // add and commit with explicit dates
            let add_ok = Command::new("git")
                .args(["add", "."])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .map(|s| s.success())
                .unwrap_or(false)
                || Command::new("git")
                    .args(["add", "-A", "."])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .map(|s| s.success())
                    .unwrap_or(false);
            assert!(add_ok, "git add failed in TempRepo::commit_with_epoch");

            let mut c = Command::new("git");
            c.args(["-c", "commit.gpgsign=false"])
                .args(["-c", "core.hooksPath=/dev/null"])
                .args(["-c", "user.name=Test"])
                .args(["-c", "user.email=test@example.com"])
                .arg("commit")
                .arg("--no-verify")
                .arg("-q")
                .arg("--allow-empty")
                .arg("-m")
                .arg("test");
            let date = format!("{ts} +0000");
            c.env("GIT_AUTHOR_NAME", name);
            c.env("GIT_AUTHOR_EMAIL", email);
            c.env("GIT_COMMITTER_NAME", name);
            c.env("GIT_COMMITTER_EMAIL", email);
            c.env("GIT_AUTHOR_DATE", &date);
            c.env("GIT_COMMITTER_DATE", &date);
            c.stdout(Stdio::null()).stderr(Stdio::null());
            assert!(c.status().unwrap().success());
        }
    }

    impl Drop for TempRepo {
        fn drop(&mut self) {
            let _ = env::set_current_dir(&self.old_dir);
            let _ = fs::remove_dir_all(&self.path);
        }
    }

    #[test]
    fn test_compute_timeline_weeks_simple_bins() {
        // Choose a fixed "now" and create timestamps in two recent weeks.
        let week = 604_800u64;
        let now = 10 * week; // arbitrary multiple
        let ts = vec![
            now - (0 * week) + 1, // this week
            now - (1 * week) + 2, // last week
            now - (1 * week) + 3, // last week
            now - (3 * week),     // 3 weeks ago
        ];
        let counts = compute_timeline_weeks(&ts, 4, now);
        // oldest -> newest bins: weeks=4 => [3w,2w,1w,0w]
        // 3w: 1, 2w:0, 1w:2, 0w:1
        assert_eq!(counts, vec![1, 0, 2, 1]);
    }

    #[test]
    fn test_compute_heatmap_utc_known_points() {
        // 1970-01-04 00:00:00 UTC is a Sunday 00h -> index 0, hour 0
        let sun_00 = 3 * 86_400;
        // 1970-01-04 13:00:00 UTC Sunday 13h
        let sun_13 = sun_00 + 13 * 3_600;
        // 1970-01-05 05:00:00 UTC Monday 05h -> day=4 -> ((4+4)%7)=1 (Mon)
        let mon_05 = 4 * 86_400 + 5 * 3_600;
        let grid = compute_heatmap_utc(&[sun_00, sun_13, mon_05]);
        assert_eq!(grid[0][0], 1);  // Sun 00
        assert_eq!(grid[0][13], 1); // Sun 13
        assert_eq!(grid[1][5], 1);  // Mon 05
    }

    #[test]
    fn test_render_timeline_no_panic() {
        render_timeline_bars(&[0, 1, 2, 3, 0, 5, 5, 1]);
        render_timeline_bars(&[]);
        render_timeline_bars(&[0, 0, 0]);
    }

    #[test]
    fn test_render_heatmap_no_panic() {
        let mut grid = [[0usize; 24]; 7];
        grid[0][0] = 1;
        grid[6][23] = 5;
        render_heatmap_ascii(grid);
    }

    #[test]
    #[ignore]
    fn test_collect_commit_timestamps_from_temp_repo() {
        // Create one temp repo and keep it the current working directory
        // while collecting timestamps.
        let repo = TempRepo::new("git-insights-vis");
        // two commits with known epochs
        let t1 = 1_696_118_400u64; // 2023-10-01 00:00:00 UTC
        let t2 = 1_696_204_800u64; // 2023-10-02 00:00:00 UTC

        // Make commits in this repo
        repo.commit_with_epoch("Alice", "alice@example.com", "a.txt", "a\n", t1);
        repo.commit_with_epoch("Bob", "bob@example.com", "a.txt", "b\n", t2);

        // Validate via our collector (runs in CWD = repo)
        let ts = collect_commit_timestamps().expect("collect timestamps");
        assert!(ts.iter().any(|&x| x == t1), "missing t1");
        assert!(ts.iter().any(|&x| x == t2), "missing t2");
    }

    #[test]
    #[ignore]
    fn test_run_timeline_and_heatmap_end_to_end() {
        // Create a repo and ensure both runners do not error.
        let repo = TempRepo::new("git-insights-vis-run");
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let t_now = now - (now % 86_400); // align to midnight
        repo.commit_with_epoch("X", "x@example.com", "x.txt", "x\n", t_now);
        repo.commit_with_epoch("Y", "y@example.com", "x.txt", "y\n", t_now + 3_600);

        // These call git under the hood; should be fine
        run_timeline(4).expect("timeline ok");
        run_heatmap().expect("heatmap ok");
    }

    #[test]
    fn test_compute_calendar_heatmap_bins() {
        // Use a synthetic "now" for stable alignment
        const DAY: u64 = 86_400;
        const WEEK: u64 = 7 * DAY;
        let now = 10 * WEEK;

        // aligned_end computed same as production logic
        let start_of_week = now - (now % WEEK);
        let aligned_end = start_of_week + WEEK - 1;

        // Place 2 commits in current week, 1 commit in previous week
        let t_curr1 = aligned_end - (1 * DAY); // within current week
        let t_curr2 = aligned_end - (2 * DAY);
        let t_prev1 = aligned_end - (8 * DAY); // previous week
        let ts = vec![t_curr1, t_curr2, t_prev1];

        let grid = super::compute_calendar_heatmap(&ts, 2, now);
        assert_eq!(grid.len(), 7);
        assert_eq!(grid[0].len(), 2);

        // Sum per column: col 0 = older week, col 1 = current week
        let mut col0 = 0usize;
        let mut col1 = 0usize;
        for r in 0..7 {
            col0 += grid[r][0];
            col1 += grid[r][1];
        }
        assert_eq!(col0, 1, "older week should have 1 commit");
        assert_eq!(col1, 2, "current week should have 2 commits");
    }

    #[test]
    fn test_render_calendar_heatmap_no_panic() {
        // Build a small 7 x 4 grid with increasing intensity
        let mut grid = vec![vec![0usize; 4]; 7];
        grid[0][0] = 1;
        grid[1][1] = 2;
        grid[2][2] = 3;
        grid[3][3] = 4;
        // Should not panic in ASCII
        super::render_calendar_heatmap_ascii(&grid);
        // Should not panic in "colored" version (uses ANSI)
        super::render_calendar_heatmap_colored(&grid);
    }

    #[test]
    fn test_print_legends_no_panic() {
        super::print_ramp_legend(false, "commits/week");
        super::print_ramp_legend(true, "commits/day");
    }
}