| 1 |
# Tests for otel-session.R functions |
| 2 |
|
| 3 |
# Helper function to create a mock domain with request info |
| 4 |
create_mock_session_domain <- function( |
| 5 |
token = "test-session-123", |
| 6 |
request = list(), |
| 7 |
session_ended_callbacks = list() |
| 8 |
) { |
| 9 |
TestMockShinySession <- R6::R6Class( |
| 10 |
"TestMockShinySession", |
| 11 |
inherit = MockShinySession, |
| 12 |
portable = FALSE, |
| 13 |
lock_objects = FALSE, |
| 14 |
public = list( |
| 15 |
# Mock onSessionEnded to capture the callback |
| 16 |
onSessionEnded = function(callback) { |
| 17 |
expect_true(is.function(callback)) |
| 18 |
self$cleanup_callbacks <- c(self$cleanup_callbacks, list(callback)) |
| 19 |
}, |
| 20 |
cleanup_callbacks = NULL, |
| 21 |
request_val = NULL |
| 22 |
), |
| 23 |
active = list( |
| 24 |
request = function(value) { |
| 25 |
if (!missing(value)) { |
| 26 |
self$request_val <- value |
| 27 |
} else { |
| 28 |
self$request_val |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
) |
| 33 |
) |
| 34 |
|
| 35 |
domain <- TestMockShinySession$new() |
| 36 |
|
| 37 |
domain$request <- request |
| 38 |
domain$token <- token |
| 39 |
|
| 40 |
domain |
| 41 |
} |
| 42 |
|
| 43 |
test_that("otel_span_session_start returns early when otel not enabled", { |
| 44 |
domain <- create_mock_session_domain() |
| 45 |
test_value <- "initial" |
| 46 |
|
| 47 |
# Mock has_otel_collect to return FALSE |
| 48 |
withr::local_options(list(shiny.otel.collect = "none")) |
| 49 |
|
| 50 |
result <- otel_span_session_start({ |
| 51 |
test_value <- "modified" |
| 52 |
"result_value" |
| 53 |
}, domain = domain) |
| 54 |
|
| 55 |
expect_equal(result, "result_value") |
| 56 |
expect_equal(test_value, "modified") |
| 57 |
# Should not have registered any callbacks |
| 58 |
expect_length(domain$cleanup_callbacks, 0) |
| 59 |
}) |
| 60 |
|
| 61 |
test_that("otel_span_session_start sets up session end callback", { |
| 62 |
domain <- create_mock_session_domain( |
| 63 |
token = "session-456", |
| 64 |
request = list(PATH_INFO = "/app", HTTP_HOST = "localhost") |
| 65 |
) |
| 66 |
|
| 67 |
test_value <- "initial" |
| 68 |
|
| 69 |
# Mock dependencies |
| 70 |
withr::local_options(list(shiny.otel.collect = "session")) |
| 71 |
|
| 72 |
local_mocked_bindings( |
| 73 |
as_attributes = function(x) x, |
| 74 |
.package = "otel" |
| 75 |
) |
| 76 |
|
| 77 |
with_mocked_bindings( |
| 78 |
has_otel_collect = function(level) level == "session", |
| 79 |
otel_session_id_attrs = function(domain) list(session.id = domain$token), |
| 80 |
otel_session_attrs = function(domain) list(PATH_INFO = "/app"), |
| 81 |
with_otel_span = function(name, expr, attributes = NULL) { |
| 82 |
expect_equal(name, "session_start") |
| 83 |
expect_true("session.id" %in% names(attributes)) |
| 84 |
expect_equal(attributes[["session.id"]], "session-456") |
| 85 |
force(expr) |
| 86 |
}, |
| 87 |
{ |
| 88 |
|
| 89 |
expect_length(domain$cleanup_callbacks, 0) |
| 90 |
|
| 91 |
result <- otel_span_session_start({ |
| 92 |
test_value <- "modified" |
| 93 |
"result_value" |
| 94 |
}, domain = domain) |
| 95 |
|
| 96 |
expect_equal(result, "result_value") |
| 97 |
expect_equal(test_value, "modified") |
| 98 |
expect_length(domain$cleanup_callbacks, 0) |
| 99 |
|
| 100 |
} |
| 101 |
) |
| 102 |
}) |
| 103 |
|
| 104 |
test_that("otel_span_session_end returns early when otel not enabled", { |
| 105 |
domain <- create_mock_session_domain() |
| 106 |
test_value <- "initial" |
| 107 |
|
| 108 |
# Mock has_otel_collect to return FALSE |
| 109 |
withr::local_options(list(shiny.otel.collect = "none")) |
| 110 |
|
| 111 |
result <- otel_span_session_end({ |
| 112 |
test_value <- "modified" |
| 113 |
"result_value" |
| 114 |
}, domain = domain) |
| 115 |
|
| 116 |
expect_equal(result, "result_value") |
| 117 |
expect_equal(test_value, "modified") |
| 118 |
}) |
| 119 |
|
| 120 |
test_that("otel_span_session_end creates span when enabled", { |
| 121 |
domain <- create_mock_session_domain(token = "session-end-test") |
| 122 |
|
| 123 |
span_created <- FALSE |
| 124 |
test_value <- "initial" |
| 125 |
|
| 126 |
# Mock dependencies |
| 127 |
withr::local_options(list(shiny.otel.collect = "session")) |
| 128 |
|
| 129 |
with_mocked_bindings( |
| 130 |
has_otel_collect = function(level) level == "session", |
| 131 |
otel_session_id_attrs = function(domain) list(session.id = domain$token), |
| 132 |
with_otel_span = function(name, expr, attributes = NULL) { |
| 133 |
span_created <<- TRUE |
| 134 |
expect_equal(name, "session_end") |
| 135 |
expect_equal(attributes[["session.id"]], "session-end-test") |
| 136 |
force(expr) |
| 137 |
}, |
| 138 |
{ |
| 139 |
result <- otel_span_session_end({ |
| 140 |
test_value <- "modified" |
| 141 |
"result_value" |
| 142 |
}, domain = domain) |
| 143 |
|
| 144 |
expect_equal(result, "result_value") |
| 145 |
expect_equal(test_value, "modified") |
| 146 |
expect_true(span_created) |
| 147 |
} |
| 148 |
) |
| 149 |
}) |
| 150 |
|
| 151 |
test_that("otel_session_attrs extracts request attributes correctly", { |
| 152 |
# Test with full request info |
| 153 |
domain <- create_mock_session_domain( |
| 154 |
request = list( |
| 155 |
PATH_INFO = "/myapp/page", |
| 156 |
HTTP_HOST = "example.com", |
| 157 |
HTTP_ORIGIN = "https://example.com", |
| 158 |
SERVER_PORT = "8080" |
| 159 |
) |
| 160 |
) |
| 161 |
|
| 162 |
attrs <- otel_session_attrs(domain) |
| 163 |
|
| 164 |
expect_equal(attrs$server.path, "/myapp/page") |
| 165 |
expect_equal(attrs$server.address, "example.com") |
| 166 |
expect_equal(attrs$server.origin, "https://example.com") |
| 167 |
expect_equal(attrs$server.port, 8080L) # Should be converted to integer |
| 168 |
}) |
| 169 |
|
| 170 |
test_that("otel_session_attrs handles websocket PATH_INFO", { |
| 171 |
domain <- create_mock_session_domain( |
| 172 |
request = list( |
| 173 |
PATH_INFO = "/myapp/websocket/", |
| 174 |
HTTP_HOST = "localhost" |
| 175 |
) |
| 176 |
) |
| 177 |
|
| 178 |
attrs <- otel_session_attrs(domain) |
| 179 |
|
| 180 |
# Should strip websocket suffix |
| 181 |
expect_equal(attrs$server.path, "/myapp/") |
| 182 |
}) |
| 183 |
|
| 184 |
test_that("otel_session_attrs handles missing request fields", { |
| 185 |
# Test with minimal request info |
| 186 |
domain <- create_mock_session_domain( |
| 187 |
request = list( |
| 188 |
HTTP_HOST = "localhost" |
| 189 |
) |
| 190 |
) |
| 191 |
|
| 192 |
attrs <- otel_session_attrs(domain) |
| 193 |
|
| 194 |
expect_equal(attrs$server.path, "") |
| 195 |
expect_equal(attrs$server.address, "localhost") |
| 196 |
expect_equal(attrs$server.origin, "") |
| 197 |
expect_equal(attrs$server.port, NA_integer_) |
| 198 |
}) |
| 199 |
|
| 200 |
test_that("otel_session_attrs handles empty request", { |
| 201 |
domain <- create_mock_session_domain(request = list()) |
| 202 |
|
| 203 |
attrs <- otel_session_attrs(domain) |
| 204 |
|
| 205 |
expect_equal(attrs$server.path, "") |
| 206 |
expect_equal(attrs$server.address, "") |
| 207 |
expect_equal(attrs$server.origin, "") |
| 208 |
expect_equal(attrs$server.port, NA_integer_) |
| 209 |
}) |
| 210 |
|
| 211 |
test_that("otel_session_attrs handles invalid SERVER_PORT gracefully", { |
| 212 |
domain <- create_mock_session_domain( |
| 213 |
request = list(SERVER_PORT = "invalid") |
| 214 |
) |
| 215 |
|
| 216 |
# Should not error even with invalid port |
| 217 |
attrs <- otel_session_attrs(domain) |
| 218 |
|
| 219 |
# Should remain as string if conversion fails |
| 220 |
expect_equal(attrs$server.port, "invalid") |
| 221 |
}) |
| 222 |
|
| 223 |
test_that("otel_session_id_attrs returns correct session ID", { |
| 224 |
domain <- create_mock_session_domain(token = "unique-session-token") |
| 225 |
|
| 226 |
attrs <- otel_session_id_attrs(domain) |
| 227 |
|
| 228 |
expect_equal(attrs$session.id, "unique-session-token") |
| 229 |
expect_length(attrs, 1) |
| 230 |
}) |
| 231 |
|
| 232 |
test_that("otel_session_id_attrs handles missing token", { |
| 233 |
domain <- create_mock_session_domain(token = NULL) |
| 234 |
|
| 235 |
attrs <- otel_session_id_attrs(domain) |
| 236 |
|
| 237 |
expect_null(attrs$session.id) |
| 238 |
}) |
| 239 |
|
| 240 |
test_that("integration test - session start with full request", { |
| 241 |
domain <- create_mock_session_domain( |
| 242 |
token = "integration-test-session", |
| 243 |
request = list( |
| 244 |
PATH_INFO = "/dashboard/", |
| 245 |
HTTP_HOST = "shiny.example.com", |
| 246 |
HTTP_ORIGIN = "https://shiny.example.com", |
| 247 |
SERVER_PORT = "3838" |
| 248 |
) |
| 249 |
) |
| 250 |
|
| 251 |
session_callback <- NULL |
| 252 |
span_attributes <- NULL |
| 253 |
|
| 254 |
# Mock dependencies |
| 255 |
withr::local_options(list(shiny.otel.collect = "session")) |
| 256 |
|
| 257 |
local_mocked_bindings( |
| 258 |
as_attributes = function(x) x, |
| 259 |
.package = "otel" |
| 260 |
) |
| 261 |
|
| 262 |
with_mocked_bindings( |
| 263 |
has_otel_collect = function(level) level == "session", |
| 264 |
otel_session_id_attrs = otel_session_id_attrs, # Use real function |
| 265 |
otel_session_attrs = otel_session_attrs, # Use real function |
| 266 |
with_otel_span = function(name, expr, attributes = NULL) { |
| 267 |
span_attributes <<- attributes |
| 268 |
force(expr) |
| 269 |
}, |
| 270 |
otel_log = function(...) {}, # Mock log function |
| 271 |
{ |
| 272 |
|
| 273 |
expect_length(domain$cleanup_callbacks, 0) |
| 274 |
|
| 275 |
result <- otel_span_session_start({ |
| 276 |
"test_result" |
| 277 |
}, domain = domain) |
| 278 |
|
| 279 |
expect_equal(result, "test_result") |
| 280 |
|
| 281 |
# Check span attributes include both session ID and request info |
| 282 |
expect_equal(span_attributes[["session.id"]], "integration-test-session") |
| 283 |
expect_equal(span_attributes[["server.path"]], "/dashboard/") |
| 284 |
expect_equal(span_attributes[["server.address"]], "shiny.example.com") |
| 285 |
expect_equal(span_attributes[["server.port"]], 3838L) |
| 286 |
} |
| 287 |
) |
| 288 |
}) |