aorecomp

Anarchy Online classic in the browser

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


server/browser_weapon_wear_admission_test.go (3480B)


      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 TestBrowserWeaponWearAdmission(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the focused native weapon wear check")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/weapon-wear-admission", time.Now().Format("20060102-150405"))
     26 	t.Chdir(t.TempDir())
     27 	for _, phase := range []string{"live", "cold"} {
     28 		if !t.Run(phase, func(t *testing.T) {
     29 			world := newServerForTest(t)
     30 			if phase == "live" {
     31 				for index := 0; index < 2; index++ {
     32 					player := world.newCharacter()
     33 					player.name, player.profession, player.level, player.appearance = fmt.Sprint("Wearaudit", index), 9, 100, 0x515
     34 					player.position[2] -= float32(index * 2)
     35 					player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
     36 					player.stats.Melee.OneHandBlunt, player.stats.Melee.FastAttack = 113, 34
     37 					player.stats.Body.Brawling = 50
     38 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     39 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("wear-audit-", index))))
     40 					if err := world.saveUser(player); err != nil {
     41 						t.Fatal(err)
     42 					}
     43 				}
     44 				// Controlled owned items isolate contract admission and source-icon recovery.
     45 				for _, template := range []uint32{122828, 121564, 154052} {
     46 					if granted, err := world.grantItem(world.characters[1], template); err != nil || !granted {
     47 						t.Fatal(granted, err)
     48 					}
     49 				}
     50 			}
     51 			owner, observer := world.characters[1], world.characters[2]
     52 			shutdown := startTestService(t, world)
     53 			mux := http.NewServeMux()
     54 			mux.Handle("/", world.httpHandler())
     55 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     56 				owner := world.characters[1]
     57 				observer := world.characters[2]
     58 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": observer.inventory,
     59 					"psychic": owner.equipmentBonus(21), "nanoMaximum": owner.maximumNano()})
     60 			}))
     61 			host := httptest.NewServer(mux)
     62 			t.Cleanup(host.Close)
     63 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     64 			defer cancel()
     65 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_weapon_wear_admission.mjs"), host.URL, filepath.Join(output, 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 			cancel()
     73 			t.Logf("Browser artifacts: %s", output)
     74 
     75 			if err != nil {
     76 				t.Fatal(err)
     77 			}
     78 			expected := []ownedItem{
     79 				{ID: 1, Type: 51018, Template: 122828, Quality: 41, Slot: 64, Count: 1},
     80 				{ID: 2, Type: 51018, Template: 121564, Quality: 1, Slot: 6, Count: 1},
     81 				{ID: 3, Type: 51018, Template: 154052, Quality: 1, Slot: 66, Count: 1},
     82 			}
     83 			if !slices.Equal(owner.inventory.Items, expected) || owner.equipmentBonus(21) != 0 || newTestInventoryServer(t).nextItem != 3 ||
     84 				owner.inventory.Credits != 0 || len(observer.inventory.Items) != 0 || observer.inventory.Credits != 0 {
     85 				t.Fatal("native refusal, supported wear or cold recovery changed the owned instances", owner.inventory)
     86 			}
     87 		}) {
     88 			return
     89 		}
     90 	}
     91 }