aorecomp

Anarchy Online classic in the browser

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


server/browser_pending_stack_test.go (2712B)


      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 	"runtime"
     14 	"slices"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserStackJoinDuringPendingEquipment(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for stack joining during pending equipment")
     22 	}
     23 
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/pending-stack", time.Now().Format("20060102-150405"))
     27 	world := newTestServer(t)
     28 	player := world.newCharacter()
     29 	player.stats = testCharacterStats()
     30 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-stack")))
     31 	if err := world.saveUser(player); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	for _, template := range []uint32{209283, 152030, 152030} {
     35 		if granted, err := world.grantItem(player, template); err != nil || !granted {
     36 			t.Fatal(granted, err)
     37 		}
     38 	}
     39 	holdTestEquipmentCompletions(t, world)
     40 	shutdown := startTestService(t, world)
     41 	mux := http.NewServeMux()
     42 	mux.Handle("/", world.httpHandler())
     43 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     44 		player := world.characters[1]
     45 		var pending any
     46 		if client := world.clients[1]; client != nil && client.equipment != nil {
     47 			pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source,
     48 				"destination": client.equipment.destination, "readyAt": client.equipment.readyAt}
     49 		}
     50 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending})
     51 	}))
     52 	mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     53 		finish := world.clients[1].equipment.readyAt
     54 		world.advanceEquipment(finish)
     55 	}))
     56 	host := httptest.NewServer(mux)
     57 	t.Cleanup(host.Close)
     58 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     59 	defer cancel()
     60 
     61 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_stack.mjs"), host.URL, output)
     62 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     63 	err := command.Run()
     64 	host.Close()
     65 	if err := shutdown(); err != nil {
     66 		t.Fatal(err)
     67 	}
     68 	t.Logf("Browser artifacts: %s", output)
     69 
     70 	if err != nil {
     71 		t.Fatal(err)
     72 	}
     73 	cold := newTestInventoryServer(t)
     74 	expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1},
     75 		{ID: 3, Type: 51005, Template: 152030, Quality: 42, Slot: 66, Count: 6}}
     76 	if !slices.Equal(cold.characters[1].inventory.Items, expected) {
     77 		t.Fatal("stack join refusal/retry changed equipment or lost charges")
     78 	}
     79 }