aorecomp

Anarchy Online classic in the browser

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


server/browser_incoming_equipment_test.go (4948B)


      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 TestBrowserIncomingEquipment(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for equipment during incoming combat")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/incoming-equipment", time.Now().Format("20060102-150405"))
     26 	t.Chdir(t.TempDir())
     27 	for _, phase := range []string{"live", "cold"} {
     28 		if !t.Run(phase, func(t *testing.T) {
     29 			world := newServerForTest(t)
     30 			world.combatRoll = landedCombatRoll
     31 			var npc *character
     32 			for _, actor := range world.characters {
     33 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (npc == nil || actor.id < npc.id) {
     34 					npc = actor
     35 				}
     36 			}
     37 			if npc == nil {
     38 				t.Fatal("no admitted Leet for incoming combat")
     39 			}
     40 			if phase == "live" {
     41 				for index := 0; index < 2; index++ {
     42 					player := world.newCharacter()
     43 					player.name, player.profession, player.level, player.appearance = fmt.Sprint("Incoming", index), 9, 100, 0x315
     44 					player.position, player.rotation = npc.position, [4]float32{0, 0, 0, 1}
     45 					player.position[2] -= float32(2 + index*3)
     46 					player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
     47 					player.stats.Melee.OneHandBlunt, player.stats.Melee.OneHandEdged, player.stats.Melee.FastAttack = 20, 20, 20
     48 					player.stats.Body.Brawling, player.stats.Body.Dimach = 20, 20
     49 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     50 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("incoming-", index))))
     51 					if err := world.saveUser(player); err != nil {
     52 						t.Fatal(err)
     53 					}
     54 				}
     55 				for _, template := range []uint32{209283, 122691} {
     56 					if granted, err := world.grantItem(world.characters[1], template); !granted || err != nil {
     57 						t.Fatal(granted, err)
     58 					}
     59 				}
     60 				world.characters[1].inventory.Items[0].Slot = 6
     61 				world.characters[1].inventory.Items[1].Slot = 64
     62 				world.characters[1].inventory.Items[1].Quality = 3
     63 				if err := world.saveInventory(world.characters[1], world.characters[1].inventory); err != nil {
     64 					t.Fatal(err)
     65 				}
     66 			}
     67 			owner := world.characters[1]
     68 			npcID := npc.id
     69 			if npc.motion != nil {
     70 				npc.motion.roaming = false
     71 			}
     72 			npc.regenerationAt = time.Now().Add(5 * time.Minute)
     73 			watcher := world.characters[2]
     74 			shutdown := startTestService(t, world)
     75 			mux := http.NewServeMux()
     76 			mux.Handle("/", world.httpHandler())
     77 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     78 				owner, npc := world.characters[1], world.characters[npcID]
     79 				pending := world.clients[1] != nil && world.clients[1].equipment != nil
     80 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "health": owner.health,
     81 					"pending": pending, "fighting": owner.fighting, "npc": npc.id, "target": npc.fighting,
     82 					"attack": npc.nextAttack, "recharge": npc.rechargeUntil})
     83 			}))
     84 			// Controlled aggression isolates an idle recipient; no earned progression is claimed.
     85 			for _, action := range []string{"start", "stop"} {
     86 				mux.HandleFunc("POST /test-"+action, testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     87 					owner, npc := world.characters[1], world.characters[npcID]
     88 					updates := make(map[*worldClient][][]byte)
     89 					if action == "start" {
     90 						world.startAttack(npc, owner, time.Now(), updates)
     91 					} else {
     92 						world.stopAttack(npc, updates)
     93 					}
     94 					world.sendCombatUpdates(updates)
     95 					w.WriteHeader(http.StatusNoContent)
     96 				}))
     97 			}
     98 			host := httptest.NewServer(mux)
     99 			t.Cleanup(host.Close)
    100 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    101 			defer cancel()
    102 
    103 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_incoming_equipment.mjs"), host.URL, filepath.Join(output, phase), phase)
    104 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    105 			err := command.Run()
    106 			host.Close()
    107 			if err := shutdown(); err != nil {
    108 				t.Fatal(err)
    109 			}
    110 			cancel()
    111 			t.Logf("Browser artifacts: %s", output)
    112 
    113 			expected := []ownedItem{
    114 				{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
    115 				{ID: 2, Type: 51018, Template: 122691, Quality: 3, Slot: 6, Count: 1},
    116 			}
    117 			if err != nil || !slices.Equal(owner.inventory.Items, expected) || owner.fighting != 0 || owner.health == 0 ||
    118 				owner.inventory.Credits != 0 || len(watcher.inventory.Items) != 0 || newTestInventoryServer(t).nextItem != 2 {
    119 				t.Fatal("incoming combat changed equipment conservation or the idle recipient", err, owner.inventory)
    120 			}
    121 		}) {
    122 			return
    123 		}
    124 	}
    125 }