test_that("Datasets are properly loaded", {
  data(wiener_arrhenius)
  data(wiener_power)
  data(gamma_arrhenius)
  data(gamma_power)
  
  expect_equal(nrow(wiener_arrhenius), 30)
  expect_equal(ncol(wiener_arrhenius), 6)
  expect_false(any(is.na(wiener_arrhenius)))
  
  expect_equal(nrow(gamma_arrhenius), 30)
  expect_equal(ncol(gamma_arrhenius), 6)
  expect_false(any(is.na(gamma_arrhenius)))
})

test_that("Wiener process MLE and MCMC fitting works without NA/NaN", {
  data(wiener_arrhenius)
  
  fit_mle <- ssad_fit(
    data = wiener_arrhenius,
    process = "wiener",
    model = "arrhenius",
    stress_levels = c(45, 65, 85),
    thresholds = c(90, 160),
    method = "mle"
  )
  
  expect_s3_class(fit_mle, "ssadfit")
  expect_false(any(is.na(fit_mle$par)))
  expect_false(any(is.nan(fit_mle$par)))
  expect_true(fit_mle$loglik > -1e6)
  
  fit_mcmc <- ssad_fit(
    data = wiener_arrhenius,
    process = "wiener",
    model = "power",
    stress_levels = c(45, 65, 85),
    thresholds = c(90, 160),
    method = "mcmc",
    n_iter = 200,
    burnin = 50
  )
  
  expect_s3_class(fit_mcmc, "ssadfit")
  expect_false(any(is.na(fit_mcmc$par)))
  expect_false(any(is.nan(fit_mcmc$par)))
})

test_that("Gamma process MLE and MCMC fitting works without NA/NaN", {
  data(gamma_arrhenius)
  
  fit_mle <- ssad_fit(
    data = gamma_arrhenius,
    process = "gamma",
    model = "arrhenius",
    stress_levels = c(45, 65, 85),
    thresholds = c(90, 160),
    method = "mle"
  )
  
  expect_s3_class(fit_mle, "ssadfit")
  expect_false(any(is.na(fit_mle$par)))
  expect_false(any(is.nan(fit_mle$par)))
  
  fit_mcmc <- ssad_fit(
    data = gamma_arrhenius,
    process = "gamma",
    model = "power",
    stress_levels = c(45, 65, 85),
    thresholds = c(90, 160),
    method = "mcmc",
    n_iter = 200,
    burnin = 50
  )
  
  expect_s3_class(fit_mcmc, "ssadfit")
  expect_false(any(is.na(fit_mcmc$par)))
  expect_false(any(is.nan(fit_mcmc$par)))
})

test_that("Geweke diagnostic works properly", {
  set.seed(42)
  samp <- matrix(rnorm(500), ncol = 2, dimnames = list(NULL, c("a", "b")))
  g_res <- geweke_diag(samp)
  
  expect_equal(nrow(g_res), 2)
  expect_false(any(is.na(g_res$z_score)))
  expect_false(any(is.nan(g_res$p_value)))
})

test_that("Reliability prediction works properly", {
  rel <- ssad_reliability(
    t = seq(100, 1000, by = 100),
    process = "wiener",
    model = "arrhenius",
    params = c(a = 5.4, b = -2500, sigma = 0.1),
    S0 = 25,
    omega_F = 200
  )
  
  expect_s3_class(rel, "ssad_reliability")
  expect_equal(length(rel$reliability), 10)
  expect_true(rel$mttf > 0)
  expect_false(any(is.na(rel$reliability)))
})

test_that("Simulation function produces clean data", {
  set.seed(42)
  sim_d <- ssad_simulate(
    n_units = 3,
    times = seq(50, 500, by = 50),
    stress_levels = c(45, 65),
    thresholds = c(90),
    process = "gamma",
    model = "arrhenius",
    params = c(a = 6.7, b = -2500, u = 4.0)
  )
  
  expect_equal(nrow(sim_d), 10)
  expect_equal(ncol(sim_d), 4)
  expect_false(any(is.na(sim_d)))
})
