test_that("saved support-audit tables can be frozen directly with canonical names", { audit <- calibration_audit_fixture() fixture <- calibration_freeze_fixture() frozen <- freeze_support_rule( diagnostic = audit$scenario_summary, rules = audit$candidate_rule_grid, rule_id = fixture$rule_id, rationale = fixture$rationale, calibration_scope = fixture$calibration_scope, provenance = fixture$provenance, simulation_settings = fixture$simulation_settings ) selected <- audit$candidate_rule_grid[ audit$candidate_rule_grid$rule_id == fixture$rule_id, , drop = FALSE ] classified <- classify_calib_reliability( audit$scenario_summary, availability_lower_column = "p_support_lower", availability_upper_column = "p_support_upper" ) evaluated <- evaluate_calib_rules( classified, audit$candidate_rule_grid, availability_column = "p_support" ) expect_identical(frozen$rule_family, "B") expect_identical(frozen$family, frozen$rule_family) expect_identical(frozen$selected_rule, selected) expect_identical(frozen$performance, evaluated$performance) expect_identical(frozen$pass_matrix, evaluated$pass_matrix) expect_identical(frozen$diagnostic, classified) expect_false("family" %in% names(frozen$selected_rule)) expect_false("family" %in% names(frozen$performance)) expect_false("n_sim" %in% names(frozen$diagnostic)) expect_identical(frozen$diagnostic$total_sim_n, audit$scenario_summary$total_sim_n) expect_identical(frozen$calibration_scope, fixture$calibration_scope) expect_identical(frozen$provenance, fixture$provenance) expect_identical(frozen$simulation_settings, fixture$simulation_settings) }) test_that("legacy family and n_sim inputs retain their original table columns", { fixture <- calibration_freeze_fixture() frozen <- freeze_calibration_fixture(fixture) expect_identical(frozen$rule_family, "B") expect_identical(frozen$family, frozen$rule_family) expect_identical(frozen$performance[names(fixture$rules)], fixture$rules) expect_identical(frozen$diagnostic[names(fixture$diagnostic)], fixture$diagnostic) expect_false("rule_family" %in% names(frozen$performance)) expect_false("total_sim_n" %in% names(frozen$diagnostic)) }) test_that("equal family aliases accept factor and character labels unchanged", { fixture <- calibration_freeze_fixture() fixture$rules$rule_family <- factor(fixture$rules$family) frozen <- freeze_calibration_fixture(fixture) expect_identical(frozen$rule_family, "B") expect_identical(frozen$family, "B") expect_identical(frozen$performance[names(fixture$rules)], fixture$rules) expect_true(is.factor(frozen$performance$rule_family)) expect_true(is.character(frozen$performance$family)) }) test_that("family aliases must be valid and agree for every candidate", { fixture <- calibration_freeze_fixture() fixture$rules$rule_family <- fixture$rules$family fixture$rules$rule_family[1] <- "different_family" expect_error(freeze_calibration_fixture(fixture), "rule_family.*family|family.*rule_family") for (column in c("rule_family", "family")) { for (invalid in list("", " ", NA, 1)) { fixture <- calibration_freeze_fixture() fixture$rules$rule_family <- fixture$rules$family fixture$rules[[column]] <- rep(invalid, nrow(fixture$rules)) expect_error(freeze_calibration_fixture(fixture), column) } fixture <- calibration_freeze_fixture() fixture$rules$rule_family <- fixture$rules$family fixture$rules[[column]] <- matrix("B", nrow = nrow(fixture$rules), ncol = 2) expect_error(freeze_calibration_fixture(fixture), "one value per row") } fixture <- calibration_freeze_fixture() fixture$rules$family <- NULL expect_error(freeze_calibration_fixture(fixture), "rule_family|family") }) test_that("canonical rule_family accepts factor labels and rejects invalid labels", { audit <- calibration_audit_fixture() rules <- audit$candidate_rule_grid rules$rule_family <- factor(rules$rule_family) frozen <- freeze_calibration_fixture(rules = rules) expect_identical(frozen$rule_family, "B") expect_identical(frozen$performance[names(rules)], rules) for (invalid in list("", NA, 1)) { rules <- audit$candidate_rule_grid rules$rule_family <- rep(invalid, nrow(rules)) expect_error(freeze_calibration_fixture(rules = rules), "rule_family") } }) test_that("matching attempted-count aliases preserve all supplied counts", { audit <- calibration_audit_fixture() diagnostic <- audit$scenario_summary diagnostic$total_sim_n <- rep(c(1000, 2000), 50) diagnostic$n_sim <- diagnostic$total_sim_n diagnostic$n_support_valid <- rep(c(990, 1980), 50) frozen <- freeze_calibration_fixture(diagnostic = diagnostic, rules = audit$candidate_rule_grid) expect_identical(frozen$diagnostic[names(diagnostic)], diagnostic) expect_equal(frozen$selected_performance$APR_confirmed, 1) expect_equal(frozen$selected_performance$UPR_conservative, 0) expect_equal(frozen$selected_performance$n_reliable, 92) diagnostic$total_sim_n[1] <- 2000 expect_error(freeze_calibration_fixture(diagnostic = diagnostic), "total_sim_n.*n_sim|n_sim.*total_sim_n") }) test_that("canonical attempted counts must be complete and match support availability", { audit <- calibration_audit_fixture() for (column in c("total_sim_n", "n_support_valid")) { diagnostic <- audit$scenario_summary diagnostic[[column]] <- NULL expect_error(freeze_calibration_fixture(diagnostic = diagnostic), "total_sim_n|n_sim|n_support_valid") } diagnostic <- audit$scenario_summary diagnostic$n_support_valid[1] <- 1001 expect_error(freeze_calibration_fixture(diagnostic = diagnostic), "total_sim_n|n_support_valid") diagnostic <- audit$scenario_summary diagnostic$p_support[1] <- 0.98 expect_error(freeze_calibration_fixture(diagnostic = diagnostic), "p_support") diagnostic <- audit$scenario_summary diagnostic$total_sim_n <- NULL diagnostic$n_support_valid <- NULL frozen <- freeze_calibration_fixture(diagnostic = diagnostic, rules = audit$candidate_rule_grid) expect_s3_class(frozen, "ahtau_support_rule") expect_identical(frozen$diagnostic[names(diagnostic)], diagnostic) expect_false(any(c("total_sim_n", "n_sim", "n_support_valid") %in% names(frozen$diagnostic))) }) test_that("both attempted-count aliases are checked even when the other is valid", { audit <- calibration_audit_fixture() for (column in c("total_sim_n", "n_sim")) { for (invalid in list(0, -1, 1.5, NA, Inf, "1000", TRUE)) { diagnostic <- audit$scenario_summary diagnostic$n_sim <- diagnostic$total_sim_n diagnostic[[column]] <- rep(invalid, nrow(diagnostic)) expect_error(freeze_calibration_fixture(diagnostic = diagnostic), column) } } for (column in c("total_sim_n", "n_sim", "n_support_valid")) { diagnostic <- audit$scenario_summary diagnostic$n_sim <- diagnostic$total_sim_n diagnostic[[column]] <- matrix(1000, nrow = nrow(diagnostic), ncol = 2) expect_error(freeze_calibration_fixture(diagnostic = diagnostic), "one value per row") } }) test_that("one candidate preserves a one-column pass matrix and the helper rates", { audit <- calibration_audit_fixture() rules <- audit$candidate_rule_grid[3, , drop = FALSE] diagnostic <- audit$scenario_summary[c(seq_len(19), 94), ] diagnostic$min_mean_n_risk <- 20 frozen <- freeze_calibration_fixture(diagnostic = diagnostic, rules = rules) classified <- classify_calib_reliability( diagnostic, availability_lower_column = "p_support_lower", availability_upper_column = "p_support_upper" ) evaluated <- evaluate_calib_rules(classified, rules, availability_column = "p_support") expect_equal(dim(frozen$pass_matrix), c(20, 1)) expect_identical(colnames(frozen$pass_matrix), "B_p010_n10") expect_identical(frozen$pass_matrix, evaluated$pass_matrix) expect_identical(frozen$performance, evaluated$performance) expect_equal(frozen$selected_performance$n_total, 20) expect_equal(frozen$selected_performance$n_pass, 20) expect_equal(frozen$selected_performance$n_not_pass, 0) expect_equal(frozen$selected_performance$UPR_conservative, 0.05) expect_equal(frozen$selected_performance$APR_confirmed, 1) expect_identical(frozen$selection$maximizing_rule_ids, "B_p010_n10") expect_error(freeze_calibration_fixture(diagnostic = diagnostic[1, ], rules = rules), "At least two calibration settings") }) test_that("canonical Family A selection still identifies the Stage 1 limitation", { audit <- calibration_audit_fixture() expect_error(freeze_calibration_fixture( diagnostic = audit$scenario_summary[1:3, ], rules = audit$candidate_rule_grid[1, , drop = FALSE], rule_id = "A_p005" ), "Stage 1") expect_error(freeze_calibration_fixture( diagnostic = audit$scenario_summary[1:3, ], rules = audit$candidate_rule_grid[1, , drop = FALSE], rule_id = "A_p005" ), "no absolute risk-count requirement") })