tests/testthat/test-agent-json.R

# Results for programs: lago_as_list(), lago_to_json() and lago_summary_text().

agent_base <- function(...) {
  args <- utils::modifyList(list(
    data = mtcars, outcome_name = "mpg", outcome_type = "continuous",
    glm_family = "gaussian", link = "identity",
    intervention_components = c("gear", "qsec"),
    intervention_lower_bounds = c(3, 14), intervention_upper_bounds = c(5, 23),
    cost_list_of_vectors = list(c(0, 4), c(4, 6)), outcome_goal = 25,
    confidence_set_grid_step_size = c(0.5, 1), quiet = TRUE
  ), list(...))
  suppressWarnings(do.call(lago_optimization, args))
}

test_that("an optimization result becomes a plain list with a fixed shape", {
  r <- agent_base()
  x <- lago_as_list(r)
  expect_identical(x$schema_version, "1")
  expect_identical(x$kind, "lago_optimization")
  expect_named(x, c("schema_version", "kind", "summary", "inputs", "recommendation", "confidence_set", "model"))
  expect_identical(x$recommendation$interventions, list(gear = r$rec_int[1], qsec = r$rec_int[2]))
  expect_identical(x$recommendation$cost, r$rec_int_cost)
  expect_true(x$recommendation$goal_reached)
  expect_equal(x$recommendation$outcome_ci$lower, unname(r$est_outcome_ci[1]))
  expect_equal(x$inputs$confidence_level, 0.95)
  expect_identical(x$inputs$cost_coefficients$qsec, list(4, 6))
  expect_identical(x$confidence_set$n_rows, nrow(r$cs))
  expect_length(x$confidence_set$rows, nrow(r$cs))
  expect_null(lago_as_list(r, confidence_set_rows = FALSE)$confidence_set$rows)
  expect_identical(x$model$coefficients[[1]]$term, "(Intercept)")
  expect_match(x$model$standard_errors, "model based")
})

test_that("the JSON reads back to the same numbers", {
  r <- agent_base()
  back <- jsonlite::fromJSON(lago_to_json(r))
  expect_identical(back$recommendation$cost, r$rec_int_cost)
  expect_identical(back$recommendation$interventions$qsec, r$rec_int[2])
  expect_identical(back$summary, lago_summary_text(r))
  expect_match(as.character(lago_to_json(r, confidence_set_rows = FALSE, pretty = TRUE)), "\n  \"kind\"", fixed = TRUE)
})

test_that("the summary says what to do and how sure the estimate is", {
  r <- agent_base()
  s <- lago_summary_text(r)
  expect_match(s, "The cheapest intervention estimated to bring mpg to at least 25 sets gear = 5 and qsec = ", fixed = TRUE)
  expect_match(s, "(95% interval ", fixed = TRUE)
  expect_match(s, "confidence set", fixed = TRUE)
  no_cs <- agent_base(include_confidence_set = FALSE)
  expect_false(grepl("confidence set", lago_summary_text(no_cs)))
  expect_false(lago_as_list(no_cs)$confidence_set$computed)
  low <- agent_base(outcome_goal = 15, outcome_goal_intention = "minimize")
  expect_match(lago_summary_text(low), "to at most 15", fixed = TRUE)
})

test_that("the summary of a shrunk result says the goal is out of reach", {
  far <- agent_base(outcome_goal = 60)
  s <- lago_summary_text(far)
  expect_match(s, "is not estimated to be reachable", fixed = TRUE)
  expect_match(s, "below the goal", fixed = TRUE)
  x <- lago_as_list(far)
  expect_false(x$recommendation$goal_reached)
  expect_null(x$recommendation$outcome_ci)
  low <- agent_base(outcome_goal = 1, outcome_goal_intention = "minimize")
  expect_match(lago_summary_text(low), "above the goal", fixed = TRUE)
})

test_that("three or more components are listed with commas", {
  expect_identical(
    LAGOtrials:::.lago_settings_text(c(1, 2, 3), c("a", "b", "c")),
    "a = 1, b = 2 and c = 3"
  )
})

test_that("a power goal and interaction terms appear in the list and the summary", {
  # a result whose goal came from a power goal
  r <- agent_base(include_confidence_set = FALSE)
  r$power_goal <- 0.8
  r$effective_outcome_goal <- 25
  expect_match(lago_summary_text(r), "comes from the power goal of 0.8", fixed = TRUE)
  expect_identical(lago_as_list(r)$inputs$power_goal, 0.8)
  d <- mtcars
  d[["gear:qsec"]] <- d$gear * d$qsec
  inter <- agent_base(
    data = d, intervention_components = c("gear", "qsec", "gear:qsec"),
    main_components = c("gear", "qsec"), include_interaction_terms = TRUE,
    include_confidence_set = FALSE
  )
  x <- lago_as_list(inter)
  expect_named(x$recommendation$interventions, c("gear", "qsec"))
  expect_identical(x$inputs$main_components, list("gear", "qsec"))
})

test_that("a confidence level other than 95% is kept", {
  r <- agent_base(confidence_set_alpha = 0.2)
  expect_equal(lago_as_list(r)$inputs$confidence_level, 0.8)
  attr(r, "lago_call_args") <- NULL
  expect_equal(LAGOtrials:::.lago_confidence_level(r), 0.95)
})

