context("(0) Basic tests for CRAN")
# These are just a very rudimentary tests that don't require time or saved files.
# The full range of tests is run locally.


test_that("Basic functionality works", {

  RoBSA:::.load_RoBSA_module()

  # fit a default model
  df <- data.frame(
    time = c(0.5, 1.0, 0.2, 0.8, 0.1, 0.1, 0.8, 0.4, 0.2, 0.8),
    cens = c(  0,   1,   1,   0,   0,   1,   0,   0,   1,   1),
    x_c  = c(0.2, 0.8, 0.1, 0.3, 0.5, 0.4, 0.2, 0.1, 0.8, 0.9),
    x_d  = c("A", "A", "A", "B", "B", "B", "A", "A", "B", "B")
  )

  fit <- suppressWarnings(RoBSA(Surv(time, cens) ~ x_c + x_d, data = df, chains = 1, burnin = 50, sample = 100, autofit = FALSE, seed = 1))

  expect_equal(TRUE, is.RoBSA(fit))

  expect_equal(
    capture_output_lines(fit, print = TRUE, width = 150),
    c("Call:"                                                                ,
      "RoBSA(formula = Surv(time, cens) ~ x_c + x_d, data = df, chains = 1, ",
      "    sample = 100, burnin = 50, autofit = FALSE, seed = 1)"            ,
      ""                                                                     ,
      "Estimates:"                                                           ,
      "       x_c     x_d[B] "                                               ,
      "-0.1492768 -0.2029159 "                                               ))

  expect_equal(
    capture_output_lines(summary(fit), print = TRUE, width = 150),
    c("Call:"                                                                                                                                                              ,
      "RoBSA(formula = Surv(time, cens) ~ x_c + x_d, data = df, chains = 1, "                                                                                              ,
      "    sample = 100, burnin = 50, autofit = FALSE, seed = 1)"                                                                                                          ,
      ""                                                                                                                                                                   ,
      "Robust Bayesian survival analysis"                                                                                                                                  ,
      "Distributions summary:"                                                                                                                                             ,
      "            Models Prior prob. Post. prob. Inclusion BF"                                                                                                            ,
      "exp-aft       4/20       0.200       0.362        2.272"                                                                                                            ,
      "weibull-aft   4/20       0.200       0.153        0.721"                                                                                                            ,
      "lnorm-aft     4/20       0.200       0.149        0.702"                                                                                                            ,
      "llogis-aft    4/20       0.200       0.142        0.662"                                                                                                            ,
      "gamma-aft     4/20       0.200       0.194        0.961"                                                                                                            ,
      ""                                                                                                                                                                   ,
      "Components summary:"                                                                                                                                                ,
      "    Models Prior prob. Post. prob. Inclusion BF"                                                                                                                    ,
      "x_c  10/20       0.500       0.470        0.888"                                                                                                                    ,
      "x_d  10/20       0.500       0.447        0.807"                                                                                                                    ,
      ""                                                                                                                                                                   ,
      "Model-averaged estimates:"                                                                                                                                          ,
      "         Mean Median  0.025 0.975"                                                                                                                                  ,
      "x_c    -0.149  0.000 -1.596 0.944"                                                                                                                                  ,
      "x_d[B] -0.203  0.000 -1.568 0.692"                                                                                                                                  ,
      "\033[0;31mThe continuous predictors 'x_c' are not scaled. Note that extra care need to be taken when specifying prior distributions for unscaled predictors.\033[0m",
      "\033[0;31mModel (1): ESS 84 is lower than the set target (500).\033[0m"                                                                                             ,
      "\033[0;31mModel (2): ESS 37 is lower than the set target (500).\033[0m"                                                                                             ,
      "\033[0;31mModel (3): ESS 33 is lower than the set target (500).\033[0m"                                                                                             ,
      "\033[0;31mModel (4): ESS 38 is lower than the set target (500).\033[0m"                                                                                             ,
      "\033[0;31mThere were another 15 warnings. To see all warnings call 'check_RoBSA(fit)'.\033[0m"))

})
