server/browser_shop_availability_test.go (3463B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "context" 6 "crypto/sha256" 7 "encoding/json" 8 "fmt" 9 "math" 10 "net/http" 11 "net/http/httptest" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "testing" 17 "time" 18 ) 19 20 func TestBrowserShopAvailability(t *testing.T) { 21 if os.Getenv("AO_GAME_TEST") != "1" { 22 t.Skip("set AO_GAME_TEST=1 for empty terminal feedback") 23 } 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/alpha-shop-availability", time.Now().Format("20060102-150405")) 27 for _, id := range []uint32{0xc008049f, 0xc02a049c, 0xc01504a2} { 28 t.Run(fmt.Sprintf("%x", id), func(t *testing.T) { 29 world := newTestServer(t) 30 machine := gamedata.ShopMachines[id] 31 // Exercise the unavailable-stock fallback independently of production catalogs. 32 stock := gamedata.ShopStock[machine.Template] 33 gamedata.ShopStock[machine.Template] = nil 34 t.Cleanup(func() { gamedata.ShopStock[machine.Template] = stock }) 35 player := world.newCharacter() 36 player.playfield, player.position = machine.Playfield, machine.Position 37 player.appearance = map[uint32]uint16{1183: 0x515, 1180: 0x315, 1186: 0x115}[machine.Playfield] 38 forward, _ := bodyAxes(machine.Rotation) 39 player.position[0] += 3 * forward[0] 40 player.position[2] += 3 * forward[2] 41 player.position[1] += .5 42 yaw := math.Atan2(float64(machine.Position[0]-player.position[0]), float64(machine.Position[2]-player.position[2])) 43 player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))} 44 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("shop-availability"))) 45 if err := world.saveUser(player); err != nil { 46 t.Fatal(err) 47 } 48 if err := world.saveInventory(player, playerInventory{Credits: 100, LastID: 1, Items: []ownedItem{ 49 {ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1}, 50 }}); err != nil { 51 t.Fatal(err) 52 } 53 creditsAfterSale := uint32(584) 54 shutdown := startTestService(t, world) 55 mux := http.NewServeMux() 56 mux.Handle("/", world.httpHandler()) 57 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 58 player := world.characters[1] 59 open, sales := false, 0 60 if client := world.clients[1]; client != nil && client.shop != nil { 61 open, sales = true, len(client.shop.items) 62 } 63 json.NewEncoder(w).Encode(map[string]any{"machine": id, "name": gamedata.ItemTemplates[machine.Template].Name, 64 "creditsAfterSale": creditsAfterSale, "open": open, "sales": sales, "inventory": player.inventory}) 65 })) 66 host := httptest.NewServer(mux) 67 defer host.Close() 68 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 69 defer cancel() 70 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_shop_availability.mjs"), host.URL, filepath.Join(output, fmt.Sprintf("%x", id))) 71 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 72 if err := command.Run(); err != nil { 73 t.Fatal(err) 74 } 75 host.Close() 76 if err := shutdown(); err != nil { 77 t.Fatal(err) 78 } 79 t.Logf("Browser artifacts: %s", filepath.Join(output, fmt.Sprintf("%x", id))) 80 cold := newTestInventoryServer(t).characters[1].inventory 81 if cold.Credits != creditsAfterSale || cold.LastID != 1 || len(cold.Items) != 0 { 82 t.Fatal("empty terminal sale changed during cold recovery", cold) 83 } 84 }) 85 } 86 }