using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Choices;
using PkmnLib.Plugin.Gen7.Scripts.Moves;
namespace PkmnLib.Plugin.Gen7.Tests.Scripts.Moves;
///
/// Tests for the move script.
/// Gen VII Bulbapedia behavior (Generations V to VII): "The target will move next on the current turn,
/// ignoring priority. [...] It fails if the target has already moved on the same turn. [...] After You fails
/// if the order remains the same after using After You."
///
public class AfterYouTests
{
private static IMoveChoice CreateChoice(IPokemon user, uint speed)
{
var choice = Substitute.For();
choice.User.Returns(user);
choice.Speed.Returns(speed);
return choice;
}
private static (AfterYou script, IExecutingMove move, IHitData hitData) CreateTestSetup(BattleChoiceQueue? queue)
{
var script = new AfterYou();
var move = Substitute.For();
var hitData = Substitute.For();
move.GetHitData(Arg.Any(), Arg.Any()).Returns(hitData);
var battle = Substitute.For();
battle.ChoiceQueue.Returns(queue);
var battleData = Substitute.For();
battleData.Battle.Returns(battle);
var user = Substitute.For();
user.BattleData.Returns(battleData);
move.User.Returns(user);
return (script, move, hitData);
}
///
/// Bulbapedia: "The target will move next on the current turn, ignoring priority."
/// The target's choice is moved in front of a faster Pokémon's remaining choice, and the move does not fail.
///
[Test]
public async Task OnSecondaryEffect_TargetLaterInQueue_TargetMovesNext()
{
// Arrange
var user = Substitute.For();
var target = Substitute.For();
var other = Substitute.For();
// Sorted by speed: user (100), other (75), target (50)
var queue = new BattleChoiceQueue([
CreateChoice(user, 100), CreateChoice(other, 75), CreateChoice(target, 50),
]);
// The user's own choice is executing, so it has been dequeued already.
queue.Dequeue();
var (script, move, hitData) = CreateTestSetup(queue);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert - the target is now the next Pokémon to move, ahead of the faster one
await Assert.That(queue.Peek()!.User).IsEqualTo(target);
hitData.DidNotReceive().Fail();
}
///
/// Bulbapedia: "It fails if the target has already moved on the same turn."
/// The target's choice is no longer in the queue, so the move fails.
///
[Test]
public void OnSecondaryEffect_TargetAlreadyMoved_Fails()
{
// Arrange
var user = Substitute.For();
var target = Substitute.For();
// Sorted by speed: target (100), user (50)
var queue = new BattleChoiceQueue([
CreateChoice(target, 100), CreateChoice(user, 50),
]);
// The target already moved this turn, and the user's own choice is executing.
queue.Dequeue();
queue.Dequeue();
var (script, move, hitData) = CreateTestSetup(queue);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
hitData.Received(1).Fail();
}
///
/// Bulbapedia: "After You fails if the order remains the same after using After You."
/// The target was already the next Pokémon to move, so using After You changes nothing and the move
/// must fail.
///
[Test]
public void OnSecondaryEffect_TargetAlreadyNext_Fails()
{
// Arrange
var user = Substitute.For();
var target = Substitute.For();
// Sorted by speed: user (100), target (50)
var queue = new BattleChoiceQueue([
CreateChoice(user, 100), CreateChoice(target, 50),
]);
// The user's own choice is executing; the target is already next.
queue.Dequeue();
var (script, move, hitData) = CreateTestSetup(queue);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
hitData.Received(1).Fail();
}
///
/// Technical test: when the battle has no active choice queue, the script returns without throwing
/// and without failing the hit.
///
[Test]
public void OnSecondaryEffect_NoChoiceQueue_DoesNothing()
{
// Arrange
var (script, move, hitData) = CreateTestSetup(null);
// Act
script.OnSecondaryEffect(move, Substitute.For(), 0);
// Assert
hitData.DidNotReceive().Fail();
}
}