aorecomp

Anarchy Online classic in the browser

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


server/browser_kerans_test.go (5945B)


      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 TestBrowserKeransQualities(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the focused native Kerans check")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/kerans", 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("Kerans", 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("kerans-", index))))
     47 					player.level = 200
     48 					player.stats.Abilities = characterAbilitiesStats{300, 300, 300, 300, 300, 300}
     49 					player.stats.Ranged.SubmachineGun, player.stats.Ranged.Burst = 400, 283
     50 					for stat, raw := range map[uint32]uint16{114: 400, 148: 283} {
     51 						if uint32(raw) > player.trainingMaximum(stat) {
     52 							t.Fatal("controlled raw skill exceeds its native cap", stat)
     53 						}
     54 					}
     55 					for stat, expected := range map[uint32]int32{114: 474, 148: 357} {
     56 						if actual, _ := player.effectiveStat(stat); actual != expected {
     57 							t.Fatal("controlled effective skill differs", stat, actual)
     58 						}
     59 					}
     60 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     61 					if err := world.saveUser(player); err != nil {
     62 						t.Fatal(err)
     63 					}
     64 				}
     65 				// Controlled contracts and a nearby original Leet isolate the weapon check.
     66 				for _, item := range []struct{ template, quality uint32 }{
     67 					{160476, 2}, {160477, 50}, {160477, 98}, {160479, 100}, {160478, 101}, {21605, 1},
     68 				} {
     69 					if granted, err := world.grantItem(world.characters[1], item.template); err != nil || !granted {
     70 						t.Fatal(granted, err)
     71 					}
     72 					world.characters[1].inventory.Items[len(world.characters[1].inventory.Items)-1].Quality = item.quality
     73 				}
     74 				world.characters[1].inventory.Items[5].Count = 200
     75 				if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil {
     76 					t.Fatal(err)
     77 				}
     78 				target.health = 100
     79 				target.position, target.motion.home = [3]float32{61, 14.75, 51.5}, [3]float32{61, 14.75, 51.5}
     80 				world.combatRoll = func(limit uint32) uint32 {
     81 					if limit == 100 {
     82 						return 99
     83 					}
     84 					return 0
     85 				}
     86 			}
     87 			// Keep the controlled target stationary and its hit damage observable.
     88 			target.motion.roaming = false
     89 			target.regenerationAt = time.Now().Add(3 * time.Minute)
     90 			targetID := target.id
     91 			shutdown := startTestService(t, world)
     92 			mux := http.NewServeMux()
     93 			mux.Handle("/", world.httpHandler())
     94 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     95 				owner, watcher := world.characters[1], world.characters[2]
     96 				health := uint32(0)
     97 				if target := world.characters[targetID]; target != nil {
     98 					health = target.health
     99 				}
    100 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory,
    101 					"target": targetID, "health": health, "fighting": owner.fighting, "ownerHealth": owner.health, "sense": owner.effectiveAbility(20), "rating": owner.attackRating(), "maxHealth": owner.maximumHealth(), "maxNano": owner.maximumNano()})
    102 			}))
    103 			host := httptest.NewServer(mux)
    104 			t.Cleanup(host.Close)
    105 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    106 			defer cancel()
    107 
    108 			if err := os.MkdirAll(filepath.Join(output, phase), 0755); err != nil {
    109 				t.Fatal(err)
    110 			}
    111 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_kerans.mjs"), host.URL, filepath.Join(output, phase))
    112 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    113 			err := command.Run()
    114 			cancel()
    115 			host.Close()
    116 			if err := shutdown(); err != nil {
    117 				t.Fatal(err)
    118 			}
    119 			copyTestDatabase(t, filepath.Join(output, phase, "world.sqlite"))
    120 			// Check committed state after owners and transports have stopped.
    121 			cold := newTestInventoryServer(t)
    122 			owner, watcher := cold.characters[1], cold.characters[2]
    123 
    124 			t.Logf("Browser artifacts: %s", output)
    125 
    126 			if err != nil {
    127 				t.Fatal(err)
    128 			}
    129 			expected := []ownedItem{
    130 				{ID: 2, Type: 51018, Template: 160477, Quality: 50, Slot: 65, Count: 1},
    131 				{ID: 3, Type: 51018, Template: 160477, Quality: 98, Slot: 66, Count: 1},
    132 				{ID: 4, Type: 51018, Template: 160479, Quality: 100, Slot: 6, Count: 1, Ammo: 119},
    133 				{ID: 5, Type: 51018, Template: 160478, Quality: 101, Slot: 68, Count: 1},
    134 				{ID: 6, Type: 51005, Template: 21605, Quality: 1, Slot: 69, Count: 80},
    135 			}
    136 
    137 			if !slices.Equal(owner.inventory.Items, expected) || owner.inventory.Credits != 145 || owner.health == 0 ||
    138 				len(watcher.inventory.Items) != 0 ||
    139 				watcher.inventory.Credits != 0 || cold.nextItem != 6 || owner.effectiveAbility(20) != 304 || owner.stats.Abilities.Sense != 300 || owner.attackRating() != 475 || owner.maximumNano() != 1050 {
    140 				t.Fatal("native wear, sale or cold recovery lost an instance or cash", owner.inventory)
    141 			}
    142 		})
    143 		if t.Failed() {
    144 			return
    145 		}
    146 	}
    147 }