aorecomp

Anarchy Online classic in the browser

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


server/browser_pet_retreat_test.go (3167B)


      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 TestBrowserPetRetreat(t *testing.T) {
     17 	if os.Getenv("AO_GAME_TEST") != "1" {
     18 		t.Skip("set AO_GAME_TEST=1 for native pet retreat")
     19 	}
     20 	_, source, _, _ := runtime.Caller(0)
     21 	root := filepath.Dir(filepath.Dir(source))
     22 	output := filepath.Join(root, "local/pet-retreat", time.Now().Format("20060102-150405"))
     23 	world := newBrowserOwnedPetWorld(t)
     24 	pet, target := world.characters[1000001], world.characters[2]
     25 	var attackDeadline time.Time
     26 	pet.rechargeUntil = time.Now().Add(5 * time.Minute)
     27 	shutdown := startTestService(t, world)
     28 	mux := http.NewServeMux()
     29 	mux.Handle("/", world.httpHandler())
     30 	mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     31 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     32 		pet := world.characters[1000001]
     33 		target := world.characters[2]
     34 		json.NewEncoder(w).Encode(map[string]any{"fighting": pet.fighting, "nextAttack": pet.nextAttack, "follow": pet.followTarget,
     35 			"petPosition": pet.position, "ownerPosition": world.characters[1].position, "targetHealth": target.health,
     36 			"ownerNano": world.characters[1].nano, "peerNano": target.nano, "peerPet": world.characters[1000002].position})
     37 	}))
     38 	// Controlled pending attack, not evidence of the original PT50 weapon binding.
     39 	mux.HandleFunc("POST /test-attack", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     40 		pet := world.characters[1000001]
     41 		target := world.characters[2]
     42 		updates := make(map[*worldClient][][]byte)
     43 		world.startAttack(pet, target, time.Now(), updates)
     44 		attackDeadline = pet.nextAttack
     45 		world.sendCombatUpdates(updates)
     46 		w.WriteHeader(http.StatusNoContent)
     47 	}))
     48 	mux.HandleFunc("POST /test-deadline", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     49 		deadline := attackDeadline
     50 		world.advanceCombat(deadline.Add(time.Nanosecond))
     51 		w.WriteHeader(http.StatusNoContent)
     52 	}))
     53 	host := httptest.NewServer(mux)
     54 	t.Cleanup(host.Close)
     55 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     56 	defer cancel()
     57 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pet_retreat.mjs"), host.URL, output)
     58 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     59 	err := command.Run()
     60 	// Inspect the retained zone state only after its owner has stopped.
     61 	var final *server
     62 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { final = state; return nil }))
     63 	host.Close()
     64 	if err := shutdown(); err != nil {
     65 		t.Fatal(err)
     66 	}
     67 	t.Logf("Browser artifacts: %s", output)
     68 
     69 	if err != nil {
     70 		t.Fatal(err)
     71 	}
     72 	if pet.fighting != 0 || !pet.nextAttack.IsZero() || pet.followTarget != 0 || final.fighting[pet.id] != nil ||
     73 		final.characters[1].nano != 25 || target.nano != 25 || len(final.characters[1].pets) != 1 || len(target.pets) != 1 || final.identities.npcs.Load()+1 != 1000003 {
     74 		t.Fatal("retreat changed ownership/cost or retained a future attack")
     75 	}
     76 }