server/browser_pending_stale_hand_test.go (3682B)
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 TestBrowserStaleHandDuringEquipment(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for a stale hand during pending equipment") 22 } 23 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/pending-stale-hand", time.Now().Format("20060102-150405")) 27 world := newTestServer(t) 28 player := world.newCharacter() 29 player.level = 2 30 player.stats.Abilities.Sense, player.stats.Abilities.Agility = 8, 8 31 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-stale-hand"))) 32 if err := world.saveUser(player); err != nil { 33 t.Fatal(err) 34 } 35 for _, template := range []uint32{209283, 85481} { 36 if granted, err := world.grantItem(player, template); err != nil || !granted { 37 t.Fatal(granted, err) 38 } 39 } 40 inventory := player.inventory 41 inventory.Items = slices.Clone(inventory.Items) 42 inventory.Items[0].Slot = 6 43 inventory.Items[1].Slot = 64 44 if err := world.saveInventory(player, inventory); err != nil { 45 t.Fatal(err) 46 } 47 holdTestEquipmentCompletions(t, world) 48 shutdown := startTestService(t, world) 49 mux := http.NewServeMux() 50 mux.Handle("/", world.httpHandler()) 51 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 52 player := world.characters[1] 53 var pending any 54 if client := world.clients[1]; client != nil && client.equipment != nil { 55 pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source, 56 "destination": client.equipment.destination, "readyAt": client.equipment.readyAt} 57 } 58 var armor [8]int32 59 for index := range armor { 60 armor[index] = player.equipmentBonus(90 + uint32(index)) 61 } 62 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending, "armor": armor}) 63 })) 64 // A committed removal with no refresh leaves the old native weapon hand stale. 65 mux.HandleFunc("POST /test-stale-view", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 66 player := world.characters[1] 67 inventory := player.inventory 68 inventory.Items = slices.Clone(inventory.Items) 69 inventory.Items[0].Slot = 65 70 if err := world.saveInventory(player, inventory); err != nil { 71 t.Error(err) 72 http.Error(w, err.Error(), http.StatusInternalServerError) 73 } 74 })) 75 mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 76 finish := world.clients[1].equipment.readyAt 77 world.advanceEquipment(finish) 78 })) 79 host := httptest.NewServer(mux) 80 t.Cleanup(host.Close) 81 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 82 defer cancel() 83 84 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_stale_hand.mjs"), host.URL, output) 85 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 86 err := command.Run() 87 host.Close() 88 if err := shutdown(); err != nil { 89 t.Fatal(err) 90 } 91 t.Logf("Browser artifacts: %s", output) 92 93 if err != nil { 94 t.Fatal(err) 95 } 96 cold := newTestInventoryServer(t) 97 expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}, 98 {ID: 2, Type: 51022, Template: 85481, Quality: 1, Slot: 21, Count: 1}} 99 if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) || 100 cold.nextItem != 2 { 101 t.Fatal("stale-source correction lost an equipment instance") 102 } 103 }