aorecomp

Anarchy Online classic in the browser

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


server/browser_tower_wear_test.go (2406B)


      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 TestBrowserTowerWearAdmission(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for tower wear admission")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/tower-wear-admission", time.Now().Format("20060102-150405"))
     26 	world := newTestServer(t)
     27 	player := world.newCharacter()
     28 	player.appearance = (appearanceFields{Race: 1, Breed: 1, Sex: 2, Side: 2}).packed()
     29 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("tower-wear")))
     30 	if err := world.saveUser(player); err != nil {
     31 		t.Fatal(err)
     32 	}
     33 	for _, template := range []uint32{203099, 29426} {
     34 		if granted, err := world.grantItem(player, template); err != nil || !granted {
     35 			t.Fatal(granted, err)
     36 		}
     37 	}
     38 	shutdown := startTestService(t, world)
     39 	mux := http.NewServeMux()
     40 	mux.Handle("/", world.httpHandler())
     41 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     42 		player := world.characters[1]
     43 		json.NewEncoder(w).Encode(player.inventory)
     44 	}))
     45 	host := httptest.NewServer(mux)
     46 	t.Cleanup(host.Close)
     47 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     48 	defer cancel()
     49 
     50 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_tower_wear.mjs"), host.URL, output)
     51 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     52 	err := command.Run()
     53 	host.Close()
     54 	if err := shutdown(); err != nil {
     55 		t.Fatal(err)
     56 	}
     57 	cancel()
     58 
     59 	t.Logf("Browser artifacts: %s", output)
     60 	if err != nil {
     61 		t.Fatal(err)
     62 	}
     63 	cold := newTestInventoryServer(t)
     64 	expected := []ownedItem{{ID: 1, Type: 51022, Template: 203099, Quality: 125, Slot: 64, Count: 1},
     65 		{ID: 2, Type: 51022, Template: 29426, Quality: 1, Slot: 21, Count: 1}}
     66 	if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) ||
     67 		player.equipmentBonus(91) != 10 || cold.characters[1].equipmentBonus(91) != 10 ||
     68 		player.inventory.Credits != 0 || cold.characters[1].inventory.Credits != 0 || cold.nextItem != 2 {
     69 		t.Fatal("tower refusal or supported shirt wear changed ownership, cash or armor effects")
     70 	}
     71 }