aorecomp

Anarchy Online classic in the browser

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


server/browser_first_aid_test.go (4077B)


      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 	"testing"
     15 	"time"
     16 )
     17 
     18 func TestBrowserFirstAidQueuedHit(t *testing.T) {
     19 	if os.Getenv("AO_GAME_TEST") != "1" {
     20 		t.Skip("set AO_GAME_TEST=1 for the controlled WASM First Aid regression")
     21 	}
     22 	for _, template := range []uint32{23314, 116629} {
     23 		t.Run(fmt.Sprint(template), func(t *testing.T) {
     24 			_, source, _, _ := runtime.Caller(0)
     25 			root := filepath.Dir(filepath.Dir(source))
     26 			output := filepath.Join(root, "local/first-aid-queued-hit", time.Now().Format("20060102-150405")+fmt.Sprint("-", template))
     27 			world := newTestServer(t)
     28 			var npc *character
     29 			for _, actor := range world.characters {
     30 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (npc == nil || actor.id < npc.id) {
     31 					npc = actor
     32 				}
     33 			}
     34 			if npc == nil {
     35 				t.Fatal("no level-one Leet for controlled reproduction")
     36 			}
     37 			players := []*character{}
     38 			for i := 0; i < 2; i++ {
     39 				player := world.newCharacter()
     40 				player.name, player.level, player.profession, player.appearance = fmt.Sprint("First Aid ", i), 10, 9, 0x315
     41 				player.position, player.rotation = npc.position, [4]float32{0, 0, 0, 1}
     42 				player.position[2] -= float32(2 + i*3)
     43 				player.stats.Body.BodyDevelopment = 55
     44 				player.stats.NanoAiding.FirstAid = 7
     45 				player.health, player.nano = 200, 1
     46 				player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("first-aid-", i))))
     47 				if err := world.saveUser(player); err != nil {
     48 					t.Fatal(err)
     49 				}
     50 				players = append(players, player)
     51 			}
     52 			if granted, err := world.grantItem(players[0], template); !granted || err != nil {
     53 				t.Fatal(granted, err)
     54 			}
     55 			players[0].inventory.Items[0].Count = 3
     56 			if err := world.saveInventory(players[0], players[0].inventory); err != nil {
     57 				t.Fatal(err)
     58 			}
     59 			npcID := npc.id
     60 			npc.motion.roaming = false
     61 			npc.regenerationAt = time.Now().Add(5 * time.Minute)
     62 			shutdown := startTestService(t, world)
     63 			mux := http.NewServeMux()
     64 			mux.Handle("/", world.httpHandler())
     65 			mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     66 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     67 				npc := world.characters[npcID]
     68 				json.NewEncoder(w).Encode(map[string]any{"owner": world.characters[1].id, "observer": world.characters[2].id, "npc": npc.id,
     69 					"health": world.characters[1].health, "nano": world.characters[1].nano, "template": template, "maximumHealth": world.characters[1].maximumHealth(), "inventory": world.characters[1].inventory,
     70 					"fighting": npc.fighting, "nextAttack": npc.nextAttack, "rechargeUntil": npc.rechargeUntil, "lock": world.characters[1].skillLocks[123]})
     71 			}))
     72 			// Controlled setup, not earned progression. Advance one production attack on
     73 			// demand, with later swings deferred beyond the observation window.
     74 			mux.HandleFunc("POST /test-attack", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     75 				npc := world.characters[npcID]
     76 				updates := make(map[*worldClient][][]byte)
     77 				world.startAttack(npc, world.characters[1], time.Now(), updates)
     78 				deadline := npc.nextAttack
     79 				world.sendCombatUpdates(updates)
     80 				world.advanceCombat(deadline)
     81 				npc.nextAttack = time.Now().Add(5 * time.Minute)
     82 				w.WriteHeader(http.StatusNoContent)
     83 			}))
     84 			host := httptest.NewServer(mux)
     85 			t.Cleanup(host.Close)
     86 			ctx, cancel := context.WithTimeout(context.Background(), 8*time.Minute)
     87 			defer cancel()
     88 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_first_aid.mjs"), host.URL, output)
     89 			command.Dir = root
     90 			command.Stdout, command.Stderr = os.Stdout, os.Stderr
     91 			err := command.Run()
     92 			host.Close()
     93 			if err := shutdown(); err != nil {
     94 				t.Fatal(err)
     95 			}
     96 			t.Logf("Browser artifacts: %s", output)
     97 			if err != nil {
     98 				t.Fatal(err)
     99 			}
    100 		})
    101 	}
    102 }