|
| 1 | +# Test peek_shinylive_app() ---- |
| 2 | + |
| 3 | +test_that("peek_shinylive_app(): handles HTML content correctly", { |
| 4 | + # Create sample HTML content with an embedded R Shiny application |
| 5 | + # The content simulates a Quarto document structure with a shinylive-r code block |
| 6 | + html_content <- '<!DOCTYPE html><html><body><main class="content" id="quarto-document-content"> |
| 7 | + <pre class="shinylive-r" data-engine="r"> |
| 8 | + #| viewerHeight: 500 |
| 9 | + ## file: app.R |
| 10 | + library(shiny) |
| 11 | + ui <- fluidPage() |
| 12 | + server <- function(input, output) {} |
| 13 | + shinyApp(ui, server) |
| 14 | + </pre> |
| 15 | + </main></body></html>' |
| 16 | + |
| 17 | + # Mock HTTP-related functions to simulate web requests |
| 18 | + testthat::local_mocked_bindings( |
| 19 | + # Mock GET to return HTML content with appropriate headers |
| 20 | + GET = function(...) base::structure( |
| 21 | + list( |
| 22 | + headers = list("content-type" = "text/html"), |
| 23 | + content = base::charToRaw(html_content) |
| 24 | + ), |
| 25 | + class = "response" |
| 26 | + ), |
| 27 | + # Mock http_error to always return FALSE (success) |
| 28 | + http_error = function(...) FALSE, |
| 29 | + # Mock content function to return the HTML content |
| 30 | + content = function(...) html_content, |
| 31 | + .package = "httr" |
| 32 | + ) |
| 33 | + |
| 34 | + # Test the function with a sample URL |
| 35 | + result <- peek_shinylive_app("http://example.com") |
| 36 | + |
| 37 | + # Verify the result is a quarto_shinylive_apps object |
| 38 | + testthat::expect_s3_class(result, "quarto_shinylive_apps") |
| 39 | +}) |
| 40 | + |
| 41 | +test_that("peek_shinylive_app(): handles app.json content correctly", { |
| 42 | + # Create sample JSON content representing a standalone Shiny application |
| 43 | + json_content <- jsonlite::toJSON(list( |
| 44 | + list( |
| 45 | + name = "app.R", |
| 46 | + content = "library(shiny)\n...", |
| 47 | + type = "text" |
| 48 | + ) |
| 49 | + )) |
| 50 | + |
| 51 | + # Mock HTTP-related functions for JSON response |
| 52 | + testthat::local_mocked_bindings( |
| 53 | + # Mock GET to return JSON content with appropriate headers |
| 54 | + GET = function(...) base::structure( |
| 55 | + list( |
| 56 | + headers = list("content-type" = "application/json"), |
| 57 | + content = base::charToRaw(json_content) |
| 58 | + ), |
| 59 | + class = "response" |
| 60 | + ), |
| 61 | + http_error = function(...) FALSE, |
| 62 | + content = function(...) json_content, |
| 63 | + .package = "httr" |
| 64 | + ) |
| 65 | + |
| 66 | + # Test the function with a URL pointing to app.json |
| 67 | + result <- peek_shinylive_app("http://example.com/app.json") |
| 68 | + |
| 69 | + # Verify the result is a standalone_shinylive_app object |
| 70 | + testthat::expect_s3_class(result, "standalone_shinylive_app") |
| 71 | +}) |
| 72 | + |
| 73 | +test_that("peek_quarto_shinylive_app(): handles app-dir format correctly", { |
| 74 | + # Create sample HTML content with a Shiny application |
| 75 | + html_content <- ' |
| 76 | + <pre class="shinylive-r" data-engine="r"> |
| 77 | + #| viewerHeight: 500 |
| 78 | + ## file: app.R |
| 79 | + library(shiny) |
| 80 | + ui <- fluidPage() |
| 81 | + server <- function(input, output) {} |
| 82 | + shinyApp(ui, server) |
| 83 | + </pre> |
| 84 | + ' |
| 85 | + |
| 86 | + # Mock HTTP-related functions |
| 87 | + testthat::local_mocked_bindings( |
| 88 | + GET = function(...) base::structure( |
| 89 | + list( |
| 90 | + headers = list("content-type" = "text/html"), |
| 91 | + content = base::charToRaw(html_content) |
| 92 | + ), |
| 93 | + class = "response" |
| 94 | + ), |
| 95 | + http_error = function(...) FALSE, |
| 96 | + content = function(...) html_content, |
| 97 | + .package = "httr" |
| 98 | + ) |
| 99 | + |
| 100 | + # Create temporary directory for output |
| 101 | + temp_dir <- base::tempfile() |
| 102 | + |
| 103 | + # Test the function with app-dir output format |
| 104 | + result <- peek_quarto_shinylive_app( |
| 105 | + "http://example.com", |
| 106 | + output_format = "app-dir", |
| 107 | + output_path = temp_dir |
| 108 | + ) |
| 109 | + |
| 110 | + # Verify result type and format |
| 111 | + testthat::expect_s3_class(result, "quarto_shinylive_apps") |
| 112 | + testthat::expect_equal(result$output_format, "app-dir") |
| 113 | + |
| 114 | + # Clean up temporary directory |
| 115 | + base::unlink(temp_dir, recursive = TRUE) |
| 116 | +}) |
| 117 | + |
| 118 | +# Test peek_standalone_shinylive_app() ---- |
| 119 | + |
| 120 | +test_that("peek_standalone_shinylive_app(): processes standalone app correctly", { |
| 121 | + # Create sample app.json content |
| 122 | + app_json <- list( |
| 123 | + list( |
| 124 | + name = "app.R", |
| 125 | + content = "library(shiny)\n...", |
| 126 | + type = "text" |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + # Mock HTTP-related functions |
| 131 | + testthat::local_mocked_bindings( |
| 132 | + # Mock GET to return JSON content with appropriate headers |
| 133 | + GET = function(...) base::structure( |
| 134 | + list( |
| 135 | + headers = list("content-type" = "application/json"), |
| 136 | + content = base::charToRaw(jsonlite::toJSON(app_json)) |
| 137 | + ), |
| 138 | + class = "response" |
| 139 | + ), |
| 140 | + http_error = function(...) FALSE, |
| 141 | + content = function(...) jsonlite::toJSON(app_json), |
| 142 | + .package = "httr" |
| 143 | + ) |
| 144 | + |
| 145 | + # Create temporary directory for output |
| 146 | + temp_dir <- base::tempfile() |
| 147 | + |
| 148 | + # Test standalone app processing |
| 149 | + result <- peek_standalone_shinylive_app( |
| 150 | + "http://example.com/app.json", |
| 151 | + output_dir = temp_dir |
| 152 | + ) |
| 153 | + |
| 154 | + # Verify result type and file creation |
| 155 | + testthat::expect_s3_class(result, "standalone_shinylive_app") |
| 156 | + testthat::expect_true(base::file.exists(base::file.path(temp_dir, "app.R"))) |
| 157 | + |
| 158 | + # Clean up temporary directory |
| 159 | + base::unlink(temp_dir, recursive = TRUE) |
| 160 | +}) |
0 commit comments