aorecomp

Anarchy Online classic in the browser

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


server/browser_resource_pickup_test.go (4420B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"crypto/sha256"
      6 	"encoding/json"
      7 	"fmt"
      8 	"math"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"slices"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserWorldResourcePickup(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for original world-resource pickup")
     23 	}
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/resource-pickup", time.Now().Format("20060102-150405"))
     27 	t.Chdir(t.TempDir())
     28 	world := newServerForTest(t)
     29 	resource := world.worldResources[1000668]
     30 	if resource.template != 150274 || resource.quality != 1 || resource.position != [3]float32{318.12518, .0011, 196.0258} {
     31 		t.Fatal("retained original Carbonrich checkpoint changed", resource)
     32 	}
     33 	for index := range 2 {
     34 		player := world.newCharacter()
     35 		player.name, player.playfield = []string{"Rockowner", "Rockwatch"}[index], resource.playfield
     36 		player.position = [3]float32{resource.position[0] + 2, resource.position[1] + .75, resource.position[2] - float32(index)*3}
     37 		angle := math.Atan2(float64(resource.position[0]-player.position[0]), float64(resource.position[2]-player.position[2])) / 2
     38 		player.rotation = [4]float32{0, float32(math.Sin(angle)), 0, float32(math.Cos(angle))}
     39 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("resource-pickup-", index))))
     40 		if err := world.saveUser(player); err != nil {
     41 			t.Fatal(err)
     42 		}
     43 		if err := world.savePosition(player); err != nil {
     44 			t.Fatal(err)
     45 		}
     46 	}
     47 	if err := world.ensureWorldResources(resource.playfield); err != nil {
     48 		t.Fatal(err)
     49 	}
     50 	var itemID uint32
     51 	for id, placement := range world.worldItems {
     52 		if placement.resource == 1000668 {
     53 			itemID = id
     54 		}
     55 	}
     56 	if itemID == 0 {
     57 		t.Fatal("original resource did not allocate its world instance")
     58 	}
     59 	lastID := world.nextItem
     60 	run := func(t *testing.T, world *server, script, phaseOutput string) {
     61 		t.Helper()
     62 		actors := world.characters
     63 		shutdown := startTestService(t, world)
     64 		mux := http.NewServeMux()
     65 		mux.Handle("/", world.httpHandler())
     66 		mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     67 			_, onGround := world.worldItems[itemID]
     68 			json.NewEncoder(w).Encode(map[string]any{"item": itemID, "definition": 1000668, "playfield": resource.playfield,
     69 				"inventory": world.characters[1].inventory, "observer": world.characters[2].inventory,
     70 				"onGround": onGround, "depleted": world.depletedResources[1000668], "lastID": world.identities.items.Load(),
     71 				"health": world.characters[1].health, "nano": world.characters[1].nano})
     72 		}))
     73 		mux.HandleFunc("POST /test-clear-failure", func(w http.ResponseWriter, r *http.Request) {
     74 			if _, err := world.db.Exec("DROP TRIGGER fail_collection"); err != nil {
     75 				http.Error(w, err.Error(), http.StatusInternalServerError)
     76 			}
     77 		})
     78 		host := httptest.NewServer(mux)
     79 		t.Cleanup(host.Close)
     80 		ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     81 		defer cancel()
     82 		command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/"+script), host.URL, phaseOutput)
     83 		command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     84 		err := command.Run()
     85 		host.Close()
     86 		if err := shutdown(); err != nil {
     87 			t.Fatal(err)
     88 		}
     89 		t.Logf("Browser artifacts: %s", output)
     90 		if err != nil {
     91 			t.Fatal(err)
     92 		}
     93 		cold := newTestInventoryServer(t)
     94 		expected := []ownedItem{{ID: itemID, Type: 51005, Template: 150274, Quality: 1, Slot: 64, Count: 1}}
     95 		if !slices.Equal(actors[1].inventory.Items, expected) || len(actors[2].inventory.Items) != 0 ||
     96 			!cold.depletedResources[1000668] || cold.nextItem != lastID {
     97 			t.Fatal("native collection or replay changed identity, counter, depletion or another owner")
     98 		}
     99 	}
    100 	t.Run("warm", func(t *testing.T) {
    101 		registerTestDatabaseCleanup(t, world)
    102 		if _, err := world.db.Exec("CREATE TRIGGER fail_collection BEFORE INSERT ON resource_depletions BEGIN SELECT RAISE(ABORT,'collection'); END"); err != nil {
    103 			t.Fatal(err)
    104 		}
    105 		run(t, world, "test_resource_pickup.mjs", filepath.Join(output, "warm"))
    106 	})
    107 	if t.Failed() {
    108 		return
    109 	}
    110 	t.Run("cold", func(t *testing.T) {
    111 		cold := newServerForTest(t)
    112 		run(t, cold, "test_resource_pickup_cold.mjs", filepath.Join(output, "cold"))
    113 		copyTestDatabase(t, filepath.Join(output, "world.sqlite"))
    114 	})
    115 }