test_that("compute_delta_auc reshapes and scores", {
  auc_long <- data.frame(
    drug_name = rep(c("DrugA", "DrugB", "DrugC"), each = 2),
    condition = rep(c("Gy0", "Gy2"), times = 3),
    auc = c(5.0, 3.1, 4.2, 4.0, 6.0, 2.0)
  )
  out <- compute_delta_auc(auc_long, conditions = c("Gy0", "Gy2"))

  expect_named(out, c(
    "drug_name", "auc_Gy0", "auc_Gy2", "delta_auc", "zscore_delta_auc"
  ))
  expect_equal(nrow(out), 3)
  # delta = treatment - baseline
  expect_equal(out$delta_auc, c(-1.9, -0.2, -4.0), tolerance = 1e-9)
  # z-score is centred and complete
  expect_false(any(is.na(out$zscore_delta_auc)))
  expect_equal(mean(out$zscore_delta_auc), 0, tolerance = 1e-9)
})

test_that("compute_delta_auc errors on missing columns", {
  expect_error(
    compute_delta_auc(data.frame(condition = "Gy0", auc = 1)),
    "not found"
  )
})

test_that("compute_delta_auc errors when conditions != 2", {
  df <- data.frame(
    drug_name = "D1",
    condition = c("Gy0", "Gy2", "Gy4"),
    auc = c(1, 2, 3)
  )
  expect_error(
    compute_delta_auc(df, conditions = c("Gy0", "Gy2", "Gy4")),
    "exactly two conditions"
  )
})
