using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Choices;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Plugin.Gen7.Scripts.Battle;
using PkmnLib.Plugin.Gen7.Scripts.Moves;
using PkmnLib.Static;
using PkmnLib.Static.Utils;
namespace PkmnLib.Plugin.Gen7.Tests.Scripts.Moves;
///
/// Tests for the move script and its attached .
/// Gen VII Bulbapedia behavior: "Fairy Lock prevents all Pokémon (except Ghost types) on the field from
/// switching out or fleeing during their next turn."
///
public class FairyLockTests
{
///
/// Creates a fully mocked test setup where the battle has a real as its volatile
/// script set.
///
private static (FairyLock script, IExecutingMove move, IPokemon target, IBattle battle, IScriptSet battleVolatile)
CreateTestSetup()
{
var script = new FairyLock();
var move = Substitute.For();
var target = Substitute.For();
var battle = Substitute.For();
battle.GetScripts().Returns(_ => new ScriptIterator(Array.Empty>()));
IScriptSet battleVolatile = new ScriptSet(battle);
battle.Volatile.Returns(battleVolatile);
var battleData = Substitute.For();
battleData.Battle.Returns(battle);
target.BattleData.Returns(battleData);
return (script, move, target, battle, battleVolatile);
}
///
/// Creates a mocked Pokémon with the given type names as its .
///
private static IPokemon CreatePokemonWithTypes(params string[] types)
{
var pokemon = Substitute.For();
pokemon.Types.Returns(types.Select((name, index) => new TypeIdentifier((byte)(index + 1), new StringKey(name)))
.ToList());
return pokemon;
}
///
/// Bulbapedia: "Fairy Lock prevents all Pokémon (except Ghost types) on the field from switching out or
/// fleeing during their next turn." Using the move puts the on the battle's
/// volatile scripts so it affects the whole field.
///
[Test]
public async Task OnSecondaryEffect_AddsFairyLockEffectToBattleVolatile()
{
// Arrange
var (script, move, target, _, battleVolatile) = CreateTestSetup();
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(battleVolatile.Contains(ScriptUtils.ResolveName())).IsTrue();
}
///
/// Technical test: without battle data on the target (outside of battle) the secondary effect does
/// nothing and does not throw.
///
[Test]
public void OnSecondaryEffect_TargetHasNoBattleData_DoesNotThrow()
{
// Arrange
var script = new FairyLock();
var move = Substitute.For();
var target = Substitute.For();
target.BattleData.Returns((IPokemonBattleData?)null);
// Act & Assert - should not throw
script.OnSecondaryEffect(move, target, 0);
}
///
/// Bulbapedia: "Fairy Lock prevents all Pokémon (except Ghost types) on the field from switching out".
/// A non-Ghost Pokémon is prevented from switching.
///
[Test]
public async Task PreventSelfSwitch_NonGhostPokemon_SwitchPrevented()
{
// Arrange
var effect = new FairyLockEffect();
var user = CreatePokemonWithTypes("normal");
var choice = Substitute.For();
choice.User.Returns(user);
var prevent = false;
// Act
effect.PreventSelfSwitch(choice, ref prevent);
// Assert
await Assert.That(prevent).IsTrue();
}
///
/// Bulbapedia: Fairy Lock prevents all Pokémon "(except Ghost types)" from switching out — a Ghost-type
/// Pokémon can still switch out.
///
[Test]
public async Task PreventSelfSwitch_GhostTypePokemon_SwitchNotPrevented()
{
// Arrange
var effect = new FairyLockEffect();
var user = CreatePokemonWithTypes("ghost");
var choice = Substitute.For();
choice.User.Returns(user);
var prevent = false;
// Act
effect.PreventSelfSwitch(choice, ref prevent);
// Assert
await Assert.That(prevent).IsFalse();
}
///
/// Bulbapedia: "Fairy Lock prevents all Pokémon (except Ghost types) on the field from switching out or
/// fleeing during their next turn." A non-Ghost Pokémon is prevented from fleeing.
///
[Test]
public async Task PreventSelfRunAway_NonGhostPokemon_FleeingPrevented()
{
// Arrange
var effect = new FairyLockEffect();
var user = CreatePokemonWithTypes("normal");
var choice = Substitute.For();
choice.User.Returns(user);
var prevent = false;
// Act
effect.PreventSelfRunAway(choice, ref prevent);
// Assert
await Assert.That(prevent).IsTrue();
}
///
/// Bulbapedia: Fairy Lock prevents all Pokémon "(except Ghost types)" from fleeing — a Ghost-type
/// Pokémon can still flee.
///
[Test]
public async Task PreventSelfRunAway_GhostTypePokemon_FleeingNotPrevented()
{
// Arrange
var effect = new FairyLockEffect();
var user = CreatePokemonWithTypes("ghost");
var choice = Substitute.For();
choice.User.Returns(user);
var prevent = false;
// Act
effect.PreventSelfRunAway(choice, ref prevent);
// Assert
await Assert.That(prevent).IsFalse();
}
///
/// Bulbapedia: the lock applies "during their next turn" — at the end of the turn Fairy Lock was used,
/// the stays on the battle so the following turn is still locked.
///
[Test]
public async Task OnEndTurn_FirstEndOfTurn_EffectRemains()
{
// Arrange
var (script, move, target, battle, battleVolatile) = CreateTestSetup();
script.OnSecondaryEffect(move, target, 0);
var effect = battleVolatile.Get()!;
// Act - end of the turn Fairy Lock was used
effect.OnEndTurn(battle, battle);
// Assert
await Assert.That(battleVolatile.Contains(ScriptUtils.ResolveName())).IsTrue();
}
///
/// Bulbapedia: the lock only lasts "during their next turn" — at the end of the turn after Fairy Lock
/// was used, the removes itself.
///
[Test]
public async Task OnEndTurn_SecondEndOfTurn_EffectRemovesItself()
{
// Arrange
var (script, move, target, battle, battleVolatile) = CreateTestSetup();
script.OnSecondaryEffect(move, target, 0);
var effect = battleVolatile.Get()!;
// Act - end of the turn Fairy Lock was used, then end of the locked turn
effect.OnEndTurn(battle, battle);
effect.OnEndTurn(battle, battle);
// Assert
await Assert.That(battleVolatile.Contains(ScriptUtils.ResolveName())).IsFalse();
}
}