test_that("prepared results retain the verified study contract", {
  app_directory <- Sys.getenv("CASCADE_APP_DIR", unset = normalizePath(file.path("..", "..")))
  data <- readRDS(file.path(app_directory, "data", "cascade_results_explorer.rds"))

  expect_equal(nrow(data$cascade_summary), 288L)
  expect_equal(nrow(data$table2), 96L)
  expect_equal(nrow(data$table3), 192L)
  expect_equal(sum(data$table2$is_table2_max), 32L)
  expect_equal(sum(data$table3$is_table3_fastest), 32L)
  expect_equal(sum(is.na(data$table3$mean_steps_to_complete_failure)), 96L)
  expect_false(any(
    data$table3$is_table3_fastest &
      is.na(data$table3$mean_steps_to_complete_failure)
  ))
  expect_true(all(data$cascade_summary$n_replications == 1000L))
  expect_equal(
    data$cascade_summary$percent_complete_failures,
    100 * data$cascade_summary$n_complete_failures /
      data$cascade_summary$n_replications
  )
})

test_that("primary result keys are unique", {
  app_directory <- Sys.getenv("CASCADE_APP_DIR", unset = normalizePath(file.path("..", "..")))
  data <- readRDS(file.path(app_directory, "data", "cascade_results_explorer.rds"))

  cascade_key <- with(
    data$cascade_summary,
    paste(density_condition, network, metric, target, threshold, sep = "|")
  )
  table2_key <- with(
    data$table2,
    paste(density_condition, network, metric, threshold, sep = "|")
  )
  table3_key <- with(
    data$table3,
    paste(density_condition, network, metric, target_centrality, threshold, sep = "|")
  )

  expect_equal(anyDuplicated(cascade_key), 0L)
  expect_equal(anyDuplicated(table2_key), 0L)
  expect_equal(anyDuplicated(table3_key), 0L)
})
