aorecomp

Anarchy Online classic in the browser

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


server/browser_inventory_grid_test.go (2615B)


      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 TestBrowserInventoryGrid(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the visible inventory grid check")
     22 	}
     23 
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/icon-grid", time.Now().Format("20060102-150405"))
     27 	world := newTestServer(t)
     28 	player := world.newCharacter()
     29 	player.playfield, player.position = 705, [3]float32{960, 16.5, 511}
     30 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("inventory-grid")))
     31 	world.characters = map[uint32]*character{player.id: player}
     32 	if err := world.saveUser(player); err != nil {
     33 		t.Fatal(err)
     34 	}
     35 	for i := 0; i < 30; i++ {
     36 		template := uint32(42640)
     37 		if i == 0 {
     38 			template = 25812
     39 		} else if i == 29 {
     40 			template = 121564
     41 		}
     42 		if granted, err := world.grantItem(player, template); err != nil || !granted {
     43 			t.Fatal(granted, err)
     44 		}
     45 	}
     46 	inventory := player.inventory
     47 	inventory.Items = nil
     48 	for _, item := range player.inventory.Items {
     49 		if item.Slot == 75 || item.Slot == 80 {
     50 			item.Slot = uint32(len(inventory.Bank))
     51 			inventory.Bank = append(inventory.Bank, item)
     52 		} else {
     53 			inventory.Items = append(inventory.Items, item)
     54 		}
     55 	}
     56 	if err := world.saveInventory(player, inventory); err != nil {
     57 		t.Fatal(err)
     58 	}
     59 	shutdown := startTestService(t, world)
     60 	mux := http.NewServeMux()
     61 	mux.Handle("/", world.httpHandler())
     62 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     63 		player := world.characters[1]
     64 		json.NewEncoder(w).Encode(player.inventory)
     65 	}))
     66 	host := httptest.NewServer(mux)
     67 	t.Cleanup(host.Close)
     68 	ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)
     69 	defer cancel()
     70 
     71 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_inventory_grid.mjs"), host.URL, output)
     72 	command.Dir = root
     73 	command.Stdout, command.Stderr = os.Stdout, os.Stderr
     74 	if err := command.Run(); err != nil {
     75 		t.Fatal(err)
     76 	}
     77 	host.Close()
     78 	if err := shutdown(); err != nil {
     79 		t.Fatal(err)
     80 	}
     81 	cold := newTestInventoryServer(t)
     82 
     83 	if !slices.Equal(player.inventory.Items, inventory.Items) || !slices.Equal(cold.characters[1].inventory.Items, inventory.Items) ||
     84 		!slices.Equal(cold.characters[1].inventory.Bank, inventory.Bank) {
     85 		t.Fatal("visual grid actions changed durable item placement")
     86 	}
     87 	t.Logf("Browser artifacts: %s", output)
     88 }