server/browser_pending_loot_test.go (3114B)
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 TestBrowserLootDuringEquipment(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for pickup during pending equipment") 22 } 23 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/pending-loot", time.Now().Format("20060102-150405")) 27 world, _ := newLootWorld(t) 28 player, corpse := world.characters[1], world.characters[1000000] 29 player.playfield, player.position = 705, [3]float32{960, 16.5, 511} 30 player.rotation = [4]float32{0, 0, 0, 1} 31 player.stats = testCharacterStats() 32 player.rechargeUntil = time.Time{} 33 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-loot"))) 34 corpse.playfield, corpse.position = 705, [3]float32{960, 16.5, 514} 35 corpse.corpse.expiresAt = time.Now().Add(time.Hour) 36 if err := world.saveUser(player); err != nil { 37 t.Fatal(err) 38 } 39 if err := world.flushWorld(); err != nil { 40 t.Fatal(err) 41 } 42 if granted, err := world.grantItem(player, 209283); err != nil || !granted { 43 t.Fatal(granted, err) 44 } 45 world.clients = make(map[uint32]*worldClient) 46 holdTestEquipmentCompletions(t, world) 47 shutdown := startTestService(t, world) 48 mux := http.NewServeMux() 49 mux.Handle("/", world.httpHandler()) 50 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 51 player := world.characters[1] 52 corpse := world.characters[1000000] 53 pending := world.clients[1] != nil && world.clients[1].equipment != nil 54 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "corpse": corpse.corpse.items, "pending": pending}) 55 })) 56 // Hold the admitted move at a controlled checkpoint until the rejected pickup. 57 mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 58 finish := world.clients[1].equipment.readyAt 59 world.advanceEquipment(finish) 60 })) 61 host := httptest.NewServer(mux) 62 t.Cleanup(host.Close) 63 ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) 64 defer cancel() 65 66 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_loot.mjs"), host.URL, output) 67 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 68 if err := command.Run(); err != nil { 69 t.Fatal(err) 70 } 71 host.Close() 72 if err := shutdown(); err != nil { 73 t.Fatal(err) 74 } 75 cold := newTestInventoryServer(t) 76 77 expected := []ownedItem{{ID: 18, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}, 78 {ID: 17, Type: 51005, Template: 42640, Quality: 1, Slot: 64, Count: 1}} 79 if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, []ownedItem{expected[1], expected[0]}) || 80 len(cold.characters[1000000].corpse.items) != 0 { 81 t.Fatal("pending equipment pickup retry did not retain both exact items") 82 } 83 t.Logf("Browser artifacts: %s", output) 84 }