aorecomp

Anarchy Online classic in the browser

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


server/browser_backpack_equipment_test.go (4633B)


      1 package main
      2 
      3 import (
      4 	"cmp"
      5 	"context"
      6 	"crypto/sha256"
      7 	"encoding/json"
      8 	"fmt"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"reflect"
     15 	"runtime"
     16 	"slices"
     17 	"testing"
     18 	"time"
     19 )
     20 
     21 func TestBrowserBackpackEquipment(t *testing.T) {
     22 	if os.Getenv("AO_GAME_TEST") != "1" {
     23 		t.Skip("set AO_GAME_TEST=1 for direct equipment from a backpack")
     24 	}
     25 
     26 	_, source, _, _ := runtime.Caller(0)
     27 	root := filepath.Dir(filepath.Dir(source))
     28 	output := filepath.Join(root, "local/backpack-equipment", time.Now().Format("20060102-150405"))
     29 	world := newTestServer(t)
     30 	for index := range 2 {
     31 		player := world.newCharacter()
     32 		player.level = 2
     33 		player.position[0] += float32(index * 2)
     34 		player.stats.Abilities.Sense, player.stats.Abilities.Agility = 8, 8
     35 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("backpack-equipment-", index))))
     36 		if err := world.saveUser(player); err != nil {
     37 			t.Fatal(err)
     38 		}
     39 	}
     40 	player := world.characters[1]
     41 	observer := world.characters[2]
     42 	inventory := playerInventory{LastID: 32, Items: []ownedItem{
     43 		{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1}},
     44 		Containers: map[uint32][]ownedItem{1: {
     45 			{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 5, Count: 1},
     46 			{ID: 3, Type: 51022, Template: 85481, Quality: 1, Slot: 0, Count: 1}}}}
     47 	// Fill every ordinary slot so direct wear cannot depend on a temporary free one.
     48 	for id := uint32(4); id <= 32; id++ {
     49 		inventory.Items = append(inventory.Items, ownedItem{ID: id, Type: 51005, Template: 95577, Quality: 1, Slot: id + 61, Count: 1})
     50 	}
     51 	if err := world.saveInventory(player, inventory); err != nil {
     52 		t.Fatal(err)
     53 	}
     54 	holdTestEquipmentCompletions(t, world)
     55 	shutdown := startTestService(t, world)
     56 	mux := http.NewServeMux()
     57 	mux.Handle("/", world.httpHandler())
     58 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     59 		player := world.characters[1]
     60 		var pending any
     61 		if client := world.clients[1]; client != nil && client.equipment != nil {
     62 			pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source,
     63 				"destination": client.equipment.destination, "readyAt": client.equipment.readyAt}
     64 		}
     65 		var armor [8]int32
     66 		for index := range armor {
     67 			armor[index] = player.equipmentBonus(90 + uint32(index))
     68 		}
     69 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending, "armor": armor})
     70 	}))
     71 	// Exercise admission and completion separately without earning a test state.
     72 	mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     73 		finish := world.clients[1].equipment.readyAt
     74 		world.advanceEquipment(finish)
     75 	}))
     76 	mux.HandleFunc("POST /test-fail-placement", func(w http.ResponseWriter, r *http.Request) {
     77 		if _, err := world.db.Exec("CREATE TRIGGER reject_native_bag_wear BEFORE DELETE ON item_locations WHEN OLD.item_id=2 BEGIN SELECT RAISE(IGNORE); END"); err != nil {
     78 			t.Error(err)
     79 			http.Error(w, err.Error(), http.StatusInternalServerError)
     80 		}
     81 	})
     82 	mux.HandleFunc("POST /test-allow-placement", func(w http.ResponseWriter, r *http.Request) {
     83 		if _, err := world.db.Exec("DROP TRIGGER reject_native_bag_wear"); err != nil {
     84 			t.Error(err)
     85 			http.Error(w, err.Error(), http.StatusInternalServerError)
     86 		}
     87 	})
     88 	host := httptest.NewServer(mux)
     89 	t.Cleanup(host.Close)
     90 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     91 	defer cancel()
     92 
     93 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_backpack_equipment.mjs"), host.URL, output)
     94 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     95 	err := command.Run()
     96 	host.Close()
     97 	if err := shutdown(); err != nil {
     98 		t.Fatal(err)
     99 	}
    100 	t.Logf("Browser artifacts: %s", output)
    101 
    102 	if err != nil {
    103 		t.Fatal(err)
    104 	}
    105 	expected := inventory
    106 	expected.Containers = nil
    107 	expected.Items = append(slices.Clone(inventory.Items), []ownedItem{
    108 		{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1},
    109 		{ID: 3, Type: 51022, Template: 85481, Quality: 1, Slot: 21, Count: 1}}...)
    110 	if !reflect.DeepEqual(player.inventory, expected) {
    111 		t.Fatal("direct contained equipment changed a carried instance")
    112 	}
    113 	slices.SortFunc(expected.Items, func(first, second ownedItem) int { return cmp.Compare(first.ID, second.ID) })
    114 	cold := newTestInventoryServer(t)
    115 	if !reflect.DeepEqual(cold.characters[1].inventory, expected) ||
    116 		cold.nextItem != 32 || len(observer.inventory.Items) != 0 {
    117 		t.Fatal("direct contained equipment lost or duplicated ownership")
    118 	}
    119 }