aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/browser_social_swimming_test.go (3260B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"crypto/sha256"
      6 	"encoding/binary"
      7 	"encoding/json"
      8 	"fmt"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserSocialSwimmingRefusal(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the swimming social-command check")
     22 	}
     23 
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/social-swimming", time.Now().Format("20060102-150405"))
     27 	world := newTestServer(t)
     28 	player, peer := world.newCharacter(), world.newCharacter()
     29 	world.characters = map[uint32]*character{player.id: player, peer.id: peer}
     30 	for _, actor := range []*character{player, peer} {
     31 		actor.playfield, actor.position = 1183, [3]float32{193.79826, 5.01, 136.83983}
     32 	}
     33 	peer.position[2] += 2
     34 	player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("social-swimming")))
     35 	for _, actor := range []*character{player, peer} {
     36 		if err := world.saveUser(actor); err != nil {
     37 			t.Fatal(err)
     38 		}
     39 	}
     40 	observer := &combatTestConn{}
     41 	peerClient := &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer}
     42 	world.clients[2] = peerClient
     43 	shutdown := startTestService(t, world)
     44 	mux := http.NewServeMux()
     45 	mux.Handle("/", world.httpHandler())
     46 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     47 		player := world.characters[1]
     48 		peerClient.mutex.Lock()
     49 		defer peerClient.mutex.Unlock()
     50 		count := 0
     51 		for _, packet := range observer.packets {
     52 			if binary.BigEndian.Uint32(packet[16:20]) == 0x3b290771 {
     53 				count++
     54 			}
     55 		}
     56 		json.NewEncoder(w).Encode(map[string]any{"mode": player.movement.components[0], "observerCommands": count,
     57 			"health": player.health, "nano": player.nano, "inventory": player.inventory})
     58 	}))
     59 	// Model a command emitted on land but processed after authoritative water entry.
     60 	// Withhold movement publication so the native sender can submit that late command.
     61 	mux.HandleFunc("POST /test-water", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 		player := world.characters[1]
     63 		player.movement.prepare(player, time.Now())
     64 		if player.movement.components[0] == 4 {
     65 			player.movement.components[0], player.movement.deepWater = 2, false
     66 		} else {
     67 			player.movement.components[0], player.movement.deepWater = 4, true
     68 		}
     69 	}))
     70 	host := httptest.NewServer(mux)
     71 	t.Cleanup(host.Close)
     72 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     73 	defer cancel()
     74 
     75 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_social_swimming.mjs"), host.URL, output)
     76 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     77 	err := command.Run()
     78 	host.Close()
     79 	if err := shutdown(); err != nil {
     80 		t.Fatal(err)
     81 	}
     82 	t.Logf("Browser artifacts: %s", output)
     83 
     84 	if err != nil {
     85 		t.Fatal(err)
     86 	}
     87 	cold := newTestInventoryServer(t).characters[1]
     88 	if cold.health != player.health || cold.nano != player.nano || len(cold.inventory.Items) != 0 || newTestInventoryServer(t).nextItem != 0 {
     89 		t.Fatal("social-command eligibility changed durable player resources or inventory")
     90 	}
     91 }