Skip to content

Commit

Permalink
Merge pull request #74 from mlverse/updates
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
edgararuiz authored Mar 19, 2024
2 parents 0d477fc + 5b28f68 commit 0048a01
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 34 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
branches: main
release:
types: [published]
workflow_dispatch:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
on:
push:
branches: main
pull_request:
branches: main

name: test-coverage

Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Suggests:
covr,
knitr,
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
VignetteBuilder: knitr
Remotes: r-lib/httr2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ importFrom(rlang,is_interactive)
importFrom(rlang,is_named)
importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,menu)
2 changes: 1 addition & 1 deletion R/backend-openai-core.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ openai_token_copilot <- function(defaults = NULL, fail = TRUE) {
if(is.null(token_url) && fail) {
abort(
c(
"There is no default the GH Copilot token URL",
"There is no default GH Copilot token URL",
"Please add a 'token_url' to your YAML file, or to chattr_defaults() "
))
}
Expand Down
13 changes: 8 additions & 5 deletions R/chattr-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ chattr_defaults <- function(type = "default",
)

ret <- chattr_defaults_get(type)
ret$type <- type
as_ch_model(ret, type)
}

as_ch_model <- function(x, type) {
x$type <- type

provider <- tolower(ret$provider)
provider <- tolower(x$provider)

sp_provider <- unlist(strsplit(provider, " - "))
if (length(sp_provider) > 1) {
Expand All @@ -117,13 +121,12 @@ chattr_defaults <- function(type = "default",
first_cl <- NULL
}

class(ret) <- c(
class(x) <- c(
paste0("ch_", prep_class_name(provider)),
first_cl,
"ch_model"
)

ret
x
}

prep_class_name <- function(x) {
Expand Down
2 changes: 1 addition & 1 deletion R/chattr-package.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @importFrom purrr map_chr map_lgl map imap set_names walk flatten
#' @importFrom purrr iwalk discard keep imap_lgl reduce transpose
#' @importFrom rlang %||% abort is_named is_interactive
#' @importFrom utils capture.output head
#' @importFrom utils capture.output head menu
#' @importFrom clipr write_clip
#' @importFrom bslib bs_theme
#' @importFrom grDevices rgb
Expand Down
10 changes: 3 additions & 7 deletions R/chattr-use.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,14 @@ ch_get_ymls <- function(menu = TRUE) {
} else {
x <- paste(.x[[1]], "-", .x[[2]])
}
paste0(trimws(.y), ": ", x, " (", .x[[3]], ") \n")
paste0(x, " (", .x[[3]], ") \n")
}) %>%
set_names(orig_names)

if(menu) {
cli_h3("chattr - Available models")
cli_text()
prep_files %>%
as.character() %>%
cli_code()
cli_text()
model_no <- readline("Select the number of the model you would like to use: ")
cli_text("Select the number of the model you would like to use: ")
model_no <- menu(prep_files)
model_label <- names(prep_files[as.integer(model_no)])
model_label
} else {
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/_snaps/backend-openai-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,30 @@
Output
[1] "{{error}}Type:invalid_request_error\nMessage: This model's maximum context length is 4097 tokens. However, your messages resulted in 22261 tokens. Please reduce the length of the messages."

# Warning messages appear

Code
app_init_message.cl_openai(list(title = "test", max_data_files = 10,
max_data_frames = 10))
Message
* Provider:
* Path/URL:
* Model:
! A list of the top 10 files will be sent externally to OpenAI with every request
To avoid this, set the number of files to be sent to 0 using `chattr::chattr_defaults(max_data_files = 0)`
! A list of the top 10 data.frames currently in your R session will be sent externally to OpenAI with every request
To avoid this, set the number of data.frames to be sent to 0 using `chattr::chattr_defaults(max_data_frames = 0)`

# Copilot httr2 request works

Code
openai_request.ch_openai_github_copilot_chat(defaults = list(path = "url"),
req_body = list())
Message
<httr2_request>
POST url
Headers:
* Authorization: '<REDACTED>'
* Editor-Version: 'vscode/9.9.9'
Body: json encoded data

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ch_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Code
ch_context_data_files(file_types = "R")
Output
[1] "Data files available: \n|- setup.R\n|- test-app_server.R\n|- test-app_ui.R\n|- test-app_utils.R\n|- test-backend-llamagpt.R\n|- test-backend-openai-core.R\n|- test-backend-openai.R\n|- test-ch-defaults-save.R\n|- test-ch-history.R\n|- test-ch-submit.R\n|- test-ch_context.R\n|- test-ch_defaults.R\n|- test-chatter-use.R\n|- test-chattr-test.R\n|- test-chattr.R\n|- test-ide.R"
[1] "Data files available: \n|- setup.R\n|- test-app_server.R\n|- test-app_ui.R\n|- test-app_utils.R\n|- test-backend-llamagpt.R\n|- test-backend-openai-core.R\n|- test-backend-openai-switch.R\n|- test-backend-openai.R\n|- test-ch-defaults-save.R\n|- test-ch-history.R\n|- test-ch-submit.R\n|- test-ch_context.R\n|- test-ch_defaults.R\n|- test-chattr-test.R\n|- test-chattr-use.R\n|- test-chattr.R\n|- test-ide.R"

---

Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/_snaps/chattr-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,28 @@
v Model started sucessfully
v Model session closed sucessfully

# Test function works for Copilot

Code
chattr_use("copilot")
Message
-- chattr
* Provider: OpenAI - GitHub Copilot Chat
* Path/URL: https://api.githubcopilot.com/chat/completions
* Model: TRUE

---

Code
chattr_test()
Message
-- Testing chattr
* Provider: OpenAI - GitHub Copilot Chat
* Path/URL: https://api.githubcopilot.com/chat/completions
* Model:
v Connection with GitHub Copilot cofirmed
|--Prompt: TEST
|--Response: TEST

21 changes: 21 additions & 0 deletions tests/testthat/_snaps/chattr-use.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Request submission works

Code
out$davinci
Output
[1] "OpenAI - Completions - text-davinci-003 (davinci) \n"

---

Code
out$gpt35
Output
[1] "OpenAI - Chat Completions - gpt-3.5-turbo (gpt35) \n"

---

Code
out$gpt4
Output
[1] "OpenAI - Chat Completions - gpt-4 (gpt4) \n"

31 changes: 31 additions & 0 deletions tests/testthat/test-app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,34 @@ test_that("File stream is processed", {
test_that("Cleanup", {
expect_null(ch_history_set(NULL))
})


test_that("app_server() function runs", {
local_mocked_bindings(
insertUI = function(...) invisible(),
observeEvent = function(...) invisible()
)
session <- list()
session$sendCustomMessage <- function(...) {}
expect_silent(
app_server(list(), list(), session = session)
)
})

test_that("Adding to history works", {
local_mocked_bindings(
ch_history = function(...) {
c(list(list("role" = "user")))
},
insertUI = function(...) list(...),
)
expect_silent(app_add_history("style", "test"))
})


test_that("app_add_assistant() function runs", {
local_mocked_bindings(
insertUI = function(...) invisible()
)
expect_silent(app_add_assistant("test\n```{r}\nx<-1\n```", list()))
})
21 changes: 21 additions & 0 deletions tests/testthat/test-backend-llamagpt.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ test_that("Chat works", {
expect_equal(readRDS(chat_file), "xxx\n")
})

test_that("Submit works", {
local_mocked_bindings(
ch_llamagpt_prompt = function(...) invisible(),
ch_llamagpt_session = function(...) invisible(),
ch_llamagpt_output = function(...) return("test")
)
defaults <- yaml::read_yaml(package_file("configs", "llamagpt.yml"))
defaults <- as_ch_model(defaults$default, "chat")
expect_equal(
ch_submit.ch_llamagpt(
defaults = defaults,
prompt = "test",
stream = TRUE,
r_file_complete = tempfile(),
r_file_stream = tempfile()
),
"test"
)
})

test_that("Restore to previews defaults", {
expect_snapshot(chattr_use("gpt35"))
})

Loading

0 comments on commit 0048a01

Please sign in to comment.