aorecomp

Anarchy Online classic in the browser

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


server/browser_effective_armor_test.go (2912B)


      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 TestBrowserEffectiveArmor(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for effective armor snapshots")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/effective-armor", time.Now().Format("20060102-150405"))
     26 	t.Chdir(t.TempDir())
     27 	for _, phase := range []string{"warm", "cold"} {
     28 		t.Run(phase, func(t *testing.T) {
     29 			world := newServerForTest(t)
     30 			if phase == "warm" {
     31 				player := world.newCharacter()
     32 				player.level = 2
     33 				player.stats.Abilities.Sense, player.stats.Abilities.Agility = 11, 11
     34 				player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("effective-armor")))
     35 				if err := world.saveUser(player); err != nil {
     36 					t.Fatal(err)
     37 				}
     38 				if granted, err := world.grantItem(player, 85481); err != nil || !granted {
     39 					t.Fatal(granted, err)
     40 				}
     41 				inventory := player.inventory
     42 				inventory.Items = slices.Clone(inventory.Items)
     43 				inventory.Items[0].Quality = 2
     44 				if err := world.saveInventory(player, inventory); err != nil {
     45 					t.Fatal(err)
     46 				}
     47 			}
     48 			player := world.characters[1]
     49 			shutdown := startTestService(t, world)
     50 			mux := http.NewServeMux()
     51 			mux.Handle("/", world.httpHandler())
     52 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     53 				player := world.characters[1]
     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, "armor": armor})
     59 			}))
     60 			host := httptest.NewServer(mux)
     61 			t.Cleanup(host.Close)
     62 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     63 			defer cancel()
     64 
     65 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_effective_armor.mjs"), host.URL, filepath.Join(output, phase), phase)
     66 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     67 			err := command.Run()
     68 			host.Close()
     69 			if err := shutdown(); err != nil {
     70 				t.Fatal(err)
     71 			}
     72 			t.Logf("Armor snapshot artifacts: %s", output)
     73 
     74 			if err != nil {
     75 				t.Fatal(err)
     76 			}
     77 			if !slices.Equal(player.inventory.Items, []ownedItem{{ID: 1, Type: 51022, Template: 85481, Quality: 2, Slot: 21, Count: 1}}) ||
     78 				newTestInventoryServer(t).nextItem != 1 || player.inventory.Credits != 0 {
     79 				t.Fatal("armor snapshot scenario changed the retained vest")
     80 			}
     81 			for index, value := range [8]int32{9, 10, 2, 2, 9, 9, 2, 6} {
     82 				if player.equipmentBonus(90+uint32(index)) != value {
     83 					t.Fatal("retained vest defenses changed", index)
     84 				}
     85 			}
     86 		})
     87 		if t.Failed() {
     88 			return
     89 		}
     90 	}
     91 }