aorecomp

Anarchy Online classic in the browser

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


server/browser_item_proxy_test.go (3895B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"context"
      6 	"crypto/sha256"
      7 	"encoding/json"
      8 	"fmt"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"reflect"
     15 	"runtime"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserAnonymousPurchasesAreUsable(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for the focused new-item proxy check")
     23 	}
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/anonymous-item-proxies", time.Now().Format("20060102-150405"))
     27 	t.Chdir(t.TempDir())
     28 	for _, phase := range []string{"purchase", "cold"} {
     29 		t.Run(phase, func(t *testing.T) {
     30 			world := newServerForTest(t)
     31 			if phase == "purchase" {
     32 				for index := 0; index < 2; index++ {
     33 					player := world.newCharacter()
     34 					player.name, player.level, player.appearance = fmt.Sprint("Proxy", index), 10, 0x515
     35 					player.playfield, player.position = 1183, [3]float32{188 - float32(index*2), 5.11, 139.5}
     36 					player.rotation = [4]float32{0, 0.70710677, 0, 0.70710677}
     37 					player.stats = testCharacterStats()
     38 					player.stats.Abilities.Strength, player.stats.Abilities.Agility = 20, 20
     39 					player.stats.Body.BodyDevelopment = 20
     40 					player.stats.NanoAiding.Treatment = 6
     41 					player.health, player.inventory.Credits = 50, 5000
     42 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("proxy-", index))))
     43 					if err := world.saveUser(player); err != nil {
     44 						t.Fatal(err)
     45 					}
     46 					if err := world.saveInventory(player, player.inventory); err != nil {
     47 						t.Fatal(err)
     48 					}
     49 				}
     50 				// Mixed fixture stock isolates delivery, not historical booth membership.
     51 				stock := gamedata.ShopStock[99383]
     52 				t.Cleanup(func() { gamedata.ShopStock[99383] = stock })
     53 				gamedata.ShopStock[99383] = []gamedata.ShopItem{{Template: 25812, Quality: 1}, {Template: 85596, Quality: 1}}
     54 			}
     55 			owner, watcher := world.characters[1], world.characters[2]
     56 			if phase == "purchase" && len(owner.inventory.Items) != 0 || phase == "cold" && len(owner.inventory.Items) != 2 {
     57 				t.Fatal("unexpected native proxy fixture phase")
     58 			}
     59 			shutdown := startTestService(t, world)
     60 			mux := http.NewServeMux()
     61 			mux.Handle("/", world.httpHandler())
     62 			mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     63 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     64 				owner := world.characters[1]
     65 				watcher := world.characters[2]
     66 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "health": owner.health,
     67 					"sitting": owner.sitting, "lock": owner.skillLocks[124], "observer": watcher.inventory})
     68 			}))
     69 			host := httptest.NewServer(mux)
     70 			t.Cleanup(host.Close)
     71 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     72 			defer cancel()
     73 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_item_proxies.mjs"), host.URL, filepath.Join(output, phase))
     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 			t.Logf("Browser artifacts: %s", output)
     81 
     82 			if err != nil {
     83 				t.Fatal(err)
     84 			}
     85 			expected := []ownedItem{
     86 				{ID: 1, Type: 51005, Template: 25812, Quality: 1, Slot: 64, Count: 49},
     87 				{ID: 2, Type: 51022, Template: 85596, Quality: 1, Slot: 24, Count: 1},
     88 			}
     89 			if !reflect.DeepEqual(owner.inventory.Items, expected) || owner.health != 80 || owner.inventory.Credits != 4600 ||
     90 				len(watcher.inventory.Items) != 0 || watcher.inventory.Credits != 5000 || newTestInventoryServer(t).nextItem != 2 || owner.equipmentBonus(90) != 1 {
     91 				t.Fatal("immediate native use/wear or cold recovery lost an instance, charge, health or cash")
     92 			}
     93 		})
     94 		if t.Failed() {
     95 			return
     96 		}
     97 	}
     98 }