skip_on_cran()

library(testthat)

test_that("indirect effect: lavaan", {

model_simple_med <-
"
m ~ a*x
y ~ b*m + x
ab := a * b
"

model_simple_med_es <- c("y ~ m" = "l",
                         "m ~ x" = "m",
                         "y ~ x" = "n")
k <- c(y = 3,
       m = 3,
       x = 3)
rel <- c(y = .70,
         m = .70,
         x = .70)

sim_only <- power4test(nrep = 5,
                       model = model_simple_med,
                       pop_es = model_simple_med_es,
                       n = 100,
                       number_of_indicators = k,
                       reliability = rel,
                       fit_model_args = list(estimator = "ML"),
                       R = 100,
                       do_the_test = FALSE,
                       progress = !is_testing(),
                       iseed = 1234)

test_ind <- power4test(object = sim_only,
                       test_fun = test_indirect_effect,
                       test_args = list(x = "x",
                                        m = "m",
                                        y = "y",
                                        mc_ci = TRUE),
                       progress = !is_testing())

(chk <- test_summary(test_ind))
expect_true(length(chk) == 1)

# # This test is sensitive the RNG. Need to revise
# test_indb <- power4test(object = sim_only,
#                        test_fun = test_indirect_effect,
#                        test_args = list(x = "x",
#                                         m = "m",
#                                         y = "y",
#                                         mc_ci = TRUE,
#                                         test_method = "pvalue"),
#                        progress = !is_testing())
# expect_equal(test_summary(test_ind)[[1]]["sig"],
#              test_summary(test_indb)[[1]]["sig"])

test_ind2 <- power4test(object = test_ind,
                       test_fun = test_indirect_effect,
                       test_args = list(x = "x",
                                        y = "m",
                                        mc_ci = TRUE),
                       progress = !is_testing())

(chk <- test_summary(test_ind2))
expect_true(length(chk) == 2)

# # This test is sensitive the RNG. Need to revise
# test_ind2b <- power4test(object = test_ind,
#                        test_fun = test_indirect_effect,
#                        test_args = list(x = "x",
#                                         y = "m",
#                                         mc_ci = TRUE,
#                                         test_method = "pvalue"),
#                        progress = !is_testing())
# expect_equal(test_summary(test_ind2)[[1]]["sig"],
#              test_summary(test_ind2b)[[1]]["sig"])

test_ind3 <- power4test(object = test_ind2,
                       test_fun = test_indirect_effect,
                       test_args = list(x = "m",
                                        y = "y",
                                        mc_ci = TRUE),
                       progress = !is_testing())

(chk <- test_summary(test_ind3))
expect_true(length(chk) == 3)

# # This test is sensitive the RNG. Need to revise
# test_ind3b <- power4test(object = test_ind2,
#                        test_fun = test_indirect_effect,
#                        test_args = list(x = "m",
#                                         y = "y",
#                                         mc_ci = TRUE,
#                                         test_method = "pvalue"),
#                        progress = !is_testing())
# expect_equal(test_summary(test_ind3)[[1]]["sig"],
#              test_summary(test_ind3b)[[1]]["sig"])

})
