test_that("BAM with several informative and uninformative reads is properly interpreted", {

    chr <- 10
    pos <- cbind(
        CHR = rep(chr, 7),
        POS = c(9, 11, 13, 15, 17, 19, 21),
        REF = c("A", "A", "A", "A", "A", "A", "A"),
        ALT = c("G", "C", "T", "C", "C", "C", "C")
    )
    ## https://github.com/samtools/hts-specs/blob/master/SAMv1.pdf
    bam_file <- make_simple_bam(
        file_stem = file.path(tempdir(), "simple"),
        sam = make_simple_sam_text(
            list(
                c("r000", "0", chr, "5", "60",
                  "2M", "*", "0", "0",
                  "AA", ":;"),
                c("r001", "0", chr, "9", "60",
                  "2M", "*", "0", "0",
                  "AA", ";:"),
                c("r002", "0", chr, "11", "60",
                  "2M", "*", "0", "0",
                  "CC", "AB"),
                c("r001", "0", chr, "11", "60",
                  "2M", "*", "0", "0",
                  "CC", "<="),
                c("r002", "0", chr, "13", "60",
                  "2M", "*", "0", "0",
                  "TT", "BC"),
                c("r003", "0", chr, "21", "60",
                  "2M", "*", "0", "0",
                  "AA", "::")
            ),
            chr
        )
    )
    expected_sample_reads <- list(
        list( ##r001
            1, NA, ## NA as can be random
            matrix(c(-26, 27), ncol = 1),
            matrix(c(0, 1), ncol = 1)
        ),
        list( ## r002
            1, NA, ## NA as is random between 1, 2
            matrix(c(32, 33), ncol = 1),
            matrix(c(1, 2), ncol = 1)
        ),
        list(
            0, 6,
            matrix(-25, ncol = 1),
            matrix(6, ncol = 1)
        )
    )

    regionName <- "region-name"
    loadBamAndConvert(
        iBam = 1,
        L = as.integer(pos[, 2]),
        pos = pos,
        nSNPs = as.integer(nrow(pos)),
        bam_files = bam_file,
        N = 1,
        sampleNames = "test-name-several",
        inputdir = tempdir(),
        regionName = regionName,
        tempdir = tempdir(),
        chr = chr,
        chrStart = 1,
        chrEnd = 100,
        save_sampleReadsInfo = TRUE
    )

    load(file_sampleReads(tempdir(), 1, regionName))
    sampleReads[[1]][[2]] <- NA ## manually blank out
    sampleReads[[2]][[2]] <- NA ## manually blank out

    expect_equal(
        sampleReads,
        expected_sample_reads
    )

    ## check read length is OK
    load(file_sampleReadsInfo(tempdir(), 1, regionName))

})
