library(testthat)

test_that("rejection_rates_args", {

model <-
"
m ~ x + z + x:z
y ~ m + x
"

model_es <-
"
m ~ x:z: s
m ~ x: m
y ~ m: m
y ~ x: s
"

out <- power4test(
            nrep = 2,
            model = model,
            pop_es = model_es,
            n = 100,
            test_fun = test_parameters,
            test_args = list(pars = "m~x"),
            ci_type = "mc",
            R = 100,
            iseed = 1234,
            parallel = FALSE,
            progress = !is_testing())

rejection_rates(out)

out2 <- power4test(
            out,
            test_fun = test_parameters,
            test_args = list(pars = c("m~z", "y~m")),
            iseed = 1234,
            parallel = FALSE,
            progress = !is_testing())

(tmp <- rejection_rates(out2))

expect_equal(nrow(tmp),
             3)

out3 <- power4test(
            out2,
            test_fun = test_index_of_mome,
            test_args = list(x = "x",
                             y = "y",
                             m = "m",
                             w = "z"),
            iseed = 1234,
            parallel = FALSE,
            progress = !is_testing(),
            rejection_rates_args = list(merge_all_tests = TRUE,
                                        collapse = "all_sig"))

(tmp <- rejection_rates(out3))

expect_equal(nrow(tmp),
             1)

(tmp <- rejection_rates(out3,
                        merge_all_tests = FALSE))

expect_equal(nrow(tmp),
             3)

(tmp <- rejection_rates(out3,
                        collapse = "none"))

expect_equal(nrow(tmp),
             4)

(tmp <- rejection_rates(out3,
                        collapse = "none",
                        merge_all_tests = FALSE))

expect_equal(nrow(tmp),
             4)

expect_error(out4 <- power4test(
                        out3,
                        rejection_rates_args = list(merge_all_tests = FALSE,
                                                    collapse = "none"))
            )

out <- power4test_by_n(out3,
                       n = c(100, 110),
                       by_seed = 1234,
                       progress = !is_testing())

(tmp <- rejection_rates(out))

expect_equal(tmp$est,
             c(NaN, NaN))

(tmp <- rejection_rates(out,
                        merge_all_tests = FALSE))

expect_equal(nrow(tmp),
             6)

(tmp <- rejection_rates(out,
                        collapse = "none",
                        merge_all_tests = FALSE))

expect_equal(nrow(tmp),
             8)

})
