use super::*;

fn rope(data: &[u8]) -> Rope {
    let mut r = Rope::new(None);
    r.push(data.to_vec());
    r
}

fn rope2(a: &[u8], b: &[u8]) -> Rope {
    let mut r = Rope::new(None);
    r.push(a.to_vec());
    r.push(b.to_vec());
    r
}

fn flat(r: &Rope) -> Vec<u8> {
    r.fw_cursor().collect()
}

#[test]
fn single_entry_no_boundary() {
    assert_eq!(end_of_last_fasta_entry(&rope(b">seq1\nACGT\n")), None);
}

#[test]
fn two_entries_cuts_at_second_header() {
    let data = b">seq1\nACGT\n>seq2\nTTTT\n";
    let r = rope(data);
    let pos = end_of_last_fasta_entry(&r).unwrap();
    assert_eq!(&flat(&r)[pos..], b">seq2\nTTTT\n");
    assert_eq!(&flat(&r)[..pos], b">seq1\nACGT\n");
}

#[test]
fn three_entries_cuts_at_last_header() {
    let data = b">s1\nAA\n>s2\nCC\n>s3\nGG\n";
    let r = rope(data);
    let pos = end_of_last_fasta_entry(&r).unwrap();
    assert_eq!(&flat(&r)[pos..], b">s3\nGG\n");
}

#[test]
fn multiline_sequence() {
    let data = b">s1\nACGT\nACGT\n>s2\nTTTT\n";
    let r = rope(data);
    let pos = end_of_last_fasta_entry(&r).unwrap();
    assert_eq!(&flat(&r)[pos..], b">s2\nTTTT\n");
}

#[test]
fn crlf_line_endings() {
    let data = b">s1\r\nACGT\r\n>s2\r\nTTTT\r\n";
    let r = rope(data);
    let pos = end_of_last_fasta_entry(&r).unwrap();
    assert_eq!(&flat(&r)[pos..], b">s2\r\nTTTT\r\n");
}

#[test]
fn boundary_spans_two_blocks() {
    let a = b">s1\nACGT\n";
    let b = b">s2\nTTTT\n";
    let r = rope2(a, b);
    let all: Vec<u8> = flat(&r);
    let pos = end_of_last_fasta_entry(&r).unwrap();
    assert_eq!(&all[pos..], b">s2\nTTTT\n");
}
