server/browser_combat_facing_test.go (2721B)
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 TestBrowserCombatTurnPublication(t *testing.T) { 19 if os.Getenv("AO_GAME_TEST") != "1" { 20 t.Skip("set AO_GAME_TEST=1 for combat turning publication") 21 } 22 23 _, source, _, _ := runtime.Caller(0) 24 root := filepath.Dir(filepath.Dir(source)) 25 output := filepath.Join(root, "local/combat-facing-research", time.Now().Format("20060102-150405")) 26 world := newTestServer(t) 27 var npc *character 28 for _, actor := range world.characters { 29 if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (npc == nil || actor.id < npc.id) { 30 npc = actor 31 } 32 } 33 if npc == nil { 34 t.Fatal("no admitted level-one Leet") 35 } 36 npc.playfield, npc.position, npc.rotation, npc.motion = 705, [3]float32{960, 16.5, 508}, [4]float32{0, 0, 0, 1}, nil 37 world.characters = map[uint32]*character{npc.id: npc} 38 for index := range 2 { 39 player := world.newCharacter() 40 player.playfield, player.position = 705, [3]float32{960 + float32(index*2), 16.5, 511} 41 player.rotation = [4]float32{0, 0, 0, 1} 42 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("combat-facing-", index)))) 43 if err := world.saveUser(player); err != nil { 44 t.Fatal(err) 45 } 46 } 47 if err := world.flushWorld(); err != nil { 48 t.Fatal(err) 49 } 50 npcID := npc.id 51 owner := world.characters[1] 52 owner.rechargeUntil = time.Now().Add(5 * time.Minute) 53 npc.rechargeUntil = owner.rechargeUntil 54 shutdown := startTestService(t, world) 55 mux := http.NewServeMux() 56 mux.Handle("/", world.httpHandler()) 57 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 58 npc := world.characters[npcID] 59 owner := world.characters[1] 60 json.NewEncoder(w).Encode(map[string]any{"npc": npc.id, "rotation": owner.rotation, 61 "position": owner.position, "fighting": owner.fighting, "health": owner.health, "targetHealth": npc.health}) 62 })) 63 host := httptest.NewServer(mux) 64 t.Cleanup(host.Close) 65 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 66 defer cancel() 67 68 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_combat_facing.mjs"), host.URL, output) 69 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 70 err := command.Run() 71 host.Close() 72 if err := shutdown(); err != nil { 73 t.Fatal(err) 74 } 75 t.Logf("Browser artifacts: %s", output) 76 77 if err != nil { 78 t.Fatal(err) 79 } 80 if owner.fighting != 0 || npc.health != 20 { 81 t.Fatal("facing fixture left combat active or executed a held attack") 82 } 83 }