aorecomp

Anarchy Online classic in the browser

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


server/browser_bank_attachment_test.go (2424B)


      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 TestBrowserBankBackpackPreservesWornIdentity(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for bank backpack attachment")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/bank-attachment", time.Now().Format("20060102-150405"))
     26 	world := newTestServer(t)
     27 	player := world.newCharacter()
     28 	player.playfield, player.position = 1183, [3]float32{193.79826, 5.01, 136.83983}
     29 	player.rotation = [4]float32{0, .70710677, 0, .70710677}
     30 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("bank-attachment")))
     31 	if err := world.saveUser(player); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	inventory := playerInventory{LastID: 2, Items: []ownedItem{
     35 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}}, Bank: []ownedItem{
     36 		{ID: 2, Type: 51017, Template: 99228, Quality: 1, Slot: 6, Count: 1}}}
     37 	if err := world.saveInventory(player, inventory); err != nil {
     38 		t.Fatal(err)
     39 	}
     40 	shutdown := startTestService(t, world)
     41 	mux := http.NewServeMux()
     42 	mux.Handle("/", world.httpHandler())
     43 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     44 		json.NewEncoder(w).Encode(world.characters[1].inventory)
     45 	}))
     46 	host := httptest.NewServer(mux)
     47 	t.Cleanup(host.Close)
     48 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     49 	defer cancel()
     50 
     51 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_bank_attachment.mjs"), host.URL, output)
     52 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     53 	err := command.Run()
     54 	host.Close()
     55 	if failure := shutdown(); failure != nil {
     56 		t.Fatal(failure)
     57 	}
     58 	t.Logf("Browser artifacts: %s", output)
     59 
     60 	if err != nil {
     61 		t.Fatal(err)
     62 	}
     63 	cold := newTestInventoryServer(t)
     64 	bank := slices.Clone(inventory.Bank)
     65 	bank[0].Slot = 0
     66 	if !slices.Equal(player.inventory.Items, inventory.Items) || !slices.Equal(cold.characters[1].inventory.Items, inventory.Items) ||
     67 		!slices.Equal(player.inventory.Bank, bank) || !slices.Equal(cold.characters[1].inventory.Bank, bank) ||
     68 		cold.nextItem != 2 {
     69 		t.Fatal("bank attachment changed worn or banked ownership")
     70 	}
     71 }