aorecomp

Anarchy Online classic in the browser

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


server/browser_shop_refusal_test.go (3193B)


      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 	"slices"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserShopSaleRefusal(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for shop sale refusal")
     23 	}
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/shop-sale-refusal", time.Now().Format("20060102-150405"))
     27 	world := newTestServer(t)
     28 	player := world.newCharacter()
     29 	player.playfield, player.position, player.appearance = 1183, [3]float32{188, 5.751, 135.5}, 0x515
     30 	player.rotation = [4]float32{0, .70710677, 0, .70710677}
     31 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("shop-sale-refusal")))
     32 	if err := world.saveUser(player); err != nil {
     33 		t.Fatal(err)
     34 	}
     35 	inventory := playerInventory{LastID: 3, Items: []ownedItem{
     36 		{ID: 1, Type: 51005, Template: 25812, Quality: 2, Slot: 64, Count: 3},
     37 		{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 65, Count: 5},
     38 		{ID: 3, Type: 51005, Template: 25812, Quality: 2, Slot: 66, Count: 7}}}
     39 	if err := world.saveInventory(player, inventory); err != nil {
     40 		t.Fatal(err)
     41 	}
     42 	shutdown := startTestService(t, world)
     43 	mux := http.NewServeMux()
     44 	mux.Handle("/", world.httpHandler())
     45 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     46 		player := world.characters[1]
     47 		open := world.clients[1] != nil && world.clients[1].shop != nil
     48 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "open": open})
     49 	}))
     50 	// Isolate an earlier committed merge without refreshing the native view.
     51 	mux.HandleFunc("POST /test-stale-view", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     52 		player := world.characters[1]
     53 		inventory := player.inventory
     54 		inventory.Items = slices.Clone(inventory.Items[1:])
     55 		inventory.Items[0].Count = 8
     56 		if err := world.saveInventory(player, inventory); err != nil {
     57 			t.Error(err)
     58 			http.Error(w, err.Error(), http.StatusInternalServerError)
     59 		}
     60 	}))
     61 	host := httptest.NewServer(mux)
     62 	t.Cleanup(host.Close)
     63 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     64 	defer cancel()
     65 
     66 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_shop_refusal.mjs"), host.URL, output)
     67 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     68 	err := command.Run()
     69 	host.Close()
     70 	if failure := shutdown(); failure != nil {
     71 		t.Fatal(failure)
     72 	}
     73 
     74 	t.Logf("Browser artifacts: %s", output)
     75 	if err != nil {
     76 		t.Fatal(err)
     77 	}
     78 	restored := newTestInventoryServer(t)
     79 	cold := restored.characters[1].inventory
     80 	expected := []ownedItem{{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 64, Count: 8},
     81 		{ID: 3, Type: 51005, Template: 25812, Quality: 2, Slot: 66, Count: 7}}
     82 	if !reflect.DeepEqual(cold.Items, expected) || !reflect.DeepEqual(player.inventory.Items, expected) ||
     83 		cold.Credits != 0 || player.inventory.Credits != 0 || restored.nextItem != 3 {
     84 		t.Fatal("sale refusal changed ownership or cash", cold)
     85 	}
     86 }