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 trimdown::Config;
use tempfile::TempDir;
use std::fs;

#[test]
fn test_config_serialization() {
    let config = Config {
        quality: 90,
        max_width: 2560,
        video_crf: 25,
        pdf_quality: "printer".to_string(),
        pdf_method: "qpdf".to_string(),
        force: true,
        verbose: true,
        dry_run: false,
        preset: Some("maximum".to_string()),
    };

    let json = serde_json::to_string(&config).unwrap();
    assert!(json.contains("\"quality\":90"));
    assert!(json.contains("\"max_width\":2560"));
    assert!(json.contains("\"video_crf\":25"));
}

#[test]
fn test_config_deserialization() {
    let json = r#"{
        "quality": 95,
        "max_width": 3840,
        "video_crf": 23,
        "pdf_quality": "maximum",
        "pdf_method": "auto",
        "force": false,
        "verbose": false,
        "dry_run": false,
        "preset": null
    }"#;

    let config: Config = serde_json::from_str(json).unwrap();
    assert_eq!(config.quality, 95);
    assert_eq!(config.max_width, 3840);
    assert_eq!(config.video_crf, 23);
    assert_eq!(config.pdf_quality, "maximum");
}

#[test]
fn test_config_with_missing_fields() {
    let json = r#"{
        "quality": 85
    }"#;

    let config: Config = serde_json::from_str(json).unwrap();
    assert_eq!(config.quality, 85);
    assert_eq!(config.max_width, 1920);
    assert_eq!(config.video_crf, 28);
}

#[test]
fn test_config_save_and_load_roundtrip() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.json");

    let original = Config {
        quality: 88,
        max_width: 2048,
        video_crf: 26,
        pdf_quality: "ebook".to_string(),
        pdf_method: "qpdf".to_string(),
        force: false,
        verbose: true,
        dry_run: false,
        preset: Some("balanced".to_string()),
    };

    original.save_to_file(&config_path).unwrap();
    let loaded = Config::load_from_file(&config_path).unwrap();

    assert_eq!(loaded.quality, original.quality);
    assert_eq!(loaded.max_width, original.max_width);
    assert_eq!(loaded.video_crf, original.video_crf);
    assert_eq!(loaded.pdf_quality, original.pdf_quality);
    assert_eq!(loaded.preset, original.preset);
}

#[test]
fn test_config_file_not_found() {
    let result = Config::load_from_file(&std::path::PathBuf::from("/nonexistent/config.json"));
    assert!(result.is_err());
}

#[test]
fn test_config_invalid_json() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("invalid.json");
    fs::write(&config_path, "not valid json").unwrap();

    let result = Config::load_from_file(&config_path);
    assert!(result.is_err());
}

#[test]
fn test_preset_application_fast() {
    let mut config = Config::default();
    config.apply_preset("fast");

    assert_eq!(config.quality, 75);
    assert_eq!(config.max_width, 1280);
    assert_eq!(config.video_crf, 32);
    assert_eq!(config.pdf_quality, "screen");
}

#[test]
fn test_preset_application_balanced() {
    let mut config = Config::default();
    config.apply_preset("balanced");

    assert_eq!(config.quality, 85);
    assert_eq!(config.max_width, 1920);
    assert_eq!(config.video_crf, 28);
    assert_eq!(config.pdf_quality, "ebook");
}

#[test]
fn test_preset_application_maximum() {
    let mut config = Config::default();
    config.apply_preset("maximum");

    assert_eq!(config.quality, 95);
    assert_eq!(config.max_width, 3840);
    assert_eq!(config.video_crf, 23);
    assert_eq!(config.pdf_quality, "maximum");
}

#[test]
fn test_preset_application_unknown() {
    let mut config = Config::default();
    let original_quality = config.quality;
    
    config.apply_preset("unknown_preset");
    
    assert_eq!(config.quality, original_quality);
}

#[test]
fn test_config_find_file_nonexistent() {
    let result = Config::find_config_file();
    assert!(result.is_none() || result.is_some());
}

#[test]
fn test_config_load_or_default() {
    let config = Config::load_or_default();
    
    assert!(config.quality >= 1 && config.quality <= 100);
    assert!(config.max_width > 0);
    assert!(config.video_crf <= 51);
}

#[test]
fn test_config_pretty_json() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("pretty.json");

    let config = Config::default();
    config.save_to_file(&config_path).unwrap();

    let contents = fs::read_to_string(&config_path).unwrap();
    assert!(contents.contains('\n'));
    assert!(contents.contains("  "));
}

#[test]
fn test_config_all_presets() {
    let presets = vec!["fast", "balanced", "maximum"];
    
    for preset in presets {
        let mut config = Config::default();
        config.apply_preset(preset);
        
        assert!(config.quality >= 1 && config.quality <= 100);
        assert!(config.max_width > 0);
        assert!(config.video_crf <= 51);
    }
}