server/browser_moved_closed_backpack_split_test.go (3520B)
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 TestBrowserMovedClosedBackpackSplit(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for closed backpack split") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/moved-closed-backpack-split", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 27 player := world.newCharacter() 28 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("moved-closed-backpack-split"))) 29 if err := world.saveUser(player); err != nil { 30 t.Fatal(err) 31 } 32 inventory := playerInventory{LastID: 3, Items: []ownedItem{ 33 {ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1}, 34 {ID: 3, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}}, 35 Containers: map[uint32][]ownedItem{1: { 36 {ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 0, Count: 3}}}} 37 if err := world.saveInventory(player, inventory); err != nil { 38 t.Fatal(err) 39 } 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 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "open": world.clients[1].backpacks[1].open}) 46 })) 47 // Commit an earlier transfer while retaining the old native source view. 48 mux.HandleFunc("POST /test-move-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 49 player := world.characters[1] 50 inventory := player.inventory 51 inventory.Containers = map[uint32][]ownedItem{3: slices.Clone(inventory.Containers[1])} 52 if err := world.saveInventory(player, inventory); err != nil { 53 t.Error(err) 54 http.Error(w, err.Error(), http.StatusInternalServerError) 55 return 56 } 57 world.clients[1].backpacks[1] = backpackHandle{1, false} 58 })) 59 host := httptest.NewServer(mux) 60 t.Cleanup(host.Close) 61 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 62 defer cancel() 63 64 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_moved_closed_backpack_split.mjs"), host.URL, output) 65 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 66 err := command.Run() 67 host.Close() 68 if err := shutdown(); err != nil { 69 t.Fatal(err) 70 } 71 72 t.Logf("Browser artifacts: %s", output) 73 if err != nil { 74 t.Fatal(err) 75 } 76 cold := newTestInventoryServer(t) 77 expected := []ownedItem{{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1}, 78 {ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 66, Count: 3}, 79 {ID: 3, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}} 80 for _, current := range []*character{player, cold.characters[1]} { 81 if len(current.inventory.Items) != 3 || len(current.inventory.Containers) != 0 || 82 current.inventory.Credits != 0 || current.inventory.LastID != 3 { 83 t.Fatal("stale split changed quantities, cash or allocation", current.inventory) 84 } 85 for _, item := range expected { 86 if !slices.Contains(current.inventory.Items, item) { 87 t.Fatal("stale split recovery lost an exact instance", current.inventory) 88 } 89 } 90 } 91 if cold.nextItem != 3 { 92 t.Fatal("refused split allocated an identity or retry repeated allocation") 93 } 94 }