tests/testthat/test-ai.R

# Tests for universal AI extraction
# Save as tests/testthat/test-ai.R

testthat::test_that("universal extractor finds a descriptive result table", {
  x <- list(
    analysis = "Descriptive statistics",
    outcome = "Hypertension",
    sample = list(n = 812),
    raw = data.frame(id = 1:1000, value = rnorm(1000)),
    table = data.frame(
      Variable = c("Male", "Age"),
      Value = c("42.3%", "57.8 +/- 13.2")
    ),
    footnote = "Values are n (%) or mean +/- SD"
  )

  z <- R4VN:::.r4vn_ai_extract_text(x, budgettokens = 1500)

  testthat::expect_true(grepl("Male", z$text, fixed = TRUE))
  testthat::expect_true(grepl("Age", z$text, fixed = TRUE))
  testthat::expect_false(grepl("1000", z$text, fixed = TRUE))
  testthat::expect_lte(z$estimated_tokens, 1500)
})

testthat::test_that("extractor works with survival-like objects without command-specific prepare", {
  x <- structure(
    list(
      title = "Survival analysis",
      outcome = "Death",
      event = "Yes",
      results = list(
        cox = data.frame(
          Variable = c("Treatment", "Age"),
          HR = c(0.71, 1.04),
          Lower = c(0.55, 1.02),
          Upper = c(0.92, 1.06),
          P = c(0.009, 0.001)
        ),
        logrank = data.frame(
          Test = "Log-rank",
          Chisq = 7.82,
          P = 0.005
        )
      ),
      html = paste(rep("<div>formatting</div>", 1000), collapse = "")
    ),
    class = "r4vn_tabsurv"
  )

  z <- R4VN:::.r4vn_ai_extract_text(x, budgettokens = 1800)

  testthat::expect_true(grepl("Treatment", z$text, fixed = TRUE))
  testthat::expect_true(grepl("Log-rank", z$text, fixed = TRUE))
  testthat::expect_false(grepl("formatting", z$text, fixed = TRUE))
  testthat::expect_lte(z$estimated_tokens, 1800)
})

testthat::test_that("extractor works with longitudinal-like objects", {
  x <- structure(
    list(
      analysis = "Longitudinal mixed model",
      result = data.frame(
        Term = c("Group", "Time", "Group x Time"),
        Beta = c(-0.20, -0.35, -0.50),
        Lower = c(-0.45, -0.50, -0.80),
        Upper = c(0.05, -0.20, -0.20),
        P = c(0.11, 0.001, 0.002)
      ),
      model = list(
        residuals = rnorm(2000),
        fitted = rnorm(2000)
      )
    ),
    class = "r4vn_tablong"
  )

  z <- R4VN:::.r4vn_ai_extract_text(x, budgettokens = 1800)

  testthat::expect_true(grepl("Group x Time", z$text, fixed = TRUE))
  testthat::expect_false(grepl("residuals", z$text, fixed = TRUE))
  testthat::expect_lte(z$estimated_tokens, 1800)
})

testthat::test_that("duplicate tables are removed", {
  tb <- data.frame(
    Variable = c("Age", "Sex"),
    Estimate = c("1.05", "0.80"),
    P = c("0.001", "0.12")
  )

  x <- list(table = tb, formatted_table = tb, result = tb)
  z <- R4VN:::.r4vn_ai_extract_text(x, budgettokens = 1500)

  testthat::expect_equal(z$kept, 1L)
})

# Live API test - run manually only:
#
# t2 <- tab(...)
# t2ai <- aiask(t2)
# t2ai$ai$comment
# t2ai$ai$input
# t2ai$ai$usage
# t2ai$ai$attempts

Try the R4VN package in your browser

Any scripts or data that you put into this service are public.

R4VN documentation built on Sept. 30, 2026, 5:13 p.m.