elio 1.0.0

Terminal-native file manager with rich previews, inline images, and mouse support.
Documentation
use super::*;
use std::path::Path;

const TEST_AUDIO_METADATA_MP3: &[u8] = &[
    0x49, 0x44, 0x33, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x54, 0x49, 0x54, 0x32, 0x00, 0x00,
    0x00, 0x0b, 0x00, 0x00, 0x00, 0x45, 0x6c, 0x69, 0x6f, 0x20, 0x54, 0x75, 0x6e, 0x65, 0x00, 0x54,
    0x50, 0x45, 0x31, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x43, 0x6f, 0x64, 0x65, 0x78, 0x00,
    0x54, 0x41, 0x4c, 0x42, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x50, 0x72, 0x65, 0x76, 0x69,
    0x65, 0x77, 0x20, 0x53, 0x75, 0x69, 0x74, 0x65, 0x00, 0x54, 0x52, 0x43, 0x4b, 0x00, 0x00, 0x00,
    0x03, 0x00, 0x00, 0x00, 0x33, 0x00, 0x54, 0x53, 0x53, 0x45, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00,
    0x00, 0x4c, 0x61, 0x76, 0x66, 0x36, 0x32, 0x2e, 0x31, 0x32, 0x2e, 0x31, 0x30, 0x30, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfb, 0x40, 0xc0, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x6e,
    0x66, 0x6f, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x02, 0xc0, 0x00, 0x68,
    0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68,
    0x68, 0x68, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e,
    0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0x8e, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4,
    0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda,
    0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xda, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x61, 0x76, 0x63, 0x36, 0x32, 0x2e, 0x32, 0x38, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x03, 0x06, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x02, 0xc0, 0x6d, 0x23, 0xe7, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
    0xfb, 0x10, 0xc4, 0x00, 0x00, 0x04, 0x84, 0x15, 0x59, 0x54, 0x60, 0x80, 0x30, 0xaa, 0x08, 0xa8,
    0x83, 0x36, 0x50, 0x00, 0x00, 0x01, 0xa0, 0x4b, 0x80, 0x60, 0x02, 0x64, 0xd3, 0xd8, 0x04, 0x00,
    0x00, 0x10, 0xb1, 0x38, 0x3e, 0x5d, 0xee, 0x04, 0x0e, 0x7e, 0xa0, 0x40, 0x10, 0x74, 0xb8, 0x3e,
    0x1f, 0xc4, 0x00, 0x84, 0x48, 0xef, 0xff, 0xf4, 0x03, 0x42, 0x81, 0x36, 0x90, 0x01, 0x80, 0xc4,
    0x48, 0x0c, 0x24, 0x88, 0xd3, 0x83, 0xdc, 0x20, 0x57, 0x45, 0xd0, 0xf2, 0x4a, 0x16, 0x0c, 0xe5,
    0x51, 0xfe, 0x5e, 0x02, 0x81, 0x7c, 0x05, 0x12, 0x0f, 0x7e, 0x1e, 0x05, 0x4e, 0xf5, 0x03, 0x42,
    0x5f, 0x9d, 0x52, 0x2e, 0x69, 0x73, 0x4c, 0xff, 0xfb, 0x12, 0xc4, 0x02, 0x03, 0xc5, 0x10, 0x1d,
    0x22, 0x1d, 0xe0, 0x00, 0x28, 0xa3, 0x04, 0x22, 0xc1, 0xaf, 0x68, 0x48, 0x07, 0x00, 0x94, 0x44,
    0x03, 0x86, 0x00, 0x20, 0x64, 0x66, 0xee, 0xd8, 0x26, 0x56, 0x83, 0x1e, 0x61, 0x88, 0x0e, 0xa6,
    0x0a, 0x40, 0x66, 0x60, 0x32, 0x04, 0xc6, 0x04, 0x20, 0x3c, 0x60, 0x4a, 0x02, 0xc5, 0xe2, 0xea,
    0x55, 0x03, 0x31, 0x43, 0x8d, 0x5b, 0x13, 0xc5, 0xf0, 0xc4, 0x04, 0x6b, 0x4d, 0xbf, 0xb5, 0xa4,
    0xdb, 0x54, 0x6b, 0xcc, 0x41, 0x02, 0x30, 0xf7, 0xd7, 0x36, 0x2e, 0x4d, 0x53, 0x03, 0x57, 0x8c,
    0xc9, 0x8f, 0x60, 0x92, 0xfc, 0x01, 0xcf, 0xcf, 0x7a, 0xea, 0x30, 0xd0, 0xe3, 0x0e, 0x1d, 0x31,
    0xff, 0xfb, 0x10, 0xc4, 0x03, 0x03, 0xc5, 0x04, 0x1f, 0x18, 0x0d, 0xfb, 0x22, 0x40, 0xad, 0x84,
    0x62, 0x81, 0xbf, 0x6c, 0x48, 0xd3, 0x83, 0x3e, 0x83, 0x30, 0xaf, 0x1b, 0xe3, 0x50, 0xce, 0x1c,
    0x34, 0xeb, 0x1b, 0x43, 0x09, 0xe0, 0x6d, 0x35, 0xcc, 0x02, 0x06, 0x6e, 0xa8, 0x75, 0x7e, 0x6b,
    0x06, 0xc9, 0xa5, 0xa1, 0xcf, 0xd7, 0xf4, 0x24, 0x41, 0x89, 0x88, 0x99, 0xb1, 0xc1, 0xbb, 0xc6,
    0x98, 0x97, 0x0e, 0xd1, 0xc1, 0x5f, 0x5c, 0x1b, 0xfc, 0x0e, 0xe1, 0x89, 0xa8, 0x4f, 0x9c, 0x23,
    0x21, 0x9e, 0x23, 0x19, 0x99, 0xf9, 0x99, 0x3c, 0x18, 0xb0, 0xb3, 0x07, 0x8c, 0x53, 0xe0, 0x1f,
    0xfa, 0xbe, 0x8a, 0x30, 0xa1, 0x13, 0x0f, 0x1c, 0xff, 0xfb, 0x12, 0xc4, 0x02, 0x83, 0xc5, 0x00,
    0x1f, 0x18, 0x0d, 0xfb, 0x22, 0x40, 0xad, 0x04, 0x24, 0x42, 0xbc, 0x00, 0x04, 0x31, 0xa3, 0xb3,
    0x3d, 0x86, 0x30, 0xa6, 0x1c, 0x93, 0x4d, 0xbe, 0x6b, 0x34, 0xb8, 0x1b, 0xf3, 0x09, 0x70, 0x70,
    0x34, 0x91, 0x01, 0x04, 0x6f, 0x26, 0x76, 0xf8, 0x6b, 0x84, 0xcd, 0x67, 0x83, 0x7f, 0xaf, 0xe8,
    0x86, 0x44, 0x60, 0x02, 0x60, 0x2e, 0x02, 0x26, 0x02, 0x00, 0x98, 0x60, 0xa4, 0x17, 0x86, 0x81,
    0xca, 0xa6, 0x67, 0x0e, 0x35, 0xe6, 0x1b, 0xa1, 0x26, 0x60, 0xd2, 0x04, 0xa6, 0x01, 0xe0, 0x6e,
    0x60, 0x5e, 0x04, 0xe6, 0x05, 0x60, 0x4e, 0x5d, 0x78, 0xbd, 0xbc, 0x2a, 0x8d, 0x01, 0x22, 0x01,
    0x02, 0xff, 0xfb, 0x10, 0xc4, 0x02, 0x80, 0x04, 0xfc, 0x3b, 0x48, 0x19, 0xa3, 0x80, 0x00, 0x90,
    0x85, 0xa0, 0xc3, 0x9e, 0x60, 0x00, 0x18, 0x2f, 0x0b, 0x0a, 0x32, 0x08, 0x9e, 0x15, 0xc1, 0xf2,
    0x65, 0xac, 0x31, 0x63, 0x25, 0x36, 0xfd, 0xc8, 0x8a, 0x59, 0xc7, 0x07, 0x63, 0x79, 0xbc, 0x99,
    0x00, 0x44, 0x45, 0x99, 0x85, 0x41, 0x5f, 0x96, 0x3c, 0x2c, 0xc0, 0x03, 0x80, 0x15, 0x0c, 0x63,
    0xb4, 0x4d, 0x4f, 0x11, 0x3e, 0x01, 0x68, 0xbc, 0x93, 0xe1, 0x36, 0x32, 0x9f, 0xa7, 0x50, 0xd5,
    0x6e, 0xa4, 0x88, 0x04, 0x24, 0x24, 0x0d, 0x41, 0xa3, 0xdf, 0x59, 0xde, 0x22, 0x4c, 0x41, 0x4d,
    0x45, 0x33, 0x2e, 0x31, 0x30, 0x31, 0x20, 0x28, 0x62,
];

