server/browser_pending_bag_stack_test.go (4056B)
1 package main 2 3 import ( 4 "context" 5 "crypto/sha256" 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "net/http/httptest" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "runtime" 14 "slices" 15 "testing" 16 "time" 17 ) 18 19 func TestBrowserBackpackStackJoinDuringPendingEquipment(t *testing.T) { 20 testBrowserBackpackStacksDuringPendingEquipment(t, false) 21 } 22 23 func TestBrowserBackpackStackSplitDuringPendingEquipment(t *testing.T) { 24 testBrowserBackpackStacksDuringPendingEquipment(t, true) 25 } 26 27 func testBrowserBackpackStacksDuringPendingEquipment(t *testing.T, split bool) { 28 t.Helper() 29 if os.Getenv("AO_GAME_TEST") != "1" { 30 t.Skip("set AO_GAME_TEST=1 for backpack stacks during pending equipment") 31 } 32 33 _, source, _, _ := runtime.Caller(0) 34 root := filepath.Dir(filepath.Dir(source)) 35 output := filepath.Join(root, "local/pending-bag-stack", time.Now().Format("20060102-150405")) 36 driver := "test_pending_bag_stack.mjs" 37 if split { 38 output = filepath.Join(root, "local/pending-bag-split", time.Now().Format("20060102-150405")) 39 driver = "test_pending_bag_split.mjs" 40 } 41 world := newTestServer(t) 42 player := world.newCharacter() 43 player.stats = testCharacterStats() 44 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-bag-stack"))) 45 if err := world.saveUser(player); err != nil { 46 t.Fatal(err) 47 } 48 for _, template := range []uint32{209283, 99228, 152030, 152030} { 49 if granted, err := world.grantItem(player, template); err != nil || !granted { 50 t.Fatal(granted, err) 51 } 52 } 53 inventory := player.inventory 54 contents := slices.Clone(inventory.Items[2:]) 55 contents[0].Slot, contents[1].Slot = 0, 1 56 inventory.Items = slices.Clone(inventory.Items[:2]) 57 inventory.Containers = map[uint32][]ownedItem{2: contents} 58 if err := world.saveInventory(player, inventory); err != nil { 59 t.Fatal(err) 60 } 61 holdTestEquipmentCompletions(t, world) 62 shutdown := startTestService(t, world) 63 mux := http.NewServeMux() 64 mux.Handle("/", world.httpHandler()) 65 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 66 player := world.characters[1] 67 var pending any 68 if client := world.clients[1]; client != nil && client.equipment != nil { 69 pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source, 70 "destination": client.equipment.destination, "readyAt": client.equipment.readyAt} 71 } 72 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending}) 73 })) 74 mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 75 finish := world.clients[1].equipment.readyAt 76 world.advanceEquipment(finish) 77 })) 78 host := httptest.NewServer(mux) 79 t.Cleanup(host.Close) 80 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 81 defer cancel() 82 83 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client", driver), host.URL, output) 84 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 85 err := command.Run() 86 host.Close() 87 if err := shutdown(); err != nil { 88 t.Fatal(err) 89 } 90 t.Logf("Browser artifacts: %s", output) 91 92 if err != nil { 93 t.Fatal(err) 94 } 95 cold := newTestInventoryServer(t) 96 expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}, 97 {ID: 2, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}} 98 expectedContents := []ownedItem{{ID: 4, Type: 51005, Template: 152030, Quality: 42, Slot: 1, Count: 6}} 99 last := uint32(4) 100 if split { 101 expectedContents = []ownedItem{{ID: 3, Type: 51005, Template: 152030, Quality: 42, Slot: 0, Count: 3}, 102 {ID: 4, Type: 51005, Template: 152030, Quality: 42, Slot: 1, Count: 3}} 103 last = 5 104 } 105 if !slices.Equal(cold.characters[1].inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Containers[2], expectedContents) || 106 cold.characters[1].inventory.LastID != last || cold.nextItem != last || newTestInventoryServer(t).nextItem != last { 107 t.Fatal("backpack stack refusal/retry changed equipment, allocation or charges") 108 } 109 }