tests/testthat/helper-model-check.R

# Helpers shared by the tests of the reply-model check and the model-name
# stops, in test-model-check.R and test-model-stops.R.

# A status-200 reply on `route` whose `model` field is `model_json`, JSON
# text, or absent when `NULL`. `text_json` is the answer text, and `NULL`
# gives a reply with no answer text.
reply_with_model <- function(route, model_json, text_json = quoted("reply")) {
  if (route == "openai") {
    content <- if (is.null(text_json)) "null" else text_json
    choice <- json_object(
      index = "0",
      message = json_object(role = quoted("assistant"), content = content),
      finish_reason = quoted("stop")
    )
    body <- json_object(
      id = quoted("chatcmpl-1"),
      model = model_json,
      choices = json_array(choice)
    )
  } else {
    items <- if (is.null(text_json)) {
      character()
    } else {
      responses_message(output_text(text_json))
    }
    body <- json_object(model = model_json, output = json_array(items))
  }
  mock_response(200L, body)
}

# A model list whose entries are given as `key = c(instance ids)`.
model_list_response <- function(...) {
  entries <- list(...)
  models <- lapply(names(entries), function(key) {
    list(
      type = "llm",
      key = key,
      loaded_instances = lapply(entries[[key]], \(id) list(id = id))
    )
  })
  mock_response(
    200L,
    as.character(jsonlite::toJSON(list(models = models), auto_unbox = TRUE))
  )
}

# Assert that `err` reports a reply from `reply_model` to a call for `asked`.
expect_mismatch <- function(err, asked, reply_model, info = NULL) {
  expect_true(inherits(err, "rlmstudio_model_mismatch"), info = info)
  expect_true(inherits(err, "rlmstudio_bad_response"), info = info)
  expect_identical(err$model, asked, info = info)
  expect_identical(err$reply_model, reply_model, info = info)
  expect_identical(err$status, 200L, info = info)
  msg <- gsub("[[:space:]]+", " ", cli::ansi_strip(conditionMessage(err)))
  expect_true(grepl(asked, msg, fixed = TRUE), info = info)
  expect_true(grepl(reply_model, msg, fixed = TRUE), info = info)
  expect_false(grepl("simplify", msg, fixed = TRUE), info = info)
}

# Log every request that reaches the real req_perform(), so a test can count
# the requests of a recorded playback.
local_request_log <- function(.env = parent.frame()) {
  log <- new.env(parent = emptyenv())
  log$urls <- character()
  real <- httr2::req_perform
  testthat::local_mocked_bindings(
    req_perform = function(req, ...) {
      log$urls <- c(log$urls, req$url)
      real(req, ...)
    },
    .package = "httr2",
    .env = .env
  )
  log
}

# Count the requests in `urls` that went to each endpoint: the chat routes,
# the embeddings route, and the model list of the lookup.
count_requests <- function(urls) {
  c(
    chat = sum(grepl("/v1/(chat/completions|responses)$", urls)),
    embed = sum(grepl("/v1/embeddings$", urls)),
    lookup = sum(grepl("/api/v1/models$", urls))
  )
}

# The URLs of the requests that a recorder captured.
recorder_urls <- function(recorder) {
  vapply(recorder$requests, \(req) req$url, character(1))
}

Try the rlmstudio package in your browser

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

rlmstudio documentation built on Dec. 1, 2026, 9:07 a.m.