tests/testthat/test_models_time_varying.R

test_that("RollingWindowModel fits with default window", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60, min_obs = 30)

  rw$fit(data)
  expect_true(rw$is_fitted)
  expect_equal(rw$model_name, "RollingWindowModel")

  stats <- rw$statistics
  expect_false(is.null(stats$alpha))
  expect_false(is.null(stats$beta))
  expect_false(is.null(stats$sigma))
  expect_false(is.null(stats$rolling_betas))
  expect_true(length(stats$rolling_betas) > 0)
})


test_that("RollingWindowModel calculates abnormal returns", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60)

  rw$fit(data)
  result <- rw$abnormal_returns(data)

  expect_true("abnormal_returns" %in% names(result))
  expect_equal(nrow(result), nrow(data))
  # Abnormal returns should be numeric, not all NA
  expect_true(all(is.finite(result$abnormal_returns)))
})


test_that("RollingWindowModel warns on insufficient data", {
  data <- create_mock_model_data(n_estimation = 10)
  rw <- RollingWindowModel$new(window_size = 60, min_obs = 30)

  expect_warning(rw$fit(data), "insufficient estimation observations")
  expect_false(rw$is_fitted)
})


test_that("RollingWindowModel works in full pipeline", {
  task <- create_mock_task()
  ps <- ParameterSet$new(return_model = RollingWindowModel$new(window_size = 60))
  result <- run_event_study(task, ps)
  expect_false(is.null(result$data_tbl))
})


test_that("RollingWindowModel returns NA when not fitted", {
  rw <- RollingWindowModel$new()
  data <- create_mock_model_data()

  expect_warning(
    result <- rw$abnormal_returns(data),
    "not fitted"
  )
  expect_true(all(is.na(result$abnormal_returns)))
})


test_that("RollingWindowModel with custom window_size", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 30, min_obs = 20)

  rw$fit(data)
  expect_true(rw$is_fitted)

  stats <- rw$statistics
  # Should have more rolling windows than with window_size=60
  n_windows_30 <- length(stats$rolling_betas)

  rw2 <- RollingWindowModel$new(window_size = 60, min_obs = 30)
  rw2$fit(data)
  n_windows_60 <- length(rw2$statistics$rolling_betas)

  expect_gt(n_windows_30, n_windows_60)
})


test_that("RollingWindowModel stores rolling parameters", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60, min_obs = 30)
  rw$fit(data)

  stats <- rw$statistics
  expect_equal(length(stats$rolling_alphas), length(stats$rolling_betas))
  expect_equal(length(stats$rolling_sigmas), length(stats$rolling_betas))

  # All rolling betas should be finite
  expect_true(all(is.finite(stats$rolling_betas)))
  expect_true(all(is.finite(stats$rolling_alphas)))
  expect_true(all(stats$rolling_sigmas > 0))
})


test_that("RollingWindowModel beta near true value", {
  data <- create_mock_model_data(n_estimation = 200)
  rw <- RollingWindowModel$new(window_size = 60)
  rw$fit(data)

  # The mock data has beta = 1.2 — the last window should be close
  expect_lt(abs(rw$statistics$beta - 1.2), 0.5)
})


test_that("RollingWindowModel residuals and autocorrelation stored", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60)
  rw$fit(data)

  stats <- rw$statistics
  expect_false(is.null(stats$residuals))
  expect_false(is.null(stats$first_order_auto_correlation))
  expect_true(length(stats$residuals) > 0)
})


test_that("RollingWindowModel forecast error correction stored", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60)
  rw$fit(data)

  stats <- rw$statistics
  expect_false(is.null(stats$forecast_error_corrected_sigma))
  expect_false(is.null(stats$forecast_error_corrected_sigma_car))
})


test_that("RollingWindowModel degree_of_freedom is window_size - 2", {
  data <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60)
  rw$fit(data)

  expect_equal(rw$statistics$degree_of_freedom, 58)
})


