aorecomp

Anarchy Online classic in the browser

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


server/browser_token_board_wear_test.go (3201B)


      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 TestBrowserTokenBoardWear(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for token-board wear")
     22 	}
     23 	for _, template := range []uint32{96352, 216368} {
     24 		t.Run(fmt.Sprint(template), func(t *testing.T) {
     25 			_, source, _, _ := runtime.Caller(0)
     26 			root := filepath.Dir(filepath.Dir(source))
     27 			output := filepath.Join(root, "local/token-board-wear", time.Now().Format("20060102-150405"), fmt.Sprint(template))
     28 			var fixture tokenBoardWearFixture
     29 			for _, board := range tokenBoardWearFixtures(t) {
     30 				if board.Template == template {
     31 					fixture = board
     32 				}
     33 			}
     34 			world := newTestServer(t)
     35 			player := world.newCharacter()
     36 			player.level, player.profession, player.stats = 190, 3, testCharacterStats()
     37 			appearance := unpackAppearance(player.appearance)
     38 			appearance.Side = fixture.Side
     39 			player.appearance = appearance.packed()
     40 			inventory := playerInventory{LastID: 1, ClanTokens: 1750, OmniTokens: 1750, Items: []ownedItem{
     41 				{ID: 1, Type: 51022, Template: template, Quality: fixture.Quality, Slot: 64, Count: 1}}}
     42 			if template == 96352 {
     43 				player.level = 1
     44 				inventory.ClanTokens = 30
     45 			}
     46 			player.playfield, player.position = 1183, [3]float32{193.79826, 5.01, 136.83983}
     47 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("token-board-wear")))
     48 			if err := world.saveUser(player); err != nil {
     49 				t.Fatal(err)
     50 			}
     51 			if err := world.saveInventory(player, inventory); err != nil {
     52 				t.Fatal(err)
     53 			}
     54 			shutdown := startTestService(t, world)
     55 			mux := http.NewServeMux()
     56 			mux.Handle("/", world.httpHandler())
     57 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     58 				player := world.characters[1]
     59 				json.NewEncoder(w).Encode(struct {
     60 					Inventory playerInventory
     61 					Maximum   [2]uint32
     62 				}{player.inventory, [2]uint32{player.maximumHealth(), player.maximumNano()}})
     63 			}))
     64 			host := httptest.NewServer(mux)
     65 			t.Cleanup(host.Close)
     66 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     67 			defer cancel()
     68 
     69 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_token_board_wear.mjs"), host.URL, output)
     70 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     71 			err := command.Run()
     72 			host.Close()
     73 			if err := shutdown(); err != nil {
     74 				t.Fatal(err)
     75 			}
     76 
     77 			t.Logf("Browser artifacts: %s", output)
     78 			if err != nil {
     79 				t.Fatal(err)
     80 			}
     81 			restored := newTestInventoryServer(t)
     82 			cold := restored.characters[1]
     83 			if !slices.Equal(player.inventory.Items, cold.inventory.Items) ||
     84 				!slices.Equal(cold.inventory.Items, []ownedItem{{ID: 1, Type: 51022, Template: template, Quality: fixture.Quality, Slot: 17, Count: 1}}) ||
     85 				cold.inventory.ClanTokens != player.inventory.ClanTokens || cold.inventory.OmniTokens != player.inventory.OmniTokens || restored.nextItem != 1 {
     86 				t.Fatal("native board wear changed cold ownership, counters or allocation")
     87 			}
     88 		})
     89 	}
     90 }