|
| 1 | +/// Schema migration guardrail tests for `get_result_schema()` (#255). |
| 2 | +/// |
| 3 | +/// These tests act as a CI-backed safety net that prevents `SLAResult` layout |
| 4 | +/// changes from being merged without a deliberate, reviewed schema version bump. |
| 5 | +/// |
| 6 | +/// # How the guard works |
| 7 | +/// |
| 8 | +/// 1. `RESULT_SCHEMA_FIELD_COUNT` in `lib.rs` records the number of named fields |
| 9 | +/// in `SLAResult`. |
| 10 | +/// 2. `RESULT_SCHEMA_VERSION` records the breaking-change counter. |
| 11 | +/// 3. The tests below assert that both constants match the actual struct shape |
| 12 | +/// and the values returned by `get_result_schema()`. |
| 13 | +/// |
| 14 | +/// If a contributor adds or removes a field from `SLAResult` without updating |
| 15 | +/// `RESULT_SCHEMA_FIELD_COUNT` and `RESULT_SCHEMA_VERSION`, the sentinel test |
| 16 | +/// `test_result_schema_field_count_sentinel` will fail — surfacing the oversight |
| 17 | +/// before the PR lands. |
| 18 | +/// |
| 19 | +/// # What to do when changing `SLAResult` |
| 20 | +/// |
| 21 | +/// See `docs/result-schema-migration-guard.md` for the full migration checklist. |
| 22 | +/// Quick summary: |
| 23 | +/// |
| 24 | +/// 1. Add / remove / change the field in `SLAResult`. |
| 25 | +/// 2. Update `RESULT_SCHEMA_FIELD_COUNT` to the new field count. |
| 26 | +/// 3. Increment `RESULT_SCHEMA_VERSION` (breaking schema change). |
| 27 | +/// 4. Update `get_result_schema()` if a new symbol descriptor is warranted. |
| 28 | +/// 5. Add a CHANGELOG entry under `[Unreleased]` → `Changed`. |
| 29 | +/// 6. Update the `expected_fields` list in |
| 30 | +/// `test_result_schema_symbols_are_stable` if a symbol changes. |
| 31 | +#[cfg(test)] |
| 32 | +mod tests { |
| 33 | + use crate::{ |
| 34 | + SLACalculatorContract, SLACalculatorContractClient, RESULT_SCHEMA_FIELD_COUNT, |
| 35 | + RESULT_SCHEMA_VERSION, |
| 36 | + }; |
| 37 | + use soroban_sdk::{testutils::Address as _, Env, Symbol}; |
| 38 | + |
| 39 | + // ----------------------------------------------------------------------- |
| 40 | + // Helpers |
| 41 | + // ----------------------------------------------------------------------- |
| 42 | + |
| 43 | + fn setup() -> (Env, SLACalculatorContractClient<'static>) { |
| 44 | + let env = Env::default(); |
| 45 | + env.mock_all_auths(); |
| 46 | + let cid = env.register_contract(None, SLACalculatorContract); |
| 47 | + let client = SLACalculatorContractClient::new(&env, &cid); |
| 48 | + let admin = soroban_sdk::Address::generate(&env); |
| 49 | + let operator = soroban_sdk::Address::generate(&env); |
| 50 | + client.initialize(&admin, &operator); |
| 51 | + (env, client) |
| 52 | + } |
| 53 | + |
| 54 | + // ----------------------------------------------------------------------- |
| 55 | + // Sentinel: field count must match RESULT_SCHEMA_FIELD_COUNT |
| 56 | + // ----------------------------------------------------------------------- |
| 57 | + |
| 58 | + /// **Migration guardrail — CI gate.** |
| 59 | + /// |
| 60 | + /// This test counts the fields of `SLAResult` by name and asserts the |
| 61 | + /// count equals `RESULT_SCHEMA_FIELD_COUNT`. It will fail if a field is |
| 62 | + /// added, removed, or renamed without updating the constant. |
| 63 | + /// |
| 64 | + /// `SLAResult` currently has 9 fields: |
| 65 | + /// outage_id, status, mttr_minutes, threshold_minutes, amount, |
| 66 | + /// payment_type, rating, config_version_hash, recorded_at |
| 67 | + /// |
| 68 | + /// Update `RESULT_SCHEMA_FIELD_COUNT` in `lib.rs` when this changes. |
| 69 | + #[test] |
| 70 | + fn test_result_schema_field_count_sentinel() { |
| 71 | + use crate::SLAResult; |
| 72 | + use soroban_sdk::{symbol_short, Env}; |
| 73 | + |
| 74 | + let env = Env::default(); |
| 75 | + |
| 76 | + // Build a representative SLAResult and destructure it exhaustively so |
| 77 | + // the compiler enforces that every field is named here. When a new |
| 78 | + // field is added the destructure will fail to compile unless the |
| 79 | + // test is updated. This is the first line of defense. |
| 80 | + let sample = SLAResult { |
| 81 | + outage_id: symbol_short!("out1"), |
| 82 | + status: symbol_short!("met"), |
| 83 | + mttr_minutes: 10, |
| 84 | + threshold_minutes: 30, |
| 85 | + amount: 750, |
| 86 | + payment_type: symbol_short!("rew"), |
| 87 | + rating: symbol_short!("excel"), |
| 88 | + config_version_hash: 0, |
| 89 | + recorded_at: 0, |
| 90 | + }; |
| 91 | + |
| 92 | + // Destructure every field explicitly — adding a field without updating |
| 93 | + // this match will cause a compile error, catching the drift at build time. |
| 94 | + let SLAResult { |
| 95 | + outage_id: _, |
| 96 | + status: _, |
| 97 | + mttr_minutes: _, |
| 98 | + threshold_minutes: _, |
| 99 | + amount: _, |
| 100 | + payment_type: _, |
| 101 | + rating: _, |
| 102 | + config_version_hash: _, |
| 103 | + recorded_at: _, |
| 104 | + } = sample; |
| 105 | + |
| 106 | + // The runtime check: ensure the constant matches the actual count. |
| 107 | + // If the struct grows and the destructure above is updated but |
| 108 | + // RESULT_SCHEMA_FIELD_COUNT is not, this assertion catches the gap. |
| 109 | + let _ = &env; // env kept for Soroban test harness compatibility |
| 110 | + assert_eq!( |
| 111 | + RESULT_SCHEMA_FIELD_COUNT, |
| 112 | + 9, |
| 113 | + "RESULT_SCHEMA_FIELD_COUNT is out of sync with SLAResult. \ |
| 114 | + Update lib.rs::RESULT_SCHEMA_FIELD_COUNT and \ |
| 115 | + RESULT_SCHEMA_VERSION when adding or removing fields." |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + // ----------------------------------------------------------------------- |
| 120 | + // get_result_schema() returns the expected version and field count |
| 121 | + // ----------------------------------------------------------------------- |
| 122 | + |
| 123 | + /// Assert that `get_result_schema()` returns the constants declared in |
| 124 | + /// `lib.rs` so any divergence between the runtime schema and the |
| 125 | + /// compile-time constants surfaces in CI. |
| 126 | + #[test] |
| 127 | + fn test_get_result_schema_version_matches_constant() { |
| 128 | + let (_env, client) = setup(); |
| 129 | + let schema = client.get_result_schema(); |
| 130 | + assert_eq!( |
| 131 | + schema.schema_version, RESULT_SCHEMA_VERSION, |
| 132 | + "get_result_schema() schema_version ({}) does not match \ |
| 133 | + RESULT_SCHEMA_VERSION constant ({}). \ |
| 134 | + Increment RESULT_SCHEMA_VERSION when the result layout changes.", |
| 135 | + schema.schema_version, RESULT_SCHEMA_VERSION |
| 136 | + ); |
| 137 | + assert_eq!( |
| 138 | + schema.result_field_count, RESULT_SCHEMA_FIELD_COUNT, |
| 139 | + "get_result_schema() result_field_count ({}) does not match \ |
| 140 | + RESULT_SCHEMA_FIELD_COUNT constant ({}). \ |
| 141 | + Update RESULT_SCHEMA_FIELD_COUNT to match the SLAResult field count.", |
| 142 | + schema.result_field_count, RESULT_SCHEMA_FIELD_COUNT |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + // ----------------------------------------------------------------------- |
| 147 | + // Symbol stability: symbol values must not change without a version bump |
| 148 | + // ----------------------------------------------------------------------- |
| 149 | + |
| 150 | + /// Assert that every result symbol returned by `get_result_schema()` still |
| 151 | + /// matches the canonical values baked into `compute_result`. |
| 152 | + /// |
| 153 | + /// If a symbol is renamed (e.g. `"met"` → `"sla_met"`) this test fails, |
| 154 | + /// prompting the contributor to increment `RESULT_SCHEMA_VERSION` and |
| 155 | + /// update `CHANGELOG.md`. |
| 156 | + #[test] |
| 157 | + fn test_result_schema_symbols_are_stable() { |
| 158 | + let (env, client) = setup(); |
| 159 | + let schema = client.get_result_schema(); |
| 160 | + |
| 161 | + // These are the canonical symbol strings baked into compute_result. |
| 162 | + // Changing any of them is a breaking wire-format change. |
| 163 | + assert_eq!(schema.status_met, Symbol::new(&env, "met")); |
| 164 | + assert_eq!(schema.status_violated, Symbol::new(&env, "viol")); |
| 165 | + assert_eq!(schema.payment_reward, Symbol::new(&env, "rew")); |
| 166 | + assert_eq!(schema.payment_penalty, Symbol::new(&env, "pen")); |
| 167 | + assert_eq!(schema.rating_exceptional, Symbol::new(&env, "top")); |
| 168 | + assert_eq!(schema.rating_excellent, Symbol::new(&env, "excel")); |
| 169 | + assert_eq!(schema.rating_good, Symbol::new(&env, "good")); |
| 170 | + assert_eq!(schema.rating_poor, Symbol::new(&env, "poor")); |
| 171 | + assert!( |
| 172 | + schema.includes_config_version_hash, |
| 173 | + "includes_config_version_hash must remain true while \ |
| 174 | + SLAResult::config_version_hash exists" |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + // ----------------------------------------------------------------------- |
| 179 | + // Deprecated symbols list is empty at schema v1 |
| 180 | + // ----------------------------------------------------------------------- |
| 181 | + |
| 182 | + /// Confirm the deprecated_symbols list is empty for schema v1. |
| 183 | + /// When a symbol is deprecated, this test must be updated to assert |
| 184 | + /// the expected entry is present rather than asserting the list is empty. |
| 185 | + #[test] |
| 186 | + fn test_result_schema_no_deprecated_symbols_at_v1() { |
| 187 | + let (_env, client) = setup(); |
| 188 | + let schema = client.get_result_schema(); |
| 189 | + assert_eq!( |
| 190 | + schema.deprecated_symbols.len(), |
| 191 | + 0, |
| 192 | + "Schema v1 should have no deprecated symbols. \ |
| 193 | + If you are introducing a deprecation, update this test to \ |
| 194 | + assert the expected DeprecatedSymbol entry is present." |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + // ----------------------------------------------------------------------- |
| 199 | + // get_config_bundle includes schema with correct version |
| 200 | + // ----------------------------------------------------------------------- |
| 201 | + |
| 202 | + /// `get_config_bundle` composes `get_result_schema` internally. |
| 203 | + /// Verify its embedded schema also reflects the current version. |
| 204 | + #[test] |
| 205 | + fn test_config_bundle_schema_version_consistent() { |
| 206 | + let (_env, client) = setup(); |
| 207 | + let bundle = client.get_config_bundle(); |
| 208 | + if let Some(b) = bundle { |
| 209 | + assert_eq!( |
| 210 | + b.schema.schema_version, RESULT_SCHEMA_VERSION, |
| 211 | + "get_config_bundle schema_version is inconsistent with RESULT_SCHEMA_VERSION" |
| 212 | + ); |
| 213 | + assert_eq!( |
| 214 | + b.schema.result_field_count, RESULT_SCHEMA_FIELD_COUNT, |
| 215 | + "get_config_bundle result_field_count is inconsistent with RESULT_SCHEMA_FIELD_COUNT" |
| 216 | + ); |
| 217 | + } else { |
| 218 | + panic!("get_config_bundle returned None after initialization"); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments