server/browser_reclaim_weapon_test.go (5668B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "context" 6 "crypto/sha256" 7 "encoding/json" 8 "fmt" 9 "net/http" 10 "net/http/httptest" 11 "os" 12 "os/exec" 13 "path/filepath" 14 "runtime" 15 "slices" 16 "testing" 17 "time" 18 ) 19 20 func TestBrowserReclaimWeapon(t *testing.T) { 21 if os.Getenv("AO_GAME_TEST") != "1" { 22 t.Skip("set AO_GAME_TEST=1 for the native retained-weapon reclaim check") 23 } 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/reclaim-weapon", time.Now().Format("20060102-150405")) 27 world := newTestServer(t) 28 player := world.newCharacter() 29 player.name, player.level, player.profession, player.appearance = "Reclaimweapon", 100, 7, 0x515 30 player.playfield, player.position = 705, [3]float32{598, 16.9, 331} 31 player.rotation = [4]float32{0, 0.70710677, 0, 0.70710677} 32 player.resurrectionTerminal, player.health, player.nano = 3221750465, 1, 1 33 player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200} 34 player.stats.Ranged.Shotgun = 300 35 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("reclaim-weapon"))) 36 if err := world.saveUser(player); err != nil { 37 t.Fatal(err) 38 } 39 if err := world.savePosition(player); err != nil { 40 t.Fatal(err) 41 } 42 for _, template := range []uint32{124261, 21609} { 43 if granted, err := world.grantItem(player, template); err != nil || !granted { 44 t.Fatal(granted, err) 45 } 46 } 47 player.inventory.Items[0].Quality, player.inventory.Items[0].Slot, player.inventory.Items[0].Ammo = 75, 6, 13 48 player.inventory.Items[1].Slot, player.inventory.Items[1].Count = 64, 100 49 if err := world.saveInventory(player, player.inventory); err != nil { 50 t.Fatal(err) 51 } 52 enemy := &character{id: 9000001, npc: true, name: "Reclaim checkpoint Leet", level: 1, 53 playfield: 705, position: [3]float32{598, 16.9, 333}, rotation: [4]float32{0, 0, 0, 1}, 54 monsterData: 17655, appearance: 229, health: 100, combatProfile: gamedata.NPCCombatProfiles[17655][1], geometry: world.geometry} 55 world.characters[enemy.id] = enemy 56 world.combatRoll = landedCombatRoll 57 shutdown := startTestService(t, world) 58 mux := http.NewServeMux() 59 mux.Handle("/", world.httpHandler()) 60 testState := func(world *server, w http.ResponseWriter, r *http.Request) { 61 player := world.characters[1] 62 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "health": player.health, 63 "playfield": player.playfield, "psychic": player.equipmentBonus(21), "shock": player.resurrectionShock, 64 "effective_psychic": player.effectiveAbility(21), "maximum_health": player.maximumHealth(), "maximum_nano": player.maximumNano()}) 65 } 66 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, testState)) 67 mux.HandleFunc("POST /test-fatal-hit", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 68 player := world.characters[1] 69 enemy := world.characters[9000001] 70 updates := make(map[*worldClient][][]byte) 71 world.startAttack(enemy, player, time.Now(), updates) 72 deadline := enemy.nextAttack 73 world.sendCombatUpdates(updates) 74 world.advanceCombat(deadline) 75 w.WriteHeader(http.StatusNoContent) 76 })) 77 mux.HandleFunc("POST /test-ready-reclaim", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 78 player := world.characters[1] 79 // Controlled clock setup isolates identity replay from the tested 75s delay. 80 inventory := player.inventory 81 for index := range inventory.Reclaim { 82 inventory.Reclaim[index].AvailableAt = time.Now().Add(-time.Second).UTC() 83 } 84 if err := world.saveInventory(player, inventory); err != nil { 85 t.Error(err) 86 w.WriteHeader(http.StatusInternalServerError) 87 return 88 } 89 w.WriteHeader(http.StatusNoContent) 90 })) 91 mux.HandleFunc("POST /test-last-shock-tick", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 92 player := world.characters[1] 93 // A controlled late-recovery checkpoint avoids six minutes of waiting. 94 player.resurrectionShock = 27 95 if err := world.savePosition(player); err != nil { 96 t.Error(err) 97 w.WriteHeader(http.StatusInternalServerError) 98 return 99 } 100 w.WriteHeader(http.StatusNoContent) 101 })) 102 host := httptest.NewServer(mux) 103 defer host.Close() 104 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 105 defer cancel() 106 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_reclaim_weapon.mjs"), host.URL, output, "live") 107 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 108 err := command.Run() 109 host.Close() 110 if err := shutdown(); err != nil { 111 t.Fatal(err) 112 } 113 cancel() 114 t.Logf("Browser artifacts: %s", output) 115 116 if err != nil { 117 t.Fatal(err) 118 } 119 cold := newServerForTest(t) 120 if !slices.Equal(player.inventory.Items, cold.characters[1].inventory.Items) || len(cold.characters[1].inventory.Reclaim) != 0 || cold.characters[1].equipmentBonus(21) != 15 || cold.characters[1].resurrectionShock != 0 { 121 t.Fatal("cold recovery changed the reclaimed weapon, ammunition or Psychic bonus") 122 } 123 startTestService(t, cold) 124 coldMux := http.NewServeMux() 125 coldMux.Handle("/", cold.httpHandler()) 126 coldMux.HandleFunc("GET /test-state", testPlayerHTTP(cold, 1, testState)) 127 coldHost := httptest.NewServer(coldMux) 128 defer coldHost.Close() 129 coldContext, coldCancel := context.WithTimeout(context.Background(), time.Minute) 130 defer coldCancel() 131 command = exec.CommandContext(coldContext, "node", filepath.Join(root, "tools/client/test_reclaim_weapon.mjs"), coldHost.URL, filepath.Join(output, "cold"), "cold") 132 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 133 if err := command.Run(); err != nil { 134 t.Fatal(err) 135 } 136 }