server/browser_stationary_clinic_test.go (5098B)
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 TestBrowserStationaryClinic(t *testing.T) { 20 if os.Getenv("AO_GAME_TEST") != "1" { 21 t.Skip("set AO_GAME_TEST=1 for stationary surgery clinics") 22 } 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/stationary-clinic", time.Now().Format("20060102-150405")) 26 t.Chdir(t.TempDir()) 27 for _, phase := range []string{"warm", "cold"} { 28 t.Run(phase, func(t *testing.T) { 29 world := newServerForTest(t) 30 if phase == "warm" { 31 for index := 0; index < 2; index++ { 32 player := world.newCharacter() 33 player.appearance, player.profession = 0x115, 1 34 player.playfield, player.position = 1183, [3]float32{176.32516 + float32(index)*2, 5.75, 131.5} 35 player.rotation = [4]float32{0, 1, 0, 0} 36 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("stationary-clinic-", index)))) 37 player.stats = testCharacterStats() 38 if index == 0 { 39 player.level = 200 40 player.stats.Abilities = characterAbilitiesStats{40, 40, 40, 40, 40, 40} 41 player.stats.Abilities.Stamina, player.stats.Abilities.Psychic = 320, 320 42 player.stats.Body.BodyDevelopment, player.stats.Body.NanoPool = 500, 500 43 player.stats.NanoAiding.Treatment = 0 44 player.health, player.nano = 1, 2 45 if player.trainingMaximum(152) < 500 || player.trainingMaximum(132) < 500 { 46 t.Fatal("clinic fixture exceeds native training caps") 47 } 48 } 49 if err := world.saveUser(player); err != nil { 50 t.Fatal(err) 51 } 52 } 53 owner := world.characters[1] 54 inventory := owner.inventory 55 inventory.Credits = 700 56 if err := world.saveInventory(owner, inventory); err != nil { 57 t.Fatal(err) 58 } 59 if granted, err := world.grantItem(owner, 101173); err != nil || !granted { 60 t.Fatal(granted, err) 61 } 62 if _, err := world.db.Exec("CREATE TRIGGER fault_clinic BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(ABORT, 'controlled clinic failure'); END"); err != nil { 63 t.Fatal(err) 64 } 65 } 66 owner := world.characters[1] 67 watcher := world.characters[2] 68 shutdown := startTestService(t, world) 69 mux := http.NewServeMux() 70 mux.Handle("/", world.httpHandler()) 71 mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery)) 72 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 73 owner := world.characters[1] 74 programs := []uint32{} 75 for _, effect := range owner.activeNanos { 76 programs = append(programs, effect.program) 77 } 78 treatment, _ := owner.effectiveStat(124) 79 json.NewEncoder(w).Encode(map[string]any{"phase": phase, "inventory": owner.inventory, "health": owner.health, "nano": owner.nano, 80 "programs": programs, "treatment": treatment, "permission": owner.canChangeImplants(time.Now()), "playfield": owner.playfield}) 81 })) 82 mux.HandleFunc("POST /test-clear-failure", func(w http.ResponseWriter, r *http.Request) { 83 if _, err := world.db.Exec("DROP TRIGGER fault_clinic"); err != nil { 84 t.Error(err) 85 w.WriteHeader(500) 86 } 87 }) 88 mux.HandleFunc("POST /test-zone", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 89 owner := world.characters[1] 90 destination := owner.userData() 91 destination.Playfield, destination.Position = 1180, [3]float32{211.67, 6.16, 173.8} 92 if err := world.updateUser(destination); err != nil { 93 t.Error(err) 94 w.WriteHeader(500) 95 } 96 })) 97 host := httptest.NewServer(mux) 98 t.Cleanup(host.Close) 99 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 100 defer cancel() 101 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_stationary_clinic.mjs"), host.URL, filepath.Join(output, phase)) 102 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 103 err := command.Run() 104 host.Close() 105 if err := shutdown(); err != nil { 106 t.Fatal(err) 107 } 108 copyTestDatabase(t, filepath.Join(output, phase, "world.sqlite")) 109 t.Logf("Browser artifacts: %s", output) 110 111 if err != nil { 112 t.Fatal(err) 113 } 114 expected := []ownedItem{{ID: 1, Type: 51022, Template: 101173, Quality: 1, Slot: 37, Count: 1}} 115 if !slices.Equal(owner.inventory.Items, expected) || owner.inventory.Credits != 400 || owner.health != 1001 || owner.nano != 1002 || 116 len(watcher.inventory.Items) != 0 || watcher.inventory.Credits != 0 || newTestInventoryServer(t).nextItem != 1 { 117 t.Fatal("native clinic use lost resources, fee or implant ownership", owner.inventory, owner.health, owner.nano) 118 } 119 if owner.canChangeImplants(time.Now()) != (phase == "warm") || (phase == "warm" && len(owner.activeNanos) != 1) || (phase == "cold" && len(owner.activeNanos) != 0) { 120 t.Fatal("native clinic replay or zoning retained the wrong access") 121 } 122 }) 123 if t.Failed() { 124 return 125 } 126 } 127 }