/ GUNRPG.Tests / PetStateTests.cs
PetStateTests.cs
1 using GUNRPG.Core.VirtualPet; 2 using Xunit; 3 4 namespace GUNRPG.Tests; 5 6 public class PetStateTests 7 { 8 [Fact] 9 public void PetState_CanBeCreated_WithAllProperties() 10 { 11 // Arrange 12 var operatorId = Guid.NewGuid(); 13 var lastUpdated = DateTimeOffset.UtcNow; 14 15 // Act 16 var petState = new PetState( 17 OperatorId: operatorId, 18 Health: 75.0f, 19 Fatigue: 30.0f, 20 Injury: 10.0f, 21 Stress: 40.0f, 22 Morale: 80.0f, 23 Hunger: 25.0f, 24 Hydration: 60.0f, 25 LastUpdated: lastUpdated 26 ); 27 28 // Assert 29 Assert.Equal(operatorId, petState.OperatorId); 30 Assert.Equal(75.0f, petState.Health); 31 Assert.Equal(30.0f, petState.Fatigue); 32 Assert.Equal(10.0f, petState.Injury); 33 Assert.Equal(40.0f, petState.Stress); 34 Assert.Equal(80.0f, petState.Morale); 35 Assert.Equal(25.0f, petState.Hunger); 36 Assert.Equal(60.0f, petState.Hydration); 37 Assert.Equal(lastUpdated, petState.LastUpdated); 38 } 39 40 [Fact] 41 public void PetState_SupportsValueEquality() 42 { 43 // Arrange 44 var operatorId = Guid.NewGuid(); 45 var lastUpdated = DateTimeOffset.UtcNow; 46 47 var petState1 = new PetState( 48 operatorId, 49 Health: 100.0f, 50 Fatigue: 0.0f, 51 Injury: 0.0f, 52 Stress: 0.0f, 53 Morale: 100.0f, 54 Hunger: 0.0f, 55 Hydration: 100.0f, 56 lastUpdated 57 ); 58 59 var petState2 = new PetState( 60 operatorId, 61 Health: 100.0f, 62 Fatigue: 0.0f, 63 Injury: 0.0f, 64 Stress: 0.0f, 65 Morale: 100.0f, 66 Hunger: 0.0f, 67 Hydration: 100.0f, 68 lastUpdated 69 ); 70 71 // Assert 72 Assert.Equal(petState1, petState2); 73 Assert.True(petState1 == petState2); 74 } 75 76 [Fact] 77 public void PetState_SupportsWithExpression_ForImmutableUpdates() 78 { 79 // Arrange 80 var originalState = new PetState( 81 OperatorId: Guid.NewGuid(), 82 Health: 100.0f, 83 Fatigue: 0.0f, 84 Injury: 0.0f, 85 Stress: 0.0f, 86 Morale: 100.0f, 87 Hunger: 0.0f, 88 Hydration: 100.0f, 89 LastUpdated: DateTimeOffset.UtcNow 90 ); 91 92 // Act - Update health using 'with' expression 93 var updatedState = originalState with { Health = 50.0f }; 94 95 // Assert 96 Assert.Equal(100.0f, originalState.Health); // Original unchanged 97 Assert.Equal(50.0f, updatedState.Health); // New instance with updated value 98 Assert.Equal(originalState.OperatorId, updatedState.OperatorId); // Other values copied 99 } 100 101 [Fact] 102 public void PetState_AcceptsMinimumValues() 103 { 104 // Arrange & Act 105 var petState = new PetState( 106 OperatorId: Guid.Empty, 107 Health: 0.0f, 108 Fatigue: 0.0f, 109 Injury: 0.0f, 110 Stress: 0.0f, 111 Morale: 0.0f, 112 Hunger: 0.0f, 113 Hydration: 0.0f, 114 LastUpdated: DateTimeOffset.MinValue 115 ); 116 117 // Assert 118 Assert.Equal(0.0f, petState.Health); 119 Assert.Equal(0.0f, petState.Fatigue); 120 Assert.Equal(0.0f, petState.Injury); 121 Assert.Equal(0.0f, petState.Stress); 122 Assert.Equal(0.0f, petState.Morale); 123 Assert.Equal(0.0f, petState.Hunger); 124 Assert.Equal(0.0f, petState.Hydration); 125 } 126 127 [Fact] 128 public void PetState_AcceptsMaximumValues() 129 { 130 // Arrange & Act 131 var petState = new PetState( 132 OperatorId: Guid.NewGuid(), 133 Health: 100.0f, 134 Fatigue: 100.0f, 135 Injury: 100.0f, 136 Stress: 100.0f, 137 Morale: 100.0f, 138 Hunger: 100.0f, 139 Hydration: 100.0f, 140 LastUpdated: DateTimeOffset.MaxValue 141 ); 142 143 // Assert 144 Assert.Equal(100.0f, petState.Health); 145 Assert.Equal(100.0f, petState.Fatigue); 146 Assert.Equal(100.0f, petState.Injury); 147 Assert.Equal(100.0f, petState.Stress); 148 Assert.Equal(100.0f, petState.Morale); 149 Assert.Equal(100.0f, petState.Hunger); 150 Assert.Equal(100.0f, petState.Hydration); 151 } 152 153 [Fact] 154 public void PetState_HasSameHashCode_ForEqualValues() 155 { 156 // Arrange 157 var operatorId = Guid.NewGuid(); 158 var lastUpdated = DateTimeOffset.UtcNow; 159 160 var petState1 = new PetState( 161 operatorId, 162 Health: 100.0f, 163 Fatigue: 0.0f, 164 Injury: 0.0f, 165 Stress: 0.0f, 166 Morale: 100.0f, 167 Hunger: 0.0f, 168 Hydration: 100.0f, 169 lastUpdated 170 ); 171 172 var petState2 = new PetState( 173 operatorId, 174 Health: 100.0f, 175 Fatigue: 0.0f, 176 Injury: 0.0f, 177 Stress: 0.0f, 178 Morale: 100.0f, 179 Hunger: 0.0f, 180 Hydration: 100.0f, 181 lastUpdated 182 ); 183 184 // Assert - Equal objects must have equal hash codes 185 Assert.Equal(petState1.GetHashCode(), petState2.GetHashCode()); 186 } 187 188 [Fact] 189 public void PetState_IsImmutable_CannotModifyPropertiesDirectly() 190 { 191 // Arrange 192 var petState = new PetState( 193 OperatorId: Guid.NewGuid(), 194 Health: 100.0f, 195 Fatigue: 0.0f, 196 Injury: 0.0f, 197 Stress: 0.0f, 198 Morale: 100.0f, 199 Hunger: 0.0f, 200 Hydration: 100.0f, 201 LastUpdated: DateTimeOffset.UtcNow 202 ); 203 204 // Act - Verify that properties are init-only 205 // This test verifies immutability at compile-time 206 // The following would not compile: 207 // petState.Health = 50.0f; 208 209 // Assert - Verify we can only create new instances with 'with' expression 210 var newState = petState with { Health = 50.0f }; 211 Assert.Equal(100.0f, petState.Health); 212 Assert.Equal(50.0f, newState.Health); 213 } 214 }