server/browser_npc_shops_test.go (3307B)
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 "reflect" 14 "runtime" 15 "testing" 16 "time" 17 ) 18 19 func TestBrowserNPCShops(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the five starter NPC vendors") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/npc-vendors", time.Now().Format("20060102-150405")) 26 for _, route := range []string{"710/district/0/fixed/18", "710/district/0/fixed/19", "950/district/1/fixed/0", "952/district/3/fixed/0", "954/district/1/fixed/0"} { 27 t.Run(route, func(t *testing.T) { 28 world := newTestServer(t) 29 var npc *character 30 for _, actor := range world.characters { 31 if actor.npcDefinitionID == route { 32 npc = actor 33 break 34 } 35 } 36 if npc == nil { 37 t.Fatal("starter vendor is absent from the original population") 38 } 39 player := world.newCharacter() 40 player.playfield, player.position, player.appearance = npc.playfield, npc.position, 0x515 41 if npc.playfield == 952 { 42 player.appearance = 0x315 43 } else if npc.playfield == 954 { 44 player.appearance = 0x115 45 } 46 player.position[1] += .75 47 player.position[2] -= 3 48 player.rotation = [4]float32{0, 0, 0, 1} 49 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("npc-shops"))) 50 if err := world.saveUser(player); err != nil { 51 t.Fatal(err) 52 } 53 inventory := playerInventory{Credits: 1000, LastID: 1, Items: []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1}}} 54 if err := world.saveInventory(player, inventory); err != nil { 55 t.Fatal(err) 56 } 57 npcID := npc.id 58 shutdown := startTestService(t, world) 59 mux := http.NewServeMux() 60 mux.Handle("/", world.httpHandler()) 61 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 62 player := world.characters[1] 63 npc := world.characters[npcID] 64 open, sales, purchases := false, 0, uint16(0) 65 if client := world.clients[1]; client != nil && client.shop != nil { 66 open, sales, purchases = true, len(client.shop.items), client.shop.purchases[0] 67 } 68 json.NewEncoder(w).Encode(map[string]any{"npc": npc.id, "route": route, "position": npc.position, 69 "open": open, "sales": sales, "purchases": purchases, "inventory": player.inventory}) 70 })) 71 host := httptest.NewServer(mux) 72 t.Cleanup(host.Close) 73 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 74 defer cancel() 75 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_npc_shops.mjs"), host.URL, filepath.Join(output, route)) 76 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 77 err := command.Run() 78 host.Close() 79 if err := shutdown(); err != nil { 80 t.Fatal(err) 81 } 82 t.Logf("Browser artifacts: %s", filepath.Join(output, route)) 83 if err != nil { 84 t.Fatal(err) 85 } 86 cold := newTestInventoryServer(t).characters[1].inventory 87 if !reflect.DeepEqual(cold, player.inventory) || len(cold.Items) != 1 || cold.Items[0].ID != 2 || cold.Credits == 1000 { 88 t.Fatal("NPC purchase/sale did not persist exactly once", cold) 89 } 90 }) 91 } 92 }