aorecomp

Anarchy Online classic in the browser

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


server/browser_pet_follow_test.go (3040B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"encoding/json"
      6 	"net/http"
      7 	"net/http/httptest"
      8 	"os"
      9 	"os/exec"
     10 	"path/filepath"
     11 	"runtime"
     12 	"testing"
     13 	"time"
     14 )
     15 
     16 func TestBrowserPetFollow(t *testing.T) {
     17 	runBrowserPetMotion(t, "follow")
     18 }
     19 
     20 func TestBrowserPetWait(t *testing.T) {
     21 	runBrowserPetMotion(t, "wait")
     22 }
     23 
     24 func newBrowserOwnedPetWorld(t *testing.T) *server {
     25 	t.Helper()
     26 	world := newBrowserPetWorld(t)
     27 	// Controlled checkpoint: both players have completed the ordinary summon action.
     28 	for id := uint32(1); id <= 2; id++ {
     29 		client := &worldClient{id: id, target: true, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
     30 		world.clients[id] = client
     31 		if err := world.handleNanoCast(client, characterActionPacket(id, [5]uint32{19, 0, 0, 53019, 43324})[20:]); err != nil {
     32 			t.Fatal(err)
     33 		}
     34 		if client.nanoCast == nil {
     35 			t.Fatal("controlled owner could not summon", id)
     36 		}
     37 		world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     38 	}
     39 	clear(world.clients)
     40 	return world
     41 }
     42 
     43 func runBrowserPetMotion(t *testing.T, scenario string) {
     44 	t.Helper()
     45 	if os.Getenv("AO_GAME_TEST") != "1" {
     46 		t.Skip("set AO_GAME_TEST=1 for native pet " + scenario)
     47 	}
     48 	_, source, _, _ := runtime.Caller(0)
     49 	root := filepath.Dir(filepath.Dir(source))
     50 	output := filepath.Join(root, "local/pet-"+scenario, time.Now().Format("20060102-150405"))
     51 	world := newBrowserOwnedPetWorld(t)
     52 	shutdown := startTestService(t, world)
     53 	mux := http.NewServeMux()
     54 	mux.Handle("/", world.httpHandler())
     55 	mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     56 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     57 		var owners []map[string]any
     58 		for id := uint32(1); id <= 2; id++ {
     59 			owner := world.characters[id]
     60 			pet := world.characters[1000000+id]
     61 			owners = append(owners, map[string]any{"id": id, "position": owner.position, "nano": owner.nano,
     62 				"pet": pet.id, "petPosition": pet.position, "follow": pet.followTarget, "paused": pet.pet.paused})
     63 		}
     64 		json.NewEncoder(w).Encode(owners)
     65 	}))
     66 	host := httptest.NewServer(mux)
     67 	t.Cleanup(host.Close)
     68 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     69 	defer cancel()
     70 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pet_"+scenario+".mjs"), host.URL, output)
     71 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     72 	err := command.Run()
     73 	// Inspect the retained zone state only after its owner has stopped.
     74 	var final *server
     75 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { final = state; return nil }))
     76 	host.Close()
     77 	if err := shutdown(); err != nil {
     78 		t.Fatal(err)
     79 	}
     80 
     81 	t.Logf("Browser artifacts: %s", output)
     82 	if err != nil {
     83 		t.Fatal(err)
     84 	}
     85 	if final.characters[1000001].followTarget != 1 || final.characters[1000002].followTarget != 0 ||
     86 		final.characters[1].nano != 25 || final.characters[2].nano != 25 {
     87 		t.Fatal("pet motion changed another owner's pet or charged another summon")
     88 	}
     89 }