test_that("a sweep becomes one entry per run, with the error of a failed run", {
  r <- agent_base(include_confidence_set = FALSE)
  s <- suppressWarnings(lago_sensitivity(r, parameter = "outcome_goal", values = c(24, 25, 60)))
  x <- lago_as_list(s)
  expect_identical(x$kind, "lago_sensitivity")
  expect_length(x$runs, 3)
  expect_identical(x$runs[[1]]$status, "ok")
  expect_true(x$runs[[3]]$shrinking_method_used)
  expect_match(x$summary, "out of reach at outcome_goal = 60", fixed = TRUE)
  # a failed run keeps its message and code
  s2 <- s
  s2$status[2] <- "error"
  attr(s2, "error_messages") <- c("25" = "it failed")
  attr(s2, "error_codes") <- c("25" = "internal")
  attr(s2, "shrinking_method_used") <- NULL
  x2 <- lago_as_list(s2)
  expect_identical(x2$runs[[2]]$error_message, "it failed")
  expect_identical(x2$runs[[2]]$error_code, "internal")
  expect_null(x2$runs[[1]]$error_message)
  expect_match(lago_summary_text(s2), "and 1 run failed", fixed = TRUE)
  one <- s[1, ]
  attributes(one)[c("parameter", "component_names", "shrinking_method_used")] <-
    attributes(s)[c("parameter", "component_names", "shrinking_method_used")]
  attr(one, "shrinking_method_used") <- FALSE
  expect_match(lago_summary_text(one), "over 1 value, and 0 runs failed. The cost is ", fixed = TRUE)
  none <- s
  none$status <- "error"
  expect_false(grepl("The cost is", lago_summary_text(none)))
})

test_that("a sweep of dates gives their text", {
  r <- agent_base(include_confidence_set = FALSE)
  s <- suppressWarnings(lago_sensitivity(r, parameter = "outcome_goal", values = c(24, 25)))
  s$value <- as.Date(c("2024-01-01", "2024-02-01"))
  expect_identical(lago_as_list(s)$runs[[2]]$value, "2024-02-01")
})

test_that("a budget result says what the budget buys", {
  base <- list(
    data = mtcars, outcome_name = "mpg", outcome_type = "continuous",
    intervention_components = c("gear", "qsec"), intervention_lower_bounds = c(3, 14),
    intervention_upper_bounds = c(5, 23), cost_list_of_vectors = list(c(0, 4), c(4, 6)),
    n_grid = 3
  )
  b <- suppressWarnings(do.call(lago_budget, c(base, budget = 120)))
  x <- lago_as_list(b)
  expect_identical(x$kind, "lago_budget")
  expect_true(x$feasible)
  expect_match(x$summary, "Within the budget of 120, the highest estimated outcome is", fixed = TRUE)
  expect_match(x$summary, "The budget binds", fixed = TRUE)
  rich <- suppressWarnings(do.call(lago_budget, c(base, budget = 1e6)))
  expect_match(lago_summary_text(rich), "does not bind", fixed = TRUE)
  poor <- suppressWarnings(do.call(lago_budget, c(base, budget = 1)))
  expect_match(lago_summary_text(poor), "No reachable intervention fits the budget of 1.", fixed = TRUE)
  expect_true(all(is.na(unlist(lago_as_list(poor)$recommendation$interventions))))
  poor$run_error <- "it failed"
  expect_match(lago_summary_text(poor), "Every run failed: it failed", fixed = TRUE)
  b$rec_int <- unname(b$rec_int)
  expect_match(lago_summary_text(b), "with 5, ", fixed = TRUE)
  low <- suppressWarnings(do.call(lago_budget, c(base, budget = 120, outcome_goal_intention = "minimize")))
  expect_match(lago_summary_text(low), "the lowest estimated outcome", fixed = TRUE)
})

test_that("other objects are refused with a code", {
  e <- tryCatch(lago_as_list(1), lago_error = function(e) e)
  expect_identical(e$code, "not_a_lago_result")
  expect_identical(e$argument, "x")
  e <- tryCatch(lago_summary_text("a"), lago_error = function(e) e)
  expect_identical(e$code, "not_a_lago_result")
})

test_that("the JSON Schema ships with the package", {
  path <- system.file("schema", "lago-result.schema.json", package = "LAGOtrials")
  expect_true(nzchar(path))
  schema <- jsonlite::fromJSON(path, simplifyVector = FALSE)
  expect_identical(schema$properties$schema_version$const, "1")
})

test_that("the report starts with the summary", {
  skip_on_cran()
  skip_if_not(rmarkdown::pandoc_available())
  r <- agent_base(include_confidence_set = FALSE)
  out <- file.path(tempdir(), "agent-summary.html")
  suppressWarnings(lago_report(r, output_file = out, open = FALSE, quiet = TRUE))
  html <- paste(readLines(out, warn = FALSE), collapse = "\n")
  expect_match(html, "Summary", fixed = TRUE)
  expect_match(html, "The cheapest intervention estimated to bring mpg", fixed = TRUE)
})

Try the LAGOtrials package in your browser

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

LAGOtrials documentation built on Dec. 4, 2026, 5:10 p.m.