server/browser_armor_requirements_test.go (3318B)
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 TestBrowserArmorRequirementBoundaries(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for native armor requirement boundaries") 22 } 23 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/armor-requirement-boundaries", time.Now().Format("20060102-150405")) 27 world := newTestServer(t) 28 var cases []map[string]any 29 var worn []bool 30 for _, anchor := range []uint32{85481, 85690} { 31 for _, abilities := range [][2]uint16{{10, 10}, {11, 9}, {11, 10}, {12, 11}} { 32 player := world.newCharacter() 33 player.level = 2 34 player.stats.Abilities.Sense, player.stats.Abilities.Agility = abilities[0], abilities[1] 35 token := fmt.Sprint("armor-requirements-", player.id) 36 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(token))) 37 if err := world.saveUser(player); err != nil { 38 t.Fatal(err) 39 } 40 if granted, err := world.grantItem(player, anchor); err != nil || !granted { 41 t.Fatal(granted, err) 42 } 43 inventory := player.inventory 44 inventory.Items = slices.Clone(inventory.Items) 45 inventory.Items[0].Quality = 2 46 if err := world.saveInventory(player, inventory); err != nil { 47 t.Fatal(err) 48 } 49 eligible := abilities[0] > 10 && abilities[1] > 9 50 cases = append(cases, map[string]any{"id": player.id, "token": token, "anchor": anchor, 51 "sense": abilities[0], "agility": abilities[1], "eligible": eligible}) 52 worn = append(worn, eligible) 53 } 54 } 55 actors := world.characters 56 shutdown := startTestService(t, world) 57 mux := http.NewServeMux() 58 mux.Handle("/", world.httpHandler()) 59 mux.HandleFunc("GET /test-cases", func(w http.ResponseWriter, r *http.Request) { 60 json.NewEncoder(w).Encode(cases) 61 }) 62 mux.HandleFunc("GET /test-state/{character}", world.playerHTTP(func(world *server, w http.ResponseWriter, r *http.Request) { 63 var id uint32 64 fmt.Sscan(r.PathValue("character"), &id) 65 json.NewEncoder(w).Encode(world.characters[id].inventory) 66 })) 67 host := httptest.NewServer(mux) 68 t.Cleanup(host.Close) 69 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 70 defer cancel() 71 72 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_armor_requirements.mjs"), host.URL, output) 73 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 74 err := command.Run() 75 host.Close() 76 if err := shutdown(); err != nil { 77 t.Fatal(err) 78 } 79 t.Logf("Armor requirement artifacts: %s", output) 80 81 if err != nil { 82 t.Fatal(err) 83 } 84 cold := newTestInventoryServer(t) 85 for index, eligible := range worn { 86 id, slot := uint32(index+1), uint32(64) 87 if eligible { 88 slot = 21 89 } 90 anchor := uint32(85481) 91 if index >= 4 { 92 anchor = 85690 93 } 94 expected := []ownedItem{{ID: id, Type: 51022, Template: anchor, Quality: 2, Slot: slot, Count: 1}} 95 if !slices.Equal(actors[id].inventory.Items, expected) || !slices.Equal(cold.characters[id].inventory.Items, expected) { 96 t.Fatal("native boundary check changed ownership or selected quality", id) 97 } 98 } 99 if cold.nextItem != 8 { 100 t.Fatal("native boundary check changed item allocation") 101 } 102 }