Debian Codegraph @

tests/testthat/test-otel-collect.R

raw
1 test_that("otel_collect_is_enabled works with valid collect levels", {
2 # Test with default "all" option
3 expect_true(otel_collect_is_enabled("none"))
4 expect_true(otel_collect_is_enabled("session"))
5 expect_true(otel_collect_is_enabled("reactive_update"))
6 expect_true(otel_collect_is_enabled("reactivity"))
7 expect_true(otel_collect_is_enabled("all"))
8 })
9
10 test_that("otel_collect_is_enabled respects hierarchy with 'none' option", {
11 # With "none" option, nothing should be enabled
12 expect_false(otel_collect_is_enabled("session", "none"))
13 expect_false(otel_collect_is_enabled("reactive_update", "none"))
14 expect_false(otel_collect_is_enabled("reactivity", "none"))
15 expect_false(otel_collect_is_enabled("all", "none"))
16 expect_true(otel_collect_is_enabled("none", "none"))
17 })
18
19 test_that("otel_collect_is_enabled respects hierarchy with 'session' option", {
20 # With "session" option, only "none" and "session" should be enabled
21 expect_true(otel_collect_is_enabled("none", "session"))
22 expect_true(otel_collect_is_enabled("session", "session"))
23 expect_false(otel_collect_is_enabled("reactive_update", "session"))
24 expect_false(otel_collect_is_enabled("reactivity", "session"))
25 expect_false(otel_collect_is_enabled("all", "session"))
26 })
27
28 test_that("otel_collect_is_enabled respects hierarchy with 'reactive_update' option", {
29 # With "reactive_update" option, "none", "session", and "reactive_update" should be enabled
30 expect_true(otel_collect_is_enabled("none", "reactive_update"))
31 expect_true(otel_collect_is_enabled("session", "reactive_update"))
32 expect_true(otel_collect_is_enabled("reactive_update", "reactive_update"))
33 expect_false(otel_collect_is_enabled("reactivity", "reactive_update"))
34 expect_false(otel_collect_is_enabled("all", "reactive_update"))
35 })
36
37 test_that("otel_collect_is_enabled respects hierarchy with 'reactivity' option", {
38 # With "reactivity" option, all except "all" should be enabled
39 expect_true(otel_collect_is_enabled("none", "reactivity"))
40 expect_true(otel_collect_is_enabled("session", "reactivity"))
41 expect_true(otel_collect_is_enabled("reactive_update", "reactivity"))
42 expect_true(otel_collect_is_enabled("reactivity", "reactivity"))
43 expect_false(otel_collect_is_enabled("all", "reactivity"))
44 })
45
46 test_that("otel_collect_is_enabled respects hierarchy with 'all' option", {
47 # With "all" option (default), everything should be enabled
48 expect_true(otel_collect_is_enabled("none", "all"))
49 expect_true(otel_collect_is_enabled("session", "all"))
50 expect_true(otel_collect_is_enabled("reactive_update", "all"))
51 expect_true(otel_collect_is_enabled("reactivity", "all"))
52 expect_true(otel_collect_is_enabled("all", "all"))
53 })
54
55 test_that("otel_collect_is_enabled uses shiny.otel.collect option", {
56 # Test that option is respected
57 withr::with_options(
58 list(shiny.otel.collect = "session"),
59 {
60 expect_true(otel_collect_is_enabled("none"))
61 expect_true(otel_collect_is_enabled("session"))
62 expect_false(otel_collect_is_enabled("reactive_update"))
63 }
64 )
65
66 withr::with_options(
67 list(shiny.otel.collect = "reactivity"),
68 {
69 expect_true(otel_collect_is_enabled("reactive_update"))
70 expect_true(otel_collect_is_enabled("reactivity"))
71 expect_false(otel_collect_is_enabled("all"))
72 }
73 )
74 })
75
76 test_that("otel_collect_is_enabled falls back to SHINY_OTEL_COLLECT env var", {
77 # Remove option to test env var fallback
78 withr::local_options(list(shiny.otel.collect = NULL))
79
80 # Test env var is respected
81 withr::local_envvar(list(SHINY_OTEL_COLLECT = "session"))
82 expect_true(otel_collect_is_enabled("none"))
83 expect_true(otel_collect_is_enabled("session"))
84 expect_false(otel_collect_is_enabled("reactive_update"))
85
86 withr::local_envvar(list(SHINY_OTEL_COLLECT = "none"))
87 expect_true(otel_collect_is_enabled("none"))
88 expect_false(otel_collect_is_enabled("session"))
89 })
90
91 test_that("otel_collect_is_enabled option takes precedence over env var", {
92 # Set conflicting option and env var
93 withr::local_options(shiny.otel.collect = "session")
94 withr::local_envvar(SHINY_OTEL_COLLECT = "all")
95
96 # Option should take precedence
97 expect_true(otel_collect_is_enabled("session"))
98 expect_false(otel_collect_is_enabled("reactive_update"))
99 })
100
101 test_that("otel_collect_is_enabled defaults to 'all' when no option or env var", {
102 # Remove both option and env var
103 withr::local_options(list(shiny.otel.collect = NULL))
104 withr::local_envvar(list(SHINY_OTEL_COLLECT = NA))
105
106 # Should default to "all"
107 expect_true(otel_collect_is_enabled("all"))
108 expect_true(otel_collect_is_enabled("reactivity"))
109 expect_true(otel_collect_is_enabled("none"))
110 })
111
112 # Tests for as_otel_collect()
113 test_that("as_otel_collect validates and returns valid collect levels", {
114 expect_equal(as_otel_collect("none"), "none")
115 expect_equal(as_otel_collect("session"), "session")
116 expect_equal(as_otel_collect("reactive_update"), "reactive_update")
117 expect_equal(as_otel_collect("reactivity"), "reactivity")
118 expect_equal(as_otel_collect("all"), "all")
119 })
120
121 test_that("as_otel_collect uses default value", {
122 expect_equal(as_otel_collect(), "all")
123 })
124
125 test_that("as_otel_collect errors on invalid input types", {
126 expect_error(as_otel_collect(123), "`collect` must be a character vector.")
127 expect_error(as_otel_collect(NULL), "`collect` must be a character vector.")
128 expect_error(as_otel_collect(TRUE), "`collect` must be a character vector.")
129 expect_error(as_otel_collect(list("all")), "`collect` must be a character vector.")
130 })
131
132 test_that("as_otel_collect errors on invalid collect levels", {
133 expect_error(as_otel_collect("invalid"), "'arg' should be one of")
134 expect_error(as_otel_collect("unknown"), "'arg' should be one of")
135 expect_error(as_otel_collect(""), "'arg' should be one of")
136 })
137
138 test_that("as_otel_collect errors on multiple values", {
139 # match.arg with several.ok = FALSE should error on multiple values
140 expect_error(as_otel_collect(c("all", "none")), "'arg' must be of length 1")
141 expect_error(as_otel_collect(c("session", "reactivity")), "'arg' must be of length 1")
142 })