#[test]
fn supported_audio_extensions_route_to_audio_preview_kind() {
    let root = temp_path("audio-kind");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let cases = [
        ("track.mp3", "MP3 audio"),
        ("track.wav", "WAV audio"),
        ("track.flac", "FLAC audio"),
        ("track.ogg", "Ogg audio"),
        ("track.m4a", "M4A audio"),
    ];

    for (name, detail) in cases {
        let path = root.join(name);
        fs::write(&path, b"not-real-audio").expect("failed to write audio fixture");

        let preview = build_preview(&file_entry(path));

        assert_eq!(preview.kind, PreviewKind::Audio);
        assert_eq!(preview.section_label(), "Audio");
        assert_eq!(preview.detail.as_deref(), Some(detail));
    }

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn audio_preview_falls_back_to_file_metadata_without_tools() {
    let root = temp_path("audio-fallback");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let path = root.join("track.flac");
    let contents = b"still-not-real-audio";
    fs::write(&path, contents).expect("failed to write audio fixture");

    let preview = build_preview(&file_entry(path));
    let line_texts: Vec<_> = preview.lines.iter().map(line_text).collect();

    assert_eq!(preview.kind, PreviewKind::Audio);
    assert_eq!(preview.detail.as_deref(), Some("FLAC audio"));
    assert!(preview.preview_visual.is_none());
    assert!(line_texts.iter().any(|line| line.contains("File Size")
        && line.contains(&crate::fs::format_size(contents.len() as u64))));
    assert!(line_texts.iter().all(|line| !line.contains("Title")));
    assert!(
        line_texts
            .iter()
            .all(|line| !line.contains("Binary or unsupported file"))
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn audio_preview_shows_metadata_with_ffprobe_only() {
    if !audio_probe_available() {
        return;
    }

    let root = temp_path("audio-metadata");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let path = root.join("track.mp3");
    if !write_test_audio_fixture(&path) {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    }

    let preview = build_preview_with_options_and_code_line_limit(
        &file_entry(path),
        &PreviewRequestOptions::Default,
        default_code_preview_line_limit(),
        default_code_preview_line_limit(),
        true,
        false,
        &|| false,
    );
    let line_texts: Vec<_> = preview.lines.iter().map(line_text).collect();

    assert_eq!(preview.kind, PreviewKind::Audio);
    assert_eq!(preview.detail.as_deref(), Some("MP3 audio"));
    assert!(preview.preview_visual.is_none());
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Title") && line.contains("Elio Tune"))
    );
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Artist") && line.contains("Codex"))
    );
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Album") && line.contains("Preview Suite"))
    );
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Track") && line.contains("3"))
    );
    assert!(line_texts.iter().any(|line| line.contains("Duration")));
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Codec") && line.contains("mp3"))
    );
    assert!(line_texts.iter().any(|line| line.contains("Bitrate")));
    assert!(line_texts.iter().any(|line| line.contains("Sample Rate")));
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Channels") && line.contains("mono"))
    );
    assert!(line_texts.iter().any(|line| line.contains("File Size")));

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn audio_preview_skips_artwork_without_overlay_support_even_when_embedded_art_exists() {
    if !audio_tools_available() {
        return;
    }

    let root = temp_path("audio-no-overlay");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let path = root.join("track.m4a");
    if !write_test_audio_fixture_with_artwork(&path) {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    }

    let preview = build_preview_with_options_and_code_line_limit(
        &file_entry(path),
        &PreviewRequestOptions::Default,
        default_code_preview_line_limit(),
        default_code_preview_line_limit(),
        true,
        false,
        &|| false,
    );
    let line_texts: Vec<_> = preview.lines.iter().map(line_text).collect();

    assert_eq!(preview.kind, PreviewKind::Audio);
    assert_eq!(preview.detail.as_deref(), Some("M4A audio"));
    assert!(preview.preview_visual.is_none());
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Title") && line.contains("Cover Track"))
    );
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Album") && line.contains("Artwork Suite"))
    );

    fs::remove_dir_all(root).expect("failed to remove temp root");
}

