server/browser_idle_health_test.go (2269B)
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 TestBrowserFullHealthReconciliation(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for native idle-health reconciliation") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/idle-health", time.Now().Format("20060102-150405")) 25 t.Chdir(t.TempDir()) 26 world := newDormantServerForTest(t) 27 for i := range 2 { 28 player := world.newCharacter() 29 player.position, player.rotation = [3]float32{52 + float32(i)*2, 14, 56}, [4]float32{0, 0, 0, 1} 30 player.health, player.nano = player.maximumHealth(), player.maximumNano() 31 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("idle-health-", i)))) 32 if err := world.saveUser(player); err != nil { 33 t.Fatal(err) 34 } 35 } 36 shutdown := startTestService(t, world) 37 mux := http.NewServeMux() 38 mux.Handle("/", world.httpHandler()) 39 mux.HandleFunc("POST /test-injure", testPlayerHTTP(world, 1, func(state *server, w http.ResponseWriter, r *http.Request) { 40 // An unannounced fixture injury must be reconciled on the next normal tick. 41 state.characters[1].health -= 9 42 state.clients[1].regenerationAt, state.clients[1].regenerationSeconds = time.Now(), 0 43 w.WriteHeader(http.StatusNoContent) 44 })) 45 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(state *server, w http.ResponseWriter, r *http.Request) { 46 json.NewEncoder(w).Encode(map[string]any{"health": state.characters[1].health, 47 "maximum": state.characters[1].maximumHealth(), "sitting": state.characters[1].sitting}) 48 })) 49 host := httptest.NewServer(mux) 50 t.Cleanup(host.Close) 51 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 52 defer cancel() 53 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_idle_health.mjs"), host.URL, output) 54 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 55 56 err := command.Run() 57 host.Close() 58 59 if failure := shutdown(); failure != nil { 60 t.Fatal(failure) 61 } 62 t.Logf("Browser artifacts: %s", output) 63 if err != nil { 64 t.Fatal(err) 65 } 66 }