server/browser_remote_reload_test.go (4589B)
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 TestBrowserRemoteReload(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the two-client remote reload check") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 for _, count := range []uint16{100, 5} { 26 t.Run(fmt.Sprint(count), func(t *testing.T) { 27 output := filepath.Join(root, "local/remote-reload", time.Now().Format("20060102-150405"), fmt.Sprint(count)) 28 world := newTestServer(t) 29 var target *character 30 for _, actor := range world.characters { 31 if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) { 32 target = actor 33 } 34 } 35 if target == nil { 36 t.Fatal("no controlled Leet target") 37 } 38 for index := 0; index < 2; index++ { 39 player := world.newCharacter() 40 player.name, player.level, player.profession, player.appearance, player.aggDef = fmt.Sprint("Reload", index), 100, 6, 0x515, 75 41 player.position = target.position 42 player.position[0] -= float32(index * 3) 43 player.position[2] -= 2 44 player.rotation = [4]float32{0, 0, 0, 1} 45 player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200} 46 player.stats.Ranged.Pistol, player.stats.Ranged.FlingShot = 351, 23 47 player.health, player.nano = player.maximumHealth(), player.maximumNano() 48 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("remote-reload-", index)))) 49 if err := world.saveUser(player); err != nil { 50 t.Fatal(err) 51 } 52 for _, template := range []uint32{123747, 21605} { 53 if granted, err := world.grantItem(player, template); err != nil || !granted { 54 t.Fatal(granted, err) 55 } 56 } 57 player.inventory.Items[0].Quality, player.inventory.Items[0].Slot = 30, 6 58 player.inventory.Items[1].Slot = 64 59 player.inventory.Items[1].Count = count 60 if index == 1 { 61 player.inventory.Items[0].Ammo, player.inventory.Items[1].Count = 9, 7 62 } 63 if err := world.saveInventory(player, player.inventory); err != nil { 64 t.Fatal(err) 65 } 66 } 67 profile := *target.combatProfile 68 profile.Health = 10000 69 target.combatProfile, target.health = &profile, 10000 70 world.combatRoll = landedCombatRoll 71 targetID := target.id 72 if target.motion != nil { 73 target.motion.roaming = false 74 } 75 target.regenerationAt = time.Now().Add(5 * time.Minute) 76 observer := slices.Clone(world.characters[2].inventory.Items) 77 owner, watcher := world.characters[1], world.characters[2] 78 shutdown := startTestService(t, world) 79 mux := http.NewServeMux() 80 mux.Handle("/", world.httpHandler()) 81 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 82 target := world.characters[targetID] 83 json.NewEncoder(w).Encode(map[string]any{"owner": world.characters[1].inventory, 84 "observer": world.characters[2].inventory, "target": target.id, "health": target.health}) 85 })) 86 host := httptest.NewServer(mux) 87 t.Cleanup(host.Close) 88 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 89 defer cancel() 90 91 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_remote_reload.mjs"), host.URL, output) 92 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 93 err := command.Run() 94 host.Close() 95 if err := shutdown(); err != nil { 96 t.Fatal(err) 97 } 98 cancel() 99 100 t.Logf("Browser artifacts: %s", output) 101 if err != nil { 102 t.Fatal(err) 103 } 104 cold := newTestInventoryServer(t) 105 loaded, remaining := min(uint32(count), 20)-1, max(int(count)-20, 0) 106 expected := []ownedItem{{ID: 1, Type: 51018, Template: 123747, Quality: 30, Slot: 6, Count: 1, Ammo: loaded}} 107 if remaining != 0 { 108 expected = append(expected, ownedItem{ID: 2, Type: 51005, Template: 21605, Quality: 1, Slot: 64, Count: uint16(remaining)}) 109 } 110 for _, current := range [][2]*character{{owner, watcher}, {cold.characters[1], cold.characters[2]}} { 111 if !slices.Equal(current[0].inventory.Items, expected) || 112 !slices.Equal(current[1].inventory.Items, observer) || cold.nextItem != 4 || 113 current[0].inventory.Credits != 0 || current[1].inventory.Credits != 0 { 114 t.Fatal("remote reload changed another owner's weapon/stack or lost a committed round") 115 } 116 } 117 }) 118 } 119 }