build_fas <- function(seqs) {
    f <- tempfile(fileext=".fas")
    con <- file(f, "w")
    for (i in seq_along(seqs)) {
        writeLines(paste0(">s", i), con)
        writeLines(seqs[i], con)
    }
    close(con)
    f
}

test_that("fa_summary computes counts, lengths, GC and ambiguity", {
    f <- build_fas(c("ACGT", "ACGN", "GT--"))
    on.exit(unlink(f))

    res <- fa_summary(f)

    expect_s3_class(res, "data.frame")
    expect_identical(dim(res), c(1L, 6L))
    expect_identical(names(res),
                     c("n_seq", "min_len", "max_len", "mean_len",
                       "GC_pct", "ambiguous_pct"))

    expect_equal(res$n_seq, 3)
    expect_equal(res$min_len, 4)
    expect_equal(res$max_len, 4)
    expect_equal(res$mean_len, 4)

    ## 12 chars total: 9 clean (A/C/G/T/U), of which 5 are G or C
    expect_equal(res$GC_pct, round(100 * 5/9, 2))
    ## 3 ambiguous: N, -, -
    expect_equal(res$ambiguous_pct, 25)
})

test_that("fa_summary accepts XStringSet input directly", {
    f <- build_fas("ACGTRYKMS")
    x <- fa_read(f, type="unknown")

    res_file <- fa_summary(f)
    res_obj  <- fa_summary(x)
    expect_identical(res_file, res_obj)

    unlink(f)
})
