# tests/testthat/test-utils.R library(testthat) library(shiny) test_that("handle_error works correctly", { # Test successful execution messages <- capture_messages({ result <- handle_error( expr = 1 + 1, success_msg = "Success" ) }) expect_equal(result, 2) expect_match(messages[1], "Success", fixed = TRUE) # Test error handling messages <- capture_messages({ result <- handle_error( expr = stop("Test error"), error_msg = "Custom error" ) }) expect_null(result) expect_match(messages[1], "Custom error", fixed = TRUE) }) test_that("create_action creates correct action structure", { action <- create_action( type = "test", data = list(x = 1), reverse_data = list(x = 0) ) expect_type(action, "list") expect_equal(action$type, "test") expect_equal(action$data, list(x = 1)) expect_equal(action$reverse_data, list(x = 0)) expect_true(!is.null(action$timestamp)) }) test_that("apply_action handles add_annotation correctly", { # Create a mock reactive environment rv <- new.env() rv$annotations <- data.frame( start = integer(), end = integer(), code = character(), stringsAsFactors = FALSE ) action <- create_action( type = "add_annotation", data = data.frame( start = 1, end = 10, code = "test_code", stringsAsFactors = FALSE ) ) # Test adding annotation apply_action(rv, action) expect_equal(nrow(rv$annotations), 1) expect_equal(rv$annotations$code[1], "test_code") # Test removing annotation apply_action(rv, action, reverse = TRUE) expect_equal(nrow(rv$annotations), 0) }) test_that("concatenate_memos combines memos correctly", { expect_equal( concatenate_memos("First memo", "Second memo"), "First memo; Second memo" ) expect_equal( concatenate_memos("", "Only memo"), "Only memo" ) expect_equal( concatenate_memos("Existing memo", ""), "Existing memo; " ) }) test_that("%||% operator works correctly", { expect_equal(NULL %||% 5, 5) expect_equal(10 %||% 5, 10) expect_equal(NULL %||% NULL, NULL) expect_equal(c(1,2) %||% 5, c(1,2)) })