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_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());
}