#![warn(clippy::all, clippy::pedantic)]
use crate::caption::{format_text_content, replace_special_chars, replace_string};
use crate::process_json_to_caption;
use serde_json::json;
use tempfile::TempDir;
use tokio::fs;
#[test]
fn test_format_text_content() -> anyhow::Result<()> {
let text = "This has too many spaces";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "This has too many spaces");
let text = "Line 1\nLine 2\n\nLine 3";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "Line 1 Line 2 Line 3");
let text = "Text\twith\ttabs";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "Text with tabs");
let text = " \t Text with spaces around \n ";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "Text with spaces around");
let text = " Mixed \t spacing \n and \r newlines ";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "Mixed spacing and newlines");
let text = "";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "");
let text = " \t \n ";
let formatted = format_text_content(text)?;
assert_eq!(formatted, "");
Ok(())
}
#[tokio::test]
async fn test_replace_string() -> anyhow::Result<()> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test_replace.txt");
let original_content = "This is a test string. This string should be replaced.";
fs::write(&file_path, original_content).await?;
replace_string(&file_path, "test", "sample").await?;
let content = fs::read_to_string(&file_path).await?;
assert_eq!(
content,
"This is a sample string. This string should be replaced."
);
replace_string(&file_path, "string", "text").await?;
let content = fs::read_to_string(&file_path).await?;
assert_eq!(
content,
"This is a sample text. This text should be replaced."
);
replace_string(&file_path, "This is a ", "").await?;
let content = fs::read_to_string(&file_path).await?;
assert_eq!(content, "sample text. This text should be replaced.");
let before = fs::read_to_string(&file_path).await?;
replace_string(&file_path, "nonexistent", "replacement").await?;
let after = fs::read_to_string(&file_path).await?;
assert_eq!(before, after);
let before = fs::read_to_string(&file_path).await?;
replace_string(&file_path, "", "replacement").await?;
let after = fs::read_to_string(&file_path).await?;
assert_eq!(before, after);
Ok(())
}
#[tokio::test]
async fn test_replace_special_chars() -> anyhow::Result<()> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test_special_chars.txt");
let special_chars_content = "Text with 'smart quotes' and \"double quotes\".";
fs::write(&file_path, special_chars_content).await?;
replace_special_chars(file_path.clone()).await?;
let content = fs::read_to_string(&file_path).await?;
assert_eq!(content, "Text with 'smart quotes' and \"double quotes\".");
let no_special_chars = "Text with regular 'quotes' and \"quotes\".";
fs::write(&file_path, no_special_chars).await?;
let metadata_before = fs::metadata(&file_path).await?;
let modified_before = metadata_before.modified()?;
replace_special_chars(file_path.clone()).await?;
let metadata_after = fs::metadata(&file_path).await?;
let modified_after = metadata_after.modified()?;
let content = fs::read_to_string(&file_path).await?;
assert_eq!(content, "Text with regular 'quotes' and \"quotes\".");
let diff = modified_after
.duration_since(modified_before)
.unwrap_or_default();
assert!(diff.as_secs() < 1, "File should not have been modified");
Ok(())
}
#[tokio::test]
async fn test_replace_string_formatting() -> anyhow::Result<()> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test_formatting.txt");
let content = " This has too many spaces \n\n and newlines ";
fs::write(&file_path, content).await?;
replace_string(&file_path, "too many", "").await?;
let result = fs::read_to_string(&file_path).await?;
assert_eq!(result, "This has spaces and newlines");
Ok(())
}
#[tokio::test]
async fn test_replace_special_chars_complex() -> anyhow::Result<()> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("complex_special_chars.txt");
let content = "Here's a mix of 'smart quotes', \"double quotes\" and regular quotes: 'normal', \"normal\".";
fs::write(&file_path, content).await?;
replace_special_chars(file_path.clone()).await?;
let result = fs::read_to_string(&file_path).await?;
assert_eq!(
result,
"Here's a mix of 'smart quotes', \"double quotes\" and regular quotes: 'normal', \"normal\"."
);
Ok(())
}
#[tokio::test]
async fn test_integration_text_processing() -> anyhow::Result<()> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("integration_test.txt");
let content = " This text has 'smart quotes' and excess \n\n whitespace.";
fs::write(&file_path, content).await?;
replace_string(&file_path, "text", "document").await?;
replace_special_chars(file_path.clone()).await?;
replace_string(&file_path, "This document has ", "").await?;
let result = fs::read_to_string(&file_path).await?;
assert_eq!(result, "'smart quotes' and excess whitespace.");
Ok(())
}
#[tokio::test]
async fn test_process_json_to_caption() -> anyhow::Result<()> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test_tags.json");
let json_data = json!({
"tag1": 0.9,
"tag2": 0.5,
"tag3": 0.1, "tag (with parens)": 0.8
});
fs::write(&file_path, serde_json::to_string_pretty(&json_data)?).await?;
process_json_to_caption(&file_path).await?;
let caption_path = file_path.with_extension("txt");
let content = fs::read_to_string(&caption_path).await?;
assert_eq!(content, "tag1, tag \\(with parens\\), tag2");
Ok(())
}