aorecomp

Anarchy Online classic in the browser

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


server/browser_closed_backpack_split_test.go (2760B)


      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 TestBrowserClosedBackpackSplit(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for closed backpack split")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/closed-backpack-split", time.Now().Format("20060102-150405"))
     26 	world := newTestServer(t)
     27 	player := world.newCharacter()
     28 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("closed-backpack-split")))
     29 	if err := world.saveUser(player); err != nil {
     30 		t.Fatal(err)
     31 	}
     32 	inventory := playerInventory{LastID: 2, Items: []ownedItem{
     33 		{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1}},
     34 		Containers: map[uint32][]ownedItem{1: {
     35 			{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 0, Count: 3}}}}
     36 	if err := world.saveInventory(player, inventory); err != nil {
     37 		t.Fatal(err)
     38 	}
     39 	shutdown := startTestService(t, world)
     40 	mux := http.NewServeMux()
     41 	mux.Handle("/", world.httpHandler())
     42 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     43 		player := world.characters[1]
     44 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "open": world.clients[1].backpacks[1].open})
     45 	}))
     46 	// Isolate a closed server handle without refreshing the cached native window.
     47 	mux.HandleFunc("POST /test-close-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     48 
     49 		world.clients[1].backpacks[1] = backpackHandle{1, false}
     50 	}))
     51 	host := httptest.NewServer(mux)
     52 	t.Cleanup(host.Close)
     53 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     54 	defer cancel()
     55 
     56 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_closed_backpack_split.mjs"), host.URL, output)
     57 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     58 	err := command.Run()
     59 	host.Close()
     60 	if err := shutdown(); err != nil {
     61 		t.Fatal(err)
     62 	}
     63 
     64 	t.Logf("Browser artifacts: %s", output)
     65 	if err != nil {
     66 		t.Fatal(err)
     67 	}
     68 	cold := newTestInventoryServer(t)
     69 	for _, current := range []*character{player, cold.characters[1]} {
     70 		if !slices.Equal(current.inventory.Items, inventory.Items) || !slices.Equal(current.inventory.Containers[1], inventory.Containers[1]) ||
     71 			current.inventory.Credits != 0 || current.inventory.LastID != 3 {
     72 			t.Fatal("closed split or normal retry lost ownership or charges", current.inventory)
     73 		}
     74 	}
     75 	if cold.nextItem != 3 {
     76 		t.Fatal("refused split allocated an identity or retry repeated allocation")
     77 	}
     78 }