test_that("define_distribution_censor creates valid distribution objects", {
  dist_norm <- dist_normal(mean = 10, sd = 2)
  expect_s3_class(dist_norm, "gpci_dist_censor")
  expect_equal(dist_norm$name, "normal")
  expect_equal(dist_norm$params$mean, 10)
  expect_equal(dist_norm$params$sd, 2)
  
  # Custom distribution using only SF
  custom_dist <- define_distribution_censor(
    name = "custom_exp",
    sf = function(x, rate) exp(-rate * x),
    params = list(rate = 0.5),
    support = c(0, Inf)
  )
  expect_s3_class(custom_dist, "gpci_dist_censor")
  expect_true(is.function(custom_dist$cdf))
  expect_true(is.function(custom_dist$pdf))
})

test_that("fit_distribution_censor estimates parameters under interval censoring", {
  set.seed(42)
  raw_x <- rnorm(30, mean = 10, sd = 1.5)
  left <- raw_x - 0.2
  right <- raw_x + 0.2
  
  dist_norm <- dist_normal(mean = 5, sd = 3)
  fitted <- fit_distribution_censor(left, right, dist_norm)
  
  expect_s3_class(fitted, "gpci_dist_censor")
  expect_true(is.finite(fitted$params$mean))
  expect_true(is.finite(fitted$params$sd))
  expect_true(fitted$params$sd > 0)
})

test_that("capability_censor computes GPCIs including Cpy without NAs", {
  set.seed(123)
  raw_x <- rnorm(25, mean = 100, sd = 5)
  left <- raw_x - 0.5
  right <- raw_x + 0.5
  
  dist_norm <- dist_normal(mean = 100, sd = 5)
  fit_cap <- capability_censor(
    data_left = left, data_right = right,
    distribution = dist_norm,
    USL = 115, LSL = 85, target = 100,
    indices = c("Cpy", "Cp", "Cpk", "Cpm", "Cpmk", "Spmk", "CpTk", "Cpc", "CNpmc", "CNpmkc"),
    mode = "moments"
  )
  
  expect_s3_class(fit_cap, "gpcifit_censor")
  estimates <- fit_cap$estimates
  expect_equal(length(estimates), 10)
  expect_false(any(is.na(estimates)))
  expect_false(any(is.nan(estimates)))
  expect_true(estimates["Cpy"] > 0)
  
  # Dispatcher test
  cpy_val <- gpci_index(fit_cap, "Cpy")
  expect_equal(unname(cpy_val), unname(estimates["Cpy"]))
})

test_that("boot_ci_censor computes CIs at 90%, 95%, 99% confidence levels", {
  set.seed(456)
  left <- c(9.5, 10.1, 8.9, 10.8, 9.7, 10.2, 9.4, 10.5)
  right <- c(10.3, 10.9, 9.5, 11.4, 10.3, 10.8, 10.0, 11.1)
  
  dist_norm <- dist_normal(mean = 10, sd = 1)
  fit_cap <- capability_censor(left, right, dist_norm, USL = 13, LSL = 7)
  
  ci_res <- boot_ci_censor(fit_cap, B = 50, alpha = c(0.10, 0.05, 0.01), method = "percentile")
  expect_s3_class(ci_res, "gpci_ci_censor")
  expect_equal(nrow(ci_res$ci_table), 21) # 7 indices * 3 alpha levels
  expect_false(any(is.na(ci_res$ci_table$lower)))
  expect_false(any(is.na(ci_res$ci_table$upper)))
})

test_that("compute_diagnostics_censor computes SE, MSE, and coverage probabilities", {
  set.seed(789)
  left <- c(4.8, 5.1, 4.5, 5.3, 4.9)
  right <- c(5.2, 5.5, 4.9, 5.7, 5.3)
  
  dist_norm <- dist_normal(mean = 5, sd = 0.5)
  fit_cap <- capability_censor(left, right, dist_norm, USL = 7, LSL = 3)
  
  diag_res <- compute_diagnostics_censor(
    fit_cap,
    true_params = list(mean = 5, sd = 0.5),
    true_indices = c(Cpy = 1.0, Cp = 1.33),
    B = 30
  )
  
  expect_s3_class(diag_res, "gpci_diagnostics_censor")
  expect_true(length(diag_res$se_params) > 0)
  expect_true(length(diag_res$se_indices) > 0)
  expect_false(any(is.na(diag_res$se_params)))
  expect_false(any(is.na(diag_res$se_indices)))
})

test_that("S3 methods function correctly", {
  set.seed(101)
  left <- c(9.8, 10.1, 9.5)
  right <- c(10.2, 10.5, 9.9)
  dist_norm <- dist_normal()
  fit <- capability_censor(left, right, dist_norm, USL = 13, LSL = 7)
  
  expect_output(print(fit))
  expect_output(summary(fit))
  expect_type(coef(fit, "indices"), "double")
  expect_type(coef(fit, "parameters"), "double")
})
