From 63c33b7a1dfa96f9d0577e9ab9cae34aaaa7a7fd Mon Sep 17 00:00:00 2001 From: Carson Date: Thu, 3 Sep 2026 09:34:32 -0500 Subject: [PATCH] fix(r): support ellmer 0.5.0's Provider/Model API in test mocks ellmer 0.5.0 moves model details from Provider into a new Model class: Provider() no longer accepts model as its second positional argument, and Chat$new() requires a separate model argument. Gate the mock chat client construction on whether ellmer::Model exists so tests pass on both old and new ellmer. Fixes #283 --- pkg-r/tests/testthat/apps/basic/app.R | 17 ++++++++++++++++- pkg-r/tests/testthat/helper-fixtures.R | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg-r/tests/testthat/apps/basic/app.R b/pkg-r/tests/testthat/apps/basic/app.R index efa2a9c7f..6a31f7463 100644 --- a/pkg-r/tests/testthat/apps/basic/app.R +++ b/pkg-r/tests/testthat/apps/basic/app.R @@ -24,12 +24,27 @@ dbDisconnect(conn) # Setup database source and QueryChat instance db_conn <- dbConnect(RSQLite::SQLite(), temp_db) +# ellmer 0.5.0 moved model details out of `Provider` and into a new `Model` +# class: `Provider()` no longer takes `model` as its second positional +# argument, and `Chat$new()` now requires a separate `model` argument. `Model` +# doesn't exist before 0.5.0, so branch on its presence to support both. +mock_chat_client <- if ( + exists("Model", where = asNamespace("ellmer"), inherits = FALSE) +) { + MockChat$new( + ellmer::Provider("test", "test"), + model = ellmer::Model(name = "test") + ) +} else { + MockChat$new(ellmer::Provider("test", "test", "test")) +} + # Create QueryChat instance qc <- QueryChat$new( data_source = db_conn, table_name = "iris", greeting = "Welcome to the test app!", - client = MockChat$new(ellmer::Provider("test", "test", "test")) + client = mock_chat_client ) ui <- page_sidebar( diff --git a/pkg-r/tests/testthat/helper-fixtures.R b/pkg-r/tests/testthat/helper-fixtures.R index 5180cf59d..7ce594ce5 100644 --- a/pkg-r/tests/testthat/helper-fixtures.R +++ b/pkg-r/tests/testthat/helper-fixtures.R @@ -173,7 +173,23 @@ mock_ellmer_chat_client <- function( private = private ) - MockChat$new(ellmer::Provider("test", "test", "test")) + new_mock_chat(MockChat) +} + +# ellmer 0.5.0 moved model details out of `Provider` and into a new `Model` +# class: `Provider()` no longer takes `model` as its second positional +# argument, and `Chat$new()` now requires a separate `model` argument. `Model` +# doesn't exist before 0.5.0, so branch on its presence to support both. +new_mock_chat <- function(MockChat, ...) { + if (exists("Model", where = asNamespace("ellmer"), inherits = FALSE)) { + MockChat$new( + ellmer::Provider("test", "test"), + model = ellmer::Model(name = "test"), + ... + ) + } else { + MockChat$new(ellmer::Provider("test", "test", "test"), ...) + } } # shinychat::chat_restore() validates that `client` is an ellmer::Chat() R6