test_that("lmer interaction is audited as an omnibus term", {
  skip_if_not_installed("lme4")
  dat <- lme4::sleepstudy
  dat$criterion <- factor(rep(c("A", "B"), length.out = nrow(dat)))
  dat$model <- factor(rep(c("M1", "M2"), each = 2, length.out = nrow(dat)))
  dat$rater <- dat$Subject

  out <- statwitness(
    Reaction ~ criterion * model + (1 | rater),
    data = dat,
    focus = "criterion:model",
    exclude = "optimizer_agreement",
    audit_level = "core"
  )

  expect_s3_class(out, "statwitness_audit")
  expect_identical(out$model_class, "lmerMod")
  expect_identical(out$focus_kind, "term")
  expect_true(any(out$tests$id == "group_label_invariance"))
  expect_true(any(out$tests$id == "mixed_convergence"))
})

test_that("glmer is selected from a random-effects formula and family", {
  skip_if_not_installed("lme4")
  set.seed(1)
  dat <- data.frame(
    rater = factor(rep(seq_len(30), each = 8)),
    criterion = factor(rep(c("A", "B"), 120)),
    model = factor(rep(c("M1", "M2"), each = 2, length.out = 240))
  )
  b <- rnorm(30, 0, 0.5)[dat$rater]
  dat$correct <- rbinom(nrow(dat), 1, plogis(-0.2 + 0.4 * (dat$model == "M2") + b))

  out <- statwitness(
    correct ~ criterion * model + (1 | rater),
    data = dat,
    family = binomial(),
    focus = "criterion:model",
    exclude = "optimizer_agreement",
    audit_level = "core"
  )

  expect_identical(out$model_class, "glmerMod")
  expect_identical(out$method, "glmer")
})
