aorecomp

Anarchy Online classic in the browser

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


server/browser_npc_recovery_test.go (4185B)


      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 TestBrowserNPCCombatRecovery(t *testing.T) {
     19 	if os.Getenv("AO_GAME_TEST") != "1" {
     20 		t.Skip("set AO_GAME_TEST=1 for NPC recovery with queued weapon hits")
     21 	}
     22 
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/npc-combat-recovery", 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 admitted level-one Leet")
     35 	}
     36 	npc.playfield, npc.position, npc.rotation = 705, [3]float32{960, 16.5, 514}, [4]float32{0, 0, 0, 1}
     37 	npc.health, npc.motion = 10, nil
     38 	world.characters = map[uint32]*character{npc.id: npc}
     39 	for index := 0; index < 2; index++ {
     40 		player := world.newCharacter()
     41 		player.playfield, player.position = 705, [3]float32{960 + float32(index*2), 16.5, 511}
     42 		player.rotation = [4]float32{0, 0, 0, 1}
     43 		player.stats = testCharacterStats()
     44 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("npc-recovery-", index))))
     45 		if err := world.saveUser(player); err != nil {
     46 			t.Fatal(err)
     47 		}
     48 	}
     49 	if err := world.flushWorld(); err != nil {
     50 		t.Fatal(err)
     51 	}
     52 	npcID := npc.id
     53 	npc.regenerationAt = time.Now().Add(5 * time.Minute)
     54 	world.characters[1].rechargeUntil = time.Now().Add(5 * time.Minute)
     55 	npc.rechargeUntil = time.Now().Add(5 * time.Minute)
     56 	shutdown := startTestService(t, world)
     57 	mux := http.NewServeMux()
     58 	mux.Handle("/", world.httpHandler())
     59 	mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     60 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     61 		npc := world.characters[npcID]
     62 		json.NewEncoder(w).Encode(map[string]any{"health": npc.health, "npc": npc.id,
     63 			"npcTarget": npc.fighting, "npcAttack": npc.nextAttack, "npcRecharge": npc.rechargeUntil,
     64 			"ownerTarget": world.characters[1].fighting, "ownerAttack": world.characters[1].nextAttack,
     65 			"ownerRecharge": world.characters[1].rechargeUntil, "counter": npc.regenerationSeconds})
     66 	}))
     67 	// Enter combat before the baseline snapshot so idle prediction cannot obscure it.
     68 	mux.HandleFunc("POST /test-engage", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     69 		updates := make(map[*worldClient][][]byte)
     70 		world.startAttack(npc, world.characters[1], time.Now(), updates)
     71 		world.queueHealthSnapshot(updates, npc)
     72 		world.sendCombatUpdates(updates)
     73 	}))
     74 	// Advance only the admitted attack's next outcome; no gameplay is earned here.
     75 	mux.HandleFunc("POST /test-swing", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     76 		deadline := world.characters[1].nextAttack
     77 		world.advanceCombat(deadline)
     78 	}))
     79 	// Place the real recovery scheduler one check before its combat tick.
     80 	mux.HandleFunc("POST /test-recover", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     81 		npc := world.characters[npcID]
     82 		now := time.Now()
     83 		npc.regenerationAt, npc.regenerationSeconds = now.Add(-2*time.Second), 58
     84 		world.advanceRegeneration(now)
     85 		npc.regenerationAt = now.Add(5 * time.Minute)
     86 	}))
     87 	host := httptest.NewServer(mux)
     88 	t.Cleanup(host.Close)
     89 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     90 	defer cancel()
     91 
     92 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_npc_recovery.mjs"), host.URL, output)
     93 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     94 	if err := command.Run(); err != nil {
     95 		t.Fatal(err)
     96 	}
     97 	host.Close()
     98 	if err := shutdown(); err != nil {
     99 		t.Fatal(err)
    100 	}
    101 
    102 	if npc.health != 0 || npc.corpse == nil || npc.regenerationSeconds != 60 {
    103 		t.Fatal("recovery regression did not reach the real final death")
    104 	}
    105 	t.Logf("Browser artifacts: %s", output)
    106 }