using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Plugin.Gen7.Scripts.Moves;
using PkmnLib.Plugin.Gen7.Scripts.Side;
namespace PkmnLib.Plugin.Gen7.Tests.Scripts.Moves;
///
/// Tests for the move script and its volatile effect script
/// .
/// Gen VII Bulbapedia behavior: "Lucky Chant prevents opponents from landing critical hits on the user's
/// party for five turns, even if the move would always result in a critical hit (e.g. Frost Breath)."
///
public class LuckyChantTests
{
///
/// Bulbapedia: the protection applies to "the user's party for five turns" — like the other
/// side-affecting moves, the effect belongs on the user's side of the field so that Pokémon switching
/// in during those five turns are protected as well.
///
[Test]
public async Task OnSecondaryEffect_Used_AddsLuckyChantEffectToUserSide()
{
// Arrange
var script = new LuckyChant();
var move = Substitute.For();
var sideScripts = Substitute.For();
var side = Substitute.For();
side.VolatileScripts.Returns(sideScripts);
var battleData = Substitute.For();
battleData.BattleSide.Returns(side);
var user = Substitute.For();
user.BattleData.Returns(battleData);
user.Volatile.Returns(Substitute.For());
move.User.Returns(user);
// Act - Lucky Chant targets the user's side; the user itself is one of the targets
script.OnSecondaryEffect(move, user, 0);
// Assert
await Assert.That(sideScripts.ReceivedCalls().Any(c => c.GetMethodInfo().Name is "Add" or "StackOrAdd"))
.IsTrue();
}
///
/// Bulbapedia: Lucky Chant "prevents opponents from landing critical hits" — the engine consults the
/// defending side through
/// (MoveTurnExecutor only runs over the attacking move's
/// own scripts), so the effect must block through that hook to protect the party.
///
[Test]
public async Task BlockIncomingCriticalHit_EffectActive_CriticalHitBlocked()
{
// Arrange
var effect = new LuckyChantEffect();
var attack = Substitute.For();
var target = Substitute.For();
var block = false;
// Act
if (effect is IScriptBlockIncomingCriticalHit blockIncoming)
blockIncoming.BlockIncomingCriticalHit(attack, target, 0, ref block);
// Assert
await Assert.That(block).IsTrue();
}
///
/// Bulbapedia: the protection lasts "for five turns" — after four end-of-turns (the turn Lucky Chant
/// was used and three more) the effect is still active.
///
[Test]
public async Task OnEndTurn_FourTurnsPassed_EffectStillActive()
{
// Arrange
var effect = new LuckyChantEffect();
var owner = Substitute.For();
owner.GetScripts().Returns(_ => new ScriptIterator(Array.Empty>()));
IScriptSet set = new ScriptSet(owner);
set.Add(effect);
// Act
for (var i = 0; i < 4; i++)
effect.OnEndTurn(owner, Substitute.For());
// Assert
await Assert.That(set.Contains(ScriptUtils.ResolveName())).IsTrue();
}
///
/// Bulbapedia: the protection lasts "for five turns" — at the end of the fifth turn the effect expires.
///
[Test]
public async Task OnEndTurn_FiveTurnsPassed_EffectExpires()
{
// Arrange
var effect = new LuckyChantEffect();
var owner = Substitute.For();
owner.GetScripts().Returns(_ => new ScriptIterator(Array.Empty>()));
IScriptSet set = new ScriptSet(owner);
set.Add(effect);
// Act
for (var i = 0; i < 5; i++)
effect.OnEndTurn(owner, Substitute.For());
// Assert
await Assert.That(set.Contains(ScriptUtils.ResolveName())).IsFalse();
}
}