Nothing
library(testthat)
library(mini007)
# QueueChat is defined in helper-queue-chat.R.
new_agent <- function(replies = list(), structured = NULL,
instruction = "You guide visitors around Algiers.") {
suppressMessages(
Agent$new("guide", instruction, QueueChat$new(replies, structured))
)
}
# ── generate_execute_r_code() ─────────────────────────────────────────────────
test_that("generate_execute_r_code() strips markdown fences and does not validate by default", {
agent <- new_agent("```r\nx <- 1 + 1; x\n```")
result <- agent$generate_execute_r_code("Add one and one")
expect_equal(result$code, "x <- 1 + 1; x")
expect_equal(result$description, "Add one and one")
expect_false(result$validated)
expect_false(result$executed)
expect_true(grepl("Add one and one", agent$llm_object$prompts[[1]], fixed = TRUE))
})
test_that("generate_execute_r_code() validates the syntax of the code", {
ok <- new_agent("nchar('Tlemcen')")$generate_execute_r_code("Count letters", validate = TRUE)
expect_true(ok$validated)
expect_equal(ok$validation_message, "Syntax is valid")
bad <- new_agent("x <- (")$generate_execute_r_code("Broken code", validate = TRUE)
expect_false(bad$validated)
expect_match(bad$validation_message, "^Syntax error")
})
test_that("generate_execute_r_code() only executes validated code", {
agent <- new_agent("1 + 1")
expect_message(
result <- agent$generate_execute_r_code("Add", validate = FALSE, execute = TRUE),
"validated first"
)
expect_false(result$executed)
bad <- suppressMessages(
new_agent("x <- (")$generate_execute_r_code("Broken", validate = TRUE, execute = TRUE)
)
expect_false(bad$executed)
})
test_that("generate_execute_r_code() executes the code in the given environment", {
env <- new.env()
agent <- new_agent("wilayas <- 58; print('counted'); wilayas")
result <- suppressMessages(agent$generate_execute_r_code(
"Count the wilayas of Algeria", validate = TRUE, execute = TRUE,
interactive = FALSE, env = env
))
expect_true(result$executed)
expect_equal(result$execution_result$value, 58)
expect_true(any(grepl("counted", result$execution_result$output)))
expect_equal(env$wilayas, 58)
expect_null(result$execution_error)
})
test_that("generate_execute_r_code() records the error of code that fails", {
agent <- new_agent("stop('the Casbah is closed')")
result <- suppressMessages(agent$generate_execute_r_code(
"Fail", validate = TRUE, execute = TRUE, interactive = FALSE, env = new.env()
))
expect_false(result$executed)
expect_equal(result$execution_error, "the Casbah is closed")
})
test_that("generate_execute_r_code() asks before executing in interactive mode", {
local_mocked_bindings(readline = function(prompt) "n", .package = "base")
env <- new.env()
expect_output(
no <- suppressMessages(new_agent("visited <- TRUE")$generate_execute_r_code(
"Visit", validate = TRUE, execute = TRUE, interactive = TRUE, env = env
)),
"visited <- TRUE", fixed = TRUE
)
expect_false(no$executed)
expect_null(env$visited)
local_mocked_bindings(readline = function(prompt) "y", .package = "base")
expect_output(
yes <- suppressMessages(new_agent("visited <- TRUE")$generate_execute_r_code(
"Visit", validate = TRUE, execute = TRUE, interactive = TRUE, env = env
)),
"visited <- TRUE", fixed = TRUE
)
expect_true(yes$executed)
expect_true(env$visited)
})
test_that("generate_execute_r_code() validates its arguments", {
agent <- new_agent()
expect_error(agent$generate_execute_r_code(1))
expect_error(agent$generate_execute_r_code("x", validate = "yes"))
expect_error(agent$generate_execute_r_code("x", env = list()))
})
# ── clear_and_summarise_messages() ───────────────────────────────────────────
test_that("clear_and_summarise_messages() does nothing without a conversation", {
agent <- new_agent()
expect_message(out <- agent$clear_and_summarise_messages(), "No conversation history")
expect_null(out)
expect_length(agent$llm_object$prompts, 0)
})
test_that("clear_and_summarise_messages() replaces the history with a summary", {
agent <- new_agent(list("Oran is on the Mediterranean.", "The user asked about Oran."))
agent$invoke("Where is Oran?")
expect_gt(length(agent$messages), 1)
suppressMessages(agent$clear_and_summarise_messages())
expect_length(agent$messages, 1)
expect_equal(agent$messages[[1]]$role, "system")
expect_match(agent$messages[[1]]$content, "Conversation Summary", fixed = TRUE)
expect_match(agent$messages[[1]]$content, "The user asked about Oran.", fixed = TRUE)
expect_match(agent$messages[[1]]$content, "You guide visitors around Algiers.", fixed = TRUE)
expect_match(agent$llm_object$get_system_prompt(), "The user asked about Oran.", fixed = TRUE)
expect_match(tail(agent$llm_object$prompts, 1), "user: Where is Oran?", fixed = TRUE)
})
# ── validate_response() ──────────────────────────────────────────────────────
test_that("validate_response() returns a valid verdict when the score is high enough", {
agent <- new_agent(structured = list(score = 0.9, feedback = "Accurate."))
verdict <- suppressMessages(agent$validate_response(
prompt = "What is the capital of Algeria?",
response = "Algiers",
validation_criteria = "Must mention Algiers"
))
expect_true(verdict$valid)
expect_equal(verdict$score, 0.9)
expect_equal(verdict$feedback, "Accurate.")
expect_equal(verdict$prompt, "What is the capital of Algeria?")
expect_equal(verdict$response, "Algiers")
expect_equal(verdict$validation_criteria, "Must mention Algiers")
expect_equal(verdict$validation_score, 0.8)
})
test_that("validate_response() returns an invalid verdict below the threshold and restores the system prompt", {
agent <- new_agent(structured = list(score = 0.5, feedback = "Wrong city."))
before <- agent$llm_object$get_system_prompt()
expect_message(
verdict <- agent$validate_response(
prompt = "What is the capital of Algeria?",
response = "Oran",
validation_criteria = "Must mention Algiers",
validation_score = 0.7
),
"invalid"
)
expect_false(verdict$valid)
expect_equal(agent$llm_object$get_system_prompt(), before)
})
test_that("validate_response() validates its arguments", {
agent <- new_agent()
expect_error(agent$validate_response("p", "r", 1))
expect_error(agent$validate_response("p", "r", "c", validation_score = 2))
})
# ── Tools ─────────────────────────────────────────────────────────────────────
test_that("generate_and_register_tool() registers the tool the LLM writes", {
code <- paste(
"dunes_height <- function(name) paste(name, 'is high')",
"ellmer::tool(",
" dunes_height,",
" name = 'dunes_height',",
" description = 'Height of a dune in the Grand Erg',",
" arguments = list(name = ellmer::type_string('Name of the dune'))",
")",
sep = "\n"
)
agent <- new_agent(paste0("```r\n", code, "\n```"))
before <- agent$llm_object$get_system_prompt()
suppressMessages(agent$generate_and_register_tool("Give the height of a dune"))
expect_equal(agent$list_tools(), "dunes_height")
expect_length(agent$llm_object$tools, 1)
expect_equal(agent$llm_object$get_system_prompt(), before)
expect_true(grepl("Give the height of a dune", agent$llm_object$prompts[[1]], fixed = TRUE))
})
test_that("list_tools(), remove_tools() and clear_tools() manage the registered tools", {
agent <- new_agent()
expect_message(expect_equal(agent$list_tools(), character(0)), "No tools")
make_tool <- function(name) {
ellmer::tool(function(x) x, name = name, description = name,
arguments = list(x = ellmer::type_string("x")))
}
suppressMessages(agent$register_tools(list(make_tool("ghardaia"), make_tool("timimoun"))))
expect_setequal(agent$list_tools(), c("ghardaia", "timimoun"))
expect_message(agent$remove_tools("biskra"), "Tool not found")
suppressMessages(agent$remove_tools("ghardaia"))
expect_equal(agent$list_tools(), "timimoun")
expect_length(agent$llm_object$tools, 1)
expect_message(agent$clear_tools(), "Cleared 1 tool")
expect_length(agent$tools, 0)
expect_length(agent$llm_object$tools, 0)
expect_error(agent$remove_tools(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.