server/browser_healing_test.go (4216B)
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 TestBrowserImprovedHealingQueuedHit(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for the focused native healing cast") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/direct-healing", time.Now().Format("20060102-150405")) 25 world := newTestServer(t) 26 world.combatRoll = landedCombatRoll 27 var enemy *character 28 for _, actor := range world.characters { 29 if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (enemy == nil || actor.id < enemy.id) { 30 enemy = actor 31 } 32 } 33 if enemy == nil { 34 t.Fatal("no controlled level-one Leet") 35 } 36 players := []*character{} 37 for index := 0; index < 2; index++ { 38 player := world.newCharacter() 39 player.name, player.level, player.profession = fmt.Sprint("Healing", index), 10, 10 40 player.stats = testCharacterStats() 41 player.stats.Body.BodyDevelopment = 55 42 player.stats.NanoAiding.BiologicalMetamorphosis, player.stats.NanoAiding.MatterMetamorphosis = 7, 5 43 player.position, player.rotation = enemy.position, [4]float32{0, 0, 0, 1} 44 player.position[2] -= float32(2 + index*3) 45 player.health, player.nano = 200, 50 46 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("healing-", index)))) 47 if err := world.saveUser(player); err != nil { 48 t.Fatal(err) 49 } 50 players = append(players, player) 51 } 52 if granted, err := world.grantItem(players[0], 28778); err != nil || !granted { 53 t.Fatal(granted, err) 54 } 55 enemyID := enemy.id 56 enemy.motion.roaming = false 57 enemy.regenerationAt = time.Now().Add(5 * time.Minute) 58 shutdown := startTestService(t, world) 59 mux := http.NewServeMux() 60 mux.Handle("/", world.httpHandler()) 61 mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery)) 62 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 63 enemy := world.characters[enemyID] 64 var cast any 65 if client := world.clients[world.characters[1].id]; client != nil && client.nanoCast != nil { 66 cast = map[string]any{"readyAt": client.nanoCast.readyAt} 67 } 68 json.NewEncoder(w).Encode(map[string]any{"owner": world.characters[1].id, "observer": world.characters[2].id, "enemy": enemy.id, 69 "health": world.characters[1].health, "nano": world.characters[1].nano, "reserved": world.characters[1].nanoReserved, "cast": cast, 70 "recharge": world.characters[1].nanoRechargeUntil, "inventory": world.characters[1].inventory, 71 "fighting": enemy.fighting, "nextAttack": enemy.nextAttack, "weaponRecharge": enemy.rechargeUntil}) 72 })) 73 // One controlled production swing near spell completion isolates queued-hit reconciliation. 74 mux.HandleFunc("POST /test-attack", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 75 enemy := world.characters[enemyID] 76 updates := make(map[*worldClient][][]byte) 77 world.startAttack(enemy, world.characters[1], time.Now(), updates) 78 deadline := enemy.nextAttack 79 world.sendCombatUpdates(updates) 80 world.advanceCombat(deadline) 81 enemy.nextAttack = time.Now().Add(5 * time.Minute) 82 w.WriteHeader(http.StatusNoContent) 83 })) 84 host := httptest.NewServer(mux) 85 defer host.Close() 86 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 87 defer cancel() 88 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_healing.mjs"), host.URL, output) 89 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 90 err := command.Run() 91 host.Close() 92 if err := shutdown(); err != nil { 93 t.Fatal(err) 94 } 95 cancel() 96 t.Logf("Browser artifacts: %s", output) 97 98 if err != nil { 99 t.Fatal(err) 100 } 101 cold := newTestInventoryServer(t).characters[players[0].id] 102 if cold.health != players[0].health || cold.nano != 37 || cold.nanoReserved != 0 || !cold.nanoRechargeUntil.Equal(players[0].nanoRechargeUntil) { 103 t.Fatal("completed native heal did not persist health, cost and recharge together") 104 } 105 }