Skip to content

Commit

Permalink
Replaces readline() with menu(), adds test for that
Browse files Browse the repository at this point in the history
  • Loading branch information
edgararuiz committed Mar 18, 2024
1 parent f26e2cd commit ceec15f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
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/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
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.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
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"

2 changes: 1 addition & 1 deletion tests/testthat/test-backend-openai-core.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ test_that("OpenAI error check works", {

test_that("Copilot httr2 request works", {
local_mocked_bindings(
openai_token = function(...) "",
openai_token = function(...) ""
)
expect_snapshot(
openai_request.ch_openai_github_copilot_chat(
Expand Down
31 changes: 28 additions & 3 deletions tests/testthat/test-chattr-use.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
test_that("Model lists output is as expected", {
skip("Will fix as part of the testing upgrades")
expect_snapshot(capture.output(ch_get_ymls(menu = FALSE)))
test_that("Request submission works", {
withr::with_envvar(
new = c("OPENAI_API_KEY" = "test"),
{
out <- ch_get_ymls(menu = FALSE)
expect_equal(class(out), "list")
expect_snapshot(out$davinci)
expect_snapshot(out$gpt35)
expect_snapshot(out$gpt4)
}
)
})

test_that("Menu works", {
withr::with_envvar(
new = c("OPENAI_API_KEY" = "test"),
{
local_mocked_bindings(
menu = function(...) {
return(1)
}
)
expect_true(
ch_get_ymls(menu = TRUE) %in% c("copilot", "davinci")
)
}
)
})

0 comments on commit ceec15f

Please sign in to comment.