diff options
Diffstat (limited to 'internal/world/ground_test.go')
| -rw-r--r-- | internal/world/ground_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/internal/world/ground_test.go b/internal/world/ground_test.go new file mode 100644 index 0000000..ef77766 --- /dev/null +++ b/internal/world/ground_test.go @@ -0,0 +1,50 @@ +package world + +import "testing" + +// TestRemoveReservedItemSkipsOtherPlayersReservations: the take loop must +// apply the same ownership check as the availability pass. With a stack +// reserved for Bob stored before a public stack, Alice's pickup must take +// the public stack only — consuming Bob's reservation instead silently +// destroys his loot while telling Alice the ground still holds it. +func TestRemoveReservedItemSkipsOtherPlayersReservations(t *testing.T) { + w := New(t.TempDir()) + w.AddReservedItem(1, "ore", 10, "bob") + w.AddGroundItem(1, "ore", 10) + + taken, ok := w.RemoveReservedItem(1, "ore", 10, "alice") + if !ok || taken != 10 { + t.Fatalf("alice's pickup of the public stack = (%d, %v), want (10, true)", taken, ok) + } + if got := w.AccessibleGround(1, "alice")["ore"]; got != 0 { + t.Errorf("alice still sees %d ore after taking the public stack; removal consumed reserved stock", got) + } + // Bob's reserved stack must be untouched and still his to take. + if got := w.AccessibleGround(1, "bob")["ore"]; got != 10 { + t.Errorf("bob's reserved stack = %d, want 10", got) + } + bobTaken, ok := w.RemoveReservedItem(1, "ore", 10, "bob") + if !ok || bobTaken != 10 { + t.Errorf("bob's pickup of his own reservation = (%d, %v), want (10, true)", bobTaken, ok) + } +} + +// TestRemoveAvailableItemTakesAccessibleStock: a request far larger than +// the ground stock succeeds and takes exactly what is accessible, skipping +// entries reserved for other players. +func TestRemoveAvailableItemTakesAccessibleStock(t *testing.T) { + w := New(t.TempDir()) + w.AddReservedItem(1, "credits", 10, "bob") + w.AddGroundItem(1, "credits", 5) + + taken := w.RemoveAvailableItem(1, "credits", 999999, "alice") + if taken != 5 { + t.Fatalf("remove-available took %d, want the 5 accessible credits", taken) + } + if got := w.AccessibleGround(1, "alice")["credits"]; got != 0 { + t.Errorf("alice still sees %d accessible credits", got) + } + if got := w.AccessibleGround(1, "bob")["credits"]; got != 10 { + t.Errorf("bob's reserved credits = %d, want 10", got) + } +} |
