server/browser_stale_stack_join_test.go (3183B)
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 TestBrowserStaleStackJoin(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for stale ordinary stack joins") 22 } 23 for _, missing := range []string{"target", "source"} { 24 t.Run(missing, func(t *testing.T) { testBrowserStaleStackJoin(t, missing) }) 25 } 26 } 27 28 func testBrowserStaleStackJoin(t *testing.T, missing string) { 29 t.Helper() 30 _, source, _, _ := runtime.Caller(0) 31 root := filepath.Dir(filepath.Dir(source)) 32 output := filepath.Join(root, "local/stale-stack-join", time.Now().Format("20060102-150405")) 33 world := newTestServer(t) 34 player := world.newCharacter() 35 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("stale-stack-join"))) 36 if err := world.saveUser(player); err != nil { 37 t.Fatal(err) 38 } 39 for range 3 { 40 if granted, err := world.grantItem(player, 25812); err != nil || !granted { 41 t.Fatal(granted, err) 42 } 43 } 44 for index, count := range []uint16{3, 5, 7} { 45 player.inventory.Items[index].Quality = 2 46 player.inventory.Items[index].Count = count 47 } 48 if err := world.saveInventory(player, player.inventory); err != nil { 49 t.Fatal(err) 50 } 51 shutdown := startTestService(t, world) 52 mux := http.NewServeMux() 53 mux.Handle("/", world.httpHandler()) 54 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 55 player := world.characters[1] 56 json.NewEncoder(w).Encode(player.inventory) 57 })) 58 // Controlled checkpoint: an earlier committed merge has no native refresh. 59 mux.HandleFunc("POST /test-stale-view", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 60 player := world.characters[1] 61 inventory := player.inventory 62 inventory.Items = slices.Clone(inventory.Items[1:]) 63 inventory.Items[0].Count = 8 64 if err := world.saveInventory(player, inventory); err != nil { 65 t.Error(err) 66 http.Error(w, err.Error(), http.StatusInternalServerError) 67 return 68 } 69 gesture := map[string]uint32{"removed": 50, "kept": 48} 70 if missing == "source" { 71 gesture = map[string]uint32{"removed": 48, "kept": 49} 72 } 73 json.NewEncoder(w).Encode(gesture) 74 })) 75 host := httptest.NewServer(mux) 76 t.Cleanup(host.Close) 77 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 78 defer cancel() 79 80 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_stale_stack_join.mjs"), host.URL, output) 81 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 82 err := command.Run() 83 host.Close() 84 if err := shutdown(); err != nil { 85 t.Fatal(err) 86 } 87 t.Logf("Browser artifacts: %s", output) 88 if err != nil { 89 t.Fatal(err) 90 } 91 cold := newTestInventoryServer(t) 92 93 expected := []ownedItem{{ID: 2, Type: 51005, Template: 25812, Quality: 2, Slot: 65, Count: 15}} 94 if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) || 95 cold.nextItem != 3 { 96 t.Fatal("stale join correction or retry changed ownership, quantity or allocation") 97 } 98 }