write_mini_fasta <- function(seqs, per_line=FALSE) {
    f <- tempfile(fileext=".fas")
    con <- file(f, "w")
    for (i in seq_along(seqs)) {
        writeLines(paste0(">seq", i), con)
        if (per_line) {
            half <- nchar(seqs[i]) %/% 2
            writeLines(substring(seqs[i], 1, half), con)
            writeLines(substring(seqs[i], half+1), con)
        } else {
            writeLines(seqs[i], con)
        }
    }
    close(con)
    f
}

test_that("guess_sequence_type infers DNA/RNA/AA from raw string", {
    expect_equal(seqmagick:::guess_sequence_type("ACGTAGCTAN"), "DNA")
    expect_equal(seqmagick:::guess_sequence_type("ACGUAGCUAN"), "RNA")
    expect_equal(seqmagick:::guess_sequence_type("MAEGKLVIWF"), "AA")
})

test_that("fa_read type='auto' detects sequence types end-to-end", {
    dna <- write_mini_fasta(c("ACGTNNAGCTTA-", "TTTAGCCAN"))
    rna <- write_mini_fasta(c("ACGUUNAGCUUA-", "UUUAGCCAN"))
    aa  <- write_mini_fasta(c("MAEGKLVIWFKLM", "MWKFTESTQNLPP"))

    expect_s4_class(fa_read(dna), "DNAStringSet")
    expect_s4_class(fa_read(rna), "RNAStringSet")
    expect_s4_class(fa_read(aa),  "AAStringSet")

    unlink(c(dna, rna, aa))
})

test_that("guess_sequence_type handles multi-line fasta records", {
    ## interleaved-style records where one sequence spans several lines
    f <- write_mini_fasta(c("ACGTACGTACGT", "TTTACGTAAAAC"), per_line=TRUE)
    expect_s4_class(fa_read(f, type="auto"), "DNAStringSet")
    unlink(f)
})
