server/browser_health_drain_test.go (3951B)
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 "slices" 15 "testing" 16 "time" 17 ) 18 19 func TestBrowserHealthDrain(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the Trader health-drain contract") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/health-drain-nanos", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 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 var players []*character 37 for index := range 2 { 38 player := world.newCharacter() 39 player.level, player.profession = 2, 7 40 player.stats.NanoAiding.BiologicalMetamorphosis, player.stats.NanoAiding.TimeAndSpace = 6, 8 41 player.stats.Body.BodyDevelopment, player.stats.Body.NanoPool = 10, 10 42 for _, trained := range [][2]uint32{{128, 6}, {131, 8}, {152, 10}, {132, 10}} { 43 if player.trainingMaximum(trained[0]) < trained[1] { 44 t.Fatal("health-drain fixture exceeds native training caps") 45 } 46 } 47 player.position = enemy.position 48 player.position[2] -= float32(4 + index*3) 49 player.health, player.nano = 20, 30 50 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("health-drain-", index)))) 51 if err := world.saveUser(player); err != nil { 52 t.Fatal(err) 53 } 54 players = append(players, player) 55 } 56 if granted, err := world.grantItem(players[0], 78417); err != nil || !granted { 57 t.Fatal(granted, err) 58 } 59 enemyID := enemy.id 60 enemy.motion.roaming = false 61 enemy.regenerationAt = time.Now().Add(5 * time.Minute) 62 enemy.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 enemy := world.characters[enemyID] 69 json.NewEncoder(w).Encode(map[string]any{"owner": world.characters[1].id, "observer": world.characters[2].id, "enemy": enemy.id, 70 "health": world.characters[1].health, "nano": world.characters[1].nano, "targetHealth": enemy.health, 71 "reserved": world.characters[1].nanoReserved, "recharge": world.characters[1].nanoRechargeUntil, 72 "inventory": world.characters[1].inventory, "peerHealth": world.characters[2].health, "peerNano": world.characters[2].nano}) 73 })) 74 host := httptest.NewServer(mux) 75 t.Cleanup(host.Close) 76 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 77 defer cancel() 78 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_health_drain.mjs"), host.URL, output) 79 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 80 err := command.Run() 81 host.Close() 82 if err := shutdown(); err != nil { 83 t.Fatal(err) 84 } 85 cancel() 86 t.Logf("Browser artifacts: %s", output) 87 88 if err != nil { 89 t.Fatal(err) 90 } 91 cold := newTestInventoryServer(t) 92 if players[0].health != 24 || players[0].nano != 15 || enemy.health != 10 || 93 cold.characters[players[0].id].health != 24 || cold.characters[players[0].id].nano != 15 || cold.characters[enemy.id].health != 10 || 94 !slices.Equal(cold.characters[players[0].id].inventory.LearnedNanos, []uint32{76742}) || 95 len(cold.characters[players[0].id].inventory.Items) != 0 || 96 !cold.characters[players[0].id].nanoRechargeUntil.Equal(players[0].nanoRechargeUntil) || 97 players[1].health != 20 || players[1].nano != 30 { 98 t.Fatal("native drain lost an effect, cost, upload or peer isolation through cold recovery") 99 } 100 }