use super::*;
use crate::working_copy::WorkingCopy;
#[test]
fn add_non_utf8_file_test() -> Result<(), anyhow::Error> {
    env_logger::try_init().unwrap_or(());
    let mut buf = Vec::new();
    use std::io::Read;
    let mut fh = std::fs::File::open("src/tests/data/1252.1")?;
    fh.read_to_end(&mut buf)?;
    let mut repo = working_copy::memory::Memory::new();
    repo.add_file("file", buf);
    let env = pristine::sanakirja::Pristine::new_anon()?;
    let mut txn = env.mut_txn_begin();
    let mut channel = txn.open_or_create_channel("main")?;
    txn.add_file("file")?;
    let store = changestore::memory::Memory::new();
    let (h, change) = record_all_change(&mut repo, &store, &mut txn, &mut channel, "")?;
    let mut v = Vec::new();
    change
        .write(
            &store,
            Some(h),
            |l, _p| format!("{}:{}", l.path, l.line),
            true,
            &mut v,
        )
        .unwrap();
    let lines: Vec<&str> = std::str::from_utf8(&v)
        .unwrap()
        .lines()
        .filter(|l| l.starts_with("+"))
        .collect();
    assert_eq!(
        vec!["+ French / Français (Windows CP 1252)", "+ €‚ƒ„…†‡, Salut"],
        lines
    );
    Ok(())
}
#[test]
fn change_non_utf8_file_test() -> Result<(), anyhow::Error> {
    env_logger::try_init().unwrap_or(());
    let mut buf = Vec::new();
    use std::io::Read;
    let mut fh = std::fs::File::open("src/tests/data/8859-1.1")?;
    fh.read_to_end(&mut buf)?;
    let mut repo = working_copy::memory::Memory::new();
    repo.add_file("file", buf);
    let env = pristine::sanakirja::Pristine::new_anon()?;
    let mut txn = env.mut_txn_begin();
    let mut channel = txn.open_or_create_channel("main")?;
    txn.add_file("file")?;
    let store = changestore::memory::Memory::new();
    record_all(&mut repo, &store, &mut txn, &mut channel, "")?;
    let mut buf = Vec::new();
    {
        use std::io::Read;
        let mut fh = std::fs::File::open("src/tests/data/8859-1.2")?;
        fh.read_to_end(&mut buf)?;
    }
    repo.write_file::<_, std::io::Error, _>("file", |w| {
        w.write_all(&buf).unwrap();
        Ok(())
    })?;
    let (h1, change1) = record_all_change(&mut repo, &store, &mut txn, &mut channel, "")?;
    // only one line was changed
    let mut v = Vec::new();
    change1
        .write(
            &store,
            Some(h1),
            |l, _p| format!("{}:{}", l.path, l.line),
            true,
            &mut v,
        )
        .unwrap();
    let lines: Vec<&str> = std::str::from_utf8(&v)
        .unwrap()
        .lines()
        .filter(|l| l.starts_with(|c| c == '-' || c == '+'))
        .collect();
    assert_eq!(
        vec![
            "- French / Français (ISO Latin-1 / ISO 8859-1)",
            "+ Français / French (ISO Latin-1 / ISO 8859-1)"
        ],
        lines
    );
    Ok(())
}
#[test]
fn delete_non_utf8_file_test() -> Result<(), anyhow::Error> {
    env_logger::try_init().unwrap_or(());
    let mut buf = Vec::new();
    use std::io::Read;
    let mut fh = std::fs::File::open("src/tests/data/gb.1")?;
    fh.read_to_end(&mut buf)?;
    let mut repo = working_copy::memory::Memory::new();
    repo.add_file("file", buf);
    let env = pristine::sanakirja::Pristine::new_anon()?;
    let mut txn = env.mut_txn_begin();
    let mut channel = txn.open_or_create_channel("main")?;
    txn.add_file("file")?;
    let store = changestore::memory::Memory::new();
    record_all(&mut repo, &store, &mut txn, &mut channel, "")?;
    let mut buf = Vec::new();
    {
        use std::io::Read;
        let mut fh = std::fs::File::open("src/tests/data/8859-1.2")?;
        fh.read_to_end(&mut buf)?;
    }
    repo.remove_path("file")?;
    let (h1, change1) = record_all_change(&mut repo, &store, &mut txn, &mut channel, "")?;
    let mut v = Vec::new();
    change1
        .write(
            &store,
            Some(h1),
            |l, _p| format!("{}:{}", l.path, l.line),
            true,
            &mut v,
        )
        .unwrap();
    let lines: Vec<&str> = std::str::from_utf8(&v)
        .unwrap()
        .lines()
        .filter(|l| l.starts_with(|c| c == '-'))
        .collect();
    assert_eq!(
        vec![
            " -Chinese / 中文,普通话,汉语 (GB 2312 / GB-encoding)",
            "- 你好"
        ],
        lines
    );
    Ok(())
}