server/browser_fallback_placement_test.go (3376B)
1 package main 2 3 import ( 4 "ao/server/assets" 5 "ao/server/gamedata" 6 "context" 7 "crypto/sha256" 8 "encoding/json" 9 "fmt" 10 "net/http" 11 "net/http/httptest" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "testing" 17 "time" 18 ) 19 20 func TestBrowserFallbackPlacementMelee(t *testing.T) { 21 if os.Getenv("AO_GAME_TEST") != "1" { 22 t.Skip("set AO_GAME_TEST=1 for the focused fallback placement check") 23 } 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/fallback-placement", time.Now().Format("20060102-150405")) 27 data := testNPCData() 28 for _, playfield := range gamedata.NPCs.Playfields { 29 if playfield.ID == 950 { 30 data.Playfields[0] = playfield 31 for _, district := range playfield.Districts { 32 if district.SourceIndex != nil && *district.SourceIndex == 5 { 33 data.Playfields[0].Districts = []gamedata.NPCDistrict{district} 34 } 35 } 36 } 37 } 38 data.Playfields[0].Districts[0].Groups[0] = *gamedata.NPCGroups["950/district/5/group/0/district_center_fallback"] 39 data.PlacementStatuses["district_center_fallback"] = gamedata.NPCs.PlacementStatuses["district_center_fallback"] 40 useNPCData(t, data) 41 useResources(t, assets.Files) 42 world := newTestServer(t) 43 enemy := world.characters[1000000] 44 for _, npc := range world.mobileNPCs[950] { 45 npc.motion.nextMove = time.Now().Add(time.Hour) 46 } 47 var players []*character 48 for index := 0; index < 2; index++ { 49 player := world.newCharacter() 50 player.name = fmt.Sprint("Fallback", index) 51 player.position = [3]float32{enemy.position[0] - 4, 20, enemy.position[2] + float32(index)*4} 52 point, normal := player.position, [3]float32{} 53 world.geometry.floor(950, world.terrain[950], player.position, &point, &normal) 54 player.position[1] = point[1] + .01 55 player.rotation = [4]float32{0, .70710677, 0, .70710677} 56 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("fallback-", index)))) 57 if err := world.saveUser(player); err != nil { 58 t.Fatal(err) 59 } 60 players = append(players, player) 61 } 62 enemyID := enemy.id 63 shutdown := startTestService(t, world) 64 mux := http.NewServeMux() 65 mux.Handle("/", world.httpHandler()) 66 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 67 enemy := world.characters[enemyID] 68 json.NewEncoder(w).Encode(map[string]any{"enemy": enemy.id, "position": enemy.position, "home": enemy.motion.home, 69 "near": [2]float32{enemy.motion.home[0] - .75, enemy.motion.home[2]}, 70 "health": enemy.health, "owner": world.characters[1].id, "ownerPosition": predictedPosition(world.characters[1], time.Now()), 71 "ownerFighting": world.characters[1].fighting}) 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_fallback_placement.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 enemy.health >= 20 || enemy.motion.home[1] == 10.8 { 91 t.Fatal("native approach and melee did not reach the grounded fallback", enemy.position, enemy.health) 92 } 93 }