aboutsummaryrefslogtreecommitdiff
path: root/internal/world/ground_test.go
blob: ef77766395528c38e405b3aa053196a8943c1a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
	}
}