init commit
This commit is contained in:
commit
1497f114e6
47 files changed
+8358
No files matched your search
@@ -0,0 +1,231 @@
|
||||
#include "ActionExecutor.h"
|
||||
#include "ActionFile.h"
|
||||
#include "BilicraftClient.h"
|
||||
|
||||
#include <climits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "protocolCraft/Packets/Game/Serverbound/ServerboundContainerClickPacket.hpp"
|
||||
#include "botcraft/Game/Inventory/Window.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Bilicraft;
|
||||
using Botcraft::InventoryType;
|
||||
|
||||
TEST(ActionExecutor, MapsEveryGenericContainerSize)
|
||||
{
|
||||
const InventoryType types[] = {InventoryType::Generic9x1, InventoryType::Generic9x2, InventoryType::Generic9x3,
|
||||
InventoryType::Generic9x4, InventoryType::Generic9x5, InventoryType::Generic9x6};
|
||||
for (int rows = 1; rows <= 6; ++rows)
|
||||
{
|
||||
EXPECT_EQ(ActionGuiSlot(types[rows - 1], 1, 1), 0);
|
||||
EXPECT_EQ(ActionGuiSlot(types[rows - 1], rows, 9), rows * 9 - 1);
|
||||
EXPECT_THROW(ActionGuiSlot(types[rows - 1], rows + 1, 1), std::runtime_error);
|
||||
}
|
||||
EXPECT_EQ(ActionGuiSlot(InventoryType::ShulkerBox, 3, 9), 26);
|
||||
EXPECT_THROW(ActionGuiSlot(InventoryType::ShulkerBox, 4, 1), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, RejectsNonNineColumnGuiRatherThanGuessing)
|
||||
{
|
||||
for (const auto type : {InventoryType::PlayerInventory, InventoryType::Default, InventoryType::Generic3x3,
|
||||
InventoryType::Furnace, InventoryType::Hopper, InventoryType::Crafting,
|
||||
InventoryType::Merchant, InventoryType::Anvil})
|
||||
EXPECT_THROW(ActionGuiSlot(type, 1, 1), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, RejectsGuiBoundsBeforeArithmetic)
|
||||
{
|
||||
for (int bad : {INT_MIN, -1, 0, 10, INT_MAX})
|
||||
EXPECT_THROW(ActionGuiSlot(InventoryType::Generic9x6, 1, bad), std::runtime_error);
|
||||
for (int bad : {INT_MIN, -1, 0, 7, INT_MAX})
|
||||
EXPECT_THROW(ActionGuiSlot(InventoryType::Generic9x6, bad, 1), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, MapsAllBackpackAndHotbarPositions)
|
||||
{
|
||||
for (int row = 1; row <= 3; ++row)
|
||||
for (int col = 1; col <= 9; ++col)
|
||||
EXPECT_EQ(ActionBackpackSlot(row, col), 9 + (row - 1) * 9 + col - 1);
|
||||
for (int slot = 1; slot <= 9; ++slot)
|
||||
EXPECT_EQ(ActionHotbarIndex(slot), slot - 1);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, RejectsBackpackAndHotbarBounds)
|
||||
{
|
||||
for (int bad : {INT_MIN, -1, 0, 4, INT_MAX})
|
||||
EXPECT_THROW(ActionBackpackSlot(bad, 1), std::runtime_error);
|
||||
for (int bad : {INT_MIN, -1, 0, 10, INT_MAX})
|
||||
{
|
||||
EXPECT_THROW(ActionBackpackSlot(1, bad), std::runtime_error);
|
||||
EXPECT_THROW(ActionHotbarIndex(bad), std::runtime_error);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, BuildsRealNumberKeySwapWithoutCursorPrediction)
|
||||
{
|
||||
const auto packet = MakeBackpackSwapPacket(3, 9, 9);
|
||||
EXPECT_EQ(packet->GetContainerId(), 0);
|
||||
EXPECT_EQ(packet->GetStateId(), -1);
|
||||
EXPECT_EQ(packet->GetSlotNum(), 35);
|
||||
EXPECT_EQ(packet->GetButtonNum(), 8);
|
||||
EXPECT_EQ(packet->GetClickType(), 2);
|
||||
EXPECT_TRUE(packet->GetChangedSlots().empty());
|
||||
EXPECT_FALSE(packet->GetCarriedItem().GetActualItem().has_value());
|
||||
ProtocolCraft::WriteContainer bytes;
|
||||
packet->Write(bytes);
|
||||
EXPECT_FALSE(bytes.empty());
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, SwapPacketRejectsInvalidPositions)
|
||||
{
|
||||
EXPECT_THROW(MakeBackpackSwapPacket(0, 1, 1), std::runtime_error);
|
||||
EXPECT_THROW(MakeBackpackSwapPacket(1, 10, 1), std::runtime_error);
|
||||
EXPECT_THROW(MakeBackpackSwapPacket(1, 1, 10), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, ResolvesDropTargetWithoutChangingTheSelection)
|
||||
{
|
||||
for (int selected = 0; selected < 9; ++selected)
|
||||
{
|
||||
EXPECT_EQ(ActionDropSlot(0, selected), selected + 1);
|
||||
for (int requested = 1; requested <= 9; ++requested)
|
||||
EXPECT_EQ(ActionDropSlot(requested, selected), requested);
|
||||
}
|
||||
for (int invalid : {INT_MIN, -1, 9, INT_MAX})
|
||||
EXPECT_THROW(ActionDropSlot(0, invalid), std::runtime_error);
|
||||
for (int invalid : {INT_MIN, -1, 10, INT_MAX})
|
||||
EXPECT_THROW(ActionDropSlot(invalid, 0), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, BuildsSingleAndWholeStackDropPacketsForEveryHotbarSlot)
|
||||
{
|
||||
for (int slot = 1; slot <= 9; ++slot)
|
||||
for (bool all : {false, true})
|
||||
{
|
||||
const auto packet = MakeHotbarDropPacket(slot, all);
|
||||
EXPECT_EQ(packet->GetContainerId(), 0);
|
||||
EXPECT_EQ(packet->GetStateId(), -1);
|
||||
EXPECT_EQ(packet->GetSlotNum(), 35 + slot);
|
||||
EXPECT_EQ(packet->GetClickType(), 4);
|
||||
EXPECT_EQ(packet->GetButtonNum(), all ? 1 : 0);
|
||||
EXPECT_TRUE(packet->GetChangedSlots().empty());
|
||||
EXPECT_FALSE(packet->GetCarriedItem().GetActualItem().has_value());
|
||||
ProtocolCraft::WriteContainer bytes;
|
||||
packet->Write(bytes);
|
||||
EXPECT_FALSE(bytes.empty());
|
||||
}
|
||||
for (int invalid : {INT_MIN, -1, 0, 10, INT_MAX})
|
||||
EXPECT_THROW(MakeHotbarDropPacket(invalid, false), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, DropRemainderHandlesEmptySingleAndFullStacksWithoutMutatingInput)
|
||||
{
|
||||
const ProtocolCraft::Slot empty;
|
||||
EXPECT_TRUE(RemainingAfterDrop(empty, false).IsEmptySlot());
|
||||
EXPECT_TRUE(RemainingAfterDrop(empty, true).IsEmptySlot());
|
||||
ProtocolCraft::Slot original;
|
||||
original.SetItemId(1);
|
||||
for (int count : {1, 2, 64})
|
||||
{
|
||||
original.SetItemCount(count);
|
||||
const auto remaining = RemainingAfterDrop(original, false);
|
||||
EXPECT_EQ(remaining.GetItemCount(), count - 1);
|
||||
EXPECT_EQ(remaining.IsEmptySlot(), count == 1);
|
||||
if (count > 1)
|
||||
{
|
||||
auto expected = original;
|
||||
expected.SetItemCount(count - 1);
|
||||
ProtocolCraft::WriteContainer actualBytes, expectedBytes;
|
||||
remaining.Write(actualBytes);
|
||||
expected.Write(expectedBytes);
|
||||
EXPECT_EQ(actualBytes, expectedBytes);
|
||||
}
|
||||
EXPECT_TRUE(RemainingAfterDrop(original, true).IsEmptySlot());
|
||||
EXPECT_EQ(original.GetItemCount(), count);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, DropDistinguishesKnownEmptySlotsFromInventoryReset)
|
||||
{
|
||||
Botcraft::Window window(InventoryType::PlayerInventory);
|
||||
EXPECT_THROW(ReadDropSlot(window, 36), std::runtime_error);
|
||||
window.SetSlot(36, ProtocolCraft::Slot());
|
||||
EXPECT_TRUE(ReadDropSlot(window, 36).IsEmptySlot());
|
||||
ProtocolCraft::Slot stack;
|
||||
stack.SetItemId(1);
|
||||
stack.SetItemCount(4);
|
||||
window.SetSlot(36, stack);
|
||||
EXPECT_EQ(ReadDropSlot(window, 36).GetItemCount(), 4);
|
||||
window.Init(); // Clears the contents but preserves window identity.
|
||||
EXPECT_THROW(ReadDropSlot(window, 36), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, OfflineFailureIncludesSourceAndLine)
|
||||
{
|
||||
BilicraftClient client;
|
||||
ActionCommand command{};
|
||||
command.kind = ActionKind::SendCommand;
|
||||
command.text = "/help";
|
||||
command.source = "offline.act";
|
||||
command.line = 42;
|
||||
try
|
||||
{
|
||||
ExecuteAction(client, command);
|
||||
FAIL() << "Offline execution should fail before sending a packet";
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
EXPECT_NE(std::string(error.what()).find("offline.act:42:"), std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, ValidatesWorldAndPackedCoordinateBounds)
|
||||
{
|
||||
EXPECT_NO_THROW(ValidateActionPosition(-29999984, -2048, 29999984));
|
||||
EXPECT_NO_THROW(ValidateActionPosition(29999984, 2047, -29999984));
|
||||
EXPECT_THROW(ValidateActionPosition(29999985, 0, 0), std::runtime_error);
|
||||
EXPECT_THROW(ValidateActionPosition(0, -2049, 0), std::runtime_error);
|
||||
EXPECT_THROW(ValidateActionPosition(0, 2048, 0), std::runtime_error);
|
||||
EXPECT_THROW(ValidateActionPosition(0, 0, INT_MIN), std::runtime_error);
|
||||
EXPECT_THROW(ValidateActionPosition(INT_MAX, 0, 0), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, MaintainingInputsWithoutShiftDoesNotRequirePlayer)
|
||||
{
|
||||
BilicraftClient client;
|
||||
EXPECT_NO_THROW(MaintainActionInputs(client));
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, ItemOnlyClicksNeverAttackKnownEmptyFrames)
|
||||
{
|
||||
EXPECT_EQ(PlanFrameAttack(false, false, false), FrameAttackIntent::None);
|
||||
EXPECT_EQ(PlanFrameAttack(false, false, true), FrameAttackIntent::None);
|
||||
EXPECT_EQ(PlanFrameAttack(false, true, false), FrameAttackIntent::TakeItem);
|
||||
EXPECT_THROW(PlanFrameAttack(false, true, true), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, ExplicitFrameRemovalPlansOnlyTheNecessaryPhases)
|
||||
{
|
||||
for (bool creative : {false, true})
|
||||
{
|
||||
EXPECT_EQ(PlanFrameAttack(true, false, creative), FrameAttackIntent::RemoveFrame);
|
||||
EXPECT_EQ(PlanFrameAttack(true, true, creative), FrameAttackIntent::TakeItem);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ActionExecutor, ChangedFrameContentsCannotTurnTakeItemIntoAnExtraRemovalAttack)
|
||||
{
|
||||
EXPECT_NO_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, true, true, false));
|
||||
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, true, false, false), std::runtime_error);
|
||||
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, false, false, false), std::runtime_error);
|
||||
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::TakeItem, false, true, true), std::runtime_error);
|
||||
EXPECT_NO_THROW(ValidateFrameAttack(FrameAttackIntent::RemoveFrame, true, false, false));
|
||||
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::RemoveFrame, true, true, false), std::runtime_error);
|
||||
EXPECT_THROW(ValidateFrameAttack(FrameAttackIntent::None, false, false, false), std::runtime_error);
|
||||
}
|
||||
} // namespace
|
||||
Reference in new issue
Block a user