server/browser_npc_water_test.go (3842B)
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 TestBrowserNPCOutdoorWaterMotion(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for the focused outdoor NPC water check") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/npc-water-motion", time.Now().Format("20060102-150405")) 25 world, npc, _ := newNPCOutdoorSurfaceWorld(t) 26 world.clients = make(map[uint32]*worldClient) 27 players := []*character{world.characters[1], world.newCharacter()} 28 for index, player := range players { 29 player.name, player.level = fmt.Sprint("Watermotion", index), 10 30 player.health = player.maximumHealth() 31 player.position = [3]float32{126, 20, 98 + float32(index)*4} 32 point, normal := player.position, [3]float32{} 33 world.geometry.floor(950, world.terrain[950], player.position, &point, &normal) 34 player.position[1] = point[1] + .01 35 player.rotation = [4]float32{0, .70710677, 0, .70710677} 36 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("water-motion-", index)))) 37 if err := world.saveUser(player); err != nil { 38 t.Fatal(err) 39 } 40 } 41 npcID := npc.id 42 npc.motion.nextMove = time.Now().Add(5 * time.Minute) 43 shutdown := startTestService(t, world) 44 mux := http.NewServeMux() 45 mux.Handle("/", world.httpHandler()) 46 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 47 npc := world.characters[npcID] 48 json.NewEncoder(w).Encode(map[string]any{"enemy": npc.id, "owner": world.characters[1].id, "position": npc.position, 49 "home": npc.motion.home, "destination": npc.motion.destination, "moving": npc.motion.moving, 50 "returning": npc.motion.returning, "mode": npc.movement.components[0], "deepWater": npc.movement.deepWater, 51 "health": npc.health, "fighting": npc.fighting, "ownerHealth": world.characters[1].health, 52 "ownerPosition": predictedPosition(world.characters[1], time.Now()), "ownerFighting": world.characters[1].fighting}) 53 })) 54 // Choose one ordinary four-metre idle route without moving or healing its actor. 55 mux.HandleFunc("POST /test-roam", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 56 npc := world.characters[npcID] 57 npc.motion.destination = [3]float32{134, 14, 98} 58 npc.motion.moving, npc.motion.nextMove = true, time.Now() 59 npc.movement.action(npc, 22) 60 packet := npcRoutePacket(npc, npc.position) 61 world.broadcast(950, 0, packet) 62 w.WriteHeader(http.StatusNoContent) 63 })) 64 mux.HandleFunc("POST /test-return", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 65 npc := world.characters[npcID] 66 npc.motion.returning = true 67 w.WriteHeader(http.StatusNoContent) 68 })) 69 mux.HandleFunc("POST /test-hold-roaming", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 70 world.characters[npcID].motion.roaming = false 71 w.WriteHeader(http.StatusNoContent) 72 })) 73 host := httptest.NewServer(mux) 74 defer host.Close() 75 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 76 defer cancel() 77 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_npc_water.mjs"), host.URL, output) 78 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 79 err := command.Run() 80 host.Close() 81 if err := shutdown(); err != nil { 82 t.Fatal(err) 83 } 84 cancel() 85 t.Logf("Browser artifacts: %s", output) 86 87 if err != nil { 88 t.Fatal(err) 89 } 90 if npc.health >= 20 || npc.movement.components[0] != 4 || !npc.movement.deepWater || players[0].health == 0 { 91 t.Fatal("native ordinary hit and pursuit did not retain the living water encounter") 92 } 93 }