server/browser_player_recovery_test.go (5305B)
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 TestBrowserPlayerCombatRecovery(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for player recovery with queued incoming hits") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/player-combat-recovery", time.Now().Format("20060102-150405")) 25 for _, bodyDevelopment := range []uint16{10, 80} { 26 t.Run(fmt.Sprint(bodyDevelopment), func(t *testing.T) { 27 world := newTestServer(t) 28 var npc *character 29 for _, actor := range world.characters { 30 if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (npc == nil || actor.id < npc.id) { 31 npc = actor 32 } 33 } 34 if npc == nil { 35 t.Fatal("no admitted level-one Leet") 36 } 37 npc.playfield, npc.position, npc.rotation = 705, [3]float32{960, 16.5, 514}, [4]float32{0, 0, 0, 1} 38 npc.motion = nil 39 world.characters = map[uint32]*character{npc.id: npc} 40 for index := 0; index < 2; index++ { 41 player := world.newCharacter() 42 player.level, player.profession = 20, 9 43 player.playfield, player.position = 705, [3]float32{960 + float32(index*2), 16.5, 511} 44 player.rotation = [4]float32{0, 0, 0, 1} 45 player.stats = testCharacterStats() 46 player.stats.Body.BodyDevelopment, player.health = bodyDevelopment, 16 47 if bodyDevelopment == 80 { 48 player.stats.Abilities.Stamina = 41 49 } 50 if uint32(bodyDevelopment) > player.trainingMaximum(152) || uint32(player.stats.Abilities.Stamina) > player.trainingMaximum(18) { 51 t.Fatal("recovery fixture exceeds the recovered training caps") 52 } 53 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("player-recovery-", index)))) 54 if err := world.saveUser(player); err != nil { 55 t.Fatal(err) 56 } 57 } 58 player := world.characters[1] 59 npcID := npc.id 60 npc.regenerationAt = time.Now().Add(5 * time.Minute) 61 world.characters[1].rechargeUntil = time.Now().Add(5 * time.Minute) 62 npc.rechargeUntil = time.Now().Add(5 * time.Minute) 63 shutdown := startTestService(t, world) 64 mux := http.NewServeMux() 65 mux.Handle("/", world.httpHandler()) 66 mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery)) 67 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 68 player := world.characters[1] 69 npc := world.characters[npcID] 70 json.NewEncoder(w).Encode(map[string]any{"health": player.health, "maximum": player.maximumHealth(), 71 "bodyDevelopment": player.stats.Body.BodyDevelopment, "npc": npc.id, "target": npc.fighting, 72 "nextAttack": npc.nextAttack, "rechargeUntil": npc.rechargeUntil, "ownerTarget": player.fighting, 73 "counter": world.clients[1].regenerationSeconds}) 74 })) 75 // Establish incoming combat and its ordinary baseline before either hit. 76 mux.HandleFunc("POST /test-engage", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 77 updates := make(map[*worldClient][][]byte) 78 world.startAttack(npc, player, time.Now(), updates) 79 world.queueHealthSnapshot(updates, player) 80 world.sendCombatUpdates(updates) 81 })) 82 mux.HandleFunc("POST /test-swing", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 83 npc := world.characters[npcID] 84 deadline := npc.nextAttack 85 world.advanceCombat(deadline) 86 })) 87 // Advance one real recovery check without waiting for its full interval. 88 mux.HandleFunc("POST /test-recover", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 89 player := world.characters[1] 90 now := time.Now() 91 interval, _ := player.regenerationIntervals() 92 world.clients[1].regenerationAt = now.Add(-2 * time.Second) 93 world.clients[1].regenerationSeconds = interval*2 - 2 94 world.advanceRegeneration(now) 95 world.clients[1].regenerationAt = now.Add(5 * time.Minute) 96 })) 97 mux.HandleFunc("POST /test-stop", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 98 updates := make(map[*worldClient][][]byte) 99 world.stopAttack(npc, updates) 100 world.queueQuietHealth(updates, player) 101 world.sendCombatUpdates(updates) 102 })) 103 host := httptest.NewServer(mux) 104 t.Cleanup(host.Close) 105 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 106 defer cancel() 107 108 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_player_recovery.mjs"), host.URL, 109 filepath.Join(output, fmt.Sprint(bodyDevelopment))) 110 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 111 if err := command.Run(); err != nil { 112 t.Fatal(err) 113 } 114 host.Close() 115 if err := shutdown(); err != nil { 116 t.Fatal(err) 117 } 118 cold := newTestInventoryServer(t) 119 120 if player.health != 3 || cold.characters[1].health != 3 || npc.fighting != 0 { 121 t.Fatal("player recovery did not retain exact health through stop/page/cold replay") 122 } 123 t.Logf("Browser artifacts: %s/%d", output, bodyDevelopment) 124 }) 125 } 126 }