server/browser_basic_shop_nanos_test.go (7176B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "context" 6 "crypto/sha256" 7 "encoding/json" 8 "fmt" 9 "math" 10 "net/http" 11 "net/http/httptest" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "slices" 17 "testing" 18 "time" 19 ) 20 21 func TestBrowserBasicShopNanos(t *testing.T) { 22 // General weapon proficiencies have all-row server coverage. 23 testBrowserBasicShopNanos(t, basicShopNanoFixtures()[:12]) 24 } 25 26 func TestBrowserMPShopNanos(t *testing.T) { 27 testBrowserBasicShopNanos(t, mpShopNanoFixtures()) 28 } 29 30 func testBrowserBasicShopNanos(t *testing.T, fixtures []basicShopNanoFixture) { 31 if os.Getenv("AO_GAME_TEST") != "1" { 32 t.Skip("set AO_GAME_TEST=1 for the basic crystal terminals") 33 } 34 _, source, _, _ := runtime.Caller(0) 35 root := filepath.Dir(filepath.Dir(source)) 36 output := filepath.Join(root, "local/alpha-nanos", time.Now().Format("20060102-150405")) 37 for _, fixture := range fixtures { 38 for _, identity := range fixture.machines { 39 t.Run(fmt.Sprintf("%x/%d", identity, fixture.crystal), func(t *testing.T) { 40 world := newTestServer(t) 41 machine := gamedata.ShopMachines[identity] 42 player := world.newCharacter() 43 player.playfield, player.position = machine.Playfield, machine.Position 44 player.appearance = map[uint32]uint16{1183: 0x515, 1180: 0x315, 1186: 0x115}[machine.Playfield] 45 forward, _ := bodyAxes(machine.Rotation) 46 player.position[0] += 3 * forward[0] 47 player.position[2] += 3 * forward[2] 48 player.position[1] += .5 49 yaw := math.Atan2(float64(machine.Position[0]-player.position[0]), float64(machine.Position[2]-player.position[2])) 50 player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))} 51 trainBasicShopNanoFixture(player, fixture.profession) 52 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("basic-shop-nanos"))) 53 if err := world.saveUser(player); err != nil { 54 t.Fatal(err) 55 } 56 inventory := playerInventory{Credits: 10000, LastID: 1, Items: []ownedItem{ 57 {ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1}, 58 }} 59 if err := world.saveInventory(player, inventory); err != nil { 60 t.Fatal(err) 61 } 62 var target *character 63 for _, actor := range world.characters { 64 if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) { 65 target = actor 66 } 67 } 68 if target == nil { 69 t.Fatal("missing original Leet fixture") 70 } 71 target.playfield, target.position, target.health = player.playfield, player.position, 1000 72 target.position[0] -= 1.5 * forward[0] 73 target.position[2] -= 1.5 * forward[2] 74 petLevel := map[uint32]uint16{43725: 2, 43719: 6, 43745: 8}[fixture.program] 75 if petLevel != 0 { 76 target.health = 16 77 } 78 world.combatRoll = landedCombatRoll 79 creditsAfterPurchase := uint32(10000 - fixture.prices[0] + 605) 80 if machine.Playfield != 1183 { 81 creditsAfterPurchase = 10000 - fixture.prices[1] + 484 82 } 83 targetID := target.id 84 target.motion.roaming = false 85 target.regenerationAt = time.Now().Add(5 * time.Minute) 86 if petLevel == 0 { 87 target.rechargeUntil = time.Now().Add(5 * time.Minute) 88 } 89 // Persist the fixture's cross-zone relocation before partitioning owners. 90 if err := world.flushWorld(); err != nil { 91 t.Fatal(err) 92 } 93 shutdown := startTestService(t, world) 94 mux := http.NewServeMux() 95 mux.Handle("/", world.httpHandler()) 96 mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery)) 97 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 98 player := world.characters[1] 99 target := world.characters[targetID] 100 open, purchases, sales := false, 0, 0 101 if client := world.clients[1]; client != nil && client.shop != nil { 102 open, sales = true, len(client.shop.items) 103 for _, count := range client.shop.purchases { 104 purchases += int(count) 105 } 106 } 107 recipient := player 108 if fixture.hostile { 109 recipient = target 110 } 111 var effects []uint32 112 for _, effect := range recipient.activeNanos { 113 effects = append(effects, effect.program) 114 } 115 var petID, looter uint32 116 if target.corpse != nil { 117 looter = target.corpse.looter 118 } 119 for id := range player.pets { 120 petID = id 121 } 122 json.NewEncoder(w).Encode(map[string]any{"machine": identity, "playfield": machine.Playfield, 123 "creditsAfterPurchase": creditsAfterPurchase, "rows": len(gamedata.ShopStock[machine.Template]), "crystal": fixture.crystal, "row": slices.Index(gamedata.ShopStock[machine.Template], gamedata.ShopItem{Template: fixture.crystal, Quality: fixture.quality}), "petLevel": petLevel, "pet": petID, "enemy": target.id, "experience": player.experience, "enemyHealth": target.health, "looter": looter, "program": fixture.program, "quality": fixture.quality, 124 "stat": fixture.stat, "bonus": fixture.bonus, "minimum": fixture.minimum, "maximum": fixture.maximum, "hostile": fixture.hostile, 125 "open": open, "purchases": purchases, "sales": sales, "inventory": player.inventory, "target": recipient.id, "health": recipient.health, 126 "nano": player.nano, "reserved": player.nanoReserved, "effects": effects, "appliedBonus": player.nanoBonus(fixture.stat), "rooted": target.movementRestricted()}) 127 })) 128 host := httptest.NewServer(mux) 129 defer host.Close() 130 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 131 defer cancel() 132 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_basic_shop_nanos.mjs"), host.URL, filepath.Join(output, fmt.Sprintf("%x/%d", identity, fixture.crystal))) 133 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 134 err := command.Run() 135 host.Close() 136 if err := shutdown(); err != nil { 137 t.Fatal(err) 138 } 139 t.Logf("Browser artifacts: %s", filepath.Join(output, fmt.Sprintf("%x/%d", identity, fixture.crystal))) 140 if err != nil { 141 t.Fatal(err) 142 } 143 cancel() 144 var coldWorld *server 145 if petLevel != 0 { 146 coldWorld = newServerForTest(t) 147 } else { 148 coldWorld = newTestInventoryServer(t) 149 } 150 cold := coldWorld.characters[1] 151 if petLevel != 0 { 152 target = coldWorld.characters[target.id] 153 if len(cold.pets) != 1 || cold.experience != 0 || target.health != 0 || target.corpse == nil || target.corpse.looter != player.id { 154 t.Fatal("purchased pet or reward did not survive restart") 155 } 156 for id := range cold.pets { 157 pet := coldWorld.characters[id] 158 if pet.level != petLevel || pet.pet.program != fixture.program { 159 t.Fatal("cold pet lost upgrade") 160 } 161 } 162 } 163 if cold.inventory.Credits != creditsAfterPurchase || cold.inventory.LastID != 2 || len(cold.inventory.Items) != 0 || 164 !slices.Equal(cold.inventory.LearnedNanos, []uint32{fixture.program}) || fixture.stat != 0 && cold.nanoBonus(fixture.stat) != fixture.bonus { 165 t.Fatal("purchased nano changed during cold recovery", cold.inventory, cold.activeNanos) 166 } 167 }) 168 } 169 } 170 }