server/browser_support_beam_test.go (5819B)
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 TestBrowserSupportBeamQualities(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the focused native SupportBeam check") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/support-beam", time.Now().Format("20060102-150405")) 26 t.Chdir(t.TempDir()) 27 for _, phase := range []string{"warm", "cold"} { 28 t.Run(phase, func(t *testing.T) { 29 world := newServerForTest(t) 30 var target *character 31 for _, actor := range world.characters { 32 if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) { 33 target = actor 34 } 35 } 36 // Use a controlled 100-HP target for exact selected-quality damage. 37 profile := *target.combatProfile 38 profile.Health = 100 39 target.combatProfile = &profile 40 if phase == "warm" { 41 for index := 0; index < 2; index++ { 42 player := world.newCharacter() 43 player.name, player.appearance = fmt.Sprint("SupportBeam", index), 0x515 44 player.position = [3]float32{60 - float32(index)*3, 14.75, 53} 45 player.rotation = [4]float32{0, 1, 0, 0} 46 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("support-beam-", index)))) 47 if index == 0 { 48 player.appearance = 0x545 49 } 50 player.level = 100 51 player.stats.Abilities = characterAbilitiesStats{28, 28, 28, 28, 28, 28} 52 player.stats.Melee.TwoHandBlunt = 51 53 player.stats.Body.Brawling, player.stats.Body.Dimach = 30, 10 54 for stat, raw := range map[uint32]uint16{107: player.stats.Melee.TwoHandBlunt, 142: player.stats.Body.Brawling, 144: player.stats.Body.Dimach} { 55 if uint32(raw) > player.trainingMaximum(stat) { 56 t.Fatal("controlled raw skill exceeds its cap", stat, raw, player.trainingMaximum(stat)) 57 } 58 } 59 for stat, expected := range map[uint32]int32{107: 57, 142: 36, 144: 16} { 60 if actual, _ := player.effectiveStat(stat); actual != expected { 61 t.Fatal("controlled effective skill differs", stat, actual) 62 } 63 } 64 player.health, player.nano = player.maximumHealth(), player.maximumNano() 65 if err := world.saveUser(player); err != nil { 66 t.Fatal(err) 67 } 68 } 69 // Controlled contracts and a nearby original Leet isolate the weapon check. 70 for _, item := range []struct{ template, quality uint32 }{ 71 {122905, 2}, {122906, 10}, {122906, 11}, {122905, 12}, 72 } { 73 if granted, err := world.grantItem(world.characters[1], item.template); err != nil || !granted { 74 t.Fatal(granted, err) 75 } 76 world.characters[1].inventory.Items[len(world.characters[1].inventory.Items)-1].Quality = item.quality 77 } 78 if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil { 79 t.Fatal(err) 80 } 81 if granted, err := world.grantItem(world.characters[2], 122905); err != nil || !granted { 82 t.Fatal(granted, err) 83 } 84 world.characters[2].inventory.Items[0].Quality = 2 85 if err := world.saveInventory(world.characters[2], world.characters[2].inventory); err != nil { 86 t.Fatal(err) 87 } 88 target.health = 100 89 target.position, target.motion.home = [3]float32{61, 14.75, 51.5}, [3]float32{61, 14.75, 51.5} 90 world.combatRoll = func(limit uint32) uint32 { 91 if limit == 100 { 92 return 99 93 } 94 return 0 95 } 96 } 97 // Keep the controlled target stationary and its hit damage observable. 98 target.motion.roaming = false 99 target.regenerationAt = time.Now().Add(3 * time.Minute) 100 targetID := target.id 101 shutdown := startTestService(t, world) 102 mux := http.NewServeMux() 103 mux.Handle("/", world.httpHandler()) 104 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 105 owner, watcher := world.characters[1], world.characters[2] 106 health := uint32(0) 107 if target := world.characters[targetID]; target != nil { 108 health = target.health 109 } 110 json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory, 111 "target": targetID, "health": health, "fighting": owner.fighting, "ownerHealth": owner.health}) 112 })) 113 host := httptest.NewServer(mux) 114 t.Cleanup(host.Close) 115 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 116 defer cancel() 117 118 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_support_beam.mjs"), host.URL, filepath.Join(output, phase)) 119 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 120 err := command.Run() 121 host.Close() 122 if failure := shutdown(); failure != nil { 123 t.Fatal(failure) 124 } 125 t.Logf("Browser artifacts: %s", output) 126 127 if err != nil { 128 t.Fatal(err) 129 } 130 world = newTestInventoryServer(t) 131 owner, watcher := world.characters[1], world.characters[2] 132 expected := []ownedItem{ 133 {ID: 2, Type: 51018, Template: 122906, Quality: 10, Slot: 65, Count: 1}, 134 {ID: 3, Type: 51018, Template: 122906, Quality: 11, Slot: 6, Count: 1}, 135 {ID: 4, Type: 51018, Template: 122905, Quality: 12, Slot: 67, Count: 1}, 136 } 137 if !slices.Equal(owner.inventory.Items, expected) || owner.inventory.Credits != 159 || owner.health == 0 || 138 !slices.Equal(watcher.inventory.Items, []ownedItem{{ID: 5, Type: 51018, Template: 122905, Quality: 2, Slot: 64, Count: 1}}) || 139 watcher.inventory.Credits != 0 || world.nextItem != 5 { 140 t.Fatal("native wear, sale or cold recovery lost an instance or cash", owner.inventory) 141 } 142 }) 143 if t.Failed() { 144 return 145 } 146 } 147 }