server/browser_pending_owned_use_test.go (2910B)
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 TestBrowserOwnedItemUseDuringEquipment(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for owned-item Use during pending equipment") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/pending-owned-use", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 27 player := world.newCharacter() 28 player.stats = testCharacterStats() 29 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-owned-use"))) 30 if err := world.saveUser(player); err != nil { 31 t.Fatal(err) 32 } 33 for _, template := range []uint32{209283, 99228} { 34 if granted, err := world.grantItem(player, template); err != nil || !granted { 35 t.Fatal(granted, err) 36 } 37 } 38 holdTestEquipmentCompletions(t, world) 39 shutdown := startTestService(t, world) 40 mux := http.NewServeMux() 41 mux.Handle("/", world.httpHandler()) 42 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 43 player := world.characters[1] 44 var pending any 45 bagOpen := false 46 if client := world.clients[1]; client != nil { 47 bagOpen = client.backpacks[1].open 48 if client.equipment != nil { 49 pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source, 50 "destination": client.equipment.destination, "readyAt": client.equipment.readyAt} 51 } 52 } 53 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending, "bagOpen": bagOpen}) 54 })) 55 // Hold completion beyond the native animation to exercise the busy reply. 56 mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 57 finish := world.clients[1].equipment.readyAt 58 world.advanceEquipment(finish) 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_pending_owned_use.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 73 t.Logf("Browser artifacts: %s", output) 74 if err != nil { 75 t.Fatal(err) 76 } 77 expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}, 78 {ID: 2, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}} 79 if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(newTestInventoryServer(t).characters[1].inventory.Items, expected) { 80 t.Fatal("busy Use or retry changed equipment or backpack ownership") 81 } 82 }