server/browser_backyard_bank_test.go (4074B)
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 "slices" 15 "testing" 16 "time" 17 ) 18 19 func TestBrowserBackyardBank(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for the focused native bank check") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/backyard-bank", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 27 for index, point := range []struct { 28 playfield uint32 29 position [3]float32 30 rotation [4]float32 31 }{ 32 {3000, [3]float32{109.204704, 115.01, 149.970703}, [4]float32{0, 0, 0, 1}}, 33 {1183, [3]float32{193.79826, 5.01, 136.83983}, [4]float32{0, 0.70710677, 0, 0.70710677}}, 34 } { 35 player := world.newCharacter() 36 player.name = fmt.Sprint("Bankcheck", index) 37 player.playfield, player.position, player.rotation = point.playfield, point.position, point.rotation 38 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("bank-check-", index)))) 39 if err := world.saveUser(player); err != nil { 40 t.Fatal(err) 41 } 42 if err := world.savePosition(player); err != nil { 43 t.Fatal(err) 44 } 45 if granted, err := world.grantItem(player, 42640); err != nil || !granted { 46 t.Fatal(granted, err) 47 } 48 } 49 actors := world.characters 50 shutdown := startTestService(t, world) 51 testState := func(w http.ResponseWriter, r *http.Request) { 52 states := make(map[uint32]json.RawMessage) 53 for _, id := range []uint32{1, 2} { 54 response := httptest.NewRecorder() 55 world.servePlayerHTTP(response, r.Clone(r.Context()), id, func(state *server, w http.ResponseWriter, r *http.Request) { 56 client := state.clients[id] 57 json.NewEncoder(w).Encode(map[string]any{"inventory": state.characters[id].inventory, "bankOpen": client != nil && client.bank != nil}) 58 }) 59 if response.Code != http.StatusOK { 60 http.Error(w, response.Body.String(), response.Code) 61 return 62 } 63 states[id] = json.RawMessage(response.Body.Bytes()) 64 } 65 json.NewEncoder(w).Encode(states) 66 } 67 mux := http.NewServeMux() 68 mux.Handle("/", world.httpHandler()) 69 mux.HandleFunc("GET /test-state", testState) 70 host := httptest.NewServer(mux) 71 defer host.Close() 72 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 73 defer cancel() 74 75 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_backyard_bank.mjs"), host.URL, output, "live") 76 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 77 err := command.Run() 78 t.Logf("Browser artifacts: %s", output) 79 80 if err != nil { 81 t.Fatal(err) 82 } 83 host.Close() 84 if err := shutdown(); err != nil { 85 t.Fatal(err) 86 } 87 cold := newServerForTest(t) 88 for _, id := range []uint32{1, 2} { 89 if len(actors[id].inventory.Items) != 0 || len(actors[id].inventory.Bank) != 1 || 90 !slices.Equal(actors[id].inventory.Items, cold.characters[id].inventory.Items) || 91 !slices.Equal(actors[id].inventory.Bank, cold.characters[id].inventory.Bank) || 92 actors[id].inventory.Credits != cold.characters[id].inventory.Credits { 93 t.Fatal("native bank deposit changed across cold recovery", id) 94 } 95 } 96 world = cold 97 actors = cold.characters 98 stopCold := startTestService(t, cold) 99 coldMux := http.NewServeMux() 100 coldMux.Handle("/", cold.httpHandler()) 101 coldMux.HandleFunc("GET /test-state", testState) 102 coldHost := httptest.NewServer(coldMux) 103 defer coldHost.Close() 104 command = exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_backyard_bank.mjs"), coldHost.URL, filepath.Join(output, "cold"), "cold") 105 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 106 if err := command.Run(); err != nil { 107 t.Fatal(err) 108 } 109 coldHost.Close() 110 if err := stopCold(); err != nil { 111 t.Fatal(err) 112 } 113 for _, id := range []uint32{1, 2} { 114 if len(actors[id].inventory.Items) != 1 || len(actors[id].inventory.Bank) != 0 || actors[id].inventory.Items[0].ID != id { 115 t.Fatal("cold native withdrawal lost the exact instance", id) 116 } 117 } 118 }