Files
bilicraft-bot/tests/InventorySyncTests.cpp
T
2026-11-24 12:43:25 +08:00

173 lines
5.6 KiB
C++

#include "ActionExecutor.h"
#include <gtest/gtest.h>
#include "botcraft/Game/Inventory/InventoryManager.hpp"
#include "botcraft/Game/Inventory/Window.hpp"
namespace
{
using namespace ProtocolCraft;
Slot Item(int count)
{
Slot item;
item.SetItemId(1);
item.SetItemCount(count);
return item;
}
class InventorySync : public testing::Test
{
protected:
Botcraft::InventoryManager inventory{nullptr}; // Packet replay only, no socket or assets.
void Full(const Slot &cursor = Slot())
{
ClientboundContainerSetContentPacket packet;
packet.SetContainerId(0);
packet.SetStateId(7);
std::vector<Slot> slots(46);
slots[0] = Item(3); // Sentinel: hotbar updates must not touch crafting output.
slots[36] = Item(8);
packet.SetItems(slots);
packet.SetCarriedItem(cursor);
packet.Dispatch(&inventory);
}
void PlayerSlot(int index, const Slot &contents)
{
ClientboundSetPlayerInventoryPacket packet;
packet.SetSlot(index);
packet.SetContents(contents);
packet.Dispatch(&inventory);
}
};
TEST_F(InventorySync, EmptyHotbarUpdateClearsHeldItemInsteadOfCraftingOutput)
{
Full();
PlayerSlot(0, Slot());
EXPECT_TRUE(inventory.GetHotbarSelected().IsEmptySlot());
EXPECT_TRUE(Bilicraft::ReadDropSlot(*inventory.GetPlayerInventory(), 36).IsEmptySlot());
EXPECT_EQ(inventory.GetPlayerInventory()->GetSlot(0).GetItemCount(), 3);
}
TEST_F(InventorySync, MapsEveryPlayerInventoryIndexToMenuSlots)
{
Full();
for (int index = 0; index <= 40; ++index)
{
const int mapped = index < 9 ? 36 + index : index < 36 ? index : index < 40 ? 44 - index : 45;
PlayerSlot(index, Item(index + 1));
EXPECT_EQ(inventory.GetPlayerInventory()->GetSlot(mapped).GetItemCount(), index + 1) << index;
}
EXPECT_EQ(inventory.GetPlayerInventory()->GetSlot(0).GetItemCount(), 3);
}
TEST_F(InventorySync, IgnoresOutOfRangeInventoryIndices)
{
Full();
const auto before = inventory.GetPlayerInventory()->GetSlots();
PlayerSlot(-1, Item(2));
PlayerSlot(41, Item(2));
PlayerSlot(65536, Item(2));
EXPECT_EQ(inventory.GetPlayerInventory()->GetSlots().size(), before.size());
EXPECT_EQ(inventory.GetPlayerInventory()->GetSlot(0).GetItemCount(), 3);
}
TEST_F(InventorySync, CursorPacketNeverOverwritesTheSelectedHotbar)
{
Full();
ClientboundSetCursorItemPacket packet;
packet.SetContents(Item(5));
packet.Dispatch(&inventory);
EXPECT_EQ(inventory.GetCursor().GetItemCount(), 5);
EXPECT_EQ(inventory.GetHotbarSelected().GetItemCount(), 8);
packet.SetContents(Slot());
packet.Dispatch(&inventory);
EXPECT_TRUE(inventory.GetCursor().IsEmptySlot());
EXPECT_EQ(inventory.GetHotbarSelected().GetItemCount(), 8);
}
TEST_F(InventorySync, FullResyncUpdatesAndClearsCursor)
{
Full(Item(4));
EXPECT_EQ(inventory.GetCursor().GetItemCount(), 4);
Full();
EXPECT_TRUE(inventory.GetCursor().IsEmptySlot());
EXPECT_EQ(inventory.GetPlayerInventory()->GetStateId(), 7);
}
TEST_F(InventorySync, SwapWithEmptyHotbarAndDropClearBothUseAuthoritativeUpdates)
{
Full();
PlayerSlot(0, Slot());
PlayerSlot(9, Item(16));
auto window = inventory.GetPlayerInventory();
ASSERT_TRUE(window->GetSlot(36).IsEmptySlot());
// Server reports swb 1 1 1: storage becomes empty, hotbar receives its stack.
PlayerSlot(9, Slot());
PlayerSlot(0, Item(16));
EXPECT_TRUE(window->GetSlot(9).IsEmptySlot());
EXPECT_EQ(window->GetSlot(36).GetItemCount(), 16);
// Then drop all: an empty authoritative slot, not a stale sixteen-item stack.
PlayerSlot(0, Slot());
EXPECT_TRUE(Bilicraft::ReadDropSlot(*window, 36).IsEmptySlot());
}
TEST_F(InventorySync, LegacyDirectInventoryUpdatesUseSameIndexMapping)
{
Full();
ClientboundContainerSetSlotPacket packet;
packet.SetContainerId(-2);
packet.SetStateId(9);
packet.SetSlot(9);
packet.SetItemStack(Item(12));
packet.Dispatch(&inventory);
EXPECT_EQ(inventory.GetPlayerInventory()->GetSlot(9).GetItemCount(), 12);
EXPECT_TRUE(inventory.GetPlayerInventory()->GetSlot(18).IsEmptySlot());
}
TEST_F(InventorySync, FullPacketWireRoundTripClearsPreviouslyOccupiedHotbar)
{
Full(Item(4));
ClientboundContainerSetContentPacket sent, received;
sent.SetContainerId(0);
sent.SetStateId(8);
sent.SetCarriedItem(Slot());
std::vector<Slot> contents(46);
// Reuse the receive object across occupied and empty updates, like a packet replay.
for (int count : {8, 0})
{
contents[36] = count == 0 ? Slot() : Item(count);
sent.SetItems(contents);
WriteContainer wire;
sent.Write(wire);
auto iter = wire.cbegin();
std::size_t remaining = wire.size();
ASSERT_EQ(ReadData<VarInt>(iter, remaining), sent.GetId());
received.Read(iter, remaining);
ASSERT_EQ(remaining, 0U);
received.Dispatch(&inventory);
EXPECT_EQ(inventory.GetHotbarSelected().GetItemCount(), count);
EXPECT_EQ(inventory.GetHotbarSelected().IsEmptySlot(), count == 0);
EXPECT_TRUE(inventory.GetCursor().IsEmptySlot());
}
}
TEST_F(InventorySync, SwapEmptyBackpackIntoOccupiedHotbarAndThenBothEmpty)
{
Full();
auto window = inventory.GetPlayerInventory();
ASSERT_TRUE(window->GetSlot(9).IsEmptySlot());
PlayerSlot(9, Item(8));
PlayerSlot(0, Slot());
EXPECT_EQ(window->GetSlot(9).GetItemCount(), 8);
EXPECT_TRUE(window->GetSlot(36).IsEmptySlot());
PlayerSlot(9, Slot());
EXPECT_TRUE(window->GetSlot(9).IsEmptySlot());
EXPECT_TRUE(window->GetSlot(36).IsEmptySlot());
}
} // namespace