aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/browser_basic_shop_armor_test.go (3594B)


      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 	"reflect"
     16 	"runtime"
     17 	"testing"
     18 	"time"
     19 )
     20 
     21 func TestBrowserBasicShopArmor(t *testing.T) {
     22 	if os.Getenv("AO_GAME_TEST") != "1" {
     23 		t.Skip("set AO_GAME_TEST=1 for the three basic armor terminals")
     24 	}
     25 	_, source, _, _ := runtime.Caller(0)
     26 	root := filepath.Dir(filepath.Dir(source))
     27 	output := filepath.Join(root, "local/alpha-basic-armor", time.Now().Format("20060102-150405"))
     28 	for _, identity := range []uint32{3221226655, 3223389340, 3222078626} {
     29 		t.Run(fmt.Sprintf("%x", identity), func(t *testing.T) {
     30 			world := newTestServer(t)
     31 			machine := gamedata.ShopMachines[identity]
     32 			player := world.newCharacter()
     33 			player.playfield, player.position = machine.Playfield, machine.Position
     34 			player.appearance = map[uint32]uint16{1183: 0x515, 1180: 0x315, 1186: 0x115}[machine.Playfield]
     35 			forward, _ := bodyAxes(machine.Rotation)
     36 			player.position[0] += 3 * forward[0]
     37 			player.position[2] += 3 * forward[2]
     38 			player.position[1] += .5
     39 			yaw := math.Atan2(float64(machine.Position[0]-player.position[0]), float64(machine.Position[2]-player.position[2]))
     40 			player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))}
     41 			// Controlled funds and minimum QL1 glove abilities; no earned-progress claim.
     42 			player.stats.Abilities.Strength, player.stats.Abilities.Agility = 8, 8
     43 			player.stats.TradeRepair.ComputerLiteracy = 0
     44 			player.health, player.nano = player.maximumHealth(), player.maximumNano()
     45 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("basic-shop-armor")))
     46 			if err := world.saveUser(player); err != nil {
     47 				t.Fatal(err)
     48 			}
     49 			inventory := playerInventory{Credits: 1000}
     50 			if err := world.saveInventory(player, inventory); err != nil {
     51 				t.Fatal(err)
     52 			}
     53 			shutdown := startTestService(t, world)
     54 			mux := http.NewServeMux()
     55 			mux.Handle("/", world.httpHandler())
     56 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     57 				player := world.characters[1]
     58 				open, purchases := false, 0
     59 				if client := world.clients[1]; client != nil && client.shop != nil {
     60 					open = true
     61 					for _, count := range client.shop.purchases {
     62 						purchases += int(count)
     63 					}
     64 				}
     65 				json.NewEncoder(w).Encode(map[string]any{"machine": identity, "playfield": machine.Playfield,
     66 					"open": open, "purchases": purchases, "inventory": player.inventory,
     67 					"armor": player.equipmentBonus(90)})
     68 			}))
     69 			host := httptest.NewServer(mux)
     70 			defer host.Close()
     71 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     72 			defer cancel()
     73 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_basic_shop_armor.mjs"), host.URL, filepath.Join(output, fmt.Sprintf("%x", identity)))
     74 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     75 			err := command.Run()
     76 			host.Close()
     77 			if err := shutdown(); err != nil {
     78 				t.Fatal(err)
     79 			}
     80 			cancel()
     81 			t.Logf("Browser artifacts: %s", filepath.Join(output, fmt.Sprintf("%x", identity)))
     82 			if err != nil {
     83 				t.Fatal(err)
     84 			}
     85 			cold := newTestInventoryServer(t).characters[player.id]
     86 			if !reflect.DeepEqual(cold.inventory, player.inventory) || cold.equipmentBonus(90) != 3 || len(cold.inventory.Items) != 1 || cold.inventory.Items[0].Template != 70562 || cold.inventory.Items[0].Slot != 24 {
     87 				t.Fatal("native armor purchase/wear did not recover")
     88 			}
     89 		})
     90 	}
     91 }