aorecomp

Anarchy Online classic in the browser

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


server/browser_hot_test.go (4892B)


      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 TestBrowserHealingOverTimeQueuedHit(t *testing.T) {
     19 	if os.Getenv("AO_GAME_TEST") != "1" {
     20 		t.Skip("set AO_GAME_TEST=1 for the focused native periodic healing check")
     21 	}
     22 	_, source, _, _ := runtime.Caller(0)
     23 	root := filepath.Dir(filepath.Dir(source))
     24 	output := filepath.Join(root, "local/nano-hot", time.Now().Format("20060102-150405"))
     25 	world := newTestServer(t)
     26 	world.combatRoll = landedCombatRoll
     27 	var enemy *character
     28 	for _, actor := range world.characters {
     29 		if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (enemy == nil || actor.id < enemy.id) {
     30 			enemy = actor
     31 		}
     32 	}
     33 	if enemy == nil {
     34 		t.Fatal("no controlled level-one Leet")
     35 	}
     36 	players := []*character{}
     37 	for index := 0; index < 2; index++ {
     38 		player := world.newCharacter()
     39 		player.name, player.level, player.profession = fmt.Sprint("PeriodicHealing", index), 10, 4
     40 		player.stats = testCharacterStats()
     41 		player.stats.Body.BodyDevelopment = 55
     42 		player.stats.NanoAiding.BiologicalMetamorphosis, player.stats.NanoAiding.MatterMetamorphosis = 19, 24
     43 		player.stats.NanoAiding.TimeAndSpace = 19
     44 		player.stats.Body.NanoPool = 100
     45 		player.position, player.rotation = enemy.position, [4]float32{0, 0, 0, 1}
     46 		player.position[2] -= float32(2 + index*3)
     47 		player.health, player.nano = 200, 200
     48 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("hot-", index))))
     49 		if err := world.saveUser(player); err != nil {
     50 			t.Fatal(err)
     51 		}
     52 		players = append(players, player)
     53 	}
     54 	if granted, err := world.grantItem(players[0], 85273); err != nil || !granted {
     55 		t.Fatal(granted, err)
     56 	}
     57 	enemyID := enemy.id
     58 	enemy.regenerationAt = time.Now().Add(5 * time.Minute)
     59 	enemy.motion.roaming = false
     60 	shutdown := startTestService(t, world)
     61 	mux := http.NewServeMux()
     62 	mux.Handle("/", world.httpHandler())
     63 	mux.HandleFunc("POST /test-defer-recovery", testBrowserHTTP(world, deferTestRecovery))
     64 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     65 		enemy := world.characters[enemyID]
     66 		var cast any
     67 		if client := world.clients[world.characters[1].id]; client != nil && client.nanoCast != nil {
     68 			cast = map[string]any{"readyAt": client.nanoCast.readyAt}
     69 		}
     70 		var effects []map[string]any
     71 		for _, effect := range world.characters[1].activeNanos {
     72 			effects = append(effects, map[string]any{"program": effect.program, "ticks": effect.ticksLeft, "nextTick": effect.nextTick})
     73 		}
     74 		json.NewEncoder(w).Encode(map[string]any{"owner": world.characters[1].id, "observer": world.characters[2].id, "enemy": enemy.id,
     75 			"health": world.characters[1].health, "nano": world.characters[1].nano, "reserved": world.characters[1].nanoReserved, "cast": cast,
     76 			"recharge": world.characters[1].nanoRechargeUntil, "effects": effects, "inventory": world.characters[1].inventory,
     77 			"fighting": enemy.fighting, "nextAttack": enemy.nextAttack, "weaponRecharge": enemy.rechargeUntil})
     78 	}))
     79 	// One controlled production swing near the first healing tick isolates queued-hit reconciliation.
     80 	mux.HandleFunc("POST /test-attack", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     81 		enemy := world.characters[enemyID]
     82 		// Observe the first real tick without later healing obscuring its result.
     83 		world.characters[1].activeNanos[0].tickDelay = 5 * time.Minute
     84 		updates := make(map[*worldClient][][]byte)
     85 		world.startAttack(enemy, world.characters[1], time.Now(), updates)
     86 		deadline := enemy.nextAttack
     87 		world.sendCombatUpdates(updates)
     88 		world.advanceCombat(deadline)
     89 		enemy.nextAttack = time.Now().Add(5 * time.Minute)
     90 		w.WriteHeader(http.StatusNoContent)
     91 	}))
     92 	host := httptest.NewServer(mux)
     93 	defer host.Close()
     94 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     95 	defer cancel()
     96 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_healing_over_time.mjs"), host.URL, output)
     97 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     98 	err := command.Run()
     99 	host.Close()
    100 	if err := shutdown(); err != nil {
    101 		t.Fatal(err)
    102 	}
    103 	cancel()
    104 	t.Logf("Browser artifacts: %s", output)
    105 
    106 	if err != nil {
    107 		t.Fatal(err)
    108 	}
    109 	cold := newTestInventoryServer(t).characters[players[0].id]
    110 	if cold.health != players[0].health || cold.nano != 116 || cold.nanoReserved != 0 || !cold.nanoRechargeUntil.Equal(players[0].nanoRechargeUntil) ||
    111 		len(cold.activeNanos) != 1 || cold.activeNanos[0].ticksLeft != 39 || !cold.activeNanos[0].nextTick.IsZero() ||
    112 		cold.activeNanos[0].tickDelay != players[0].activeNanos[0].tickDelay {
    113 		t.Fatal("native HoT lost committed health, cost, progress or its paused interval")
    114 	}
    115 }