server/browser_backyard_message_test.go (4400B)
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 TestBrowserBackyardExitExplanation(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for the focused backyard refusal check") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/backyard-message", time.Now().Format("20060102-150405")) 25 world := newTestServer(t) 26 var door zoneDoor 27 for _, candidate := range world.zoneLinks["target"][3000].Doors { 28 if candidate.ID == 3221294008 { 29 door = candidate 30 } 31 } 32 var arrival [3]float32 33 for _, candidate := range world.zoneLinks["target"][705].Doors { 34 if candidate.ID == 3223782081 { 35 arrival = candidate.Arrival.Position 36 } 37 } 38 // Start on the authored backyard arrival's side of the original exit plane. 39 x, y, z, w := door.Rotation[0], door.Rotation[1], door.Rotation[2], door.Rotation[3] 40 normal := [2]float32{2*x*z + 2*y*w, 1 - 2*x*x - 2*y*y} 41 if (arrival[0]-door.Position[0])*normal[0]+(arrival[2]-door.Position[2])*normal[1] < 0 { 42 normal[0], normal[1] = -normal[0], -normal[1] 43 } 44 start := [3]float32{door.Position[0] + 4*normal[0], arrival[1], door.Position[2] + 4*normal[1]} 45 near := [3]float32{door.Position[0] + 0.4*normal[0], arrival[1], door.Position[2] + 0.4*normal[1]} 46 for index := 0; index < 2; index++ { 47 player := world.newCharacter() 48 player.name, player.playfield, player.appearance = fmt.Sprint("Exitmessage", index), 3000, 0x515 49 player.position, player.rotation = start, [4]float32{0, 0, 0, 1} 50 if index == 1 { 51 player.position[0] += 0.8 52 player.position[2] -= 1 53 } 54 player.health, player.nano = player.maximumHealth(), player.maximumNano() 55 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("exit-message-", index)))) 56 if err := world.saveUser(player); err != nil { 57 t.Fatal(err) 58 } 59 player.inventory.Credits = uint32(999 + index) 60 if err := world.saveInventory(player, player.inventory); err != nil { 61 t.Fatal(err) 62 } 63 } 64 shutdown := startTestService(t, world) 65 checks := 0 66 mux := http.NewServeMux() 67 mux.Handle("/", world.httpHandler()) 68 mux.HandleFunc("GET /test-state", func(w http.ResponseWriter, r *http.Request) { 69 var rows []json.RawMessage 70 for _, id := range []uint32{1, 2} { 71 response := httptest.NewRecorder() 72 world.servePlayerHTTP(response, r.Clone(r.Context()), id, func(state *server, w http.ResponseWriter, r *http.Request) { 73 player := state.characters[id] 74 json.NewEncoder(w).Encode(map[string]any{"id": player.id, "playfield": player.playfield, 75 "position": predictedPosition(player, time.Now()), "credits": player.inventory.Credits}) 76 }) 77 if response.Code != http.StatusOK { 78 http.Error(w, response.Body.String(), response.Code) 79 return 80 } 81 rows = append(rows, json.RawMessage(response.Body.Bytes())) 82 } 83 var completed int 84 owner, err := world.world.owner(1) 85 if err == nil { 86 err = owner.call(zoneTestCommand(func(*server) error { completed = checks; return nil })) 87 } 88 if err != nil { 89 http.Error(w, err.Error(), http.StatusInternalServerError) 90 return 91 } 92 json.NewEncoder(w).Encode(map[string]any{"players": rows, "checks": completed, "chats": len(readChatState(t, world).clients), "start": start, "near": near}) 93 }) 94 mux.HandleFunc("POST /test-check", testPlayerHTTP(world, 1, func(state *server, w http.ResponseWriter, r *http.Request) { 95 state.advanceZoneCrossings(time.Now()) 96 checks++ 97 })) 98 host := httptest.NewServer(mux) 99 defer host.Close() 100 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 101 defer cancel() 102 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_backyard_message.mjs"), host.URL, output) 103 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 104 err := command.Run() 105 host.Close() 106 if err := shutdown(); err != nil { 107 t.Fatal(err) 108 } 109 cancel() 110 t.Logf("Browser artifacts: %s", output) 111 112 if err != nil { 113 t.Fatal(err) 114 } 115 cold := newTestInventoryServer(t) 116 if cold.characters[1].playfield != 3000 || cold.characters[1].inventory.Credits != 999 || 117 cold.characters[2].playfield != 705 || cold.characters[2].inventory.Credits != 1000 { 118 t.Fatal("rejected or qualified native exit changed the wrong destination or funds") 119 } 120 }