vibe_coding_tracker 0.5.16

Vibe Coding Tracker - AI coding assistant telemetry/usage parser, aggregate JSONL events into CodeAnalysis results
Documentation
// Unit tests for utils/directory.rs
//
// Tests directory traversal and file filtering utilities

use std::fs::{self, File};
use std::io::Write;
use tempfile::tempdir;
use vibe_coding_tracker::cli::TimeRange;
use vibe_coding_tracker::utils::directory::{
    collect_files_with_dates, is_claude_session_file, is_gemini_chat_file, is_json_file,
};

#[test]
fn test_is_json_file_jsonl() {
    // Test JSONL file extension
    let path = std::path::Path::new("test.jsonl");
    assert!(is_json_file(path));
}

#[test]
fn test_is_json_file_json() {
    // Test JSON file extension
    let path = std::path::Path::new("test.json");
    assert!(is_json_file(path));
}

#[test]
fn test_is_json_file_txt() {
    // Test non-JSON file extension
    let path = std::path::Path::new("test.txt");
    assert!(!is_json_file(path));
}

#[test]
fn test_is_json_file_no_extension() {
    // Test file without extension
    let path = std::path::Path::new("test");
    assert!(!is_json_file(path));
}

#[test]
fn test_is_json_file_uppercase() {
    // Test uppercase extension
    let path = std::path::Path::new("test.JSON");
    assert!(!is_json_file(path)); // Case-sensitive
}

#[test]
fn test_is_gemini_chat_file_valid() {
    // Test valid Gemini chat file
    let path = std::path::Path::new("/home/user/.gemini/tmp/hash/chats/chat.json");
    assert!(is_gemini_chat_file(path));
}

#[test]
fn test_is_gemini_chat_file_wrong_parent() {
    // Test file not in chats directory
    let path = std::path::Path::new("/home/user/.gemini/tmp/hash/other/file.json");
    assert!(!is_gemini_chat_file(path));
}

#[test]
fn test_is_gemini_chat_file_wrong_extension() {
    // Test file in chats directory but wrong extension
    let path = std::path::Path::new("/home/user/.gemini/tmp/hash/chats/file.txt");
    assert!(!is_gemini_chat_file(path));
}

#[test]
fn test_is_gemini_chat_file_no_parent() {
    // Test file without parent
    let path = std::path::Path::new("file.json");
    assert!(!is_gemini_chat_file(path));
}

#[test]
fn test_collect_files_with_dates_empty_dir() {
    // Test collecting files from empty directory
    let dir = tempdir().unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 0);
}

#[test]
fn test_collect_files_with_dates_nonexistent_dir() {
    // Test collecting files from non-existent directory
    let results =
        collect_files_with_dates("/nonexistent/path", is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 0);
}

#[test]
fn test_collect_files_with_dates_with_files() {
    // Test collecting JSON files from directory
    let dir = tempdir().unwrap();

    // Create some JSON files
    File::create(dir.path().join("file1.json")).unwrap();
    File::create(dir.path().join("file2.jsonl")).unwrap();
    File::create(dir.path().join("file3.txt")).unwrap(); // Should be filtered out

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 2);

    // Check that date fields are set
    for file_info in &results {
        assert!(!file_info.modified_date.is_empty());
        assert!(file_info.modified_date.contains('-')); // Should be YYYY-MM-DD format
    }
}

#[test]
fn test_collect_files_with_dates_nested_directories() {
    // Test collecting files from nested directories
    let dir = tempdir().unwrap();

    // Create nested structure
    fs::create_dir_all(dir.path().join("subdir1")).unwrap();
    fs::create_dir_all(dir.path().join("subdir2")).unwrap();

    File::create(dir.path().join("file1.json")).unwrap();
    File::create(dir.path().join("subdir1/file2.json")).unwrap();
    File::create(dir.path().join("subdir2/file3.jsonl")).unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 3);
}

#[test]
fn test_collect_files_with_dates_filter_function() {
    // Test that filter function works correctly
    let dir = tempdir().unwrap();

    File::create(dir.path().join("file1.json")).unwrap();
    File::create(dir.path().join("file2.jsonl")).unwrap();
    File::create(dir.path().join("file3.txt")).unwrap();

    // Custom filter: only .txt files
    let results = collect_files_with_dates(
        dir.path(),
        |p| p.extension().is_some_and(|e| e == "txt"),
        TimeRange::All,
    )
    .unwrap();

    assert_eq!(results.len(), 1);
}

#[test]
fn test_collect_files_with_dates_no_matching_files() {
    // Test when no files match filter
    let dir = tempdir().unwrap();

    File::create(dir.path().join("file1.txt")).unwrap();
    File::create(dir.path().join("file2.md")).unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 0);
}

#[test]
fn test_file_info_path() {
    // Test that FileInfo contains correct paths
    let dir = tempdir().unwrap();
    let file_path = dir.path().join("test.json");
    File::create(&file_path).unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].path, file_path);
}

#[test]
fn test_file_info_date_format() {
    // Test that date format is YYYY-MM-DD
    let dir = tempdir().unwrap();
    File::create(dir.path().join("test.json")).unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 1);

    let date = &results[0].modified_date;
    assert_eq!(date.len(), 10); // YYYY-MM-DD is 10 chars
    assert_eq!(date.chars().filter(|&c| c == '-').count(), 2); // Two dashes
}

