server/browser_graft_organic_test.go (6381B)
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 "reflect" 14 "runtime" 15 "testing" 16 "time" 17 ) 18 19 func TestBrowserGraftAndOrganicArmorQualities(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for Graft and Organic native armor checks") 22 } 23 stamp := time.Now().Format("20060102-150405") 24 for _, family := range []struct { 25 name string 26 pairs [6][2]uint32 27 stamina, strength uint16 28 ip, wallet uint32 29 armor [8]int32 30 }{ 31 {name: "graft", pairs: [6][2]uint32{{120579, 142679}, {120565, 142681}, {120581, 142683}, {120582, 142685}, {120583, 142687}, {120584, 142689}}, stamina: 278, strength: 253, ip: 938, wallet: 6302, armor: [8]int32{138, 156, 156, 120, 103, 35, 138, 138}}, 32 {name: "organic", pairs: [6][2]uint32{{120585, 142680}, {120586, 142682}, {120587, 142684}, {120588, 142686}, {120589, 142688}, {120590, 142690}}, stamina: 273, strength: 248, ip: 958, wallet: 5593, armor: [8]int32{138, 156, 156, 120, 35, 35, 138, 138}}, 33 } { 34 t.Run(family.name, func(t *testing.T) { 35 _, source, _, _ := runtime.Caller(0) 36 root := filepath.Dir(filepath.Dir(source)) 37 output := filepath.Join(root, "local/graft-organic-armor", stamp, family.name) 38 t.Chdir(t.TempDir()) 39 world := newServerForTest(t) 40 owner := world.newCharacter() 41 owner.name, owner.level, owner.appearance = family.name+"owner", 100, 0x515 42 owner.playfield, owner.position = 1183, [3]float32{188, 5.11, 139.5} 43 owner.rotation = [4]float32{0, 0.70710677, 0, 0.70710677} 44 owner.stats.Abilities.Stamina, owner.stats.Abilities.Strength = family.stamina, family.strength 45 owner.stats.ImprovementPoints, owner.inventory.Credits = 2000, 100 46 owner.health, owner.nano = owner.maximumHealth(), owner.maximumNano() 47 owner.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("armor-owner"))) 48 watcher := world.newCharacter() 49 watcher.name, watcher.playfield, watcher.position = family.name+"watch", 1183, [3]float32{186, 5.11, 134.5} 50 watcher.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("armor-watch"))) 51 for _, player := range []*character{owner, watcher} { 52 if err := world.saveUser(player); err != nil { 53 t.Fatal(err) 54 } 55 } 56 // Controlled contracts isolate selected qualities without earning levels or gear. 57 for _, item := range []struct{ template, quality, slot uint32 }{ 58 {family.pairs[1][0], 2, 18}, {family.pairs[0][0], 2, 27}, {family.pairs[3][0], 2, 30}, 59 {family.pairs[5][0], 2, 23}, {family.pairs[5][0], 2, 25}, {family.pairs[2][0], 2, 24}, {family.pairs[4][0], 2, 21}, 60 {family.pairs[2][1], 100, 64}, {family.pairs[2][0], 101, 65}, {family.pairs[2][0], 200, 66}, 61 } { 62 if granted, err := world.grantItem(owner, item.template); err != nil || !granted { 63 t.Fatal(granted, err) 64 } 65 owner.inventory.Items[len(owner.inventory.Items)-1].Quality = item.quality 66 owner.inventory.Items[len(owner.inventory.Items)-1].Slot = item.slot 67 } 68 if err := world.saveInventory(owner, owner.inventory); err != nil { 69 t.Fatal(err) 70 } 71 72 // Each phase drains browser sessions before its database closes. 73 t.Run("warm", func(t *testing.T) { 74 registerTestDatabaseCleanup(t, world) 75 shutdown := startTestService(t, world) 76 mux := http.NewServeMux() 77 mux.Handle("/", world.httpHandler()) 78 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 79 owner, watcher := world.characters[1], world.characters[2] 80 armor := [8]int32{} 81 for index := range armor { 82 armor[index] = owner.equipmentBonus(90 + uint32(index)) 83 } 84 json.NewEncoder(w).Encode(map[string]any{"family": family.name, "inventory": owner.inventory, "stats": owner.stats, 85 "armor": armor, "observer": watcher.inventory, "pending": world.clients[owner.id].equipment != nil}) 86 })) 87 host := httptest.NewServer(mux) 88 t.Cleanup(host.Close) 89 ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) 90 defer cancel() 91 92 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_graft_organic_armor.mjs"), host.URL, output) 93 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 94 err := command.Run() 95 host.Close() 96 if err := shutdown(); err != nil { 97 t.Fatal(err) 98 } 99 t.Logf("Browser artifacts: %s", output) 100 101 if err != nil { 102 t.Fatal(err) 103 } 104 if len(owner.inventory.Items) != 9 || owner.inventory.Credits != family.wallet || owner.stats.ImprovementPoints != family.ip || 105 owner.stats.Abilities.Stamina != family.stamina+1 || owner.stats.Abilities.Strength != family.strength+1 || len(watcher.inventory.Items) != 0 { 106 t.Fatal("native armor contract lost ownership, selected-quality price or trained abilities") 107 } 108 }) 109 if t.Failed() { 110 return 111 } 112 t.Run("cold", func(t *testing.T) { 113 cold := newServerForTest(t) 114 for _, player := range []*character{owner, watcher} { 115 // Loaded inventory views share the world's last allocated item identity. 116 expected := player.inventory 117 expected.LastID = 10 118 if !reflect.DeepEqual(expected, cold.characters[player.id].inventory) || player.stats != cold.characters[player.id].stats { 119 t.Fatal("completed armor contract changed on cold loading", player.id) 120 } 121 } 122 for index, armor := range family.armor { 123 if cold.characters[owner.id].equipmentBonus(90+uint32(index)) != armor { 124 t.Fatal("cold armor or independent sleeves changed", index) 125 } 126 } 127 mux := http.NewServeMux() 128 startTestService(t, cold) 129 mux.Handle("/", cold.httpHandler()) 130 mux.HandleFunc("GET /test-state", func(w http.ResponseWriter, r *http.Request) { 131 json.NewEncoder(w).Encode(map[string]string{"family": family.name}) 132 }) 133 host := httptest.NewServer(mux) 134 t.Cleanup(host.Close) 135 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 136 defer cancel() 137 138 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_graft_organic_cold.mjs"), host.URL, filepath.Join(output, "cold")) 139 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 140 if err := command.Run(); err != nil { 141 t.Fatal(err) 142 } 143 }) 144 }) 145 } 146 }