Nothing
# A status-200 model list is read only when it has the shape that the package
# reads. `list_models()` aborts with `rlmstudio_bad_response` on any other
# shape, and `lms_server_ready()` reports FALSE for it. Both apply the rules in
# `model_list_fault()`. The bodies here are built as JSON text with the helpers
# in `helper-json-forms.R`. The server is mocked through the shared recorder
# (D-004).
instance_fields <- c(id = '"inst-1"')
valid_instance <- shape_object(instance_fields)
model_fields <- function(type = '"llm"', key = '"m"', instances = "[]") {
c(
type = type,
key = key,
display_name = '"M"',
size_bytes = "1073741824",
max_context_length = "32768",
loaded_instances = instances
)
}
valid_model <- shape_object(model_fields())
loaded_model <- shape_object(model_fields(instances = shape_array(valid_instance)))
list_body <- function(models) shape_object(c(models = shape_array(models)))
# The three places an entry fault sits: the only entry, the first of two
# entries, and the second after a valid entry.
entry_places <- function(bad, good) {
list(
"only entry" = bad,
"first of two" = c(bad, good),
"second of two" = c(good, bad)
)
}
# Every fault body, as a list of cases. Each case holds the body, the texts the
# message must contain, and a label. A non-object entry also needs "is not a
# JSON object", because the name of its array appears in every message about
# that array.
fault_cases <- function() {
cases <- list()
add <- function(label, body, names) {
cases[[length(cases) + 1L]] <<- list(label = label, body = body, names = names)
}
# The body itself, as each JSON form other than an object.
for (form in setdiff(names(json_forms), c("empty object", "object"))) {
add(paste("body as", form), json_forms[[form]], "not a JSON object")
}
# L1: `models` must be an array.
top <- c(models = shape_array(valid_model))
add("models absent", shape_object(top, drop = "models"), "`models`")
add("models extended", shape_object(top, extend = "models"), "`models`")
for (form in setdiff(names(json_forms), c("empty array", "array"))) {
add(paste("models as", form), shape_object(c(models = json_forms[[form]])), "`models`")
}
# L2 and L3: the fields of a model entry, in each of the three places.
model_faults <- list()
for (field in c("type", "key")) {
model_faults[[paste(field, "absent")]] <- list(shape_object(model_fields(), drop = field), field)
model_faults[[paste(field, "extended")]] <- list(shape_object(model_fields(), extend = field), field)
for (form in setdiff(names(json_forms), "string")) {
fields <- model_fields()
fields[[field]] <- json_forms[[form]]
model_faults[[paste(field, "as", form)]] <- list(shape_object(fields), field)
}
}
field <- "loaded_instances"
model_faults[[paste(field, "absent")]] <- list(shape_object(model_fields(), drop = field), field)
model_faults[[paste(field, "extended")]] <- list(shape_object(model_fields(), extend = field), field)
for (form in setdiff(names(json_forms), c("empty array", "array"))) {
model_faults[[paste(field, "as", form)]] <- list(
shape_object(model_fields(instances = json_forms[[form]])), field
)
}
for (field in c("size_bytes", "max_context_length")) {
for (form in setdiff(names(json_forms), c("number", "null"))) {
fields <- model_fields()
fields[[field]] <- json_forms[[form]]
model_faults[[paste(field, "as", form)]] <- list(shape_object(fields), field)
}
}
for (form in setdiff(names(json_forms), c("empty object", "object"))) {
model_faults[[paste("model entry as", form)]] <- list(json_forms[[form]], "models", "is not a JSON object")
}
for (name in names(model_faults)) {
fault <- model_faults[[name]]
places <- entry_places(fault[[1]], valid_model)
for (place in names(places)) {
add(
paste(name, "in", place),
list_body(places[[place]]),
c(paste0("`", fault[[2]], "`"), fault[-(1:2)])
)
}
}
# L4: the fields of an instance entry. Each fault sits in three places among
# the instances, and the model holding it sits in three places among models.
instance_faults <- list(
"id absent" = shape_object(instance_fields, drop = "id"),
"id extended" = shape_object(instance_fields, extend = "id"),
"id empty" = shape_object(c(id = '""')),
"id blank" = shape_object(c(id = '" "'))
)
for (form in setdiff(names(json_forms), "string")) {
instance_faults[[paste("id as", form)]] <- shape_object(c(id = json_forms[[form]]))
}
instance_names <- rep(list("`id`"), length(instance_faults))
for (form in setdiff(names(json_forms), c("empty object", "object"))) {
instance_faults[[paste("instance entry as", form)]] <- json_forms[[form]]
instance_names <- c(instance_names, list(c("`loaded_instances`", "is not a JSON object")))
}
for (k in seq_along(instance_faults)) {
inner <- entry_places(instance_faults[[k]], valid_instance)
for (inner_place in names(inner)) {
bad_model <- shape_object(model_fields(instances = shape_array(inner[[inner_place]])))
outer <- entry_places(bad_model, loaded_model)
for (outer_place in names(outer)) {
add(
paste(names(instance_faults)[[k]], "in", inner_place, "instance,", outer_place, "model"),
list_body(outer[[outer_place]]),
instance_names[[k]]
)
}
}
}
cases
}
# Bodies that pass: `size_bytes` or `max_context_length` absent, `null`, or
# under an extended name, an empty model list, and a list with a loaded model.
pass_cases <- function() {
cases <- list(
"no models" = list_body(character(0)),
"one loaded model" = list_body(c(valid_model, loaded_model))
)
for (field in c("size_bytes", "max_context_length")) {
cases[[paste(field, "absent")]] <- list_body(shape_object(model_fields(), drop = field))
cases[[paste(field, "null")]] <- list_body(shape_object(replace(model_fields(), field, "null")))
cases[[paste(field, "extended")]] <- list_body(shape_object(model_fields(), extend = field))
}
cases
}
test_that("each rule of the model list aborts list_models() with rlmstudio_bad_response", {
testthat::skip_on_cran()
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
withr::local_options(rlmstudio.quiet = TRUE)
for (case in fault_cases()) {
local_request_sequence(list(mock_response(200L, case$body)))
cnd <- shape_raised_by(list_models(quiet = TRUE))
expect_true(inherits(cnd, "rlmstudio_bad_response"), info = case$label)
if (!inherits(cnd, "rlmstudio_bad_response")) next
expect_identical(cnd$status, 200L, info = case$label)
message <- conditionMessage(cnd)
first_line <- strsplit(message, "\n", fixed = TRUE)[[1]][[1]]
expect_match(first_line, "API List Failed", fixed = TRUE, info = case$label)
for (text in case$names) {
expect_match(message, text, fixed = TRUE, info = case$label)
}
expect_no_match(message, "simplify", fixed = TRUE, info = case$label)
}
})
test_that("a model list that passes the rules returns without error", {
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
for (label in names(pass_cases())) {
local_request_sequence(list(mock_response(200L, pass_cases()[[label]])))
result <- shape_raised_by(list_models(quiet = TRUE))
expect_null(result, label = label)
}
})
test_that("a fault in an entry that the filters drop still aborts", {
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
withr::local_options(rlmstudio.quiet = TRUE)
# The bad entry is an embedding model, and the call asks for LLMs only.
bad_embedding <- shape_object(model_fields(type = '"embedding"'), drop = "key")
local_request_sequence(list(mock_response(200L, list_body(c(loaded_model, bad_embedding)))))
expect_error(list_models(type = "llm"), class = "rlmstudio_bad_response")
# The bad entry is not loaded, and the call asks for loaded models only.
bad_unloaded <- shape_object(model_fields(key = "1"))
local_request_sequence(list(mock_response(200L, list_body(c(loaded_model, bad_unloaded)))))
expect_error(list_models(loaded = TRUE), class = "rlmstudio_bad_response")
})
test_that("each fault body makes lms_server_ready() report FALSE", {
for (case in fault_cases()) {
local_request_sequence(list(mock_response(200L, case$body)))
expect_identical(lms_server_ready(), FALSE, info = case$label)
}
for (label in names(pass_cases())) {
local_request_sequence(list(mock_response(200L, pass_cases()[[label]])))
expect_identical(lms_server_ready(), TRUE, info = label)
}
})
test_that("an empty model list returns an empty data frame and informs", {
withr::local_options(rlmstudio.quiet = FALSE)
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
local_request_sequence(list(mock_response(200L, '{"models": []}')))
expect_message(
result <- withVisible(list_models()),
"No models found on host"
)
expect_identical(result$value, data.frame())
expect_false(result$visible)
local_request_sequence(list(mock_response(200L, '{"models": []}')))
expect_no_message(list_models(quiet = TRUE))
})
test_that("the LM Studio docs example of the model list reads as a data frame", {
# The "Response" block of lmstudio-ai/docs 1_developer/2_rest/list.md, as
# of commit 2e643a417b.
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
body <- paste(readLines(test_path("fixtures", "list-docs-example.json")), collapse = "\n")
local_request_sequence(list(mock_response(200L, body)))
result <- list_models(quiet = TRUE)
expect_identical(
result$key,
c(
"google/gemma-4-26b-a4b",
"deepseek-r1",
"text-embedding-nomic-embed-text-v1.5-embedding"
)
)
expect_identical(result$state, c("loaded", "unloaded", "unloaded"))
expect_identical(result$type, c("llm", "llm", "embedding"))
expect_identical(result$size_gb, round(c(17990911801, 40492610355, 274290560) / 1024^3, 2))
local_request_sequence(list(mock_response(200L, body)))
expect_identical(lms_server_ready(), TRUE)
})
test_that("an empty model list reaches the callers as nothing loaded", {
withr::local_options(rlmstudio.quiet = FALSE)
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
recorder <- local_request_sequence(list(mock_response(200L, '{"models": []}')))
expect_message(lms_unload_all(), "No models are currently loaded", fixed = TRUE)
expect_identical(length(recorder$requests), 1L)
# With nothing loaded, lms_load() goes on to the load request.
recorder <- local_request_sequence(list(
mock_response(200L, '{"models": []}'),
mock_response(200L, '{"status": "loaded"}')
))
result <- suppressMessages(lms_load("a-model"))
expect_identical(result, "a-model")
expect_identical(length(recorder$requests), 2L)
expect_identical(
request_target(recorder$requests[[2]])$path,
"/api/v1/models/load"
)
})
test_that("a model list with the wrong shape aborts the callers before any other request", {
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
withr::local_options(rlmstudio.quiet = TRUE)
callers <- list(
lms_load = function() lms_load("a-model"),
lms_unload_all = function() lms_unload_all()
)
bad_list <- list_body(shape_object(model_fields(), drop = "key"))
for (name in names(callers)) {
test_that(name, {
# One response only: a second request raises a plain error from the mock,
# which is not the class asserted here.
recorder <- local_request_sequence(list(mock_response(200L, bad_list)))
cnd <- shape_raised_by(callers[[name]]())
expect_true(inherits(cnd, "rlmstudio_bad_response"), info = name)
expect_match(conditionMessage(cnd), "API List Failed", fixed = TRUE, info = name)
expect_match(conditionMessage(cnd), "`key`", fixed = TRUE, info = name)
expect_identical(length(recorder$requests), 1L, info = name)
})
}
})
test_that("lms_unload_all unloads each instance by its id, in body order", {
testthat::local_mocked_bindings(is_server_running = function(...) TRUE)
unloaded <- character(0)
testthat::local_mocked_bindings(
lms_unload = function(model, ...) {
unloaded <<- c(unloaded, model)
invisible(model)
}
)
# Each instance carries a decoy field before `id`, so a read of the first
# field gives the wrong ids.
two_instances <- shape_array(c(
'{"decoy": "wrong-1", "id": "a-1"}',
'{"decoy": "wrong-2", "id": "a-2"}'
))
body <- list_body(c(
shape_object(model_fields(key = '"a"', instances = two_instances)),
valid_model,
shape_object(model_fields(key = '"b"', instances = '[{"id": "b-1"}]'))
))
local_request_sequence(list(mock_response(200L, body)))
result <- suppressMessages(lms_unload_all())
expect_identical(unloaded, c("a-1", "a-2", "b-1"))
expect_identical(result, c("a-1", "a-2", "b-1"))
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.