aorecomp

Anarchy Online classic in the browser

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


server/browser_inspection_health_test.go (3019B)


      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 	"testing"
     15 	"time"
     16 )
     17 
     18 func TestBrowserInspectionQueuedHit(t *testing.T) {
     19 	if os.Getenv("AO_GAME_TEST") != "1" {
     20 		t.Skip("set AO_GAME_TEST=1 for inspection during a queued hit")
     21 	}
     22 
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/inspection-health", time.Now().Format("20060102-150405"))
     26 	world := newTestServer(t)
     27 	var npc *character
     28 	for _, actor := range world.characters {
     29 		if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (npc == nil || actor.id < npc.id) {
     30 			npc = actor
     31 		}
     32 	}
     33 	if npc == nil {
     34 		t.Fatal("no level-one Leet for inspection reproduction")
     35 	}
     36 	for index := 0; index < 2; index++ {
     37 		player := world.newCharacter()
     38 		player.level, player.profession, player.appearance = 10, 9, 0x315
     39 		player.position, player.rotation = npc.position, [4]float32{0, 0, 0, 1}
     40 		player.position[2] -= float32(2 + index*3)
     41 		player.stats.Body.BodyDevelopment = 55
     42 		player.health = 200
     43 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("inspection-", index))))
     44 		if err := world.saveUser(player); err != nil {
     45 			t.Fatal(err)
     46 		}
     47 	}
     48 	npcID := npc.id
     49 	npc.regenerationAt = time.Now().Add(5 * time.Minute)
     50 	npc.motion.roaming = false
     51 	shutdown := startTestService(t, world)
     52 	mux := http.NewServeMux()
     53 	mux.Handle("/", world.httpHandler())
     54 	mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     55 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     56 		npc := world.characters[npcID]
     57 		json.NewEncoder(w).Encode(map[string]any{"health": world.characters[1].health, "npc": npc.id,
     58 			"nextAttack": npc.nextAttack, "rechargeUntil": npc.rechargeUntil, "fighting": npc.fighting})
     59 	}))
     60 	// Advance one real swing; no later hits or regeneration obscure inspection.
     61 	mux.HandleFunc("POST /test-attack", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 		npc := world.characters[npcID]
     63 		updates := make(map[*worldClient][][]byte)
     64 		world.startAttack(npc, world.characters[1], time.Now(), updates)
     65 		deadline := npc.nextAttack
     66 		world.sendCombatUpdates(updates)
     67 		world.advanceCombat(deadline)
     68 		npc.nextAttack = time.Now().Add(5 * time.Minute)
     69 	}))
     70 	host := httptest.NewServer(mux)
     71 	t.Cleanup(host.Close)
     72 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     73 	defer cancel()
     74 
     75 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_inspection_health.mjs"), host.URL, output)
     76 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     77 	err := command.Run()
     78 	host.Close()
     79 	if err := shutdown(); err != nil {
     80 		t.Fatal(err)
     81 	}
     82 
     83 	if err != nil {
     84 		t.Fatal(err)
     85 	}
     86 	t.Logf("Browser artifacts: %s", output)
     87 }