aorecomp

Anarchy Online classic in the browser

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


server/browser_starter_shops_test.go (3969B)


      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 TestBrowserStarterShops(t *testing.T) {
     22 	if os.Getenv("AO_GAME_TEST") != "1" {
     23 		t.Skip("set AO_GAME_TEST=1 for the four training-ground terminals")
     24 	}
     25 	_, source, _, _ := runtime.Caller(0)
     26 	root := filepath.Dir(filepath.Dir(source))
     27 	output := filepath.Join(root, "local/alpha-shops", time.Now().Format("20060102-150405"))
     28 	for _, identity := range []uint32{0xc00003b6, 0xc00003b8, 0xc00003ba, 0xc00103ba} {
     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{950: 0x515, 952: 0x315, 954: 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 level-five patient, empty finite pistol and sale loot.
     42 			// Funding and trained skills are fixtures, not earned-progression evidence.
     43 			player.level, player.health, player.nano = 5, 1, 1
     44 			player.stats.Body.BodyDevelopment, player.stats.Body.NanoPool = 40, 40
     45 			player.stats.NanoAiding.Treatment, player.stats.NanoAiding.FirstAid, player.stats.NanoAiding.TimeAndSpace = 10, 10, 10
     46 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("starter-shops")))
     47 			if err := world.saveUser(player); err != nil {
     48 				t.Fatal(err)
     49 			}
     50 			inventory := playerInventory{Credits: 1000, LastID: 2, Items: []ownedItem{
     51 				{ID: 1, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1},
     52 				{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     53 			}}
     54 			if err := world.saveInventory(player, inventory); err != nil {
     55 				t.Fatal(err)
     56 			}
     57 			shutdown := startTestService(t, world)
     58 			mux := http.NewServeMux()
     59 			mux.Handle("/", world.httpHandler())
     60 			mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     61 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 				player := world.characters[1]
     63 				open, sales, purchases := false, 0, 0
     64 				if client := world.clients[1]; client != nil && client.shop != nil {
     65 					open, sales = true, len(client.shop.items)
     66 					for _, count := range client.shop.purchases {
     67 						purchases += int(count)
     68 					}
     69 				}
     70 				json.NewEncoder(w).Encode(map[string]any{"machine": identity, "playfield": machine.Playfield,
     71 					"open": open, "sales": sales, "purchases": purchases, "inventory": player.inventory,
     72 					"health": player.health, "nano": player.nano})
     73 			}))
     74 			host := httptest.NewServer(mux)
     75 			defer host.Close()
     76 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     77 			defer cancel()
     78 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_starter_shops.mjs"), host.URL, filepath.Join(output, fmt.Sprintf("%x", identity)))
     79 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     80 			err := command.Run()
     81 			host.Close()
     82 			if err := shutdown(); err != nil {
     83 				t.Fatal(err)
     84 			}
     85 			t.Logf("Browser artifacts: %s", filepath.Join(output, fmt.Sprintf("%x", identity)))
     86 			if err != nil {
     87 				t.Fatal(err)
     88 			}
     89 			cold := newTestInventoryServer(t).characters[1]
     90 			if !reflect.DeepEqual(cold.inventory, player.inventory) || cold.health != player.health || cold.nano != player.nano {
     91 				t.Fatal("starter purchases and resource use changed during cold recovery")
     92 			}
     93 		})
     94 	}
     95 }