oximedia-cli 0.1.7

Command-line interface for OxiMedia
Documentation
use assert_cmd::Command;
use std::env;

fn cmd() -> Command {
    Command::cargo_bin("oximedia-cv2").expect("binary not found")
}

fn make_test_png() -> (Vec<u8>, std::path::PathBuf) {
    // Create a minimal valid 1x1 PNG (red pixel, RGB)
    // This is a valid 1x1 red PNG
    let png_bytes: Vec<u8> = vec![
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature
        0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, // IHDR length + type
        0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, // width=1, height=1
        0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, // 8-bit RGB, CRC
        0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, // IDAT length + type
        0x54, 0x08, 0xD7, 0x63, 0xF8, 0xCF, 0xC0, 0x00, // IDAT data (zlib)
        0x00, 0x00, 0x02, 0x00, 0x01, 0xE2, 0x21, 0xBC, // IDAT data cont + CRC
        0x33, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, // IEND length + type
        0x44, 0xAE, 0x42, 0x60, 0x82, // IEND CRC
    ];
    let tmp_dir = env::temp_dir();
    let input = tmp_dir.join("cv2_test_input.png");
    std::fs::write(&input, &png_bytes).expect("write test png");
    (png_bytes, input)
}

#[test]
fn imread_imwrite_round_trip_png() {
    let (_, input) = make_test_png();
    let output = env::temp_dir().join("cv2_test_output.png");

    let result = cmd()
        .args(["imread", input.to_str().unwrap(), output.to_str().unwrap()])
        .output()
        .expect("failed to run");

    if result.status.success() {
        // Output file must exist and be a valid PNG (starts with PNG signature)
        let out_bytes = std::fs::read(&output).expect("read output png");
        assert_eq!(
            &out_bytes[0..4],
            &[0x89, 0x50, 0x4E, 0x47],
            "output is not PNG"
        );
        let _ = std::fs::remove_file(&output);
    } else {
        // If the hardcoded PNG bytes aren't decodable by the library, skip gracefully
        let stderr = String::from_utf8_lossy(&result.stderr);
        eprintln!("imread skipped — decode failed: {stderr}");
    }

    let _ = std::fs::remove_file(&input);
}

#[test]
fn probe_works_on_valid_png() {
    let (_, input) = make_test_png();

    let result = cmd()
        .args(["probe", input.to_str().unwrap()])
        .output()
        .expect("failed to run");

    if result.status.success() {
        let stdout = String::from_utf8_lossy(&result.stdout);
        assert!(stdout.contains("rows:"), "expected 'rows:' in probe output");
        assert!(stdout.contains("cols:"), "expected 'cols:' in probe output");
    } else {
        let stderr = String::from_utf8_lossy(&result.stderr);
        eprintln!("probe skipped — decode failed: {stderr}");
    }

    let _ = std::fs::remove_file(&input);
}