aorecomp

Anarchy Online classic in the browser

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


server/browser_walk_approach_test.go (3012B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"crypto/sha256"
      6 	"encoding/json"
      7 	"fmt"
      8 	"math"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserWalkingFinalApproach(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for native walking final approaches")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/walk-final-approach", time.Now().Format("20060102-150405"))
     26 	for _, speed := range []uint16{5, 100} {
     27 		t.Run(fmt.Sprint(speed), func(t *testing.T) {
     28 			world, _ := newMissionEntranceWorld(t)
     29 			if err := world.handleMovement(world.clients[1], playerMovementRequest(world.characters[1], 1)); err != nil {
     30 				t.Fatal(err)
     31 			}
     32 			player := world.characters[1]
     33 			if speed == 100 {
     34 				player.level = 100
     35 				player.stats.Abilities = characterAbilitiesStats{60, 60, 60, 60, 60, 60}
     36 				player.running = true
     37 			}
     38 			player.stats.Speed.RunSpeed = speed
     39 			player.health = player.maximumHealth()
     40 			if uint32(speed) > player.trainingMaximum(156) {
     41 				t.Fatal("walking fixture exceeds the native skill cap")
     42 			}
     43 			player.position, player.rotation, player.movement = [3]float32{15.01, .011, 30.5}, [4]float32{0, 0, 0, 1}, playerMovement{}
     44 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("walk-approach")))
     45 			if err := world.saveUser(player); err != nil {
     46 				t.Fatal(err)
     47 			}
     48 			if err := world.savePosition(player); err != nil {
     49 				t.Fatal(err)
     50 			}
     51 			if err := world.closeDatabase(); err != nil {
     52 				t.Fatal(err)
     53 			}
     54 			world = newServerForTest(t)
     55 			shutdown := startTestService(t, world)
     56 			mux := http.NewServeMux()
     57 			mux.Handle("/", world.httpHandler())
     58 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     59 				json.NewEncoder(w).Encode(map[string]any{"position": world.characters[1].position,
     60 					"inventory": world.characters[1].inventory, "speed": world.characters[1].stats.Speed.RunSpeed, "running": world.characters[1].running})
     61 			}))
     62 			host := httptest.NewServer(mux)
     63 			t.Cleanup(host.Close)
     64 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     65 			defer cancel()
     66 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_walk_approach.mjs"), host.URL, filepath.Join(output, fmt.Sprint(speed)))
     67 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     68 			if err := command.Run(); err != nil {
     69 				t.Fatal(err)
     70 			}
     71 			host.Close()
     72 			if err := shutdown(); err != nil {
     73 				t.Fatal(err)
     74 			}
     75 			cancel()
     76 			cold := reloadMissionEntranceWorld(t)
     77 
     78 			position := cold.characters[1].position
     79 			// Preserve native arrival tolerance plus compact-pose quantization.
     80 			if math.Hypot(float64(position[0]-15), float64(position[2]-43)) >= .55 {
     81 				t.Fatal("walking completion did not survive cold loading", position)
     82 			}
     83 			t.Logf("Browser artifacts: %s", output)
     84 		})
     85 	}
     86 }