aorecomp

Anarchy Online classic in the browser

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


server/browser_pet_dismiss_test.go (3965B)


      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 TestBrowserPetDismiss(t *testing.T) {
     17 	if os.Getenv("AO_GAME_TEST") != "1" {
     18 		t.Skip("set AO_GAME_TEST=1 for native pet dismissal")
     19 	}
     20 	_, source, _, _ := runtime.Caller(0)
     21 	root := filepath.Dir(filepath.Dir(source))
     22 	output := filepath.Join(root, "local/pet-dismiss", time.Now().Format("20060102-150405"))
     23 	world := newBrowserOwnedPetWorld(t)
     24 	for _, phase := range []string{"live", "cold"} {
     25 		if phase == "cold" {
     26 			if err := world.closeDatabase(); err != nil {
     27 				t.Fatal(err)
     28 			}
     29 			world = newServerForTest(t)
     30 		}
     31 		if !t.Run(phase, func(t *testing.T) {
     32 			pet := world.characters[1000001]
     33 			var attackDeadline time.Time
     34 			if pet != nil {
     35 				pet.rechargeUntil = time.Now().Add(5 * time.Minute)
     36 			}
     37 			shutdown := startTestService(t, world)
     38 			mux := http.NewServeMux()
     39 			mux.Handle("/", world.httpHandler())
     40 			mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     41 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     42 				var owners []map[string]any
     43 				for id := uint32(1); id <= 2; id++ {
     44 					owner := world.characters[id]
     45 					pets := []uint32{}
     46 					for pet := range owner.pets {
     47 						pets = append(pets, pet)
     48 					}
     49 					owners = append(owners, map[string]any{"id": id, "pets": pets, "nano": owner.nano, "health": owner.health})
     50 				}
     51 				var rows uint32
     52 				if err := world.db.QueryRow("SELECT count(*) FROM pets").Scan(&rows); err != nil {
     53 					t.Error(err)
     54 					http.Error(w, err.Error(), http.StatusInternalServerError)
     55 					return
     56 				}
     57 				var fighting uint32
     58 				var deadline time.Time
     59 				if pet != nil {
     60 					fighting, deadline = pet.fighting, pet.nextAttack
     61 				}
     62 				json.NewEncoder(w).Encode(map[string]any{"owners": owners, "savedPets": rows, "nextNPC": world.identities.npcs.Load() + 1,
     63 					"scheduled": world.fighting[1000001] != nil, "retiredFighting": fighting, "retiredDeadline": deadline})
     64 			}))
     65 			// Hold a production-scheduler swing; this does not bind the PT50 weapon.
     66 			mux.HandleFunc("POST /test-attack", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     67 				updates := make(map[*worldClient][][]byte)
     68 				world.startAttack(pet, world.characters[1], time.Now(), updates)
     69 				attackDeadline = pet.nextAttack
     70 				world.sendCombatUpdates(updates)
     71 			}))
     72 			mux.HandleFunc("POST /test-deadline", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     73 				deadline := attackDeadline
     74 				world.advanceCombat(deadline.Add(time.Nanosecond))
     75 			}))
     76 			host := httptest.NewServer(mux)
     77 			t.Cleanup(host.Close)
     78 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     79 			defer cancel()
     80 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pet_dismiss.mjs"), host.URL, filepath.Join(output, phase), phase)
     81 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     82 			err := command.Run()
     83 			// Inspect the retained zone state only after its owner has stopped.
     84 			var final *server
     85 			callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { final = state; return nil }))
     86 			host.Close()
     87 			if err := shutdown(); err != nil {
     88 				t.Fatal(err)
     89 			}
     90 
     91 			t.Logf("Browser artifacts: %s", output)
     92 			if err != nil {
     93 				t.Fatal(err)
     94 			}
     95 			if final.characters[1000001] != nil || final.characters[1000002] == nil || final.characters[1000003] == nil ||
     96 				final.fighting[1000001] != nil ||
     97 				len(final.characters[1].pets) != 1 || len(final.characters[2].pets) != 1 || len(final.petExpiry) != 2 ||
     98 				final.characters[1].nano != 15 || final.characters[2].nano != 25 || final.identities.npcs.Load()+1 != 1000004 {
     99 				t.Fatal("dismissal/replacement changed the peer or reused an identity")
    100 			}
    101 		}) {
    102 			return
    103 		}
    104 	}
    105 }