server/browser_bau_cyber_test.go (5176B)
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 TestBrowserBauCyberArmorQualities(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 to run the real WASM client in Chromium") 22 } 23 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local", "game-tests", "bau-cyber-"+time.Now().Format("20060102-150405")) 27 t.Chdir(t.TempDir()) 28 world := newServerForTest(t) 29 owner := world.newCharacter() 30 owner.name, owner.level, owner.appearance = "BauCyberowner", 100, 0x5b5 31 owner.playfield, owner.position = 1183, [3]float32{188, 5.11, 139.5} 32 owner.rotation = [4]float32{0, 0.70710677, 0, 0.70710677} 33 owner.stats.Abilities.Intelligence, owner.stats.Abilities.Psychic = 263, 238 34 owner.stats.ImprovementPoints, owner.inventory.Credits = 2000, 100 35 owner.health, owner.nano = owner.maximumHealth(), owner.maximumNano() 36 owner.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("bau-cyber-owner"))) 37 watcher := world.newCharacter() 38 watcher.name, watcher.playfield, watcher.position = "BauCyberwatch", 1183, [3]float32{186, 5.11, 134.5} 39 watcher.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("bau-cyber-watch"))) 40 for _, player := range []*character{owner, watcher} { 41 if err := world.saveUser(player); err != nil { 42 t.Fatal(err) 43 } 44 if err := world.savePosition(player); err != nil { 45 t.Fatal(err) 46 } 47 } 48 49 // Seed a controlled item-contract fixture before either native client connects. 50 for _, item := range []struct{ template, quality, slot uint32 }{ 51 {85571, 2, 18}, {85530, 2, 27}, {85654, 2, 30}, 52 {85759, 2, 23}, {85759, 2, 25}, {85611, 2, 24}, {85710, 2, 21}, 53 {85708, 2, 64}, {85610, 100, 65}, {85611, 101, 66}, {85611, 200, 67}, 54 } { 55 if granted, err := world.grantItem(owner, item.template); err != nil || !granted { 56 t.Fatal(granted, err) 57 } 58 owner.inventory.Items[len(owner.inventory.Items)-1].Quality = item.quality 59 owner.inventory.Items[len(owner.inventory.Items)-1].Slot = item.slot 60 } 61 if err := world.saveInventory(owner, owner.inventory); err != nil { 62 t.Fatal(err) 63 } 64 shutdown := startTestService(t, world) 65 mux := http.NewServeMux() 66 mux.Handle("/", world.httpHandler()) 67 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 68 owner, watcher := world.characters[1], world.characters[2] 69 armor := [8]int32{} 70 for index := range armor { 71 armor[index] = owner.equipmentBonus(90 + uint32(index)) 72 } 73 json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "stats": owner.stats, 74 "armor": armor, "observer": watcher.inventory, "pending": world.clients[owner.id].equipment != nil}) 75 })) 76 host := httptest.NewServer(mux) 77 defer host.Close() 78 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) 79 defer cancel() 80 81 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_bau_cyber_armor.mjs"), host.URL, output) 82 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 83 err := command.Run() 84 host.Close() 85 if err := shutdown(); err != nil { 86 t.Fatal(err) 87 } 88 cancel() 89 t.Logf("Browser artifacts: %s", output) 90 if failure := os.MkdirAll(output, 755); failure != nil { 91 t.Fatal(failure) 92 } 93 copyTestDatabase(t, filepath.Join(output, "world.sqlite")) 94 if err != nil { 95 t.Fatal(err) 96 } 97 cold := newServerForTest(t) 98 99 for _, player := range []*character{owner, watcher} { 100 restored := cold.characters[player.id] 101 if !reflect.DeepEqual(player.inventory.Items, restored.inventory.Items) || player.inventory.Credits != restored.inventory.Credits || player.stats != restored.stats || 102 player.position != restored.position || player.rotation != restored.rotation { 103 t.Fatal("native armor outcome changed across cold loading", player.id) 104 } 105 } 106 if len(owner.inventory.Items) != 10 || owner.inventory.Credits != 5380 || owner.stats.ImprovementPoints != 1499 || 107 owner.stats.Abilities.Intelligence != 264 || owner.stats.Abilities.Psychic != 239 || len(watcher.inventory.Items) != 0 { 108 t.Fatal("native armor fixture lost ownership, selected-quality sale or ability training") 109 } 110 for index, armor := range [8]int32{156, 156, 138, 35, 103, 120, 35, 120} { 111 if owner.equipmentBonus(90+uint32(index)) != armor || cold.characters[owner.id].equipmentBonus(90+uint32(index)) != armor { 112 t.Fatal("native worn armor or independent sleeves changed", index) 113 } 114 } 115 116 // Reconstruct the completed item contract in two fresh native clients. 117 host.Close() 118 startTestService(t, cold) 119 coldHost := httptest.NewServer(cold.httpHandler()) 120 t.Cleanup(coldHost.Close) 121 coldContext, coldCancel := context.WithTimeout(context.Background(), 2*time.Minute) 122 defer coldCancel() 123 command = exec.CommandContext(coldContext, "node", filepath.Join(root, "tools/client/test_bau_cyber_cold.mjs"), coldHost.URL, filepath.Join(output, "cold")) 124 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 125 if err := command.Run(); err != nil { 126 t.Fatal(err) 127 } 128 }