test_that("setup_interface adds cmdstanr when brms_backend = cmdstanr", { libraries <- character() same_library <- function(pkg) { libraries <<- c(libraries, pkg) invisible(NULL) } local_mocked_bindings( is_installed = function(pkg) TRUE, install_backend_package = function(...) { stop("install_backend_package should not run") }, .same_library = same_library, setup_cmdstanr = function(...) invisible(NULL), setup_brms = function(...) invisible(NULL), .package = "stanflow" ) setup_interface( interface = "brms", brms_backend = "cmdstanr", cores = 2, quiet = TRUE ) expect_equal(libraries, c("brms", "cmdstanr")) }) test_that("setup_interface aborts when cores are missing", { withr::local_options(list(mc.cores = NULL)) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE), "cores" ) }) test_that("setup_interface aborts when interface is missing", { expect_error( setup_interface(), "interface" ) }) test_that("setup_interface installs backends when reinstall = TRUE", { installed <- character() local_mocked_bindings( is_installed = function(pkg) TRUE, install_backend_package = function(pkg, ...) { installed <<- c(installed, pkg) }, .same_library = function(pkg) NULL, setup_cmdstanr = function(...) invisible(NULL), setup_rstan = function(...) invisible(NULL), .package = "stanflow" ) invisible( setup_interface( interface = c("cmdstanr", "rstan"), force = TRUE, reinstall = TRUE, cores = 2, quiet = TRUE ) ) expect_equal(installed, c("cmdstanr", "rstan")) }) test_that("setup_interface installs missing packages when not installed", { installed <- character() local_mocked_bindings( is_installed = function(pkg) FALSE, install_backend_package = function(pkg, ...) { installed <<- c(installed, pkg) }, .same_library = function(pkg) NULL, setup_brms = function(...) invisible(NULL), .package = "stanflow" ) invisible( setup_interface( interface = c("brms"), brms_backend = "rstan", force = FALSE, cores = 2, quiet = TRUE ) ) expect_equal(installed, "brms") }) test_that("setup_interface dry_run reports missing package setup without side effects", { calls <- character() local_mocked_bindings( is_installed = function(pkg) FALSE, is_interactive_session = function() FALSE, .same_library = function(pkg) { calls <<- c(calls, paste0("library:", pkg)) }, .package = "stanflow" ) withr::local_options(list(mc.cores = NULL, brms.backend = NULL)) expect_snapshot( setup_interface( interface = "brms", brms_backend = "rstan", force = FALSE, cores = 2, quiet = FALSE, dry_run = TRUE ) ) expect_equal(calls, character()) expect_null(getOption("mc.cores")) expect_null(getOption("brms.backend")) }) expect_setup_interface_dry_run_snapshot <- function(interface) { local_mocked_bindings( is_installed = function(pkg) TRUE, is_interactive_session = function() FALSE, .same_library = function(pkg) stop("dry run should not attach packages"), .package = "stanflow" ) if ("cmdstanr" %in% interface) { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() stop("missing"), cmdstan_version = function() stop("missing"), .package = "cmdstanr" ) } expect_snapshot( setup_interface( interface = interface, brms_backend = "rstan", cores = 1, quiet = FALSE, dry_run = TRUE ) ) } local({ interfaces <- c("brms", "cmdstanr", "rstan", "rstanarm") combinations <- do.call( c, Map(\(k) combn(interfaces, k, simplify = FALSE), seq_along(interfaces)) ) for (interface in combinations) { label <- paste(interface, collapse = ", ") test_that(paste0("setup_interface dry_run output is stable for ", label), { expect_setup_interface_dry_run_snapshot(interface) }) } }) test_that("setup_interface dry_run emits output even when quiet = TRUE", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) stop("should not attach"), .package = "stanflow" ) expect_snapshot( setup_interface( interface = "brms", brms_backend = "rstan", cores = 2, quiet = TRUE, dry_run = TRUE ) ) }) test_that("setup_interface dry_run returns packages that would be attached", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) stop("dry run should not attach packages"), setup_rstan = function(...) invisible(NULL), .package = "stanflow" ) expect_equal( setup_interface("rstan", brms_backend = "rstan", cores = 2, dry_run = TRUE), "rstan" ) }) test_that("setup_interface runs backend setup helpers", { calls <- character() local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) { calls <<- c(calls, paste0("library:", pkg)) invisible(NULL) }, setup_cmdstanr = function(...) { calls <<- c(calls, "setup_cmdstanr") invisible(NULL) }, setup_rstan = function(...) { calls <<- c(calls, "setup_rstan") invisible(NULL) }, setup_brms = function(...) { calls <<- c(calls, "setup_brms") invisible(NULL) }, setup_rstanarm = function(...) { calls <<- c(calls, "setup_rstanarm") invisible(NULL) }, .package = "stanflow" ) invisible( setup_interface( interface = c("cmdstanr", "rstan", "brms", "rstanarm"), brms_backend = "cmdstanr", cores = 2, quiet = TRUE, force = FALSE ) ) expect_equal( calls, c( "setup_cmdstanr", "library:cmdstanr", "setup_rstan", "library:rstan", "setup_brms", "library:brms", "setup_rstanarm", "library:rstanarm" ) ) }) test_that("setup_interface is silent when quiet = TRUE", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) NULL, setup_brms = function(...) invisible(NULL), .package = "stanflow" ) output <- capture_messages( setup_interface( interface = "brms", brms_backend = "rstan", cores = 2, quiet = TRUE ) ) expect_equal(output, character()) }) test_that("setup_interface defaults to stanflow.quiet option", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) NULL, setup_brms = function(...) invisible(NULL), .package = "stanflow" ) withr::local_options(list(stanflow.quiet = TRUE)) silent <- capture_messages( setup_interface(interface = "brms", brms_backend = "rstan", cores = 2) ) expect_equal(silent, character()) withr::local_options(list(stanflow.quiet = FALSE)) noisy <- capture_messages( setup_interface(interface = "brms", brms_backend = "rstan", cores = 2) ) expect_true(length(noisy) > 0) }) test_that("install_backend_package installs from Stan universe when dev = TRUE", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") )) called <- list() local_mocked_bindings( install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" ) install_backend_package( "cmdstanr", dev = TRUE, quiet = TRUE, force = TRUE, reinstall = FALSE ) expect_equal(called$pkg, "cmdstanr") expect_equal(unname(called$repos[1]), "https://stan-dev.r-universe.dev") }) test_that("install_backend_package respects quiet = FALSE", { called <- list() local_mocked_bindings( install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" ) install_backend_package( "cmdstanr", dev = FALSE, quiet = FALSE, force = TRUE, reinstall = FALSE ) expect_false(called$quiet) }) test_that("install_backend_package installs from multiverse when dev = FALSE", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") )) called <- list() local_mocked_bindings( install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" ) install_backend_package( "cmdstanr", dev = FALSE, quiet = TRUE, force = TRUE, reinstall = FALSE ) expect_equal(called$pkg, "cmdstanr") expect_equal(unname(called$repos[1]), "https://community.r-multiverse.org") expect_equal(unname(called$repos[2]), unname(getOption("repos"))) }) test_that("install_backend_package aborts when user declines interactive install", { withr::local_options(list(stanflow.force_interactive = TRUE)) local_mocked_bindings( menu = function(...) 2, install.packages = function(...) stop("should not reach install"), .package = "utils" ) expect_error( install_backend_package( "cmdstanr", dev = FALSE, quiet = TRUE, force = FALSE, reinstall = FALSE ), "Installation of" ) }) test_that("install_backend_package installs when user accepts interactive prompt", { called <- list() withr::local_options(list(stanflow.force_interactive = TRUE)) local_mocked_bindings( menu = function(...) 1, install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" ) invisible( install_backend_package( "cmdstanr", dev = FALSE, quiet = TRUE, force = FALSE, reinstall = FALSE ) ) expect_equal(called$pkg, "cmdstanr") }) test_that("setup_brms configures brms backend", { withr::local_options(list(mc.cores = NULL, brms.backend = NULL)) setup_brms(quiet = TRUE, brms_backend = "cmdstanr", cores = 8) expect_equal(getOption("mc.cores"), 8) expect_equal(getOption("brms.backend"), "cmdstanr") }) test_that("setup_brms emits configuration message when quiet = FALSE", { withr::local_options(list(mc.cores = NULL, brms.backend = NULL)) expect_snapshot( setup_brms(quiet = FALSE, brms_backend = "rstan", cores = 4) ) }) test_that("setup_rstan configures parallel cores and rstan options", { withr::local_options(list(mc.cores = NULL)) rstan_args <- NULL skip_if_not_installed("rstan") rstan_options <- function(...) { rstan_args <<- list(...) invisible(NULL) } local_mocked_bindings( rstan_options = rstan_options, .package = "rstan" ) setup_rstan(quiet = TRUE, cores = 6, rstan_auto_write = FALSE) expect_equal(getOption("mc.cores"), 6) expect_false(rstan_args$auto_write) }) test_that("setup_rstan emits configuration message when quiet = FALSE", { withr::local_options(list(mc.cores = NULL)) skip_if_not_installed("rstan") local_mocked_bindings( rstan_options = function(...) NULL, .package = "rstan" ) expect_snapshot( setup_rstan(quiet = FALSE, cores = 3, rstan_auto_write = FALSE) ) }) test_that("setup_rstan dry_run does not require rstan or set options", { withr::local_options(list(mc.cores = NULL)) expect_snapshot( setup_rstan( quiet = FALSE, cores = 6, rstan_auto_write = FALSE, dry_run = TRUE ) ) expect_null(getOption("mc.cores")) }) test_that("setup_rstanarm emits configuration message when quiet = FALSE", { withr::local_options(list(mc.cores = NULL)) expect_snapshot( setup_rstanarm(quiet = FALSE, cores = 5) ) }) test_that("setup_rstanarm dry_run does not set options", { withr::local_options(list(mc.cores = NULL)) expect_snapshot( setup_rstanarm( quiet = FALSE, cores = 5, dry_run = TRUE ) ) expect_null(getOption("mc.cores")) }) test_that("setup_brms dry_run does not set options", { withr::local_options(list(mc.cores = NULL, brms.backend = NULL)) expect_snapshot( setup_brms( quiet = FALSE, brms_backend = "cmdstanr", cores = 8, dry_run = TRUE ) ) expect_null(getOption("mc.cores")) expect_null(getOption("brms.backend")) }) test_that("setup_cmdstanr aborts when toolchain check fails", { skip_on_cran() skip_if_not_installed("cmdstanr") expect_error( { local_mocked_bindings( check_cmdstan_toolchain = function(...) stop("broken"), .package = "cmdstanr" ) setup_cmdstanr(quiet = TRUE, force = FALSE, cores = 2) }, "CmdStan toolchain check failed" ) }) test_that("setup_cmdstanr installs CmdStan when not ready and force = TRUE", { skip_on_cran() skip_if_not_installed("cmdstanr") installed <- FALSE install_cmdstan <- function(...) { installed <<- TRUE invisible(NULL) } local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() stop("missing"), cmdstan_version = function() stop("missing"), install_cmdstan = install_cmdstan, .package = "cmdstanr" ) invisible(setup_cmdstanr(quiet = TRUE, force = TRUE, cores = 2)) expect_true(installed) }) test_that("setup_cmdstanr dry_run does not require cmdstanr to be installed", { withr::local_options(list(mc.cores = NULL)) local_mocked_bindings( is_interactive_session = function() FALSE, .package = "stanflow" ) if (requireNamespace("cmdstanr", quietly = TRUE)) { local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() stop("missing"), cmdstan_version = function() stop("missing"), .package = "cmdstanr" ) } expect_snapshot( setup_cmdstanr( quiet = FALSE, force = FALSE, cores = 2, dry_run = TRUE ) ) expect_null(getOption("mc.cores")) }) test_that("setup_cmdstanr dry_run skips mutations but runs detection", { skip_on_cran() skip_if_not_installed("cmdstanr") calls <- character() local_mocked_bindings( check_cmdstan_toolchain = function(...) { calls <<- c(calls, "toolchain") TRUE }, cmdstan_path = function() { calls <<- c(calls, "path") stop("missing") }, cmdstan_version = function() { calls <<- c(calls, "version") stop("missing") }, install_cmdstan = function(...) { calls <<- c(calls, "install") invisible(NULL) }, .package = "cmdstanr" ) local_mocked_bindings( is_interactive_session = function() FALSE, .package = "stanflow" ) withr::local_options(list(mc.cores = NULL)) expect_snapshot( setup_cmdstanr( quiet = FALSE, force = FALSE, cores = 2, dry_run = TRUE ) ) expect_equal(calls, "path") expect_null(getOption("mc.cores")) }) test_that("setup_cmdstanr dry_run does not perform update checks", { skip_on_cran() skip_if_not_installed("cmdstanr") calls <- character() local_mocked_bindings( check_cmdstan_toolchain = function(...) { calls <<- c(calls, "toolchain") TRUE }, cmdstan_path = function() { calls <<- c(calls, "path") "/tmp" }, cmdstan_version = function() { calls <<- c(calls, "version") "2.31.0" }, install_cmdstan = function(...) { calls <<- c(calls, "install") invisible(NULL) }, .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) { calls <<- c(calls, "updates") stop("dry run should not check updates") }, .package = "base" ) withr::local_options(list(mc.cores = NULL)) expect_snapshot( setup_cmdstanr( quiet = FALSE, force = FALSE, check_updates = TRUE, cores = 2, dry_run = TRUE ) ) expect_equal(calls, c("path", "version")) expect_null(getOption("mc.cores")) }) test_that("setup_cmdstanr returns invisibly when up to date", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", install_cmdstan = function(...) stop("should not install"), .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("no network"), .package = "base" ) result <- setup_cmdstanr( quiet = TRUE, force = FALSE, check_updates = FALSE, cores = 2 ) expect_identical(result, TRUE) }) test_that("setup_cmdstanr does not check updates by default", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", install_cmdstan = function(...) stop("should not install"), .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("should not check updates"), .package = "base" ) expect_identical( setup_cmdstanr(quiet = TRUE, force = FALSE, cores = 2), TRUE ) }) test_that("setup_cmdstanr ignores failed update checks when CmdStan is ready", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", install_cmdstan = function(...) stop("should not install"), .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("no network"), .package = "base" ) result <- setup_cmdstanr( quiet = TRUE, force = FALSE, check_updates = TRUE, cores = 2 ) expect_identical(result, TRUE) }) test_that("try_fetch_latest_cmdstan_version parses latest release", { local_mocked_bindings( readLines = function(...) { c( "{", ' "tag_name": "v2.37.0"', "}" ) }, .package = "base" ) expect_equal(try_fetch_latest_cmdstan_version(), numeric_version("2.37.0")) }) test_that("try_fetch_latest_cmdstan_version returns NULL on bad responses", { local_mocked_bindings( readLines = function(...) "{}", .package = "base" ) expect_null(try_fetch_latest_cmdstan_version()) local_mocked_bindings( readLines = function(...) stop("offline"), .package = "base" ) expect_null(try_fetch_latest_cmdstan_version()) }) test_that("setup_cmdstanr is silent when quiet = TRUE", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("no network"), .package = "base" ) output <- capture_messages( setup_cmdstanr( quiet = TRUE, force = FALSE, check_updates = FALSE, cores = 2 ) ) expect_equal(output, character()) }) test_that("setup_interface aborts when install_backend_package fails", { local_mocked_bindings( is_installed = function(pkg) FALSE, install_backend_package = function(pkg, ...) { cli::cli_abort("Boom") }, setup_cmdstanr = function(...) invisible(NULL), .package = "stanflow" ) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE, cores = 2), "Boom" ) }) test_that("setup_interface handles same_library errors gracefully", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) stop("library error"), setup_cmdstanr = function(...) invisible(NULL), .package = "stanflow" ) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE, cores = 2), "library error" ) }) test_that("setup_interface does not attach packages before setup succeeds", { attached <- character() local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) { attached <<- c(attached, pkg) }, setup_cmdstanr = function(...) cli::cli_abort("Boom"), .package = "stanflow" ) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE, cores = 2), "Boom" ) expect_equal(attached, character()) }) test_that("setup_interface warns when brms_backend adds cmdstanr", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) NULL, install_backend_package = function(...) stop("install should not run"), setup_cmdstanr = function(...) invisible(NULL), setup_brms = function(...) invisible(NULL), .package = "stanflow" ) expect_snapshot( setup_interface( interface = c("brms"), brms_backend = "cmdstanr", cores = 2, quiet = FALSE ) ) })