server/browser_depleted_stack_split_test.go (3116B)
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 TestBrowserDepletedStackSplit(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the depleted ordinary-stack split") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/depleted-stack-split", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 27 player := world.newCharacter() 28 player.stats = testCharacterStats() 29 player.stats.Abilities = characterAbilitiesStats{80, 80, 80, 80, 80, 80} 30 player.stats.NanoAiding.Treatment = 100 31 player.health = 1 32 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("depleted-stack-split"))) 33 if err := world.saveUser(player); err != nil { 34 t.Fatal(err) 35 } 36 inventory := playerInventory{LastID: 2, Items: []ownedItem{ 37 {ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}, 38 {ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 64, Count: 3}}} 39 if err := world.saveInventory(player, inventory); err != nil { 40 t.Fatal(err) 41 } 42 shutdown := startTestService(t, world) 43 mux := http.NewServeMux() 44 mux.Handle("/", world.httpHandler()) 45 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 46 player := world.characters[1] 47 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "health": player.health}) 48 })) 49 // Isolate an earlier committed consumption with a stale three-charge native row. 50 mux.HandleFunc("POST /test-deplete-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 51 player := world.characters[1] 52 inventory := player.inventory 53 inventory.Items = slices.Clone(inventory.Items) 54 inventory.Items[1].Count = 1 55 if err := world.saveInventory(player, inventory); err != nil { 56 t.Error(err) 57 http.Error(w, err.Error(), http.StatusInternalServerError) 58 } 59 })) 60 host := httptest.NewServer(mux) 61 t.Cleanup(host.Close) 62 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 63 defer cancel() 64 65 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_depleted_stack_split.mjs"), host.URL, output) 66 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 67 err := command.Run() 68 host.Close() 69 if err := shutdown(); err != nil { 70 t.Fatal(err) 71 } 72 t.Logf("Browser artifacts: %s", output) 73 74 if err != nil { 75 t.Fatal(err) 76 } 77 cold := newTestInventoryServer(t) 78 expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}} 79 for _, current := range []*character{player, cold.characters[1]} { 80 if !slices.Equal(current.inventory.Items, expected) || current.health != 38 || current.inventory.LastID != 2 { 81 t.Fatal("depleted split correction/use changed equipment, ownership or health", current.inventory, current.health) 82 } 83 } 84 if cold.nextItem != 2 { 85 t.Fatal("depleted split allocated an identity") 86 } 87 }