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