server/browser_home_defender_test.go (5351B)
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 TestBrowserHomeDefenderQualities(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the focused native HomeDefender check") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/home-defender", 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("HomeDefender", 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("home-defender-", index)))) 47 player.level = 100 48 player.stats.Abilities = characterAbilitiesStats{28, 28, 28, 28, 28, 28} 49 player.stats.Ranged.Shotgun = 32 50 if uint32(player.stats.Ranged.Shotgun) > player.trainingMaximum(115) { 51 t.Fatal("controlled Shotgun exceeds its native cap") 52 } 53 if actual, _ := player.effectiveStat(115); actual != 38 { 54 t.Fatal("controlled effective Shotgun differs", actual) 55 } 56 player.health, player.nano = player.maximumHealth(), player.maximumNano() 57 if err := world.saveUser(player); err != nil { 58 t.Fatal(err) 59 } 60 } 61 // Controlled contracts and a nearby original Leet isolate the weapon check. 62 for _, item := range []struct{ template, quality uint32 }{ 63 {124230, 2}, {124231, 7}, {124231, 8}, {124230, 9}, {21613, 1}, 64 } { 65 if granted, err := world.grantItem(world.characters[1], item.template); err != nil || !granted { 66 t.Fatal(granted, err) 67 } 68 world.characters[1].inventory.Items[len(world.characters[1].inventory.Items)-1].Quality = item.quality 69 } 70 if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil { 71 t.Fatal(err) 72 } 73 target.health = 100 74 target.position, target.motion.home = [3]float32{61, 14.75, 51.5}, [3]float32{61, 14.75, 51.5} 75 world.combatRoll = func(limit uint32) uint32 { 76 if limit == 100 { 77 return 99 78 } 79 return 0 80 } 81 } 82 // Keep the controlled target stationary and its hit damage observable. 83 target.motion.roaming = false 84 target.regenerationAt = time.Now().Add(3 * time.Minute) 85 targetID := target.id 86 shutdown := startTestService(t, world) 87 mux := http.NewServeMux() 88 mux.Handle("/", world.httpHandler()) 89 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 90 owner, watcher := world.characters[1], world.characters[2] 91 health := uint32(0) 92 if target := world.characters[targetID]; target != nil { 93 health = target.health 94 } 95 json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory, 96 "target": targetID, "health": health, "fighting": owner.fighting, "ownerHealth": owner.health}) 97 })) 98 host := httptest.NewServer(mux) 99 t.Cleanup(host.Close) 100 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 101 defer cancel() 102 103 if err := os.MkdirAll(filepath.Join(output, phase), 0755); err != nil { 104 t.Fatal(err) 105 } 106 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_home_defender.mjs"), host.URL, filepath.Join(output, phase)) 107 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 108 err := command.Run() 109 cancel() 110 host.Close() 111 if err := shutdown(); err != nil { 112 t.Fatal(err) 113 } 114 copyTestDatabase(t, filepath.Join(output, phase, "world.sqlite")) 115 // Check committed state after owners and transports have stopped. 116 cold := newTestInventoryServer(t) 117 owner, watcher := cold.characters[1], cold.characters[2] 118 119 t.Logf("Browser artifacts: %s", output) 120 121 if err != nil { 122 t.Fatal(err) 123 } 124 expected := []ownedItem{ 125 {ID: 2, Type: 51018, Template: 124231, Quality: 7, Slot: 65, Count: 1}, 126 {ID: 3, Type: 51018, Template: 124231, Quality: 8, Slot: 6, Count: 1, Ammo: 24}, 127 {ID: 4, Type: 51018, Template: 124230, Quality: 9, Slot: 67, Count: 1}, 128 {ID: 5, Type: 51005, Template: 21613, Quality: 1, Slot: 68, Count: 75}, 129 } 130 if !slices.Equal(owner.inventory.Items, expected) || owner.inventory.Credits != 113 || owner.health == 0 || 131 len(watcher.inventory.Items) != 0 || 132 watcher.inventory.Credits != 0 || cold.nextItem != 5 { 133 t.Fatal("native wear, sale or cold recovery lost an instance or cash", owner.inventory) 134 } 135 }) 136 if t.Failed() { 137 return 138 } 139 } 140 }