vibestats 2.3.1

CLI that syncs Claude Code and Codex session activity to a private GitHub repo and renders a profile heatmap + analytics dashboard.
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
//! Logger module for vibestats.
//!
//! Appends structured log entries to `~/.config/vibestats/vibestats.log`.
//! Rotates the file at 1 MB (renames to `vibestats.log.1`).
//!
//! # Silent failure contract
//!
//! All IO errors are silently discarded. This module **never** writes to
//! stdout or stderr, and **never** panics. It is designed to be called from
//! Claude Code hook hot-paths where any terminal output would break the hook
//! protocol (NFR10, NFR11).

use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

// ─── Path helpers ─────────────────────────────────────────────────────────────

fn log_path() -> PathBuf {
    let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
    PathBuf::from(home)
        .join(".config")
        .join("vibestats")
        .join("vibestats.log")
}

// ─── Timestamp (stdlib only, no chrono) ───────────────────────────────────────

/// Convert Unix epoch seconds to `(year, month, day, hour, minute, second)`.
///
/// Uses a straightforward day/month/year extraction without external crates.
fn epoch_to_datetime(secs: u64) -> (u64, u64, u64, u64, u64, u64) {
    let s = secs % 60;
    let total_min = secs / 60;
    let min = total_min % 60;
    let total_hours = total_min / 60;
    let h = total_hours % 24;
    let mut days = total_hours / 24; // days since 1970-01-01

    // Determine the year by iterating through years from 1970.
    let mut year: u64 = 1970;
    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;
    }

    // Determine the month.
    let leap = is_leap_year(year);
    let month_days: [u64; 12] = [
        31,
        if leap { 29 } else { 28 },
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31,
    ];
    let mut month: u64 = 1;
    for &md in &month_days {
        if days < md {
            break;
        }
        days -= md;
        month += 1;
    }

    let day = days + 1; // 1-indexed
    (year, month, day, h, min, s)
}

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

fn utc_timestamp() -> String {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let (y, mo, d, h, min, s) = epoch_to_datetime(secs);
    format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", y, mo, d, h, min, s)
}

// ─── Rotation ─────────────────────────────────────────────────────────────────

const MAX_LOG_BYTES: u64 = 1_048_576; // 1 MB

fn rotate_if_needed(log_path: &Path) {
    if let Ok(meta) = std::fs::metadata(log_path) {
        if meta.len() >= MAX_LOG_BYTES {
            // Derive the rotated path by appending `.1` to the full file name
            // (e.g. `vibestats.log` -> `vibestats.log.1`). This preserves the
            // original file name rather than hard-coding `vibestats.log.1`,
            // so the function is correct for any log path the caller supplies.
            let rotated = match (log_path.parent(), log_path.file_name()) {
                (Some(parent), Some(name)) => {
                    let mut rotated_name = name.to_os_string();
                    rotated_name.push(".1");
                    parent.join(rotated_name)
                }
                _ => return,
            };
            let _ = std::fs::rename(log_path, rotated); // ignore errors
        }
    }
}

// ─── Core write helper (accepts path — enables testability) ───────────────────

fn write_log_entry(log_path: &Path, level: &str, message: &str) -> std::io::Result<()> {
    use std::io::Write;

    // Ensure the parent directory exists.
    if let Some(parent) = log_path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }

    rotate_if_needed(log_path);

    let line = format!("{} {} {}\n", utc_timestamp(), level, message);

    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(log_path)?;

    file.write_all(line.as_bytes())?;
    Ok(())
}

// ─── Public API ───────────────────────────────────────────────────────────────

/// Write a structured log entry to `~/.config/vibestats/vibestats.log`.
///
/// All IO errors are silently discarded. Nothing is written to stdout or
/// stderr under any circumstances.
pub fn log(level: &str, message: &str) {
    let _ = write_log_entry(&log_path(), level, message);
}

/// Convenience wrapper: log at INFO level.
#[allow(dead_code)] // intentionally kept: public API convenience wrapper
pub fn info(message: &str) {
    log("INFO", message);
}

/// Convenience wrapper: log at ERROR level.
pub fn error(message: &str) {
    log("ERROR", message);
}

