server/browser_traded_weapon_test.go (5069B)
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 TestBrowserTradedWeapon(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the focused native traded-weapon check") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/traded-weapon", 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 if phase == "warm" { 37 for index := 0; index < 2; index++ { 38 player := world.newCharacter() 39 player.name, player.appearance = fmt.Sprint("Trader", index), 0x515 40 player.position = [3]float32{60, 14.75, 53 - float32(index)*3} 41 player.rotation = [4]float32{0, 1, 0, 0} 42 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("traded-weapon-", index)))) 43 player.level = 200 44 player.stats.Abilities = characterAbilitiesStats{300, 300, 300, 300, 300, 300} 45 player.stats.Ranged.SubmachineGun, player.stats.Ranged.Burst = 400, 283 46 for stat, raw := range map[uint32]uint16{114: 400, 148: 283} { 47 if uint32(raw) > player.trainingMaximum(stat) { 48 t.Fatal("controlled raw skill exceeds its native cap", stat) 49 } 50 } 51 for stat, expected := range map[uint32]int32{114: 474, 148: 357} { 52 if actual, _ := player.effectiveStat(stat); actual != expected { 53 t.Fatal("controlled effective skill differs", stat, actual) 54 } 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 // A loaded selected-quality weapon and separate ammunition isolate ownership. 62 for _, template := range []uint32{160477, 21605} { 63 if granted, err := world.grantItem(world.characters[1], template); err != nil || !granted { 64 t.Fatal(granted, err) 65 } 66 } 67 world.characters[1].inventory.Items[0].Quality = 50 68 world.characters[1].inventory.Items[0].Ammo = 13 69 if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil { 70 t.Fatal(err) 71 } 72 target.position, target.motion.home = [3]float32{61, 14.75, 48.5}, [3]float32{61, 14.75, 48.5} 73 world.combatRoll = func(limit uint32) uint32 { 74 if limit == 100 { 75 return 99 76 } 77 return 0 78 } 79 } 80 owner, watcher := world.characters[1], world.characters[2] 81 targetID := target.id 82 if target.motion != nil { 83 target.motion.roaming = false 84 } 85 target.regenerationAt = time.Now().Add(5 * time.Minute) 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, target := world.characters[1], world.characters[2], world.characters[targetID] 91 json.NewEncoder(w).Encode(map[string]any{"owner": owner.inventory, "recipient": watcher.inventory, 92 "target": target.id, "health": target.health, "fighting": owner.fighting, "ownerHealth": owner.health, "ownerSense": owner.effectiveAbility(20), "recipientSense": watcher.effectiveAbility(20)}) 93 })) 94 host := httptest.NewServer(mux) 95 t.Cleanup(host.Close) 96 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 97 defer cancel() 98 99 if err := os.MkdirAll(filepath.Join(output, phase), 0755); err != nil { 100 t.Fatal(err) 101 } 102 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_traded_weapon.mjs"), host.URL, filepath.Join(output, phase)) 103 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 104 err := command.Run() 105 host.Close() 106 if err := shutdown(); err != nil { 107 t.Fatal(err) 108 } 109 cancel() 110 copyTestDatabase(t, filepath.Join(output, phase, "world.sqlite")) 111 t.Logf("Browser artifacts: %s", output) 112 113 if err != nil { 114 t.Fatal(err) 115 } 116 expected := []ownedItem{{ID: 1, Type: 51018, Template: 160477, Quality: 50, Slot: 6, Count: 1, Ammo: 12}} 117 if !slices.Equal(watcher.inventory.Items, expected) || 118 !slices.Equal(owner.inventory.Items, []ownedItem{{ID: 2, Type: 51005, Template: 21605, Quality: 1, Slot: 65, Count: 100}}) || 119 owner.inventory.Credits != 0 || watcher.inventory.Credits != 0 || newTestInventoryServer(t).nextItem != 2 || 120 owner.effectiveAbility(20) != 300 || watcher.effectiveAbility(20) != 302 { 121 t.Fatal("native weapon trade lost ownership, rounds or Sense", owner.inventory, watcher.inventory) 122 } 123 }) 124 if t.Failed() { 125 return 126 } 127 } 128 }