server/browser_pending_edit_test.go (2531B)
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 TestBrowserPendingEquipmentEdit(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for a live edit during pending equipment") 22 } 23 24 _, source, _, _ := runtime.Caller(0) 25 root := filepath.Dir(filepath.Dir(source)) 26 output := filepath.Join(root, "local/pending-edit", time.Now().Format("20060102-150405")) 27 world := newTestServer(t) 28 player := world.newCharacter() 29 player.stats = testCharacterStats() 30 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-edit"))) 31 if err := world.saveUser(player); err != nil { 32 t.Fatal(err) 33 } 34 if granted, err := world.grantItem(player, 209283); err != nil || !granted { 35 t.Fatal(granted, err) 36 } 37 holdTestEquipmentCompletions(t, world) 38 shutdown := startTestService(t, world) 39 mux := http.NewServeMux() 40 mux.Handle("/", world.httpHandler()) 41 mux.HandleFunc("POST /test-edit/{character}", world.playerHTTP((*server).editUser)) 42 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 43 player := world.characters[1] 44 client := world.clients[1] 45 json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, 46 "pending": client != nil && client.equipment != nil}) 47 })) 48 // Complete the post-edit retry only after native admission is observed. 49 mux.HandleFunc("POST /test-complete", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 50 finish := world.clients[1].equipment.readyAt 51 world.advanceEquipment(finish) 52 })) 53 host := httptest.NewServer(mux) 54 t.Cleanup(host.Close) 55 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 56 defer cancel() 57 58 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_edit.mjs"), host.URL, output) 59 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 60 if err := command.Run(); err != nil { 61 t.Fatal(err) 62 } 63 host.Close() 64 if err := shutdown(); err != nil { 65 t.Fatal(err) 66 } 67 cold := newTestInventoryServer(t) 68 69 expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}} 70 if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) { 71 t.Fatal("live edit lost or changed the pending weapon") 72 } 73 t.Logf("Browser artifacts: %s", output) 74 }