|
| 1 | +// Copyright 2025 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package eras_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/blinklabs-io/dingo/ledger/eras" |
| 21 | +) |
| 22 | + |
| 23 | +func TestGetEraById(t *testing.T) { |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + eraId uint |
| 27 | + expected *eras.EraDesc |
| 28 | + wantNil bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "Byron era (ID=0)", |
| 32 | + eraId: 0, |
| 33 | + expected: &eras.ByronEraDesc, |
| 34 | + wantNil: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Shelley era (ID=1)", |
| 38 | + eraId: 1, |
| 39 | + expected: &eras.ShelleyEraDesc, |
| 40 | + wantNil: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "Allegra era (ID=2)", |
| 44 | + eraId: 2, |
| 45 | + expected: &eras.AllegraEraDesc, |
| 46 | + wantNil: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Mary era (ID=3)", |
| 50 | + eraId: 3, |
| 51 | + expected: &eras.MaryEraDesc, |
| 52 | + wantNil: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Alonzo era (ID=4)", |
| 56 | + eraId: 4, |
| 57 | + expected: &eras.AlonzoEraDesc, |
| 58 | + wantNil: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Babbage era (ID=5)", |
| 62 | + eraId: 5, |
| 63 | + expected: &eras.BabbageEraDesc, |
| 64 | + wantNil: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Conway era (ID=6)", |
| 68 | + eraId: 6, |
| 69 | + expected: &eras.ConwayEraDesc, |
| 70 | + wantNil: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "Invalid era ID (does not exist)", |
| 74 | + eraId: 999, |
| 75 | + wantNil: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "Gap era ID (ID=7, non-existent era)", |
| 79 | + eraId: 7, |
| 80 | + wantNil: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Gap era ID (ID=8, non-existent era)", |
| 84 | + eraId: 8, |
| 85 | + wantNil: true, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "Gap era ID (ID=10, non-existent era)", |
| 89 | + eraId: 10, |
| 90 | + wantNil: true, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + result := eras.GetEraById(tt.eraId) |
| 97 | + |
| 98 | + if tt.wantNil { |
| 99 | + if result != nil { |
| 100 | + t.Errorf( |
| 101 | + "GetEraById(%d) expected nil, got %v", |
| 102 | + tt.eraId, |
| 103 | + result, |
| 104 | + ) |
| 105 | + } |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + if result == nil { |
| 110 | + t.Errorf( |
| 111 | + "GetEraById(%d) expected era descriptor, got nil", |
| 112 | + tt.eraId, |
| 113 | + ) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + if result.Id != tt.expected.Id { |
| 118 | + t.Errorf( |
| 119 | + "GetEraById(%d) ID mismatch: expected %d, got %d", |
| 120 | + tt.eraId, |
| 121 | + tt.expected.Id, |
| 122 | + result.Id, |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + if result.Name != tt.expected.Name { |
| 127 | + t.Errorf( |
| 128 | + "GetEraById(%d) Name mismatch: expected %s, got %s", |
| 129 | + tt.eraId, |
| 130 | + tt.expected.Name, |
| 131 | + result.Name, |
| 132 | + ) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// TestGetEraById_ConsistencyWithErasArray verifies that GetEraById returns the same |
| 139 | +// results as direct array access for valid IDs that happen to match array indices |
| 140 | +func TestGetEraById_ConsistencyWithErasArray(t *testing.T) { |
| 141 | + // Test eras that have matching array indices (Byron, Shelley, Allegra, Mary, Alonzo) |
| 142 | + validArrayIndices := []uint{0, 1, 2, 3, 4} |
| 143 | + |
| 144 | + for _, eraId := range validArrayIndices { |
| 145 | + t.Run(t.Name(), func(t *testing.T) { |
| 146 | + // Get via GetEraById |
| 147 | + resultById := eras.GetEraById(eraId) |
| 148 | + |
| 149 | + // Get via direct array access (this should work for these specific IDs) |
| 150 | + var resultByArray *eras.EraDesc |
| 151 | + if int(eraId) < len(eras.Eras) { |
| 152 | + resultByArray = &eras.Eras[eraId] |
| 153 | + } |
| 154 | + |
| 155 | + if resultById == nil && resultByArray != nil { |
| 156 | + t.Errorf( |
| 157 | + "GetEraById(%d) returned nil but array access succeeded", |
| 158 | + eraId, |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + if resultById != nil && resultByArray == nil { |
| 163 | + t.Errorf( |
| 164 | + "GetEraById(%d) succeeded but array access failed", |
| 165 | + eraId, |
| 166 | + ) |
| 167 | + } |
| 168 | + |
| 169 | + if resultById != nil && resultByArray != nil { |
| 170 | + if resultById.Id != resultByArray.Id { |
| 171 | + t.Errorf( |
| 172 | + "GetEraById(%d) ID mismatch with array: %d vs %d", |
| 173 | + eraId, |
| 174 | + resultById.Id, |
| 175 | + resultByArray.Id, |
| 176 | + ) |
| 177 | + } |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// TestGetEraById_HandlesGapsInEraIds tests that the function properly handles |
| 184 | +// the gaps in era IDs (like missing 5, 6, 8) without panicking |
| 185 | +func TestGetEraById_HandlesGapsInEraIds(t *testing.T) { |
| 186 | + // These era IDs don't exist and should return nil |
| 187 | + gapIds := []uint{7, 8, 9, 10, 100, 1000} |
| 188 | + |
| 189 | + for _, eraId := range gapIds { |
| 190 | + t.Run(t.Name(), func(t *testing.T) { |
| 191 | + // This should not panic and should return nil |
| 192 | + result := eras.GetEraById(eraId) |
| 193 | + if result != nil { |
| 194 | + t.Errorf( |
| 195 | + "GetEraById(%d) expected nil for gap era ID, got %v", |
| 196 | + eraId, |
| 197 | + result, |
| 198 | + ) |
| 199 | + } |
| 200 | + }) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// TestGetEraById_AllKnownEras verifies that all eras in the Eras array |
| 205 | +// can be retrieved by their actual ID (not array index) |
| 206 | +func TestGetEraById_AllKnownEras(t *testing.T) { |
| 207 | + for i, era := range eras.Eras { |
| 208 | + t.Run(era.Name, func(t *testing.T) { |
| 209 | + result := eras.GetEraById(era.Id) |
| 210 | + |
| 211 | + if result == nil { |
| 212 | + t.Errorf( |
| 213 | + "GetEraById(%d) for %s era returned nil", |
| 214 | + era.Id, |
| 215 | + era.Name, |
| 216 | + ) |
| 217 | + return |
| 218 | + } |
| 219 | + |
| 220 | + if result.Id != era.Id { |
| 221 | + t.Errorf( |
| 222 | + "GetEraById(%d) ID mismatch: expected %d, got %d", |
| 223 | + era.Id, |
| 224 | + era.Id, |
| 225 | + result.Id, |
| 226 | + ) |
| 227 | + } |
| 228 | + |
| 229 | + if result.Name != era.Name { |
| 230 | + t.Errorf( |
| 231 | + "GetEraById(%d) Name mismatch: expected %s, got %s", |
| 232 | + era.Id, |
| 233 | + era.Name, |
| 234 | + result.Name, |
| 235 | + ) |
| 236 | + } |
| 237 | + |
| 238 | + // Verify it's pointing to the same era descriptor |
| 239 | + if result != &eras.Eras[i] { |
| 240 | + t.Errorf( |
| 241 | + "GetEraById(%d) returned different era descriptor than expected", |
| 242 | + era.Id, |
| 243 | + ) |
| 244 | + } |
| 245 | + }) |
| 246 | + } |
| 247 | +} |
0 commit comments