#[test]
fn audio_preview_attaches_inline_cover_when_tools_and_artwork_are_available() {
    if !audio_tools_available() {
        return;
    }

    let root = temp_path("audio-cover");
    fs::create_dir_all(&root).expect("failed to create temp root");
    let path = root.join("track.m4a");
    if !write_test_audio_fixture_with_artwork(&path) {
        fs::remove_dir_all(&root).expect("failed to remove temp root");
        return;
    }

    let preview = build_preview_with_options_and_code_line_limit(
        &file_entry(path),
        &PreviewRequestOptions::Default,
        default_code_preview_line_limit(),
        default_code_preview_line_limit(),
        true,
        true,
        &|| false,
    );
    let visual = preview
        .preview_visual
        .clone()
        .expect("audio preview should attach artwork");
    let line_texts: Vec<_> = preview.lines.iter().map(line_text).collect();

    assert_eq!(preview.kind, PreviewKind::Audio);
    assert_eq!(preview.detail.as_deref(), Some("M4A audio"));
    assert_eq!(visual.kind, PreviewVisualKind::Cover);
    assert_eq!(visual.layout, PreviewVisualLayout::Inline);
    assert!(visual.path.exists());
    assert!(visual.size > 0);
    assert!(
        line_texts
            .iter()
            .any(|line| line.contains("Title") && line.contains("Cover Track"))
    );
    assert!(line_texts.iter().any(|line| line.contains("Codec")));

    let _ = fs::remove_file(visual.path);
    fs::remove_dir_all(root).expect("failed to remove temp root");
}

fn audio_tools_available() -> bool {
    Command::new("ffprobe").arg("-version").output().is_ok()
        && Command::new("ffmpeg").arg("-version").output().is_ok()
}

fn audio_probe_available() -> bool {
    Command::new("ffprobe").arg("-version").output().is_ok()
}

fn write_test_audio_fixture(path: &Path) -> bool {
    fs::write(path, TEST_AUDIO_METADATA_MP3).is_ok()
}

fn write_test_audio_fixture_with_artwork(path: &Path) -> bool {
    let cover_path = path.with_extension("png");
    write_test_raster_image(&cover_path, ImageFormat::Png, 32, 32);
    let created = Command::new("ffmpeg")
        .args([
            "-loglevel",
            "error",
            "-y",
            "-f",
            "lavfi",
            "-i",
            "sine=frequency=660:duration=1",
            "-i",
        ])
        .arg(&cover_path)
        .args([
            "-map",
            "0:a",
            "-map",
            "1:v",
            "-c:a",
            "aac",
            "-c:v",
            "mjpeg",
            "-disposition:v",
            "attached_pic",
            "-metadata",
            "title=Cover Track",
            "-metadata",
            "artist=Codex",
            "-metadata",
            "album=Artwork Suite",
            "-metadata",
            "track=7",
        ])
        .arg(path)
        .status()
        .is_ok_and(|status| status.success());
    let _ = fs::remove_file(cover_path);
    created
}