/// Convenience wrapper: log at WARN level.
#[allow(dead_code)] // intentionally kept: public API convenience wrapper
pub fn warn(message: &str) {
    log("WARN", message);
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::io::Write;

    /// Helper: write a log entry to a caller-supplied path (for test isolation).
    fn write_entry_to(path: &Path, level: &str, msg: &str) {
        let _ = write_log_entry(path, level, msg);
    }

    fn unique_test_dir(name: &str) -> PathBuf {
        // Use process ID + test name to avoid cross-test collisions.
        // Also clean any leftover state from a previous failed run so
        // tests that assert on file contents (append order, rotation)
        // are not polluted by stale files.
        let dir =
            std::env::temp_dir().join(format!("vibestats_test_{}_{}", name, std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        dir
    }

    // ── AC #1: log entry format ───────────────────────────────────────────────

    /// Parse a log line and verify it matches `YYYY-MM-DDTHH:MM:SSZ LEVEL message`.
    fn assert_log_line_format(line: &str, expected_level: &str, expected_msg: &str) {
        // Minimum length: "1970-01-01T00:00:00Z " = 21 chars + level + " " + msg
        assert!(line.len() >= 21, "line too short: {line:?}");

        // Timestamp portion: first 20 chars
        let ts = &line[..20];
        let rest = &line[20..]; // should start with " LEVEL message"

        // Verify timestamp structure: YYYY-MM-DDTHH:MM:SSZ
        assert!(ts.ends_with('Z'), "timestamp must end with Z: {ts}");
        let chars: Vec<char> = ts.chars().collect();
        assert_eq!(chars[4], '-', "YYYY-MM sep missing");
        assert_eq!(chars[7], '-', "MM-DD sep missing");
        assert_eq!(chars[10], 'T', "date-time T sep missing");
        assert_eq!(chars[13], ':', "HH:MM sep missing");
        assert_eq!(chars[16], ':', "MM:SS sep missing");
        // All other chars must be digits
        for (i, &c) in chars.iter().enumerate() {
            if ![4, 7, 10, 13, 16, 19].contains(&i) {
                assert!(
                    c.is_ascii_digit(),
                    "expected digit at pos {i} in ts, got {c:?}"
                );
            }
        }

        // Rest should be " LEVEL message"
        let expected_rest = format!(" {} {}", expected_level, expected_msg);
        assert_eq!(rest, expected_rest, "level/message portion mismatch");
    }

    #[test]
    fn test_log_format_matches_spec() {
        let dir = unique_test_dir("format");
        let _ = fs::create_dir_all(&dir);
        let log_file = dir.join("test.log");

        write_entry_to(&log_file, "INFO", "hello world");

        let content = fs::read_to_string(&log_file).expect("log file should exist");
        let line = content.lines().next().expect("should have one line");

        assert_log_line_format(line, "INFO", "hello world");

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_log_format_ends_with_newline() {
        let dir = unique_test_dir("newline");
        let _ = fs::create_dir_all(&dir);
        let log_file = dir.join("test.log");

        write_entry_to(&log_file, "ERROR", "something went wrong");

        let content = fs::read_to_string(&log_file).expect("log file should exist");
        assert!(content.ends_with('\n'), "log entry must end with newline");

        let _ = fs::remove_dir_all(&dir);
    }

    // ── AC #1 + AC #2: append semantics ──────────────────────────────────────

    #[test]
    fn test_multiple_entries_are_appended() {
        let dir = unique_test_dir("append");
        let _ = fs::create_dir_all(&dir);
        let log_file = dir.join("test.log");

        write_entry_to(&log_file, "INFO", "first");
        write_entry_to(&log_file, "INFO", "second");
        write_entry_to(&log_file, "INFO", "third");

        let content = fs::read_to_string(&log_file).expect("log file should exist");
        let lines: Vec<&str> = content.lines().collect();

        assert_eq!(lines.len(), 3, "should have 3 log entries");
        assert!(lines[0].ends_with("first"), "first entry missing");
        assert!(lines[1].ends_with("second"), "second entry missing");
        assert!(lines[2].ends_with("third"), "third entry missing");

        let _ = fs::remove_dir_all(&dir);
    }

    // ── AC #2: rotation at exactly 1 MB ──────────────────────────────────────

    #[test]
    fn test_rotation_at_1mb_threshold() {
        let dir = unique_test_dir("rotate");
        let _ = fs::create_dir_all(&dir);
        let log_file = dir.join("vibestats.log");
        let rotated_file = dir.join("vibestats.log.1");

        // Fill the file to exactly MAX_LOG_BYTES.
        {
            let mut f = fs::OpenOptions::new()
                .create(true)
                .truncate(true)
                .write(true)
                .open(&log_file)
                .expect("should create log file");
            let filler = vec![b'x'; MAX_LOG_BYTES as usize];
            f.write_all(&filler).expect("should write filler");
        }

        // Next write should trigger rotation.
        write_entry_to(&log_file, "INFO", "after rotation");

        assert!(
            rotated_file.exists(),
            "vibestats.log.1 should exist after rotation"
        );
        assert!(
            log_file.exists(),
            "vibestats.log should be recreated after rotation"
        );

        let new_content = fs::read_to_string(&log_file).expect("new log should exist");
        assert!(
            new_content.contains("after rotation"),
            "new log should contain the post-rotation entry"
        );

        // The rotated file should NOT contain "after rotation"
        let rotated_content = fs::read_to_string(&rotated_file).expect("rotated log should exist");
        assert!(
            !rotated_content.contains("after rotation"),
            "rotated log should not contain the new entry"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_rotation_preserves_file_stem() {
        // Verify rotation derives the rotated name from the caller-supplied
        // log path rather than hard-coding `vibestats.log.1`. This uses a
        // non-default file name (`custom.log`) to prove the point.
        let dir = unique_test_dir("rotate_custom");
        let _ = fs::create_dir_all(&dir);
        let log_file = dir.join("custom.log");
        let rotated_file = dir.join("custom.log.1");

        {
            let mut f = fs::OpenOptions::new()
                .create(true)
                .truncate(true)
                .write(true)
                .open(&log_file)
                .expect("should create log file");
            let filler = vec![b'x'; MAX_LOG_BYTES as usize];
            f.write_all(&filler).expect("should write filler");
        }

        write_entry_to(&log_file, "INFO", "rotated custom");

        assert!(
            rotated_file.exists(),
            "custom.log.1 should exist after rotation (rotated name must be derived from log path)"
        );
        assert!(
            !dir.join("vibestats.log.1").exists(),
            "must not create vibestats.log.1 when source log is custom.log"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_no_rotation_below_1mb() {
        let dir = unique_test_dir("no_rotate");
        let _ = fs::create_dir_all(&dir);
        let log_file = dir.join("vibestats.log");
        let rotated_file = dir.join("vibestats.log.1");

        // File is below threshold.
        {
            let mut f = fs::OpenOptions::new()
                .create(true)
                .truncate(true)
                .write(true)
                .open(&log_file)
                .expect("should create log file");
            let filler = vec![b'x'; (MAX_LOG_BYTES - 1) as usize];
            f.write_all(&filler).expect("should write filler");
        }

        write_entry_to(&log_file, "INFO", "no rotation");

        assert!(
            !rotated_file.exists(),
            "vibestats.log.1 should NOT exist when below threshold"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    // ── epoch_to_datetime ─────────────────────────────────────────────────────

    #[test]
    fn test_epoch_to_datetime_known_values() {
        // Unix epoch itself: 1970-01-01T00:00:00Z → 0
        let (y, mo, d, h, min, s) = epoch_to_datetime(0);
        assert_eq!((y, mo, d, h, min, s), (1970, 1, 1, 0, 0, 0));

        // 2000-01-01T00:00:00Z → 946684800
        let (y, mo, d, h, min, s) = epoch_to_datetime(946_684_800);
        assert_eq!((y, mo, d, h, min, s), (2000, 1, 1, 0, 0, 0));

        // 2025-04-10T00:00:00Z → 1744243200
        let (y, mo, d, h, min, s) = epoch_to_datetime(1_744_243_200);
        assert_eq!((y, mo, d, h, min, s), (2025, 4, 10, 0, 0, 0));

        // 2026-01-01T00:00:00Z → 1767225600
        let (y, mo, d, h, min, s) = epoch_to_datetime(1_767_225_600);
        assert_eq!((y, mo, d, h, min, s), (2026, 1, 1, 0, 0, 0));

        // A time with hours/minutes/seconds: 2026-04-11T03:07:00Z
        // Days from 1970-01-01 to 2026-04-11:
        //   2026-01-01 = 1767225600 (verified above)
        //   + 100 days (Jan=31, Feb=28, Mar=31, Apr 1-11 = 10 → offset 100) * 86400
        //   + 3*3600 + 7*60
        // = 1767225600 + 8640000 + 10800 + 420 = 1775876820
        let (y, mo, d, h, min, s) = epoch_to_datetime(1_775_876_820);
        assert_eq!((y, mo, d, h, min, s), (2026, 4, 11, 3, 7, 0));
    }

    #[test]
    fn test_utc_timestamp_format() {
        let ts = utc_timestamp();
        // Should be exactly "YYYY-MM-DDTHH:MM:SSZ" = 20 chars
        assert_eq!(ts.len(), 20, "timestamp should be 20 characters: {ts}");
        assert!(ts.ends_with('Z'), "timestamp must end with Z");
        assert_eq!(&ts[4..5], "-", "separator at pos 4 should be -");
        assert_eq!(&ts[7..8], "-", "separator at pos 7 should be -");
        assert_eq!(&ts[10..11], "T", "T separator at pos 10");
        assert_eq!(&ts[13..14], ":", "colon at pos 13");
        assert_eq!(&ts[16..17], ":", "colon at pos 16");
    }

    // ── Silent failure: log path non-existent parent ──────────────────────────

    #[test]
    fn test_log_to_non_existent_parent_creates_dir() {
        let dir = unique_test_dir("mkdir");
        // Do NOT pre-create the dir — the module should create it.
        let log_file = dir.join("sub").join("vibestats.log");

        // Should not panic, and should create the file.
        let _ = write_log_entry(&log_file, "INFO", "dir created");

        assert!(
            log_file.exists(),
            "log file should be created even if parent was absent"
        );

        let _ = fs::remove_dir_all(&dir);
    }
}