## ---- ai_test_retest ------------------------------------------------- test_that("test-retest ICC is high for stable repeated measurements", { # 5 prompts, 2 occasions, near-identical across occasions -> high ICC responses <- cbind( c(1, 2, 3, 4, 5), c(1.1, 2.0, 3.1, 3.9, 5.0) ) out <- ai_test_retest(responses) expect_type(out, "list") expect_true(all(c("icc", "msb", "msj", "mse") %in% names(out))) expect_true(is.finite(out$icc)) expect_gt(out$icc, 0.90) }) test_that("test-retest ICC is low when occasions are unrelated noise", { set.seed(1) responses <- cbind(rnorm(30), rnorm(30)) # independent columns out <- ai_test_retest(responses) expect_lt(out$icc, 0.5) }) test_that("test-retest requires at least 2 rows and 2 columns", { expect_error(ai_test_retest(matrix(1:3, nrow = 1)), "at least 2 rows") expect_error(ai_test_retest(matrix(1:3, ncol = 1)), "at least 2 rows") }) test_that("test-retest drops rows with missing values and messages", { responses <- cbind(c(1, 2, 3, NA, 5), c(1, 2, 3, 4, 5)) expect_message(ai_test_retest(responses), "dropping") }) test_that("test-retest errors when too few complete rows remain", { responses <- cbind(c(1, NA, NA), c(1, 2, 3)) expect_error(suppressMessages(ai_test_retest(responses)), "complete rows") }) ## ---- ai_internal_consistency --------------------------------------- test_that("Cronbach's alpha is high for strongly correlated items", { set.seed(2) latent <- rnorm(50) responses <- sapply(1:4, function(i) latent + rnorm(50, sd = 0.2)) out <- ai_internal_consistency(responses) expect_gt(out$alpha, 0.90) expect_equal(out$n_items, 4) }) test_that("internal consistency errors with fewer than 2 items", { expect_error(ai_internal_consistency(matrix(1:5, ncol = 1)), "at least 2 items") }) test_that("internal consistency returns NA alpha when total variance is zero", { responses <- matrix(rep(c(1, 1), each = 5), ncol = 2) # constant rows expect_warning(out <- ai_internal_consistency(responses), "variance is zero") expect_true(is.na(out$alpha)) }) ## ---- ai_reliability (wrapper) -------------------------------------- test_that("ai_reliability returns test_retest and optional consistency", { set.seed(3) responses <- matrix(rnorm(30, mean = 5), nrow = 10, ncol = 3) out <- ai_reliability(responses) expect_s3_class(out, "aiEvalR_reliability") expect_true("test_retest" %in% names(out)) expect_false("internal_consistency" %in% names(out)) out2 <- ai_reliability(responses, items = TRUE) expect_true("internal_consistency" %in% names(out2)) }) ## ---- ai_bootstrap_reliability -------------------------------------- test_that("bootstrap reliability returns an interval bracketing the estimate", { set.seed(4) latent <- rnorm(20) responses <- cbind(latent + rnorm(20, sd = 0.3), latent + rnorm(20, sd = 0.3)) out <- ai_bootstrap_reliability(responses, n_boot = 200, seed = 42) expect_true(all(c("estimate", "ci_lower", "ci_upper", "n_valid") %in% names(out))) expect_lte(out$ci_lower, out$estimate) expect_gte(out$ci_upper, out$estimate) }) test_that("bootstrap reliability is reproducible with a fixed seed", { set.seed(5) responses <- cbind(rnorm(15), rnorm(15)) a <- ai_bootstrap_reliability(responses, n_boot = 100, seed = 99) b <- ai_bootstrap_reliability(responses, n_boot = 100, seed = 99) expect_equal(a$ci_lower, b$ci_lower) expect_equal(a$ci_upper, b$ci_upper) })