Files
Project-M/Assets/_Project/Tests/EditMode/InventoryMathTests.cs
T
kronic e23bebc84b Tests: inventory EditMode coverage (DR-026)
InventoryMathTests (stack/slot-cap/withdraw/CountOf); InventoryHarvestTests (owned harvest -> inventory with ledger untouched, full-bag spill, no-matching-player -> ledger fallback); InventoryDepositSystemTests (specific/all/unresolvable). The 8 existing ResourceHarvestSystemTests stay green via the genuine no-owner fallback. 228/228 EditMode pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 09:43:47 -07:00

90 lines
3.9 KiB
C#

using NUnit.Framework;
using ProjectM.Simulation;
using Unity.Entities;
namespace ProjectM.Tests
{
/// <summary>
/// Plain-Entities EditMode tests for the pure <see cref="InventoryMath"/> stacking logic (the per-player
/// bag math): top-up-then-append with a per-item stack cap, a max slot count returning a remainder, and
/// back-to-front withdraw clamped to availability. A bare world hosts an entity that owns the
/// <see cref="InventorySlot"/> buffer (a DynamicBuffer needs an entity); no systems run.
/// </summary>
public class InventoryMathTests
{
static (World world, DynamicBuffer<InventorySlot> buffer) MakeBuffer()
{
var world = new World("InventoryMathTest");
var em = world.EntityManager;
var e = em.CreateEntity();
var buffer = em.AddBuffer<InventorySlot>(e);
return (world, buffer);
}
[Test]
public void Deposit_TopsUpExistingStack_ThenAppends_NewStack()
{
var (world, buf) = MakeBuffer();
using (world)
{
int r1 = InventoryMath.Deposit(buf, itemId: 1, count: 5, stackMax: 10, maxSlots: 4);
Assert.AreEqual(0, r1, "5 fits in one fresh stack.");
Assert.AreEqual(1, buf.Length);
int r2 = InventoryMath.Deposit(buf, itemId: 1, count: 8, stackMax: 10, maxSlots: 4);
Assert.AreEqual(0, r2, "8 more tops the first stack to 10 then appends 3.");
Assert.AreEqual(2, buf.Length, "A second stack is appended once the first fills.");
Assert.AreEqual(13, InventoryMath.CountOf(buf, 1));
Assert.AreEqual(10, buf[0].Count, "First stack is capped at stackMax.");
Assert.AreEqual(3, buf[1].Count);
}
}
[Test]
public void Deposit_FillsMultipleStacks_UpToSlotCap_ReturnsRemainder()
{
var (world, buf) = MakeBuffer();
using (world)
{
// 2 slots * stackMax 10 = 20 capacity; depositing 25 leaves a remainder of 5.
int r = InventoryMath.Deposit(buf, itemId: 2, count: 25, stackMax: 10, maxSlots: 2);
Assert.AreEqual(5, r, "Past the slot cap, the overflow is returned as a remainder.");
Assert.AreEqual(2, buf.Length);
Assert.AreEqual(20, InventoryMath.CountOf(buf, 2));
}
}
[Test]
public void Withdraw_TakesAcrossStacks_BackToFront_Clamped_ReturnsTaken()
{
var (world, buf) = MakeBuffer();
using (world)
{
InventoryMath.Deposit(buf, itemId: 3, count: 25, stackMax: 10, maxSlots: 4); // 10,10,5
int taken = InventoryMath.Withdraw(buf, itemId: 3, count: 12);
Assert.AreEqual(12, taken);
Assert.AreEqual(13, InventoryMath.CountOf(buf, 3));
int takenAll = InventoryMath.Withdraw(buf, itemId: 3, count: 100);
Assert.AreEqual(13, takenAll, "Withdraw clamps to what is available.");
Assert.AreEqual(0, InventoryMath.CountOf(buf, 3));
Assert.AreEqual(0, buf.Length, "Emptied stacks are dropped.");
}
}
[Test]
public void Deposit_ZeroItemId_OrNonPositiveCount_AreNoOps()
{
var (world, buf) = MakeBuffer();
using (world)
{
Assert.AreEqual(7, InventoryMath.Deposit(buf, itemId: 0, count: 7, stackMax: 10, maxSlots: 4),
"Depositing the empty id deposits nothing and returns the full count.");
Assert.AreEqual(0, InventoryMath.Deposit(buf, itemId: 1, count: 0, stackMax: 10, maxSlots: 4));
Assert.AreEqual(0, InventoryMath.Deposit(buf, itemId: 1, count: -3, stackMax: 10, maxSlots: 4));
Assert.AreEqual(0, buf.Length, "No rows were written.");
}
}
}
}