aorecomp

Anarchy Online classic in the browser

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


server/browser_stale_worn_deposit_test.go (4030B)


      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 	"reflect"
     14 	"runtime"
     15 	"slices"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserStaleWornContainerDeposit(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for a stale worn container deposit")
     23 	}
     24 
     25 	_, source, _, _ := runtime.Caller(0)
     26 	root := filepath.Dir(filepath.Dir(source))
     27 	output := filepath.Join(root, "local/stale-worn-deposit", time.Now().Format("20060102-150405"))
     28 	world := newTestServer(t)
     29 	player := world.newCharacter()
     30 	player.level = 2
     31 	player.stats.Abilities.Sense, player.stats.Abilities.Agility = 8, 8
     32 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("stale-worn-deposit")))
     33 	if err := world.saveUser(player); err != nil {
     34 		t.Fatal(err)
     35 	}
     36 	inventory := playerInventory{LastID: 3, Items: []ownedItem{
     37 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     38 		{ID: 2, Type: 51022, Template: 85481, Quality: 1, Slot: 21, Count: 1},
     39 		{ID: 3, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}}}
     40 	if err := world.saveInventory(player, inventory); err != nil {
     41 		t.Fatal(err)
     42 	}
     43 	holdTestEquipmentCompletions(t, world)
     44 	shutdown := startTestService(t, world)
     45 	mux := http.NewServeMux()
     46 	mux.Handle("/", world.httpHandler())
     47 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     48 		player := world.characters[1]
     49 		var pending any
     50 		if client := world.clients[1]; client != nil && client.equipment != nil {
     51 			pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source,
     52 				"destination": client.equipment.destination, "readyAt": client.equipment.readyAt}
     53 		}
     54 		var armor [8]int32
     55 		for index := range armor {
     56 			armor[index] = player.equipmentBonus(90 + uint32(index))
     57 		}
     58 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending, "armor": armor})
     59 	}))
     60 	// Commit the vest to its actual bag without refreshing the old worn view.
     61 	mux.HandleFunc("POST /test-stale-view", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 		player := world.characters[1]
     63 		inventory := player.inventory
     64 		inventory.Items = []ownedItem{inventory.Items[0], inventory.Items[2]}
     65 		vest := player.inventory.Items[1]
     66 		vest.Slot = 0
     67 		inventory.Containers = map[uint32][]ownedItem{3: {vest}}
     68 		if err := world.saveInventory(player, inventory); err != nil {
     69 			t.Error(err)
     70 			http.Error(w, err.Error(), http.StatusInternalServerError)
     71 		}
     72 	}))
     73 	mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     74 		finish := world.clients[1].equipment.readyAt
     75 		world.advanceEquipment(finish)
     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_stale_worn_deposit.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 	t.Logf("Browser artifacts: %s", output)
     90 
     91 	if err != nil {
     92 		t.Fatal(err)
     93 	}
     94 	expected := playerInventory{LastID: 3, Items: []ownedItem{
     95 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1},
     96 		{ID: 3, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1},
     97 		{ID: 2, Type: 51022, Template: 85481, Quality: 1, Slot: 21, Count: 1}}}
     98 	if !reflect.DeepEqual(player.inventory, expected) {
     99 		t.Fatal("stale worn deposit changed an owned instance")
    100 	}
    101 	slices.SortFunc(expected.Items, func(a, b ownedItem) int { return int(a.ID) - int(b.ID) })
    102 	cold := newTestInventoryServer(t)
    103 	if !reflect.DeepEqual(cold.characters[1].inventory, expected) || cold.nextItem != 3 {
    104 		t.Fatal("stale worn deposit lost or duplicated durable ownership")
    105 	}
    106 }