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() {
let path = std::path::Path::new("test.jsonl");
assert!(is_json_file(path));
}
#[test]
fn test_is_json_file_json() {
let path = std::path::Path::new("test.json");
assert!(is_json_file(path));
}
#[test]
fn test_is_json_file_txt() {
let path = std::path::Path::new("test.txt");
assert!(!is_json_file(path));
}
#[test]
fn test_is_json_file_no_extension() {
let path = std::path::Path::new("test");
assert!(!is_json_file(path));
}
#[test]
fn test_is_json_file_uppercase() {
let path = std::path::Path::new("test.JSON");
assert!(!is_json_file(path)); }
#[test]
fn test_is_gemini_chat_file_valid() {
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() {
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() {
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() {
let path = std::path::Path::new("file.json");
assert!(!is_gemini_chat_file(path));
}
#[test]
fn test_collect_files_with_dates_empty_dir() {
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() {
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() {
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();
let results = collect_files_with_dates(dir.path(), is_json_file, TimeRange::All).unwrap();
assert_eq!(results.len(), 2);
for file_info in &results {
assert!(!file_info.modified_date.is_empty());
assert!(file_info.modified_date.contains('-')); }
}
#[test]
fn test_collect_files_with_dates_nested_directories() {
let dir = tempdir().unwrap();
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() {
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();
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() {
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() {
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() {
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); assert_eq!(date.chars().filter(|&c| c == '-').count(), 2); }
#[test]
fn test_collect_files_ignores_directories() {
let dir = tempdir().unwrap();
fs::create_dir(dir.path().join("test.json")).unwrap();
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); }
#[test]
fn test_is_json_file_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() {
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() {
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() {
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));
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() {
let top_level = std::path::Path::new("/home/user/.claude/projects/proj/sess.jsonl");
assert!(is_claude_session_file(top_level));
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() {
let meta = std::path::Path::new(
"/home/user/.claude/projects/proj/sess/subagents/agent-afda1991051a0eb93.meta.json",
);
assert!(!is_claude_session_file(meta));
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));
let plain_json = std::path::Path::new("/home/user/.claude/projects/proj/notes.json");
assert!(!is_claude_session_file(plain_json));
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() {
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")));
}