aorecomp

Anarchy Online classic in the browser

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


server/browser_brawl_test.go (4307B)


      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 TestBrowserBrawl(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for native fists and stocked Sledgehammer Brawl")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/alpha-brawl", time.Now().Format("20060102-150405"))
     26 	for _, name := range []string{"fists", "sledgehammer"} {
     27 		t.Run(name, func(t *testing.T) {
     28 			world := newTestServer(t)
     29 			fields := creationForm()
     30 			fields.Set("profession", "2")
     31 			if response := creationRequest(world, accountVisitor(t, world), fields); response.Code != http.StatusNoContent {
     32 				t.Fatal(response.Code, response.Body.String())
     33 			}
     34 			player := world.characters[2]
     35 			damage := 9
     36 			if name == "sledgehammer" {
     37 				// The stocked QL10 upgrade uses the existing cap-checked checkpoint.
     38 				trainBasicShopWeaponFixture(t, player)
     39 				player.inventory.Items[3].Type, player.inventory.Items[3].Template, player.inventory.Items[3].Quality = 51018, 129026, 10
     40 				damage = 33
     41 			}
     42 			world.combatRoll = func(uint32) uint32 { return 0 }
     43 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("brawl")))
     44 			if err := world.saveUser(player); err != nil {
     45 				t.Fatal(err)
     46 			}
     47 			if err := world.saveInventory(player, player.inventory); err != nil {
     48 				t.Fatal(err)
     49 			}
     50 			var target *character
     51 			for _, actor := range world.characters {
     52 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     53 					target = actor
     54 				}
     55 			}
     56 			if target == nil {
     57 				t.Fatal("original Leet unavailable")
     58 			}
     59 			forward, _ := bodyAxes(player.rotation)
     60 			target.position = [3]float32{player.position[0] + 2*forward[0], player.position[1], player.position[2] + 2*forward[2]}
     61 			target.health = 1000
     62 			if err := world.flushWorld(); err != nil {
     63 				t.Fatal(err)
     64 			}
     65 			// Keep target attacks and recovery beyond this focused special-action check.
     66 			target.rechargeUntil = time.Now().Add(5 * time.Minute)
     67 			target.regenerationAt = target.rechargeUntil
     68 			target.motion.roaming = false
     69 			targetID := target.id
     70 			shutdown := startTestService(t, world)
     71 			mux := http.NewServeMux()
     72 			mux.Handle("/", world.httpHandler())
     73 			// Suppress ordinary player attacks only after the real equipment action.
     74 			mux.HandleFunc("POST /test-hold-attacks", testPlayerHTTP(world, 2, func(world *server, w http.ResponseWriter, r *http.Request) {
     75 				world.characters[2].rechargeUntil = time.Now().Add(5 * time.Minute)
     76 				w.WriteHeader(http.StatusNoContent)
     77 			}))
     78 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 2, func(world *server, w http.ResponseWriter, r *http.Request) {
     79 				player, target := world.characters[2], world.characters[targetID]
     80 				json.NewEncoder(w).Encode(map[string]any{"target": target.id, "health": target.health, "fighting": player.fighting,
     81 					"locked": time.Now().Before(player.skillLocks[142]), "inventory": player.inventory, "level": player.level,
     82 					"ip": player.stats.ImprovementPoints, "damage": damage, "weapon": name, "stunned": target.brawlStunned()})
     83 			}))
     84 			host := httptest.NewServer(mux)
     85 			defer host.Close()
     86 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     87 			defer cancel()
     88 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_brawl.mjs"), host.URL, filepath.Join(output, name))
     89 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     90 			err := command.Run()
     91 			cancel()
     92 			host.Close()
     93 			if err := shutdown(); err != nil {
     94 				t.Fatal(err)
     95 			}
     96 			t.Logf("Browser artifacts: %s", filepath.Join(output, name))
     97 			if err != nil {
     98 				t.Fatal(err)
     99 			}
    100 			cold := newTestInventoryServer(t)
    101 			if cold.characters[target.id].health != uint32(1000-2*damage) || !reflect.DeepEqual(cold.characters[2].inventory, player.inventory) ||
    102 				!cold.characters[2].skillLocks[142].Equal(player.skillLocks[142]) || cold.characters[2].skillLocks[142].IsZero() {
    103 				t.Fatal("native Brawl damage, ownership or lock changed on cold recovery")
    104 			}
    105 		})
    106 	}
    107 }