aorecomp

Anarchy Online classic in the browser

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


server/browser_replaced_stack_split_test.go (3685B)


      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 TestBrowserReplacedStackSplit(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for a replaced ordinary split source")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/replaced-stack-split", time.Now().Format("20060102-150405"))
     26 	world := newTestServer(t)
     27 	player := world.newCharacter()
     28 	player.stats = testCharacterStats()
     29 	player.stats.Abilities = characterAbilitiesStats{80, 80, 80, 80, 80, 80}
     30 	player.stats.Ranged.Pistol = 100
     31 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("replaced-stack-split")))
     32 	if err := world.saveUser(player); err != nil {
     33 		t.Fatal(err)
     34 	}
     35 	inventory := playerInventory{LastID: 2,
     36 		Items: []ownedItem{{ID: 1, Type: 51005, Template: 25812, Quality: 2, Slot: 64, Count: 3}},
     37 		Bank:  []ownedItem{{ID: 2, Type: 51018, Template: 123742, Quality: 2, Slot: 0, Count: 1, Ammo: 3}}}
     38 	if err := world.saveInventory(player, inventory); err != nil {
     39 		t.Fatal(err)
     40 	}
     41 	holdTestEquipmentCompletions(t, world)
     42 	shutdown := startTestService(t, world)
     43 	mux := http.NewServeMux()
     44 	mux.Handle("/", world.httpHandler())
     45 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     46 		player := world.characters[1]
     47 		pending := world.clients[1].equipment != nil
     48 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending})
     49 	}))
     50 	// Isolate an earlier committed transfer while the original source remains native.
     51 	mux.HandleFunc("POST /test-replace-source", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     52 		player := world.characters[1]
     53 		inventory := player.inventory
     54 		inventory.Items, inventory.Bank = slices.Clone(inventory.Bank), slices.Clone(inventory.Items)
     55 		inventory.Items[0].Slot, inventory.Bank[0].Slot = 64, 0
     56 		if err := world.saveInventory(player, inventory); err != nil {
     57 			t.Error(err)
     58 			http.Error(w, err.Error(), http.StatusInternalServerError)
     59 		}
     60 	}))
     61 	mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 		finish := world.clients[1].equipment.readyAt
     63 		world.advanceEquipment(finish)
     64 	}))
     65 	host := httptest.NewServer(mux)
     66 	t.Cleanup(host.Close)
     67 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     68 	defer cancel()
     69 
     70 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_replaced_stack_split.mjs"), host.URL, output)
     71 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     72 	err := command.Run()
     73 	host.Close()
     74 	if err := shutdown(); err != nil {
     75 		t.Fatal(err)
     76 	}
     77 	t.Logf("Browser artifacts: %s", output)
     78 
     79 	if err != nil {
     80 		t.Fatal(err)
     81 	}
     82 	cold := newTestInventoryServer(t)
     83 	expected := playerInventory{LastID: 2,
     84 		Items: []ownedItem{{ID: 2, Type: 51018, Template: 123742, Quality: 2, Slot: 6, Count: 1, Ammo: 3}},
     85 		Bank:  []ownedItem{{ID: 1, Type: 51005, Template: 25812, Quality: 2, Slot: 0, Count: 3}}}
     86 	for _, current := range []*character{player, cold.characters[1]} {
     87 		if !slices.Equal(current.inventory.Items, expected.Items) || !slices.Equal(current.inventory.Bank, expected.Bank) ||
     88 			current.inventory.LastID != 2 || current.inventory.Credits != 0 {
     89 			t.Fatal("replaced split changed exact ownership, charges or loaded rounds", current.inventory)
     90 		}
     91 	}
     92 	if cold.nextItem != 2 {
     93 		t.Fatal("replaced source correction allocated an item")
     94 	}
     95 }