server/browser_equipment_replacement_test.go (3091B)
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 "testing" 15 "time" 16 ) 17 18 func TestBrowserEquipmentReplacement(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for the controlled two-client replacement regression") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/equipment-replacement", time.Now().Format("20060102-150405")) 25 t.Chdir(t.TempDir()) 26 world := newDormantServerForTest(t) 27 for i := 0; i < 2; i++ { 28 player := world.newCharacter() 29 player.name, player.level, player.profession = fmt.Sprint("Replacement ", i), 10, 9 30 player.position[2] -= float32(i * 2) 31 player.stats.Melee.OneHandBlunt, player.stats.Melee.SneakAttack, player.stats.Melee.FastAttack = 20, 20, 20 32 player.stats.Body.Brawling, player.stats.Body.Dimach = 20, 20 33 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("replacement-", i)))) 34 if err := world.saveUser(player); err != nil { 35 t.Fatal(err) 36 } 37 } 38 player := world.characters[1] 39 if granted, err := world.grantItem(player, 121564); !granted || err != nil { 40 t.Fatal(granted, err) 41 } 42 player.inventory.Items[0].Slot = 6 43 if granted, err := world.grantItem(player, 122691); !granted || err != nil { 44 t.Fatal(granted, err) 45 } 46 player.inventory.Items[1].Quality = 3 47 for range 29 { 48 if granted, err := world.grantItem(player, 121567); !granted || err != nil { 49 t.Fatal(granted, err) 50 } 51 } 52 if err := world.saveInventory(player, player.inventory); err != nil { 53 t.Fatal(err) 54 } 55 56 for _, phase := range []string{"live", "cold"} { 57 if phase == "cold" { 58 world = newDormantServerForTest(t) 59 } 60 if !t.Run(phase, func(t *testing.T) { 61 shutdown := startTestService(t, world) 62 mux := http.NewServeMux() 63 mux.Handle("/", world.httpHandler()) 64 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 65 var pending any 66 if client := world.clients[1]; client != nil && client.equipment != nil { 67 pending = map[string]any{"source": client.equipment.source, "destination": client.equipment.destination, 68 "replacedID": client.equipment.replacedID, "readyAt": client.equipment.readyAt} 69 } 70 json.NewEncoder(w).Encode(map[string]any{"inventory": world.characters[1].inventory, "pending": pending}) 71 })) 72 host := httptest.NewServer(mux) 73 t.Cleanup(host.Close) 74 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) 75 defer cancel() 76 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_equipment_replacement.mjs"), host.URL, filepath.Join(output, phase), phase) 77 command.Dir = root 78 command.Stdout, command.Stderr = os.Stdout, os.Stderr 79 err := command.Run() 80 host.Close() 81 if failure := shutdown(); failure != nil { 82 t.Fatal(failure) 83 } 84 if err != nil { 85 t.Fatal(err) 86 } 87 t.Logf("Browser artifacts: %s/%s", output, phase) 88 }) { 89 return 90 } 91 } 92 }