232 lines
7.9 KiB
Rust
232 lines
7.9 KiB
Rust
use std::fs::{self, File};
|
||
|
||
use app_lib::catalog::FormatCatalog;
|
||
use app_lib::models::{AppPreferences, ConversionOptions, HistoryEntry, OutputDirectoryMode, ThemeMode};
|
||
use app_lib::services::{ArchiveEngine, ImageEngine};
|
||
|
||
#[test]
|
||
fn test_format_catalog_extensions() {
|
||
assert_eq!(FormatCatalog::normalize_extension("photo.JPEG"), "jpg");
|
||
assert_eq!(FormatCatalog::normalize_extension("doc.markdown"), "md");
|
||
assert_eq!(FormatCatalog::normalize_extension("graphic.jpegxl"), "jxl");
|
||
assert_eq!(FormatCatalog::normalize_extension("image.PNG"), "png");
|
||
assert_eq!(FormatCatalog::normalize_extension("video.MP4"), "mp4");
|
||
assert_eq!(FormatCatalog::normalize_extension("audio.FLAC"), "flac");
|
||
assert_eq!(FormatCatalog::normalize_extension("archive.TGZ"), "gz");
|
||
}
|
||
|
||
#[test]
|
||
fn test_format_catalog_classification() {
|
||
assert!(FormatCatalog::is_image("test.png"));
|
||
assert!(FormatCatalog::is_image("test.jxl"));
|
||
assert!(FormatCatalog::is_image("test.webp"));
|
||
assert!(!FormatCatalog::is_image("test.pdf"));
|
||
|
||
assert!(FormatCatalog::is_video("clip.mp4"));
|
||
assert!(FormatCatalog::is_video("movie.mkv"));
|
||
assert!(!FormatCatalog::is_video("song.mp3"));
|
||
|
||
assert!(FormatCatalog::is_audio("song.mp3"));
|
||
assert!(FormatCatalog::is_audio("recording.wav"));
|
||
|
||
assert!(FormatCatalog::is_pdf("document.pdf"));
|
||
assert!(FormatCatalog::is_markdown("notes.md"));
|
||
assert!(FormatCatalog::is_document("notes.md"));
|
||
assert!(FormatCatalog::is_document("sheet.xlsx"));
|
||
|
||
assert!(FormatCatalog::is_archive("backup.zip"));
|
||
assert!(FormatCatalog::is_archive("data.7z"));
|
||
}
|
||
|
||
#[test]
|
||
fn test_format_catalog_choices_markdown_includes_docx_math() {
|
||
let files = vec!["formulas.md".to_string()];
|
||
let choices = FormatCatalog::get_choices_for_files(&files, None, true);
|
||
assert!(!choices.is_empty());
|
||
assert!(choices.iter().any(|c| c.id == "docx"));
|
||
assert!(choices.iter().any(|c| c.id == "docx_math" && c.display_name == "Denklem DOCX"));
|
||
assert!(choices.iter().any(|c| c.id == "pdf"));
|
||
}
|
||
|
||
#[test]
|
||
fn test_format_catalog_choices_images() {
|
||
let files = vec!["photo.png".to_string()];
|
||
let choices = FormatCatalog::get_choices_for_files(&files, None, true);
|
||
assert!(choices.iter().any(|c| c.id == "jpg"));
|
||
assert!(choices.iter().any(|c| c.id == "webp"));
|
||
assert!(choices.iter().any(|c| c.id == "jxl"));
|
||
assert!(choices.iter().any(|c| c.id == "qr_read"));
|
||
}
|
||
|
||
#[test]
|
||
fn test_format_catalog_favorites_filter() {
|
||
let files = vec!["photo.png".to_string()];
|
||
let hidden = vec!["bmp".to_string(), "tiff".to_string()];
|
||
let choices = FormatCatalog::get_choices_for_files(&files, Some(&hidden), true);
|
||
assert!(!choices.iter().any(|c| c.id == "bmp"));
|
||
assert!(!choices.iter().any(|c| c.id == "tiff"));
|
||
assert!(choices.iter().any(|c| c.id == "jpg"));
|
||
}
|
||
|
||
#[test]
|
||
fn test_image_engine_conversion_and_resize() {
|
||
let temp_dir = std::env::temp_dir().join(format!("ld_test_img_{}", uuid::Uuid::new_v4()));
|
||
fs::create_dir_all(&temp_dir).unwrap();
|
||
|
||
let input_path = temp_dir.join("test_input.png");
|
||
// Create a 200x150 colored image
|
||
let img = image::RgbImage::from_fn(200, 150, |x, y| {
|
||
image::Rgb([(x % 255) as u8, (y % 255) as u8, 128])
|
||
});
|
||
img.save(&input_path).unwrap();
|
||
|
||
let opts = ConversionOptions {
|
||
target_width: Some(100),
|
||
target_height: Some(75),
|
||
image_quality: 85,
|
||
..Default::default()
|
||
};
|
||
|
||
// Convert to JPG
|
||
let jpg_result = ImageEngine::convert_single(
|
||
&input_path.to_string_lossy(),
|
||
"jpg",
|
||
&temp_dir,
|
||
&opts,
|
||
)
|
||
.unwrap();
|
||
|
||
assert!(jpg_result.exists());
|
||
assert_eq!(jpg_result.extension().unwrap(), "jpg");
|
||
|
||
let loaded = image::open(&jpg_result).unwrap();
|
||
assert_eq!(loaded.width(), 100);
|
||
assert_eq!(loaded.height(), 75);
|
||
|
||
// Convert to WebP
|
||
let webp_result = ImageEngine::convert_single(
|
||
&input_path.to_string_lossy(),
|
||
"webp",
|
||
&temp_dir,
|
||
&opts,
|
||
)
|
||
.unwrap();
|
||
assert!(webp_result.exists());
|
||
assert_eq!(webp_result.extension().unwrap(), "webp");
|
||
|
||
let _ = fs::remove_dir_all(temp_dir);
|
||
}
|
||
|
||
#[test]
|
||
fn test_image_unique_path_generation() {
|
||
let temp_dir = std::env::temp_dir().join(format!("ld_test_uniq_{}", uuid::Uuid::new_v4()));
|
||
fs::create_dir_all(&temp_dir).unwrap();
|
||
|
||
let file1 = temp_dir.join("output.png");
|
||
File::create(&file1).unwrap();
|
||
|
||
let unique = ImageEngine::get_unique_path(&file1, false);
|
||
assert_ne!(file1, unique);
|
||
assert!(unique.to_string_lossy().contains("output (1).png"));
|
||
|
||
let _ = fs::remove_dir_all(temp_dir);
|
||
}
|
||
|
||
#[test]
|
||
fn test_archive_engine_zip_and_slip_security() {
|
||
let temp_dir = std::env::temp_dir().join(format!("ld_test_zip_{}", uuid::Uuid::new_v4()));
|
||
let extract_dir = temp_dir.join("extracted");
|
||
fs::create_dir_all(&temp_dir).unwrap();
|
||
|
||
let file1 = temp_dir.join("sample.txt");
|
||
fs::write(&file1, "LD Converter Tauri v2 Rust Test").unwrap();
|
||
|
||
let zip_dest = temp_dir.join("test_archive.zip");
|
||
let input_paths = vec![file1.to_string_lossy().to_string()];
|
||
|
||
// 1. Create ZIP
|
||
let created = ArchiveEngine::create_zip(&input_paths, &zip_dest).unwrap();
|
||
assert!(created.exists());
|
||
assert!(fs::metadata(&created).unwrap().len() > 0);
|
||
|
||
// 2. Extract ZIP
|
||
let extracted_path = ArchiveEngine::extract_archive(
|
||
&created.to_string_lossy(),
|
||
&extract_dir,
|
||
None,
|
||
)
|
||
.unwrap();
|
||
assert!(extracted_path.exists());
|
||
|
||
let extracted_file = extract_dir.join("sample.txt");
|
||
assert!(extracted_file.exists());
|
||
let content = fs::read_to_string(extracted_file).unwrap();
|
||
assert_eq!(content, "LD Converter Tauri v2 Rust Test");
|
||
|
||
let _ = fs::remove_dir_all(temp_dir);
|
||
}
|
||
|
||
#[test]
|
||
fn test_preferences_serialization() {
|
||
let prefs = AppPreferences {
|
||
theme: ThemeMode::Dark,
|
||
output_directory_mode: OutputDirectoryMode::Desktop,
|
||
default_image_quality: 95,
|
||
start_with_windows: false,
|
||
..Default::default()
|
||
};
|
||
|
||
let json = serde_json::to_string(&prefs).unwrap();
|
||
let deserialized: AppPreferences = serde_json::from_str(&json).unwrap();
|
||
|
||
assert_eq!(deserialized.theme, ThemeMode::Dark);
|
||
assert_eq!(deserialized.output_directory_mode, OutputDirectoryMode::Desktop);
|
||
assert_eq!(deserialized.default_image_quality, 95);
|
||
assert!(deserialized.auto_update_on_startup);
|
||
assert!(!deserialized.start_with_windows);
|
||
|
||
// Eski preferences.json dosyalarındaki miras alanlar sorunsuz yoksayılmalıdır.
|
||
let legacy_json = r#"{
|
||
"enabled": true,
|
||
"drag_modifier": "Shift",
|
||
"output_directory_mode": "SameAsSource",
|
||
"custom_output_directory": "",
|
||
"overwrite_existing": false,
|
||
"play_sound_effects": true,
|
||
"theme": "Dark",
|
||
"default_image_quality": 100,
|
||
"custom_ffmpeg_path": "",
|
||
"custom_pandoc_path": "",
|
||
"custom_seven_zip_path": "",
|
||
"favorite_format_ids": ["bmp"],
|
||
"auto_update_on_startup": true,
|
||
"start_with_windows": true,
|
||
"settings_version": 2
|
||
}"#;
|
||
let legacy_deserialized: AppPreferences = serde_json::from_str(legacy_json).unwrap();
|
||
assert!(legacy_deserialized.start_with_windows);
|
||
assert_eq!(legacy_deserialized.default_image_quality, 100);
|
||
}
|
||
|
||
#[test]
|
||
fn test_history_serialization() {
|
||
let entry = HistoryEntry {
|
||
id: "task-123".to_string(),
|
||
title: "test.pdf".to_string(),
|
||
format_summary: "DOCX".to_string(),
|
||
input_files: vec!["C:/path/test.pdf".to_string()],
|
||
output_files: vec!["C:/path/test.docx".to_string()],
|
||
original_size_bytes: 1024,
|
||
output_size_bytes: 2048,
|
||
timestamp: chrono::Utc::now(),
|
||
success: true,
|
||
error_message: None,
|
||
};
|
||
|
||
let json = serde_json::to_string(&entry).unwrap();
|
||
let deserialized: HistoryEntry = serde_json::from_str(&json).unwrap();
|
||
|
||
assert_eq!(deserialized.id, "task-123");
|
||
assert_eq!(deserialized.format_summary, "DOCX");
|
||
assert!(deserialized.success);
|
||
}
|