tests/testthat/test-payload.R

small_field <- function() {
    set.seed(7)
    v <- array(runif(3 * 8 * 10, 1, 90), c(3L, 8L, 10L))
    v[3, 1:4, 1:4] <- NA_real_
    vol_field(v, lon = seq(0, 9), lat = seq(40, 47), depth = c(0, 10, 20),
              name = "q", unit = "u",
              mask = matrix(c(TRUE, rep(FALSE, 79)), 8, 10))
}

test_that("a payload writes the files the manifest names", {
    p <- vol_payload(small_field(), tempfile("pl"))
    expect_s3_class(p, "vol_payload")
    expect_true(all(file.exists(file.path(p$dir, p$files))))
    expect_identical(length(p$manifest$slices), p$manifest$nz)
    expect_true(all(vapply(p$manifest$slices, function(s)
        nchar(s$sha256) == 64L, TRUE)))
})

test_that("the manifest digests are the digests of the files", {
    p <- vol_payload(small_field(), tempfile("pl"))
    for (s in p$manifest$slices)
        expect_identical(s$sha256, digest::digest(file.path(p$dir, s$file),
                                                  algo = "sha256", file = TRUE))
})

test_that("the payload is reproducible when the timestamp is pinned", {
    f <- small_field()
    a <- vol_payload(f, tempfile("pl"), created = "2026-01-01T00:00:00+00:00")
    b <- vol_payload(f, tempfile("pl"), created = "2026-01-01T00:00:00+00:00")
    for (n in a$files)
        expect_identical(readBin(file.path(a$dir, n), "raw",
                                 file.size(file.path(a$dir, n))),
                         readBin(file.path(b$dir, n), "raw",
                                 file.size(file.path(b$dir, n))))
})

test_that("decimation reduces the grid and keeps the corners", {
    f <- small_field()
    p <- vol_payload(f, tempfile("pl"), step = 2L)
    expect_identical(p$manifest$nx, 5L)
    expect_identical(p$manifest$lon0, f$lon[1])
})

test_that("the single file carries the payload and opens on its own", {
    p <- vol_payload(small_field(), tempfile("pl"))
    out <- vol_standalone(p, path = tempfile(fileext = ".html"))
    txt <- paste(readLines(out, warn = FALSE), collapse = "\n")
    expect_true(grepl("window.__VOLUME_MANIFEST__", txt, fixed = TRUE))
    expect_true(grepl("data:image/png;base64,", txt, fixed = TRUE))
    expect_gt(file.size(out), file.size(vol_viewer_template()))
})

test_that("the methods report the payload", {
    p <- vol_payload(small_field(), tempfile("pl"))
    expect_output(print(p), "Browser payload")
    s <- summary(p)
    expect_s3_class(s, "summary.vol_payload")
    expect_identical(nrow(s$levels), 3L)
    expect_output(print(s), "Total =")
})

test_that("the default colour scale of a field of small values has a width", {
    # Regression: limits rounded to integers gave 0 to 0 for a field in [0, 0.01].
    set.seed(11)
    f <- vol_field(array(runif(60, 0, 0.01), c(3L, 4L, 5L)), 1:5, 1:4, c(0, 10, 20))
    d <- vol_payload(f, tempfile("pl"), created = "x")$manifest$display
    expect_lt(d$vmin, d$vmax)
    expect_gt(d$vmin, 0)
    expect_lt(d$vmax, 0.011)
    # a field spanning tens of units keeps integer limits, as the case study does
    g <- small_field()
    dg <- vol_payload(g, tempfile("pl"), created = "x")$manifest$display
    expect_type(dg$vmin, "integer")
})

test_that("the single file goes to the temporary directory by default", {
    # Regression: the default path was beside the template, inside the
    # installed package.
    p <- vol_payload(small_field(), tempfile("pl"))
    out <- vol_standalone(p)
    on.exit(unlink(out))
    expect_identical(normalizePath(dirname(out)), normalizePath(tempdir()))
})

test_that("a field that is not geographic is reported without metres", {
    f <- small_field()
    f$geographic <- FALSE
    p <- vol_payload(f, tempfile("pl"))
    expect_output(print(p), "depths 0 to 20\n", fixed = FALSE)
})

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.