aorecomp

Anarchy Online classic in the browser

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


server/browser_health_payment_test.go (3237B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"encoding/json"
      6 	"net/http"
      7 	"net/http/httptest"
      8 	"os"
      9 	"os/exec"
     10 	"path/filepath"
     11 	"runtime"
     12 	"testing"
     13 	"time"
     14 )
     15 
     16 func TestBrowserHealthPayment(t *testing.T) {
     17 	if os.Getenv("AO_GAME_TEST") != "1" {
     18 		t.Skip("set AO_GAME_TEST=1 for native Trader team healing and debt")
     19 	}
     20 	_, source, _, _ := runtime.Caller(0)
     21 	root := filepath.Dir(filepath.Dir(source))
     22 	output := filepath.Join(root, "local/health-payment", time.Now().Format("20060102-150405"))
     23 	world := newBrowserPetWorld(t)
     24 	for _, id := range []uint32{1, 2} {
     25 		player := world.characters[id]
     26 		player.profession, player.level = 7, 10
     27 		player.stats.Body.BodyDevelopment, player.stats.Body.NanoPool = 55, 50
     28 		player.stats.NanoAiding.BiologicalMetamorphosis, player.stats.NanoAiding.TimeAndSpace = 10, 9
     29 		player.health, player.nano, player.inventory.LearnedNanos = 100, 100, nil
     30 		if err := world.saveUser(player); err != nil {
     31 			t.Fatal(err)
     32 		}
     33 		if err := world.saveInventory(player, player.inventory); err != nil {
     34 			t.Fatal(err)
     35 		}
     36 		world.clients[id] = &worldClient{id: id, conn: &combatTestConn{}, ready: true, target: true}
     37 	}
     38 	formTestTeam(t, world, 1, 2)
     39 	delete(world.clients, 1)
     40 	delete(world.clients, 2)
     41 	if ok, err := world.grantItem(world.characters[1], 43378); err != nil || !ok {
     42 		t.Fatal(ok, err)
     43 	}
     44 
     45 	actors := world.characters
     46 	shutdown := startTestService(t, world)
     47 	mux := http.NewServeMux()
     48 	mux.Handle("/", world.httpHandler())
     49 	mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     50 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     51 		owner, member := world.characters[1], world.characters[2]
     52 		var ticks uint32
     53 		if len(owner.activeNanos) != 0 {
     54 			ticks = owner.activeNanos[0].ticksLeft
     55 		}
     56 		json.NewEncoder(w).Encode(map[string]any{"health": owner.health, "memberHealth": member.health,
     57 			"nano": owner.nano, "reserved": owner.nanoReserved, "ticks": ticks, "active": len(owner.activeNanos),
     58 			"learned": owner.inventory.LearnedNanos, "items": len(owner.inventory.Items)})
     59 	}))
     60 	host := httptest.NewServer(mux)
     61 	defer host.Close()
     62 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     63 	defer cancel()
     64 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_health_payment.mjs"), host.URL, output)
     65 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     66 	err := command.Run()
     67 	host.Close()
     68 	if err := shutdown(); err != nil {
     69 		t.Fatal(err)
     70 	}
     71 	cancel()
     72 	t.Logf("Browser artifacts: %s", output)
     73 	if err != nil {
     74 		t.Fatal(err)
     75 	}
     76 
     77 	// Pending debt, healed teammates and two exact debits survive cold recovery.
     78 	cold := newServerForTest(t)
     79 	owner, saved := actors[1], cold.characters[1]
     80 	if saved.health != owner.health || saved.nano != 82 || saved.nanoReserved != 0 || len(saved.inventory.Items) != 0 ||
     81 		len(saved.activeNanos) != 1 || saved.activeNanos[0].program != 43372 || saved.activeNanos[0].ticksLeft != owner.activeNanos[0].ticksLeft ||
     82 		saved.activeNanos[0].tickDelay != owner.activeNanos[0].tickDelay || cold.characters[2].health != actors[2].health {
     83 		t.Fatal("native team heal or pending debt changed on cold recovery")
     84 	}
     85 }