server/browser_corpse_slope_test.go (3895B)
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 TestBrowserAquaanCorpseSlope(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for the recorded Aquaan corpse slope") 21 } 22 t.Run("recorded", func(t *testing.T) { testBrowserAquaanCorpseSlope(t, false) }) 23 // Below the client's one-metre pose-copy threshold, actor removal matters. 24 t.Run("nearby actor", func(t *testing.T) { testBrowserAquaanCorpseSlope(t, true) }) 25 } 26 27 func testBrowserAquaanCorpseSlope(t *testing.T, nearbyActor bool) { 28 t.Helper() 29 _, source, _, _ := runtime.Caller(0) 30 root := filepath.Dir(filepath.Dir(source)) 31 output := filepath.Join(root, "local/alpha-corpse-slope", time.Now().Format("20060102-150405")) 32 world := newTestServer(t) 33 var target *character 34 for _, actor := range world.characters { 35 if actor.npcDefinitionID == "950/district/6/fixed/0" { 36 target = actor 37 } 38 } 39 if target == nil { 40 t.Fatal("missing original training boss placement") 41 } 42 // Reproduce the recorded buried pose without replaying the boss fight. 43 target.position = [3]float32{186, 13.912122, 174.375} 44 target.rotation = [4]float32{0, -.930363, 0, .366640} 45 target.health = 1 46 player := world.newCharacter() 47 player.playfield, player.position = 950, [3]float32{184, 0, 174.375} 48 player.position[1], _ = world.terrain[950].elevation(player.position[0], player.position[2]) 49 player.position[1] += .2 50 player.rotation = [4]float32{0, .70710677, 0, .70710677} 51 if nearbyActor { 52 // The retained opposite-facing control needs only about 20 cm of clearance. 53 target.position = [3]float32{190.5, 15.437122, 179.5} 54 target.rotation = [4]float32{0, .33313298, 0, .94287986} 55 player.position = [3]float32{192.5, 0, 179.5} 56 player.position[1], _ = world.terrain[950].elevation(player.position[0], player.position[2]) 57 player.position[1] += .2 58 player.rotation = [4]float32{0, -.70710677, 0, .70710677} 59 } 60 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("corpse-slope"))) 61 if err := world.saveUser(player); err != nil { 62 t.Fatal(err) 63 } 64 if err := world.flushWorld(); err != nil { 65 t.Fatal(err) 66 } 67 world.combatRoll = landedCombatRoll 68 targetID := target.id 69 target.motion = nil 70 target.regenerationAt = time.Now().Add(5 * time.Minute) 71 shutdown := startTestService(t, world) 72 mux := http.NewServeMux() 73 mux.Handle("/", world.httpHandler()) 74 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 75 player := world.characters[1] 76 target := world.characters[targetID] 77 var items []ownedItem 78 var credits uint32 79 if target.corpse != nil { 80 items, credits = target.corpse.items, target.corpse.credits 81 } 82 json.NewEncoder(w).Encode(map[string]any{"target": target.id, "position": target.position, "rotation": target.rotation, "nearbyActor": nearbyActor, 83 "health": target.health, "corpse": target.corpse != nil, "items": items, "credits": credits, "inventory": player.inventory}) 84 })) 85 host := httptest.NewServer(mux) 86 defer host.Close() 87 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 88 defer cancel() 89 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_corpse_slope.mjs"), host.URL, output) 90 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 91 err := command.Run() 92 host.Close() 93 if err := shutdown(); err != nil { 94 t.Fatal(err) 95 } 96 t.Logf("Browser artifacts: %s", output) 97 if err != nil { 98 t.Fatal(err) 99 } 100 cold := newTestInventoryServer(t) 101 if cold.characters[target.id].position != target.position || cold.characters[target.id].rotation != target.rotation || 102 cold.characters[1].inventory.Credits != player.inventory.Credits { 103 t.Fatal("corpse placement or collected cash changed after cold load") 104 } 105 }