aorecomp

Anarchy Online classic in the browser

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


server/browser_stale_bag_stack_test.go (3536B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"crypto/sha256"
      6 	"encoding/json"
      7 	"fmt"
      8 	"maps"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"slices"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserStaleBackpackStackJoin(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for stale backpack stack joins")
     23 	}
     24 	for _, missing := range []string{"target", "source"} {
     25 		t.Run(missing, func(t *testing.T) {
     26 			_, source, _, _ := runtime.Caller(0)
     27 			root := filepath.Dir(filepath.Dir(source))
     28 			output := filepath.Join(root, "local/stale-bag-stack", time.Now().Format("20060102-150405"))
     29 			world := newTestServer(t)
     30 			player := world.newCharacter()
     31 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("stale-bag-stack")))
     32 			if err := world.saveUser(player); err != nil {
     33 				t.Fatal(err)
     34 			}
     35 			inventory := playerInventory{LastID: 4, Items: []ownedItem{
     36 				{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1}},
     37 				Containers: map[uint32][]ownedItem{1: {
     38 					{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 0, Count: 3},
     39 					{ID: 3, Type: 51005, Template: 25812, Quality: 2, Slot: 1, Count: 5},
     40 					{ID: 4, Type: 51005, Template: 25812, Quality: 2, Slot: 2, Count: 7}}}}
     41 			if err := world.saveInventory(player, inventory); err != nil {
     42 				t.Fatal(err)
     43 			}
     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 				json.NewEncoder(w).Encode(player.inventory)
     50 			}))
     51 			// Controlled checkpoint: an earlier merge committed without a native refresh.
     52 			mux.HandleFunc("POST /test-stale-view", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     53 				player := world.characters[1]
     54 				inventory := player.inventory
     55 				inventory.Containers = maps.Clone(inventory.Containers)
     56 				inventory.Containers[1] = slices.Clone(inventory.Containers[1][1:])
     57 				inventory.Containers[1][0].Count = 8
     58 				if err := world.saveInventory(player, inventory); err != nil {
     59 					t.Error(err)
     60 					http.Error(w, err.Error(), http.StatusInternalServerError)
     61 					return
     62 				}
     63 				gesture := map[string]uint32{"removed": 2, "kept": 0}
     64 				if missing == "source" {
     65 					gesture = map[string]uint32{"removed": 0, "kept": 1}
     66 				}
     67 				json.NewEncoder(w).Encode(gesture)
     68 			}))
     69 			host := httptest.NewServer(mux)
     70 			t.Cleanup(host.Close)
     71 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     72 			defer cancel()
     73 
     74 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_stale_bag_stack.mjs"), host.URL, output)
     75 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     76 			err := command.Run()
     77 			host.Close()
     78 			if err := shutdown(); err != nil {
     79 				t.Fatal(err)
     80 			}
     81 			t.Logf("Browser artifacts: %s", output)
     82 
     83 			if err != nil {
     84 				t.Fatal(err)
     85 			}
     86 			cold := newTestInventoryServer(t)
     87 			expected := []ownedItem{{ID: 3, Type: 51005, Template: 25812, Quality: 2, Slot: 1, Count: 15}}
     88 			if !slices.Equal(player.inventory.Containers[1], expected) || !slices.Equal(cold.characters[1].inventory.Containers[1], expected) ||
     89 				len(cold.characters[1].inventory.Items) != 1 || cold.characters[1].inventory.Items[0] != inventory.Items[0] ||
     90 				cold.nextItem != 4 {
     91 				t.Fatal("stale backpack join changed ownership, quantity or allocation")
     92 			}
     93 		})
     94 	}
     95 }