server/browser_courtyard_insurance_test.go (2096B)
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 TestBrowserCourtyardInsurance(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for the focused courtyard approach") 21 } 22 _, source, _, _ := runtime.Caller(0) 23 root := filepath.Dir(filepath.Dir(source)) 24 output := filepath.Join(root, "local/courtyard-access", time.Now().Format("20060102-150405")) 25 world := newTestServer(t) 26 player := world.newCharacter() 27 player.name, player.appearance, player.headMesh = "Courtyard", 0x4c5, 40111 28 player.stats = defaultCharacterStats(4) 29 player.health = player.maximumHealth() 30 player.playfield, player.position = 705, [3]float32{902.5, 16.41, 579} 31 player.rotation = [4]float32{0, 1, 0, 0} 32 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("courtyard-check"))) 33 if err := world.saveUser(player); err != nil { 34 t.Fatal(err) 35 } 36 if err := world.savePosition(player); err != nil { 37 t.Fatal(err) 38 } 39 shutdown := startTestService(t, world) 40 mux := http.NewServeMux() 41 mux.Handle("/", world.httpHandler()) 42 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 43 player := world.characters[1] 44 json.NewEncoder(w).Encode(map[string]any{"position": player.position, "scanner": player.resurrectionTerminal}) 45 })) 46 host := httptest.NewServer(mux) 47 defer host.Close() 48 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 49 defer cancel() 50 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_courtyard_insurance.mjs"), host.URL, output) 51 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 52 err := command.Run() 53 host.Close() 54 if err := shutdown(); err != nil { 55 t.Fatal(err) 56 } 57 cancel() 58 t.Logf("Browser artifacts: %s", output) 59 60 if err != nil { 61 t.Fatal(err) 62 } 63 if player.resurrectionTerminal != 3221357249 { 64 t.Fatal("courtyard approach did not reach and save at the original scanner") 65 } 66 }