server/browser_ground_drop_test.go (4421B)
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 TestBrowserGroundDropRefusal(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for native ground-drop inspection") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/ground-drop", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 27 player := world.newCharacter() 28 player.playfield, player.position = 500, [3]float32{320.12518, .7511, 194.0258} 29 player.rotation = [4]float32{0, 1, 0, 0} 30 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("ground-drop"))) 31 if err := world.saveUser(player); err != nil { 32 t.Fatal(err) 33 } 34 inventory := playerInventory{LastID: 3, Items: []ownedItem{ 35 {ID: 1, Type: 51018, Template: 123742, Quality: 2, Slot: 64, Count: 1, Ammo: 3}, 36 {ID: 2, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}, 37 {ID: 3, Type: 51005, Template: 25812, Quality: 2, Slot: 66, Count: 3}}} 38 if err := world.saveInventory(player, inventory); err != nil { 39 t.Fatal(err) 40 } 41 if err := world.closeDatabase(); err != nil { 42 t.Fatal(err) 43 } 44 for _, phase := range []string{"refusal", "stale", "stale-contained", "cold"} { 45 t.Run(phase, func(t *testing.T) { 46 world := newServerForTest(t) 47 player := world.characters[1] 48 shutdown := startTestService(t, world) 49 mux := http.NewServeMux() 50 mux.Handle("/", world.httpHandler()) 51 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 52 player := world.characters[1] 53 var groundItems int 54 if err := world.db.QueryRow("SELECT count(*) FROM item_locations WHERE mission_id IS NOT NULL OR resource_id IS NOT NULL").Scan(&groundItems); err != nil { 55 http.Error(w, err.Error(), http.StatusInternalServerError) 56 return 57 } 58 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "worldItems": groundItems, "lastID": world.identities.items.Load()}) 59 })) 60 // Isolate a completed bag deposit without publishing its source refresh. 61 mux.HandleFunc("POST /test-stale-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 62 player := world.characters[1] 63 inventory := player.inventory 64 index := slices.IndexFunc(inventory.Items, func(item ownedItem) bool { return item.ID == 1 }) 65 item := inventory.Items[index] 66 item.Slot = 0 67 inventory.Items = slices.Delete(slices.Clone(inventory.Items), index, index+1) 68 inventory.Containers = map[uint32][]ownedItem{2: {item}} 69 if err := world.saveInventory(player, inventory); err != nil { 70 t.Error(err) 71 http.Error(w, err.Error(), http.StatusInternalServerError) 72 } 73 })) 74 mux.HandleFunc("POST /test-stale-contained-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 75 player := world.characters[1] 76 inventory := player.inventory 77 item := inventory.Containers[2][0] 78 item.Slot = 1 79 inventory.Containers = map[uint32][]ownedItem{2: {item}} 80 if err := world.saveInventory(player, inventory); err != nil { 81 t.Error(err) 82 http.Error(w, err.Error(), http.StatusInternalServerError) 83 } 84 })) 85 host := httptest.NewServer(mux) 86 t.Cleanup(host.Close) 87 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 88 defer cancel() 89 90 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_ground_drop.mjs"), host.URL, filepath.Join(output, phase), phase) 91 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 92 err := command.Run() 93 host.Close() 94 if err := shutdown(); err != nil { 95 t.Fatal(err) 96 } 97 98 t.Logf("Browser artifacts: %s", output) 99 if err != nil { 100 t.Fatal(err) 101 } 102 cold := newTestInventoryServer(t) 103 if len(player.inventory.Items) != 3 || len(player.inventory.Containers) != 0 || cold.nextItem != 8 || len(cold.worldItems) != 5 { 104 t.Fatal("ground-drop refusal changed item ownership, contents or allocation") 105 } 106 for _, item := range inventory.Items { 107 if !slices.Contains(player.inventory.Items, item) { 108 t.Fatal("ground-drop refusal changed an exact owned item", item) 109 } 110 } 111 }) 112 if t.Failed() { 113 return 114 } 115 } 116 }