use std::{
fs::File,
io::{BufRead, BufReader, Write},
path::PathBuf,
};
use tiny_dc::index::DirectoryIndex;
#[test]
fn directory_index_z_returns_correct_result() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_test_dir = temp_dir.path().join("test_dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let temp_test_dir_other = temp_dir.path().join("test_dir_other");
std::fs::create_dir_all(&temp_test_dir_other).unwrap();
let temp_other_dir = temp_dir.path().join("other_dir");
std::fs::create_dir_all(&temp_other_dir).unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut file = File::create(&index_file_path).unwrap();
let mock_data = vec![
(temp_test_dir.to_str().unwrap(), 100, 100),
(temp_test_dir_other.to_str().unwrap(), 150, 100),
(temp_other_dir.to_str().unwrap(), 100, 100),
];
for line in mock_data {
writeln!(file, "{}|{}|{}\n", line.0, line.1, line.2).unwrap();
}
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index.z(vec!["test".into()]).unwrap();
assert_eq!(result, Some(temp_test_dir_other.to_str().unwrap().into()));
}
#[test]
fn directory_index_z_returns_correct_result_for_case_sensitive_query() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_test_dir = temp_dir.path().join("Test_Dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let temp_test_dir_other = temp_dir.path().join("test_dir_other");
std::fs::create_dir_all(&temp_test_dir_other).unwrap();
let temp_other_dir = temp_dir.path().join("other_dir");
std::fs::create_dir_all(&temp_other_dir).unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut file = File::create(&index_file_path).unwrap();
let mock_data = vec![
(temp_test_dir.to_str().unwrap(), 100, 100),
(temp_test_dir_other.to_str().unwrap(), 150, 100),
(temp_other_dir.to_str().unwrap(), 100, 100),
];
for line in mock_data {
writeln!(file, "{}|{}|{}\n", line.0, line.1, line.2).unwrap();
}
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index.z(vec!["Test".into()]).unwrap();
assert_eq!(result, Some(temp_test_dir.to_str().unwrap().into()));
}
#[test]
fn directory_index_z_returns_correct_result_for_multiple_queries() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_test_dir = temp_dir.path().join("test_dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let temp_test_dir_other = temp_dir.path().join("test_dir_other");
std::fs::create_dir_all(&temp_test_dir_other).unwrap();
let temp_other_dir = temp_dir.path().join("other_dir");
std::fs::create_dir_all(&temp_other_dir).unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut file = File::create(&index_file_path).unwrap();
let mock_data = vec![
(temp_test_dir.to_str().unwrap(), 100, 100),
(temp_test_dir_other.to_str().unwrap(), 150, 100),
(temp_other_dir.to_str().unwrap(), 100, 100),
];
for line in mock_data {
writeln!(file, "{}|{}|{}\n", line.0, line.1, line.2).unwrap();
}
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index
.z(vec!["other".into(), "test".into()])
.unwrap();
assert_eq!(result, Some(temp_test_dir_other.to_str().unwrap().into()));
}
#[test]
fn directory_index_z_returns_existing_path_only() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_test_dir = temp_dir.path().join("test_dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let temp_test_dir_other = temp_dir.path().join("test_dir_other");
std::fs::create_dir_all(&temp_test_dir_other).unwrap();
let temp_other_dir = temp_dir.path().join("other_dir");
std::fs::create_dir_all(&temp_other_dir).unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut file = File::create(&index_file_path).unwrap();
let mock_data = vec![
(temp_test_dir.to_str().unwrap(), 100, 100),
(temp_test_dir_other.to_str().unwrap(), 150, 100),
(temp_other_dir.to_str().unwrap(), 100, 100),
];
std::fs::remove_dir_all(&temp_test_dir_other).unwrap();
for line in mock_data {
writeln!(file, "{}|{}|{}\n", line.0, line.1, line.2).unwrap();
}
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index.z(vec!["test".into()]).unwrap();
assert_eq!(result, Some(temp_test_dir.to_str().unwrap().into()));
}
#[test]
fn directory_index_z_returns_none_for_no_match() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_test_dir = temp_dir.path().join("test_dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let temp_test_dir_other = temp_dir.path().join("test_dir_other");
std::fs::create_dir_all(&temp_test_dir_other).unwrap();
let temp_other_dir = temp_dir.path().join("other_dir");
std::fs::create_dir_all(&temp_other_dir).unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut file = File::create(&index_file_path).unwrap();
let mock_data = vec![
(temp_test_dir.to_str().unwrap(), 100, 100),
(temp_test_dir_other.to_str().unwrap(), 150, 100),
(temp_other_dir.to_str().unwrap(), 100, 100),
];
for line in mock_data {
writeln!(file, "{}|{}|{}\n", line.0, line.1, line.2).unwrap();
}
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index.z(vec!["non-existent".into()]).unwrap();
assert_eq!(result, None);
}
#[test]
fn directory_index_z_returns_correct_result_for_common_parent() {
let temp_dir = tempfile::tempdir().unwrap();
let common_parent = temp_dir.path().join("common_parent");
std::fs::create_dir_all(&common_parent).unwrap();
let child_dir = common_parent.join("child");
std::fs::create_dir_all(&child_dir).unwrap();
let nested_dir = child_dir.join("nested");
std::fs::create_dir_all(&nested_dir).unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut file = File::create(&index_file_path).unwrap();
let mock_data = vec![
(common_parent.to_str().unwrap(), 100, 100), (child_dir.to_str().unwrap(), 200, 200), (nested_dir.to_str().unwrap(), 300, 300), ];
for line in mock_data {
writeln!(file, "{}|{}|{}", line.0, line.1, line.2).unwrap();
}
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index.z(vec!["common".into()]).unwrap();
assert_eq!(result, Some(common_parent.to_str().unwrap().into()),);
}
#[test]
fn directory_index_z_returns_none_for_empty_index() {
let temp_dir = tempfile::tempdir().unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
let result = directory_index.z(vec!["nonexistent".into()]).unwrap();
assert_eq!(result, None);
}
#[test]
fn directory_index_push_creates_index_file() {
let temp_dir = tempfile::tempdir().unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let temp_test_dir = temp_dir.path().join("test_dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
directory_index.push(temp_test_dir.clone()).unwrap();
assert!(index_file_path.exists());
let file = File::open(&index_file_path).unwrap();
let reader = BufReader::new(file);
let lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect();
let line = &lines[0];
let parts: Vec<&str> = line.split('|').collect();
assert_eq!(parts.len(), 3);
assert_eq!(parts[0], temp_test_dir.to_str().unwrap());
}
fn get_index_file_lines(index_file_path: &PathBuf) -> Vec<String> {
let file = File::open(index_file_path).unwrap();
let reader = BufReader::new(file);
reader.lines().map(|line| line.unwrap()).collect()
}
#[test]
fn directory_index_push_multiple_times_updates_entry_rank() {
let temp_dir = tempfile::tempdir().unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let temp_test_dir = temp_dir.path().join("test_dir");
std::fs::create_dir_all(&temp_test_dir).unwrap();
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
directory_index.push(temp_test_dir.clone()).unwrap();
let lines = get_index_file_lines(&index_file_path);
let line = &lines[0];
let parts: Vec<&str> = line.split('|').collect();
assert_eq!(parts.len(), 3);
assert_eq!(parts[1], "0");
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
directory_index.push(temp_test_dir.clone()).unwrap();
let lines = get_index_file_lines(&index_file_path);
let line = &lines[0];
let parts: Vec<&str> = line.split('|').collect();
assert_eq!(parts.len(), 3);
assert_eq!(parts[1], "1");
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
directory_index.push(temp_test_dir.clone()).unwrap();
let lines = get_index_file_lines(&index_file_path);
let line = &lines[0];
let parts: Vec<&str> = line.split('|').collect();
assert_eq!(parts.len(), 3);
assert_eq!(parts[1], "1.99");
}
#[test]
fn directory_index_push_non_existent_path_does_is_a_no_op() {
let temp_dir = tempfile::tempdir().unwrap();
let index_file_path = temp_dir.path().join(".tiny-dc");
let mut directory_index = DirectoryIndex::try_from(index_file_path.clone()).unwrap();
directory_index
.push(PathBuf::from("/non/existent/path"))
.unwrap();
assert!(index_file_path.exists());
let lines = get_index_file_lines(&index_file_path);
assert_eq!(lines.len(), 0);
}