aorecomp

Anarchy Online classic in the browser

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


server/browser_replaced_backpack_split_test.go (4171B)


      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 TestBrowserReplacedBackpackSplit(t *testing.T) {
     20 	testBrowserStaleBackpackSplit(t, false)
     21 }
     22 
     23 func TestBrowserEmptyBackpackSplit(t *testing.T) {
     24 	testBrowserStaleBackpackSplit(t, true)
     25 }
     26 
     27 func testBrowserStaleBackpackSplit(t *testing.T, empty bool) {
     28 	t.Helper()
     29 	if os.Getenv("AO_GAME_TEST") != "1" {
     30 		t.Skip("set AO_GAME_TEST=1 for stale backpack split")
     31 	}
     32 	_, source, _, _ := runtime.Caller(0)
     33 	root := filepath.Dir(filepath.Dir(source))
     34 	output := filepath.Join(root, "local/replaced-backpack-split", time.Now().Format("20060102-150405"))
     35 	if empty {
     36 		output = filepath.Join(root, "local/empty-backpack-split", time.Now().Format("20060102-150405"))
     37 	}
     38 	world := newTestServer(t)
     39 	player := world.newCharacter()
     40 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("replaced-backpack-split")))
     41 	if err := world.saveUser(player); err != nil {
     42 		t.Fatal(err)
     43 	}
     44 	inventory := playerInventory{LastID: 4, Items: []ownedItem{
     45 		{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1},
     46 		{ID: 3, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}},
     47 		Containers: map[uint32][]ownedItem{1: {
     48 			{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 0, Count: 3}}, 3: {
     49 			{ID: 4, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}}}}
     50 	if err := world.saveInventory(player, inventory); err != nil {
     51 		t.Fatal(err)
     52 	}
     53 	shutdown := startTestService(t, world)
     54 	mux := http.NewServeMux()
     55 	mux.Handle("/", world.httpHandler())
     56 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     57 		player := world.characters[1]
     58 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "open": world.clients[1].backpacks[1].open})
     59 	}))
     60 	// Commit an earlier transfer while retaining the old native source view.
     61 	mux.HandleFunc("POST /test-move-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 		player := world.characters[1]
     63 		inventory := player.inventory
     64 		inventory.Containers = map[uint32][]ownedItem{1: slices.Clone(inventory.Containers[3]), 3: slices.Clone(inventory.Containers[1])}
     65 		if empty {
     66 			stack := inventory.Containers[3][0]
     67 			stack.Slot = 1
     68 			inventory.Containers[3] = []ownedItem{inventory.Containers[1][0], stack}
     69 			delete(inventory.Containers, 1)
     70 		}
     71 		if err := world.saveInventory(player, inventory); err != nil {
     72 			t.Error(err)
     73 			http.Error(w, err.Error(), http.StatusInternalServerError)
     74 			return
     75 		}
     76 	}))
     77 	host := httptest.NewServer(mux)
     78 	t.Cleanup(host.Close)
     79 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     80 	defer cancel()
     81 
     82 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_replaced_backpack_split.mjs"), host.URL, output)
     83 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     84 	err := command.Run()
     85 	host.Close()
     86 	if err := shutdown(); err != nil {
     87 		t.Fatal(err)
     88 	}
     89 
     90 	t.Logf("Browser artifacts: %s", output)
     91 	if err != nil {
     92 		t.Fatal(err)
     93 	}
     94 	cold := newTestInventoryServer(t)
     95 	expected := []ownedItem{{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1},
     96 		{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 67, Count: 3},
     97 		{ID: 3, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1},
     98 		{ID: 4, Type: 51018, Template: 123742, Quality: 2, Slot: 66, Count: 1, Ammo: 3}}
     99 	for _, current := range []*character{player, cold.characters[1]} {
    100 		if len(current.inventory.Items) != 4 || len(current.inventory.Containers) != 0 ||
    101 			current.inventory.Credits != 0 || current.inventory.LastID != 4 {
    102 			t.Fatal("stale split changed quantities, cash or allocation", current.inventory)
    103 		}
    104 		for _, item := range expected {
    105 			if !slices.Contains(current.inventory.Items, item) {
    106 				t.Fatal("stale split recovery lost an exact instance", current.inventory)
    107 			}
    108 		}
    109 	}
    110 	if cold.nextItem != 4 {
    111 		t.Fatal("refused split allocated an identity or retry repeated allocation")
    112 	}
    113 }