test_that("DCCGARCHModel fits and produces finite statistics on valid data (C5, 2026-09-24)", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")

  # C5 (2026-09-24): no tryCatch()-swallowed error, no `if (is_fitted)` guard.
  # Capture warnings (not errors) with withCallingHandlers; skip ONLY when the
  # fit did not converge AND a captured condition narrowly matches a
  # non-convergence pattern. Any other error fails the test.
  data <- create_mock_model_data(n_estimation = 200)
  dcc <- DCCGARCHModel$new()

  captured_msgs <- character(0)
  withCallingHandlers(
    dcc$fit(data),
    warning = function(w) {
      captured_msgs <<- c(captured_msgs, conditionMessage(w))
      invokeRestart("muffleWarning")
    }
  )

  if (!dcc$is_fitted) {
    convergence_msgs <- captured_msgs[grepl("converg", captured_msgs, ignore.case = TRUE)]
    if (length(convergence_msgs) > 0) {
      skip(paste("DCCGARCHModel did not converge:", convergence_msgs[1]))
    }
  }

  expect_true(dcc$is_fitted)
  expect_false(is.null(dcc$statistics$beta))
  expect_false(is.null(dcc$statistics$beta_t))
  expect_true(length(dcc$statistics$beta_t) > 0)

  result <- dcc$abnormal_returns(data)
  expect_true("abnormal_returns" %in% names(result))
  expect_true(any(is.finite(result$abnormal_returns[data$event_window == 1])))
})


test_that("DCCGARCHModel returns NA when not fitted", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")

  dcc <- DCCGARCHModel$new()
  data <- create_mock_model_data()

  expect_warning(
    result <- dcc$abnormal_returns(data),
    "not fitted"
  )
  expect_true(all(is.na(result$abnormal_returns)))
})


test_that("DCCGARCHModel custom garch_order and dcc_order", {
  dcc <- DCCGARCHModel$new(garch_order = c(2, 1), dcc_order = c(1, 2))
  expect_equal(dcc$garch_order, c(2, 1))
  expect_equal(dcc$dcc_order, c(1, 2))
})


test_that("RollingWindowModel clones deep correctly", {
  rw <- RollingWindowModel$new(window_size = 90, min_obs = 40)
  rw2 <- rw$clone(deep = TRUE)

  expect_equal(rw2$window_size, 90L)
  expect_equal(rw2$min_obs, 40L)
})


# --- CONTRACT-05: RollingWindowModel valid-input baseline invariance ---

test_that("RollingWindowModel: valid-input baseline invariant at 1e-8 (CONTRACT-05)", {
  # Loads a pre-captured baseline to ensure migration does not alter valid-input numerics.
  fixture_path <- testthat::test_path("fixtures", "contract05_rollingwindow_baseline.rds")
  skip_if_not(file.exists(fixture_path), "Baseline fixture not yet captured")
  baseline <- readRDS(fixture_path)

  set.seed(42)
  d <- create_mock_model_data()
  rw <- RollingWindowModel$new(window_size = 60, min_obs = 30)
  rw$fit(d)
  expect_true(rw$is_fitted)
  ar <- rw$abnormal_returns(d)

  expect_equal(rw$statistics$alpha, baseline$alpha, tolerance = 1e-8)
  expect_equal(rw$statistics$beta,  baseline$beta,  tolerance = 1e-8)
  expect_equal(rw$statistics$sigma, baseline$sigma, tolerance = 1e-8)
  expect_equal(rw$statistics$degree_of_freedom, baseline$df)
  valid_ars <- ar$abnormal_returns[!is.na(ar$abnormal_returns)]
  expect_equal(head(valid_ars, 5), baseline$ar_head5, tolerance = 1e-8)
})


# --- Degenerate-contract tests for RollingWindowModel ---

