context("update.mvgam")

skip_on_cran()

test_that("update() working correctly", {
  # Can update trend_model
  mod <- update(mvgam:::mvgam_example1,
                trend_model = AR(p = 2),
                run_model = FALSE)
  expect_true(inherits(mod, 'mvgam_prefit'))
  expect_true(attr(mod$model_data, 'trend_model') == 'AR2')
  expect_true(any(grepl('ar2~std_normal();',
                        gsub(' ', '', mod$model_file), fixed = TRUE)))
  expect_true(mod$call == mvgam:::mvgam_example1$call)

  # Update trend_model and formula
  mod <- update(mvgam:::mvgam_example1,
                formula = y ~ s(season, k = 6) - 1,
                trend_model = PW(),
                run_model = FALSE)
  expect_true(inherits(mod, 'mvgam_prefit'))
  expect_true(attr(mod$model_data, 'trend_model') == 'PWlinear')
  expect_true(any(grepl('to_vector(delta_trend)~double_exponential(0,changepoint_scale);',
                        gsub(' ', '', mod$model_file), fixed = TRUE)))
  expect_true(mod$call != mvgam:::mvgam_example1$call)
  expect_true(rlang::f_rhs(mod$call) == 's(season, k = 6) - 1')

  # Errors should pass from mvgam()
  expect_error(update(mvgam:::mvgam_example1,
                      formula = y ~ s(season, k = 6) - 1,
                      trend_model = PW(growth = 'logistic'),
                      run_model = FALSE),
               'Capacities must be supplied as a variable named "cap" for logistic growth')

  # Update to include shared observation params
  mod <- update(mvgam:::mvgam_example1,
                share_obs_params = TRUE,
                run_model = FALSE)
  expect_true(any(grepl('real<lower=0>sigma_obs;',
                        gsub(' ', '', mod$model_file), fixed = TRUE)))
})

