namespace SOR.Game.Tests; using SOR.Game.Abilities; using Xunit; /// /// Tests for the save-time ability validation used by the admin CardEditor. /// public sealed class AbilityValidatorTests { private const string ValidActive = """ [{ "Type": "Active", "Name": "Bolt", "ActionGroup": { "LogicOp": "And", "Actions": [{ "Target": { "Scope": "Battlefield", "Type": "Unit", "Ownership": "Enemy", "Count": { "N": 1 } }, "Effects": [ { "$type": "DealDamage", "Amount": { "Fixed": 3 } } ] }], "Groups": [] } }] """; [Fact] public void ValidAbility_PassesForUnitAndEffectCards() { Assert.Empty(AbilityValidator.Validate(ValidActive, "Spirit")); Assert.Empty(AbilityValidator.Validate(ValidActive, "Repentance")); Assert.Empty(AbilityValidator.Validate(null, "Spirit")); Assert.Empty(AbilityValidator.Validate("", "Spirit")); } [Fact] public void UnparseableJson_IsRejected() { const string badEnum = """ [{ "Type": "Passive", "ActionGroup": { "LogicOp": "And", "Actions": [{ "Target": { "Scope": "Battlefield", "Type": "Unit", "Ownership": "ExactSelf", "Count": { "N": 1 } }, "Effects": [ { "$type": "ModifyHealth", "Amount": { "Fixed": 1 }, "StatTarget": "Base" } ] }], "Groups": [] } }] """; var errors = AbilityValidator.Validate(badEnum, "Spirit"); Assert.Single(errors); Assert.Contains("deserialize", errors[0]); } [Fact] public void UnimplementedTrigger_IsRejected() { const string json = """ [{ "Type": "Triggered", "Name": "Dead Trigger", "TriggerEvent": { "$type": "OnStatModified", "Ownership": "Any" }, "ActionGroup": { "LogicOp": "And", "Actions": [], "Groups": [] } }] """; var errors = AbilityValidator.Validate(json, "Spirit"); Assert.Contains(errors, e => e.Contains("never fire")); } [Fact] public void TriggeredWithoutEvent_IsRejected() { const string json = """ [{ "Type": "Triggered", "Name": "Empty", "ActionGroup": { "LogicOp": "And", "Actions": [], "Groups": [] } }] """; var errors = AbilityValidator.Validate(json, "Spirit"); Assert.Contains(errors, e => e.Contains("no TriggerEvent")); } [Fact] public void UnimplementedEffect_IsRejected() { const string json = """ [{ "Type": "Active", "Name": "NoOp", "ActionGroup": { "LogicOp": "And", "Actions": [{ "Target": { "Scope": "Battlefield", "Type": "Unit", "Ownership": "Enemy", "Count": { "N": 1 } }, "Effects": [ { "$type": "ChannelCard", "PayCost": true } ] }], "Groups": [] } }] """; var errors = AbilityValidator.Validate(json, "Spirit"); Assert.Contains(errors, e => e.Contains("no engine implementation")); } [Fact] public void EffectCard_RangeAndNonActive_AreRejected() { const string json = """ [{ "Type": "Active", "Name": "Ranged Smite", "Range": { "Min": 1, "Max": 2 }, "ActionGroup": { "LogicOp": "And", "Actions": [{ "Target": { "Scope": "Battlefield", "Type": "Unit", "Ownership": "Enemy", "Count": { "N": 1 } }, "Effects": [ { "$type": "DealDamage", "Amount": { "Fixed": 1 } } ] }], "Groups": [] } }, { "Type": "Passive", "Name": "Never Runs", "ActionGroup": { "LogicOp": "And", "Actions": [], "Groups": [] } }] """; var errors = AbilityValidator.Validate(json, "Repentance"); Assert.Contains(errors, e => e.Contains("Remove the Range")); Assert.Contains(errors, e => e.Contains("only Active abilities execute")); // The same definitions are fine on a battlefield unit. Assert.Empty(AbilityValidator.Validate(json, "Spirit")); } [Fact] public void PassiveWithOrGroup_IsRejected() { const string json = """ [{ "Type": "Passive", "Name": "Choose Buff", "ActionGroup": { "LogicOp": "Or", "ChooserRole": "Self", "Actions": [{ "Target": { "Scope": "Battlefield", "Type": "Unit", "Ownership": "ExactSelf", "Count": { "N": 1 } }, "Effects": [ { "$type": "ModifyDamage", "Amount": { "Fixed": 1 } } ] }], "Groups": [] } }] """; var errors = AbilityValidator.Validate(json, "Spirit"); Assert.Contains(errors, e => e.Contains("OR/XOR")); } }