trimdown 0.1.6

Fast file compression tool for Office documents (PPTX, DOCX, XLSX), PDFs, videos, images, OpenDocument formats, and archives with streaming support
Documentation
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
use trimdown::{Cli, PdfQuality, PdfMethod, detect_file_type, FileType};

#[test]
fn test_file_type_detection() {
    assert_eq!(
        detect_file_type(&PathBuf::from("test.pptx")).unwrap(),
        FileType::PowerPoint
    );
    assert_eq!(
        detect_file_type(&PathBuf::from("test.pdf")).unwrap(),
        FileType::Pdf
    );
    assert_eq!(
        detect_file_type(&PathBuf::from("test.mp4")).unwrap(),
        FileType::Video
    );
    assert_eq!(
        detect_file_type(&PathBuf::from("test.docx")).unwrap(),
        FileType::Word
    );
    assert_eq!(
        detect_file_type(&PathBuf::from("test.txt")).unwrap(),
        FileType::Unsupported
    );
}

#[test]
fn test_cli_defaults() {
    let cli = Cli {
        command: None,
        input: Some(PathBuf::from("test.pptx")),
        output: None,
        folder: false,
        quality: 85,
        max_width: 1920,
        video_crf: 28,
        video_max_width: 1920,
        pdf_quality: PdfQuality::Ebook,
        pdf_method: PdfMethod::Auto,
        force: false,
        verbose: false,
        dry_run: false,
        preset: None,
        parallel: false,
    preserve_metadata: false,
    streaming: false,
    memory_limit: None,
    };

    assert_eq!(cli.quality, 85);
    assert_eq!(cli.max_width, 1920);
    assert_eq!(cli.video_crf, 28);
    assert_eq!(cli.pdf_quality, PdfQuality::Ebook);
    assert_eq!(cli.pdf_method, PdfMethod::Auto);
}

#[test]
fn test_output_path_generation() {
    let temp_dir = TempDir::new().unwrap();
    let input_path = temp_dir.path().join("test.pptx");
    fs::write(&input_path, b"test").unwrap();

    let stem = input_path.file_stem().unwrap().to_str().unwrap();
    let extension = input_path.extension().unwrap_or_default().to_str().unwrap();
    let output_path = input_path.with_file_name(format!("{}_compressed.{}", stem, extension));

    assert_eq!(
        output_path.file_name().unwrap().to_str().unwrap(),
        "test_compressed.pptx"
    );
}

#[test]
fn test_supported_extensions() {
    let supported = vec![
        "pptx", "ppt", "pdf", "mp4", "avi", "mov", "wmv", "mkv", "m4v", "flv", "webm", "docx", "doc"
    ];

    for ext in supported {
        let path = PathBuf::from(format!("test.{}", ext));
        let file_type = detect_file_type(&path).unwrap();
        assert_ne!(file_type, FileType::Unsupported, "Extension {} should be supported", ext);
    }
}

#[test]
fn test_case_insensitive_extension() {
    assert_eq!(
        detect_file_type(&PathBuf::from("test.PPTX")).unwrap(),
        FileType::PowerPoint
    );
    assert_eq!(
        detect_file_type(&PathBuf::from("test.PDF")).unwrap(),
        FileType::Pdf
    );
    assert_eq!(
        detect_file_type(&PathBuf::from("test.Mp4")).unwrap(),
        FileType::Video
    );
}

#[test]
fn test_pdf_quality_display() {
    assert_eq!(format!("{}", PdfQuality::Screen), "screen");
    assert_eq!(format!("{}", PdfQuality::Ebook), "ebook");
    assert_eq!(format!("{}", PdfQuality::Printer), "printer");
    assert_eq!(format!("{}", PdfQuality::Prepress), "prepress");
    assert_eq!(format!("{}", PdfQuality::Maximum), "maximum");
}

#[test]
fn test_cli_cloning() {
    let cli = Cli {
        command: None,
        input: Some(PathBuf::from("test.pptx")),
        output: None,
        folder: false,
        quality: 85,
        max_width: 1920,
        video_crf: 28,
        video_max_width: 1920,
        pdf_quality: PdfQuality::Ebook,
        pdf_method: PdfMethod::Auto,
        force: false,
        verbose: false,
        dry_run: false,
        preset: None,
        parallel: false,
    preserve_metadata: false,
    streaming: false,
    memory_limit: None,
    };

    let cloned = cli.clone();
    assert_eq!(cli.quality, cloned.quality);
    assert_eq!(cli.max_width, cloned.max_width);
    assert_eq!(cli.video_crf, cloned.video_crf);
}