aorecomp

Anarchy Online classic in the browser

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


server/browser_concrete_cushion_test.go (5751B)


      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 TestBrowserConcreteCushionQualities(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the focused native ConcreteCushion check")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/concrete-cushion", 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 			if phase == "warm" {
     37 				for index := 0; index < 2; index++ {
     38 					player := world.newCharacter()
     39 					player.name, player.appearance = fmt.Sprint("ConcreteCushion", index), 0x515
     40 					player.position = [3]float32{60 - float32(index)*3, 14.75, 53}
     41 					player.rotation = [4]float32{0, 1, 0, 0}
     42 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("concrete_cushion-", index))))
     43 					player.level = 200
     44 					player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
     45 					player.stats.Melee.OneHandBlunt, player.stats.MiscWeapons.HeavyWeapons = 238, 142
     46 					player.stats.Melee.SneakAttack, player.stats.Melee.FastAttack = 112, 112
     47 					player.stats.Body.Brawling, player.stats.Body.Dimach = 144, 17
     48 					for stat, raw := range map[uint32]uint16{102: player.stats.Melee.OneHandBlunt, 110: player.stats.MiscWeapons.HeavyWeapons, 146: player.stats.Melee.SneakAttack, 147: player.stats.Melee.FastAttack, 142: player.stats.Body.Brawling, 144: player.stats.Body.Dimach} {
     49 						if uint32(raw) > player.trainingMaximum(stat) {
     50 							t.Fatal("controlled raw skill exceeds its cap", stat, raw, player.trainingMaximum(stat))
     51 						}
     52 					}
     53 					for stat, expected := range map[uint32]int32{102: 287, 110: 191, 146: 161, 147: 161, 142: 193, 144: 66} {
     54 						if actual, _ := player.effectiveStat(stat); actual != expected {
     55 							t.Fatal("controlled effective skill differs", stat, actual)
     56 						}
     57 					}
     58 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     59 					if err := world.saveUser(player); err != nil {
     60 						t.Fatal(err)
     61 					}
     62 				}
     63 				// Controlled contracts and a nearby original Leet isolate the weapon check.
     64 				for _, item := range []struct{ template, quality uint32 }{
     65 					{125456, 11}, {125457, 84}, {125457, 85}, {125456, 86},
     66 				} {
     67 					if granted, err := world.grantItem(world.characters[1], item.template); err != nil || !granted {
     68 						t.Fatal(granted, err)
     69 					}
     70 					world.characters[1].inventory.Items[len(world.characters[1].inventory.Items)-1].Quality = item.quality
     71 				}
     72 				if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil {
     73 					t.Fatal(err)
     74 				}
     75 				target.position, target.motion.home = [3]float32{61, 14.75, 51.5}, [3]float32{61, 14.75, 51.5}
     76 				world.combatRoll = func(limit uint32) uint32 {
     77 					if limit == 100 {
     78 						return 99
     79 					}
     80 					return 0
     81 				}
     82 			}
     83 			// Keep recovery outside the focused single-hit assertion.
     84 			target.regenerationAt = time.Now().Add(5 * time.Minute)
     85 			targetID := target.id
     86 			shutdown := startTestService(t, world)
     87 			mux := http.NewServeMux()
     88 			mux.Handle("/", world.httpHandler())
     89 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     90 				owner, watcher := world.characters[1], world.characters[2]
     91 				health := uint32(0)
     92 				if target := world.characters[targetID]; target != nil {
     93 					health = target.health
     94 				}
     95 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory,
     96 					"target": targetID, "health": health, "fighting": owner.fighting, "ownerHealth": owner.health,
     97 					"abilities": [2]int32{owner.effectiveAbility(16), owner.effectiveAbility(18)}, "rating": owner.attackRating(),
     98 					"maxHealth": owner.maximumHealth(), "maxNano": owner.maximumNano()})
     99 			}))
    100 			host := httptest.NewServer(mux)
    101 			t.Cleanup(host.Close)
    102 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    103 			defer cancel()
    104 
    105 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_concrete_cushion.mjs"), host.URL, filepath.Join(output, phase))
    106 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    107 			err := command.Run()
    108 			cancel()
    109 			host.Close()
    110 			if err := shutdown(); err != nil {
    111 				t.Fatal(err)
    112 			}
    113 			// Check committed state after owners and transports have stopped.
    114 			cold := newTestInventoryServer(t)
    115 			owner, watcher := cold.characters[1], cold.characters[2]
    116 			t.Logf("Browser artifacts: %s", output)
    117 
    118 			if err != nil {
    119 				t.Fatal(err)
    120 			}
    121 			expected := []ownedItem{
    122 				{ID: 2, Type: 51018, Template: 125457, Quality: 84, Slot: 65, Count: 1},
    123 				{ID: 3, Type: 51018, Template: 125457, Quality: 85, Slot: 6, Count: 1},
    124 				{ID: 4, Type: 51018, Template: 125456, Quality: 86, Slot: 67, Count: 1},
    125 			}
    126 			if !slices.Equal(owner.inventory.Items, expected) || owner.inventory.Credits != 42 || owner.health == 0 ||
    127 				len(watcher.inventory.Items) != 0 ||
    128 				watcher.inventory.Credits != 0 || cold.nextItem != 4 || owner.equipmentBonus(16) != 8 || owner.equipmentBonus(18) != 8 {
    129 				t.Fatal("native wear, sale or cold recovery lost an instance or cash", owner.inventory)
    130 			}
    131 		})
    132 		if t.Failed() {
    133 			return
    134 		}
    135 	}
    136 }