aorecomp

Anarchy Online classic in the browser

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


server/browser_vektor_test.go (4789B)


      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 	"reflect"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserVektorQualities(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the controlled WASM Vektor regression")
     22 	}
     23 
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/game-tests", "vektor-"+time.Now().Format("20060102-150405"))
     27 	world := newTestServer(t)
     28 	var target *character
     29 	for _, actor := range world.characters {
     30 		if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     31 			target = actor
     32 		}
     33 	}
     34 	if target == nil {
     35 		t.Fatal("no Leet for the controlled shot")
     36 	}
     37 	owner := world.newCharacter()
     38 	owner.name, owner.level, owner.profession, owner.appearance = "Vektorowner", 100, 7, 0x315
     39 	owner.position = target.position
     40 	owner.position[2] -= 2
     41 	owner.rotation = [4]float32{0, 0, 0, 1}
     42 	owner.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
     43 	owner.stats.Ranged.Shotgun, owner.aggDef = 300, 75
     44 	owner.health, owner.nano = owner.maximumHealth(), owner.maximumNano()
     45 	owner.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("vektor-owner")))
     46 	if owner.trainingMaximum(115) < 300 {
     47 		t.Fatal("controlled Shotgun fixture exceeds the native training cap")
     48 	}
     49 	watcher := world.newCharacter()
     50 	watcher.name, watcher.position = "Vektorwatch", target.position
     51 	watcher.position[0] -= 3
     52 	watcher.position[2] -= 5
     53 	watcher.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("vektor-watch")))
     54 	for _, player := range []*character{owner, watcher} {
     55 		if err := world.saveUser(player); err != nil {
     56 			t.Fatal(err)
     57 		}
     58 		if err := world.savePosition(player); err != nil {
     59 			t.Fatal(err)
     60 		}
     61 	}
     62 
     63 	// Seed legal selected-quality weapons and ammunition, not earned progression.
     64 	for _, item := range []struct{ template, quality uint32 }{
     65 		{124256, 2}, {124259, 37}, {124261, 75}, {124260, 125}, {21609, 1}, {21605, 1}, {21613, 1},
     66 	} {
     67 		if granted, err := world.grantItem(owner, item.template); err != nil || !granted {
     68 			t.Fatal(granted, err)
     69 		}
     70 		owner.inventory.Items[len(owner.inventory.Items)-1].Quality = item.quality
     71 	}
     72 	for index := 4; index < 7; index++ {
     73 		owner.inventory.Items[index].Count = 100
     74 	}
     75 	if err := world.saveInventory(owner, owner.inventory); err != nil {
     76 		t.Fatal(err)
     77 	}
     78 	profile := *target.combatProfile
     79 	profile.Health = 10000
     80 	target.combatProfile, target.health = &profile, 10000
     81 	world.combatRoll = landedCombatRoll
     82 	// Keep the controlled target stationary and damage observable.
     83 	target.motion.roaming = false
     84 	target.regenerationAt = time.Now().Add(3 * 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 := world.characters[1]
     91 		target := world.characters[targetID]
     92 		json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "psychic": owner.equipmentBonus(21),
     93 			"target": target.id, "health": target.health, "recharge": owner.rechargeUntil, "fighting": owner.fighting})
     94 	}))
     95 	host := httptest.NewServer(mux)
     96 	defer host.Close()
     97 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     98 	defer cancel()
     99 
    100 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_vektor.mjs"), host.URL, output, "live")
    101 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    102 	err := command.Run()
    103 	host.Close()
    104 	if err := shutdown(); err != nil {
    105 		t.Fatal(err)
    106 	}
    107 	cancel()
    108 	t.Logf("Browser artifacts: %s", output)
    109 	if err != nil {
    110 		t.Fatal(err)
    111 	}
    112 	host.Close()
    113 	cold := newServerForTest(t)
    114 
    115 	if !reflect.DeepEqual(owner.inventory, cold.characters[owner.id].inventory) || owner.equipmentBonus(21) != 15 ||
    116 		cold.characters[owner.id].equipmentBonus(21) != 15 || owner.inventory.Items[1].Ammo != 39 || owner.inventory.Items[4].Count != 60 {
    117 		t.Fatal("cold weapon identity, loaded rounds, energy stack or Psychic bonus differs")
    118 	}
    119 	startTestService(t, cold)
    120 	coldHost := httptest.NewServer(cold.httpHandler())
    121 	defer coldHost.Close()
    122 	coldContext, coldCancel := context.WithTimeout(context.Background(), time.Minute)
    123 	defer coldCancel()
    124 	command = exec.CommandContext(coldContext, "node", filepath.Join(root, "tools/client/test_vektor.mjs"), coldHost.URL, filepath.Join(output, "cold"), "cold")
    125 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    126 	if err := command.Run(); err != nil {
    127 		t.Fatal(err)
    128 	}
    129 }