aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/browser_icon_persistence_test.go (3434B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"context"
      6 	"crypto/sha256"
      7 	"encoding/json"
      8 	"fmt"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"slices"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserInventoryLayoutTransitions(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for the inventory layout transition check")
     23 	}
     24 
     25 	_, source, _, _ := runtime.Caller(0)
     26 	root := filepath.Dir(filepath.Dir(source))
     27 	output := filepath.Join(root, "local/icon-layout-transitions", time.Now().Format("20060102-150405"))
     28 	world := newTestServer(t)
     29 	player := world.newCharacter()
     30 	player.appearance = 0x515
     31 	player.level, player.stats, player.health = 10, testCharacterStats(), 50
     32 	player.playfield, player.position = 705, [3]float32{960, 16.5, 511}
     33 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("icon-persistence")))
     34 	if err := world.saveUser(player); err != nil {
     35 		t.Fatal(err)
     36 	}
     37 	for _, template := range []uint32{121564, 42640, 42640, 25812} {
     38 		if granted, err := world.grantItem(player, template); err != nil || !granted {
     39 			t.Fatal(granted, err)
     40 		}
     41 	}
     42 	inventory := player.inventory
     43 	inventory.Credits = 5000
     44 	if err := world.saveInventory(player, inventory); err != nil {
     45 		t.Fatal(err)
     46 	}
     47 	// Controlled stock isolates the inventory transition, not booth membership.
     48 	stock := gamedata.ShopStock[99383]
     49 	t.Cleanup(func() { gamedata.ShopStock[99383] = stock })
     50 	gamedata.ShopStock[99383] = []gamedata.ShopItem{{Template: 25812, Quality: 1}}
     51 	shutdown := startTestService(t, world)
     52 	mux := http.NewServeMux()
     53 	mux.Handle("/", world.httpHandler())
     54 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     55 		json.NewEncoder(w).Encode(world.characters[player.id].inventory)
     56 	}))
     57 	// Use the normal control transition to reach each checkpoint without travel.
     58 	mux.HandleFunc("POST /test-zone", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     59 		destination := world.characters[player.id].userData()
     60 		if r.URL.Query().Get("shop") == "1" {
     61 			destination.Playfield, destination.Position = 1183, [3]float32{188, 5.11, 139.5}
     62 			destination.Rotation = [4]float32{0, 0.70710677, 0, 0.70710677}
     63 		} else {
     64 			destination.Playfield, destination.Position = 705, [3]float32{960, 16.5, 511}
     65 			destination.Rotation = [4]float32{0, 0, 0, 1}
     66 		}
     67 		if err := world.updateUser(destination); err != nil {
     68 			http.Error(w, err.Error(), http.StatusInternalServerError)
     69 		}
     70 	}))
     71 	host := httptest.NewServer(mux)
     72 	t.Cleanup(host.Close)
     73 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     74 	defer cancel()
     75 
     76 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_icon_persistence.mjs"), host.URL, output)
     77 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     78 	err := command.Run()
     79 	host.Close()
     80 	if err := shutdown(); err != nil {
     81 		t.Fatal(err)
     82 	}
     83 	t.Logf("Browser artifacts: %s", output)
     84 
     85 	if err != nil {
     86 		t.Fatal(err)
     87 	}
     88 	cold := newTestInventoryServer(t)
     89 	expected := []ownedItem{
     90 		inventory.Items[0], inventory.Items[3],
     91 		{ID: 5, Type: 51005, Template: 25812, Quality: 1, Slot: 65, Count: 50},
     92 	}
     93 	if !slices.Equal(player.inventory.Items, expected) ||
     94 		!slices.Equal(cold.characters[player.id].inventory.Items, expected) {
     95 		t.Fatal("sale, purchase or recovery changed retained item instances")
     96 	}
     97 }