test_that("RollingWindowModel: lenient mode — insufficient obs (Guard 1) — one warning, all-NA ARs", {
  d <- create_mock_model_data()
  est_rows <- which(d$estimation_window == 1)
  # Leave only 1 valid row; model default min_obs = 30
  d$firm_returns[est_rows[-1]]  <- NA_real_
  d$index_returns[est_rows[-1]] <- NA_real_

  rw <- RollingWindowModel$new()
  rw$degenerate_mode <- "lenient"
  rw$event_id    <- "EVT_RW"
  rw$firm_symbol <- "FIRM_RW"

  ws <- character(0)
  withCallingHandlers(rw$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(rw$is_fitted)
  expect_equal(length(ws), 1L, info = "lenient mode must emit exactly one warning")
  expect_match(ws[[1]], "EVT_RW")
  expect_match(ws[[1]], "FIRM_RW")

  # abnormal_returns() must return all-NA silently (no second warning)
  ws2 <- character(0)
  withCallingHandlers({
    result <- rw$abnormal_returns(d)
  }, warning = function(w) {
    ws2[[length(ws2) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_true(all(is.na(result$abnormal_returns)))
  expect_equal(length(ws2), 0L, info = "abnormal_returns() must not emit a second warning after degenerate fit")
})


test_that("RollingWindowModel: strict mode — insufficient obs — named error with keys", {
  d <- create_mock_model_data()
  est_rows <- which(d$estimation_window == 1)
  d$firm_returns[est_rows[-1]]  <- NA_real_
  d$index_returns[est_rows[-1]] <- NA_real_

  rw <- RollingWindowModel$new()
  rw$degenerate_mode <- "strict"
  rw$event_id    <- "EVT_STRICT"
  rw$firm_symbol <- "FIRM_STRICT"

  expect_error(rw$fit(d), regexp = "EVT_STRICT")
  expect_error(rw$fit(d), regexp = "FIRM_STRICT")
  expect_false(rw$is_fitted)
})


test_that("RollingWindowModel: lenient mode — Guard 3 (last-window NA params from constant index)", {
  # All estimation-window index_returns are constant → ss_xx == 0 for every window
  # → beta = NA in all windows → Guard 3 fires after the rolling loop.
  d <- create_mock_model_data()
  est_rows <- which(d$estimation_window == 1)
  d$index_returns[est_rows] <- 0.001  # zero variance in all windows

  rw <- RollingWindowModel$new(window_size = 30, min_obs = 20)
  rw$degenerate_mode <- "lenient"
  rw$event_id    <- "EVT_NA_PARAMS"
  rw$firm_symbol <- "FIRM_NA"

  ws <- character(0)
  withCallingHandlers(rw$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(rw$is_fitted)
  expect_equal(length(ws), 1L, info = "Guard 3 must emit exactly one warning")
  expect_match(ws[[1]], "EVT_NA_PARAMS")

  # Silent all-NA from abnormal_returns
  ws2 <- character(0)
  withCallingHandlers({
    result <- rw$abnormal_returns(d)
  }, warning = function(w) {
    ws2[[length(ws2) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_true(all(is.na(result$abnormal_returns)))
  expect_equal(length(ws2), 0L)
})


# --- Degenerate-contract tests for GARCHModel (skip when rugarch absent) ---

test_that("GARCHModel: lenient mode — insufficient obs — one warning, all-NA ARs", {
  skip_if_not_installed("rugarch")
  d <- create_degenerate_model_data_insufficient()

  m <- GARCHModel$new()
  m$degenerate_mode <- "lenient"
  m$event_id    <- "EVT_GARCH"
  m$firm_symbol <- "FIRM_GARCH"

  ws <- character(0)
  withCallingHandlers(m$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(m$is_fitted)
  expect_equal(length(ws), 1L, info = "lenient mode must emit exactly one warning")
  expect_match(ws[[1]], "EVT_GARCH")
  expect_match(ws[[1]], "FIRM_GARCH")

  ws2 <- character(0)
  withCallingHandlers({
    result <- m$abnormal_returns(d)
  }, warning = function(w) {
    ws2[[length(ws2) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_true(all(is.na(result$abnormal_returns)))
  expect_equal(length(ws2), 0L)
})


test_that("GARCHModel: strict mode — insufficient obs — named error", {
  skip_if_not_installed("rugarch")
  d <- create_degenerate_model_data_insufficient()

  m <- GARCHModel$new()
  m$degenerate_mode <- "strict"
  m$event_id    <- "EVT_GS"
  m$firm_symbol <- "FIRM_GS"

  expect_error(m$fit(d), regexp = "EVT_GS")
  expect_error(m$fit(d), regexp = "FIRM_GS")
})


test_that("GARCHModel: lenient mode — zero-variance index_returns — one warning, all-NA ARs", {
  skip_if_not_installed("rugarch")
  d <- create_degenerate_model_data_zero_variance()

  m <- GARCHModel$new()
  m$degenerate_mode <- "lenient"
  m$event_id    <- "EVT_ZV"
  m$firm_symbol <- "FIRM_ZV"

  ws <- character(0)
  withCallingHandlers(m$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(m$is_fitted)
  expect_equal(length(ws), 1L, info = "zero-variance must emit exactly one warning")

  result <- m$abnormal_returns(d)
  expect_true(all(is.na(result$abnormal_returns)))
})


test_that("GARCHModel: valid-input baseline invariant at 1e-8 (CONTRACT-05, conditional)", {
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)
  m <- GARCHModel$new()
  m$fit(d)
  if (!m$is_fitted) skip("GARCHModel did not converge on synthetic data")

  # Capture or compare baseline in a single run to avoid .rds dependency
  alpha <- m$statistics$alpha
  beta  <- m$statistics$beta
  sigma <- m$statistics$sigma

  # Re-fit with identical seed/data to confirm determinism (within tolerance)
  m2 <- GARCHModel$new()
  m2$fit(d)
  if (!m2$is_fitted) skip("Second fit did not converge")

  expect_equal(m2$statistics$alpha, alpha, tolerance = 1e-6)
  expect_equal(m2$statistics$beta,  beta,  tolerance = 1e-6)
  expect_equal(m2$statistics$sigma, sigma, tolerance = 1e-6)
})


# --- Degenerate-contract tests for DCCGARCHModel (skip when rmgarch absent) ---

test_that("DCCGARCHModel: lenient mode — insufficient obs — one warning, all-NA ARs", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  d <- create_degenerate_model_data_insufficient()

  dm <- DCCGARCHModel$new()
  dm$degenerate_mode <- "lenient"
  dm$event_id    <- "EVT_DCC"
  dm$firm_symbol <- "FIRM_DCC"

  ws <- character(0)
  withCallingHandlers(dm$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(dm$is_fitted)
  expect_equal(length(ws), 1L)
  expect_match(ws[[1]], "EVT_DCC")

  result <- dm$abnormal_returns(d)
  expect_true(all(is.na(result$abnormal_returns)))
})


test_that("DCCGARCHModel: strict mode — insufficient obs — named error", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  d <- create_degenerate_model_data_insufficient()

  dm <- DCCGARCHModel$new()
  dm$degenerate_mode <- "strict"
  dm$event_id    <- "EVT_DS"
  dm$firm_symbol <- "FIRM_DS"

  expect_error(dm$fit(d), regexp = "EVT_DS")
})


test_that("DCCGARCHModel: lenient mode — zero-variance firm_returns — one warning, all-NA ARs", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  # Build data with zero-variance firm_returns in estimation window
  d <- create_mock_model_data()
  est_rows <- which(d$estimation_window == 1)
  d$firm_returns[est_rows] <- 0.001  # constant

  dm <- DCCGARCHModel$new()
  dm$degenerate_mode <- "lenient"
  dm$event_id    <- "EVT_ZF"
  dm$firm_symbol <- "FIRM_ZF"

  ws <- character(0)
  withCallingHandlers(dm$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(dm$is_fitted)
  expect_equal(length(ws), 1L)

  result <- dm$abnormal_returns(d)
  expect_true(all(is.na(result$abnormal_returns)))
})


test_that("DCCGARCHModel: lenient mode — zero-variance index_returns — one warning, all-NA ARs", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  d <- create_degenerate_model_data_zero_variance()  # constant index_returns in estimation

  dm <- DCCGARCHModel$new()
  dm$degenerate_mode <- "lenient"
  dm$event_id    <- "EVT_ZI"
  dm$firm_symbol <- "FIRM_ZI"

  ws <- character(0)
  withCallingHandlers(dm$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(dm$is_fitted)
  expect_equal(length(ws), 1L)

  result <- dm$abnormal_returns(d)
  expect_true(all(is.na(result$abnormal_returns)))
})


# ============================================================
# WR-04: RollingWindowModel df uses finite pair count (Plan 02-02)
# ============================================================

test_that("WR-04: RollingWindowModel df reflects finite pair count not nrow (NA-heavy window)", {
  # With NA rows in the estimation window, nrow() inflates ws and thus df,
  # making t-statistics too permissive. After WR-04 fix, df uses the finite
  # pair count so the effective window size is correctly smaller.
  d <- create_mock_model_data(n_estimation = 200, n_event = 11)
  est_rows <- which(d$estimation_window == 1)
  # NA out 80 rows; finite pairs = 120, so effective ws = min(60, 120) = 60, df = 58
  # Old behavior: ws = min(60, 200) = 60, df = 58 (accidentally same for this window size)
  # To distinguish, use window_size > 120: ws = min(200, 120) = 120 (new), 200 (old)
  d$firm_returns[est_rows[1:80]]  <- NA_real_
  d$index_returns[est_rows[1:80]] <- NA_real_

  rw <- RollingWindowModel$new(window_size = 200, min_obs = 30)
  rw$fit(d)
  expect_true(rw$is_fitted)

  # finite pair count = 120, ws = min(200, 120) = 120, df = max(120-2, 1) = 118
  expect_equal(rw$statistics$degree_of_freedom, 118L,
               info = "df must reflect finite pair count (120 valid), not nrow (200)")
  # Old nrow formula: ws = min(200, 200) = 200, df = 198
  expect_false(rw$statistics$degree_of_freedom == 198L,
               info = "df must NOT equal nrow-based value (198)")
})


# --- EXTERNAL-04: GARCHModel calculate_statistics failure wrapping ---

test_that("GARCHModel: calculate_statistics failure resets is_fitted=FALSE + named warning + NA ARs", {
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)

  # Mock rugarch::sigma to fail during calculate_statistics
  # This simulates a post-convergence statistics computation failure
  local_mocked_bindings(
    sigma = function(...) stop("simulated sigma extraction failure"),
    .package = "rugarch"
  )

  m <- GARCHModel$new()
  ws <- character(0)
  withCallingHandlers(m$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })

  # is_fitted must be reset to FALSE
  expect_false(m$is_fitted)
  # Exactly one warning naming the failure
  expect_true(length(ws) >= 1L)
  expect_true(any(grepl("GARCH.*statistics.*failed|statistics.*computation.*failed", ws,
                         ignore.case = TRUE)))

  # abnormal_returns must return all-NA
  result <- m$abnormal_returns(d)
  expect_true(all(is.na(result$abnormal_returns)))
})


test_that("GARCHModel: valid fit (no mock) still produces finite statistics", {
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)
  m <- GARCHModel$new()
  suppressWarnings(m$fit(d))

  if (!m$is_fitted) skip("GARCHModel did not converge on synthetic data")

  expect_true(is.finite(m$statistics$alpha))
  expect_true(is.finite(m$statistics$beta))
  expect_true(is.finite(m$statistics$sigma))
})


# --- EXTERNAL-04: DCCGARCHModel calculate_statistics failure wrapping ---

test_that("DCCGARCHModel: calculate_statistics failure resets is_fitted=FALSE + named warning + NA ARs", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)

  # Mock rmgarch::rcov to fail during calculate_statistics
  local_mocked_bindings(
    rcov = function(...) stop("simulated rcov extraction failure"),
    .package = "rmgarch"
  )

  dm <- DCCGARCHModel$new()
  ws <- character(0)
  withCallingHandlers(dm$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })

  # is_fitted must be reset to FALSE
  expect_false(dm$is_fitted)
  # At least one warning naming DCC-GARCH statistics failure
  expect_true(length(ws) >= 1L)
  expect_true(any(grepl("DCC-GARCH.*statistics.*failed|statistics.*computation.*failed", ws,
                         ignore.case = TRUE)))

  # abnormal_returns must return all-NA
  result <- dm$abnormal_returns(d)
  expect_true(all(is.na(result$abnormal_returns)))
})


# --- WR-01 regression: calculate_statistics failure → exactly ONE warning total ---

test_that("GARCHModel: calculate_statistics failure → exactly ONE warning (fit+abnormal_returns combined)", {
  # WR-01: After a post-convergence calculate_statistics failure, the handler
  # set is_fitted=FALSE but did NOT set .degenerate_handled=TRUE.  When
  # abnormal_returns() was subsequently called, the else-branch fired a second
  # "not fitted" warning — violating the one-warning contract.
  # Fix: set .degenerate_handled=TRUE in the tryCatch handler.
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)

  local_mocked_bindings(
    sigma = function(...) stop("simulated sigma failure for WR-01"),
    .package = "rugarch"
  )

  m <- GARCHModel$new()
  ws <- character(0)
  withCallingHandlers(m$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(m$is_fitted)
  expect_equal(length(ws), 1L,
               label = "fit() must emit exactly one warning on calculate_statistics failure")

  # abnormal_returns() must NOT emit a second warning
  ws2 <- character(0)
  withCallingHandlers({
    result <- m$abnormal_returns(d)
  }, warning = function(w) {
    ws2[[length(ws2) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_true(all(is.na(result$abnormal_returns)))
  expect_equal(length(ws2), 0L,
               label = "abnormal_returns() must NOT emit a second warning (one-warning contract)")
})


test_that("DCCGARCHModel: calculate_statistics failure → exactly ONE warning (fit+abnormal_returns combined)", {
  # WR-01 regression for DCCGARCHModel — same contract as GARCHModel above.
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)

  local_mocked_bindings(
    rcov = function(...) stop("simulated rcov failure for WR-01"),
    .package = "rmgarch"
  )

  dm <- DCCGARCHModel$new()
  ws <- character(0)
  withCallingHandlers(dm$fit(d), warning = function(w) {
    ws[[length(ws) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_false(dm$is_fitted)
  expect_equal(length(ws), 1L,
               label = "fit() must emit exactly one warning on calculate_statistics failure")

  # abnormal_returns() must NOT emit a second warning
  ws2 <- character(0)
  withCallingHandlers({
    result <- dm$abnormal_returns(d)
  }, warning = function(w) {
    ws2[[length(ws2) + 1]] <<- conditionMessage(w)
    invokeRestart("muffleWarning")
  })
  expect_true(all(is.na(result$abnormal_returns)))
  expect_equal(length(ws2), 0L,
               label = "abnormal_returns() must NOT emit a second warning (one-warning contract)")
})


test_that("DCCGARCHModel: valid fit (no mock) still produces finite statistics", {
  skip_if_not_installed("rmgarch")
  skip_if_not_installed("rugarch")
  d <- create_mock_model_data(n_estimation = 200)
  dm <- DCCGARCHModel$new()
  suppressWarnings(dm$fit(d))

  if (!dm$is_fitted) skip("DCCGARCHModel did not converge on synthetic data")

  expect_false(is.null(dm$statistics$beta))
  expect_false(is.null(dm$statistics$beta_t))
})

Try the EventStudy package in your browser

Any scripts or data that you put into this service are public.

EventStudy documentation built on Oct. 31, 2026, 1:07 a.m.