aorecomp

Anarchy Online classic in the browser

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


server/browser_kinetic_axe_test.go (5412B)


      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 TestBrowserKineticAxeQualities(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the focused native KineticAxe check")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/kinetic-axe", 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 			var target *character
     31 			for _, actor := range world.characters {
     32 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     33 					target = actor
     34 				}
     35 			}
     36 			// Use a controlled 100-HP target for exact selected-quality damage.
     37 			profile := *target.combatProfile
     38 			profile.Health = 100
     39 			target.combatProfile = &profile
     40 			if phase == "warm" {
     41 				for index := 0; index < 2; index++ {
     42 					player := world.newCharacter()
     43 					player.name, player.appearance = fmt.Sprint("KineticAxe", index), 0x515
     44 					player.position = [3]float32{60 - float32(index)*3, 14.75, 53}
     45 					player.rotation = [4]float32{0, 1, 0, 0}
     46 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("kinetic-axe-", index))))
     47 					if index == 0 {
     48 						player.level = 100
     49 						player.stats.Abilities = characterAbilitiesStats{28, 28, 28, 28, 28, 28}
     50 						player.stats.Melee.OneHandEdged = 50
     51 						player.stats.Body.Brawling, player.stats.Melee.FastAttack = 29, 23
     52 						for stat, raw := range map[uint32]uint16{103: player.stats.Melee.OneHandEdged, 142: player.stats.Body.Brawling, 147: player.stats.Melee.FastAttack} {
     53 							if uint32(raw) > player.trainingMaximum(stat) {
     54 								t.Fatal("controlled raw skill exceeds its native cap", stat, raw, player.trainingMaximum(stat))
     55 							}
     56 						}
     57 						for stat, expected := range map[uint32]int32{103: 56, 142: 35, 147: 29} {
     58 							if actual, _ := player.effectiveStat(stat); actual != expected {
     59 								t.Fatal("controlled effective skill differs", stat, actual)
     60 							}
     61 						}
     62 					}
     63 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     64 					if err := world.saveUser(player); err != nil {
     65 						t.Fatal(err)
     66 					}
     67 				}
     68 				// Controlled contracts and a nearby original Leet isolate the weapon check.
     69 				for _, item := range []struct{ template, quality uint32 }{
     70 					{209283, 2}, {209284, 12}, {209284, 13}, {209283, 14},
     71 				} {
     72 					if granted, err := world.grantItem(world.characters[1], item.template); err != nil || !granted {
     73 						t.Fatal(granted, err)
     74 					}
     75 					world.characters[1].inventory.Items[len(world.characters[1].inventory.Items)-1].Quality = item.quality
     76 				}
     77 				if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil {
     78 					t.Fatal(err)
     79 				}
     80 				target.health = 100
     81 				target.position, target.motion.home = [3]float32{61, 14.75, 51.5}, [3]float32{61, 14.75, 51.5}
     82 				world.combatRoll = func(limit uint32) uint32 {
     83 					if limit == 100 {
     84 						return 99
     85 					}
     86 					return 0
     87 				}
     88 			}
     89 			// Keep the controlled target stationary and its hit damage observable.
     90 			target.motion.roaming = false
     91 			target.regenerationAt = time.Now().Add(3 * time.Minute)
     92 			targetID := target.id
     93 			shutdown := startTestService(t, world)
     94 			mux := http.NewServeMux()
     95 			mux.Handle("/", world.httpHandler())
     96 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     97 				owner, watcher := world.characters[1], world.characters[2]
     98 				health := uint32(0)
     99 				if target := world.characters[targetID]; target != nil {
    100 					health = target.health
    101 				}
    102 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory,
    103 					"target": targetID, "health": health, "fighting": owner.fighting, "ownerHealth": owner.health})
    104 			}))
    105 			host := httptest.NewServer(mux)
    106 			t.Cleanup(host.Close)
    107 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    108 			defer cancel()
    109 
    110 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_kinetic_axe.mjs"), host.URL, filepath.Join(output, phase))
    111 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    112 			err := command.Run()
    113 			host.Close()
    114 			if failure := shutdown(); failure != nil {
    115 				t.Fatal(failure)
    116 			}
    117 			t.Logf("Browser artifacts: %s", output)
    118 
    119 			if err != nil {
    120 				t.Fatal(err)
    121 			}
    122 			world = newTestInventoryServer(t)
    123 			owner, watcher := world.characters[1], world.characters[2]
    124 			expected := []ownedItem{
    125 				{ID: 2, Type: 51018, Template: 209284, Quality: 12, Slot: 65, Count: 1},
    126 				{ID: 3, Type: 51018, Template: 209284, Quality: 13, Slot: 6, Count: 1},
    127 				{ID: 4, Type: 51018, Template: 209283, Quality: 14, Slot: 67, Count: 1},
    128 			}
    129 			if !slices.Equal(owner.inventory.Items, expected) || owner.inventory.Credits != 631 || owner.health == 0 ||
    130 				len(watcher.inventory.Items) != 0 || watcher.inventory.Credits != 0 || world.nextItem != 4 {
    131 				t.Fatal("native wear, sale or cold recovery lost an instance or cash", owner.inventory)
    132 			}
    133 		})
    134 		if t.Failed() {
    135 			return
    136 		}
    137 	}
    138 }