#[test]
fn test_collect_files_ignores_directories() {
    // Test that directories are not included in results
    let dir = tempdir().unwrap();

    // Create a directory with .json in name
    fs::create_dir(dir.path().join("test.json")).unwrap();

    // Create an actual file
    File::create(dir.path().join("real.json")).unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 1); // Only the file, not the directory
}

#[test]
fn test_is_json_file_with_dots_in_name() {
    // Test files with dots in name
    let path = std::path::Path::new("my.test.file.json");
    assert!(is_json_file(path));

    let path2 = std::path::Path::new("my.test.file.jsonl");
    assert!(is_json_file(path2));
}

#[test]
fn test_is_gemini_chat_file_multiple_levels() {
    // Test with multiple directory levels
    let path = std::path::Path::new("/a/b/c/d/chats/file.json");
    assert!(is_gemini_chat_file(path));
}

#[test]
fn test_collect_files_with_content() {
    // Test that files with content are collected
    let dir = tempdir().unwrap();
    let file_path = dir.path().join("test.json");

    let mut file = File::create(&file_path).unwrap();
    writeln!(file, r#"{{"key": "value"}}"#).unwrap();

    let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
    assert_eq!(results.len(), 1);
    assert!(results[0].path.exists());
}

#[test]
fn test_is_json_file_excludes_meta_sidecars() {
    // *.meta.json sidecars live next to Claude Code subagent sessions and must not
    // be picked up by the generic json filter (they would otherwise be parsed and
    // mis-detected as Codex logs).
    let path = std::path::Path::new("agent-afda1991051a0eb93.meta.json");
    assert!(!is_json_file(path));

    let nested =
        std::path::Path::new("/home/user/.claude/projects/proj/sess/subagents/agent-x.meta.json");
    assert!(!is_json_file(nested));

    // Pre-emptive defense: reject `.meta.jsonl` too, in case Claude Code ever
    // switches the sidecar format to line-delimited JSON.
    let meta_jsonl =
        std::path::Path::new("/home/user/.claude/projects/proj/sess/subagents/agent-x.meta.jsonl");
    assert!(!is_json_file(meta_jsonl));
}

#[test]
fn test_is_claude_session_file_accepts_jsonl() {
    // Top-level Claude session logs
    let top_level = std::path::Path::new("/home/user/.claude/projects/proj/sess.jsonl");
    assert!(is_claude_session_file(top_level));

    // Subagent session logs sit one extra level deeper
    let subagent = std::path::Path::new(
        "/home/user/.claude/projects/proj/sess/subagents/agent-afda1991051a0eb93.jsonl",
    );
    assert!(is_claude_session_file(subagent));
}

#[test]
fn test_is_claude_session_file_rejects_non_jsonl() {
    // Metadata sidecars — current format (`.meta.json`)
    let meta = std::path::Path::new(
        "/home/user/.claude/projects/proj/sess/subagents/agent-afda1991051a0eb93.meta.json",
    );
    assert!(!is_claude_session_file(meta));

    // Metadata sidecars — hypothetical future format (`.meta.jsonl`). Has `.jsonl`
    // extension, so without the sidecar check it would slip through.
    let meta_jsonl = std::path::Path::new(
        "/home/user/.claude/projects/proj/sess/subagents/agent-afda1991051a0eb93.meta.jsonl",
    );
    assert!(!is_claude_session_file(meta_jsonl));

    // Plain .json files (not something Claude Code writes in this tree)
    let plain_json = std::path::Path::new("/home/user/.claude/projects/proj/notes.json");
    assert!(!is_claude_session_file(plain_json));

    // Other pasted artifacts
    let image = std::path::Path::new("/home/user/.claude/projects/proj/sess/paste.png");
    assert!(!is_claude_session_file(image));
}

#[test]
fn test_collect_claude_session_files_includes_subagents() {
    // Simulates the `~/.claude/projects/<project>/<session>/subagents/` layout:
    //   projects/<project>/
    //     sess-a.jsonl                          <- top-level session
    //     sess-a/subagents/agent-one.jsonl      <- subagent session
    //     sess-a/subagents/agent-one.meta.json  <- metadata sidecar (ignored)
    //     sess-a/screenshot.png                 <- pasted artifact (ignored)
    let dir = tempdir().unwrap();
    let project = dir.path().join("-home-user-repo");
    let session_subdir = project.join("sess-a");
    let subagents = session_subdir.join("subagents");
    fs::create_dir_all(&subagents).unwrap();

    File::create(project.join("sess-a.jsonl")).unwrap();
    File::create(subagents.join("agent-one.jsonl")).unwrap();
    File::create(subagents.join("agent-one.meta.json")).unwrap();
    File::create(subagents.join("agent-two.meta.jsonl")).unwrap();
    File::create(session_subdir.join("screenshot.png")).unwrap();

    let results =
        collect_files_with_dates(dir.path(), is_claude_session_file, TimeRange::All).unwrap();
    let names: Vec<String> = results
        .iter()
        .filter_map(|f| f.path.file_name()?.to_str().map(String::from))
        .collect();

    assert_eq!(results.len(), 2, "collected: {:?}", names);
    assert!(names.contains(&"sess-a.jsonl".to_string()));
    assert!(names.contains(&"agent-one.jsonl".to_string()));
    assert!(!names.iter().any(|n| n.contains(".meta.")));
    assert!(!names.iter().any(|n| n.ends_with(".png")));
}