aorecomp

Anarchy Online classic in the browser

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


server/browser_pending_equipment_test.go (3234B)


      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 TestBrowserSecondEquipmentMove(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for a second move during pending equipment")
     22 	}
     23 
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/pending-equipment", 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-equipment")))
     31 	if err := world.saveUser(player); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	for range 2 {
     35 		if granted, err := world.grantItem(player, 209283); err != nil || !granted {
     36 			t.Fatal(granted, err)
     37 		}
     38 	}
     39 	if granted, err := world.grantItem(player, 85481); err != nil || !granted {
     40 		t.Fatal(granted, err)
     41 	}
     42 	inventory := player.inventory
     43 	inventory.Items = slices.Clone(inventory.Items)
     44 	inventory.Items[2].Slot = 21
     45 	if err := world.saveInventory(player, inventory); err != nil {
     46 		t.Fatal(err)
     47 	}
     48 	holdTestEquipmentCompletions(t, world)
     49 	shutdown := startTestService(t, world)
     50 	mux := http.NewServeMux()
     51 	mux.Handle("/", world.httpHandler())
     52 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     53 		player := world.characters[1]
     54 		var pending any
     55 		if client := world.clients[1]; client != nil && client.equipment != nil {
     56 			pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source,
     57 				"destination": client.equipment.destination, "replacedID": client.equipment.replacedID, "readyAt": client.equipment.readyAt}
     58 		}
     59 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending})
     60 	}))
     61 	// Hold each admitted move until the native refusal or retry has been observed.
     62 	mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     63 		finish := world.clients[1].equipment.readyAt
     64 		world.advanceEquipment(finish)
     65 	}))
     66 	host := httptest.NewServer(mux)
     67 	t.Cleanup(host.Close)
     68 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     69 	defer cancel()
     70 
     71 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_equipment.mjs"), host.URL, output)
     72 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     73 	t.Logf("Browser artifacts: %s", output)
     74 	if err := command.Run(); err != nil {
     75 		t.Fatal(err)
     76 	}
     77 	host.Close()
     78 	if err := shutdown(); err != nil {
     79 		t.Fatal(err)
     80 	}
     81 	cold := newTestInventoryServer(t)
     82 
     83 	expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 65, Count: 1},
     84 		{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1},
     85 		{ID: 3, Type: 51022, Template: 85481, Quality: 1, Slot: 21, Count: 1}}
     86 	if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) ||
     87 		cold.nextItem != 3 {
     88 		t.Fatal("pending move refusal/retry lost the exact equipment instances")
     89 	}
     90 }