server/browser_pending_stale_worn_test.go (3669B)
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 TestBrowserStaleWornSourceDuringEquipment(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for a stale worn source 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-worn", 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-worn"))) 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[1].Slot = 21 43 if err := world.saveInventory(player, inventory); err != nil { 44 t.Fatal(err) 45 } 46 holdTestEquipmentCompletions(t, world) 47 shutdown := startTestService(t, world) 48 mux := http.NewServeMux() 49 mux.Handle("/", world.httpHandler()) 50 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 51 player := world.characters[1] 52 var pending any 53 if client := world.clients[1]; client != nil && client.equipment != nil { 54 pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source, 55 "destination": client.equipment.destination, "readyAt": client.equipment.readyAt} 56 } 57 var armor [8]int32 58 for index := range armor { 59 armor[index] = player.equipmentBonus(90 + uint32(index)) 60 } 61 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending, "armor": armor}) 62 })) 63 // A committed removal with no refresh leaves the worn vest and its effects stale. 64 mux.HandleFunc("POST /test-stale-view", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 65 player := world.characters[1] 66 inventory := player.inventory 67 inventory.Items = slices.Clone(inventory.Items) 68 inventory.Items[1].Slot = 65 69 if err := world.saveInventory(player, inventory); err != nil { 70 t.Error(err) 71 http.Error(w, err.Error(), http.StatusInternalServerError) 72 } 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(), 2*time.Minute) 81 defer cancel() 82 83 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_stale_worn.mjs"), 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: 51022, Template: 85481, Quality: 1, Slot: 21, Count: 1}} 98 if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) || 99 cold.nextItem != 2 { 100 t.Fatal("stale-source correction lost an equipment instance") 101 } 102 }