aorecomp

Anarchy Online classic in the browser

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


server/browser_electronicum_test.go (5118B)


      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 	"reflect"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserElectronicumQualities(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the focused native Electronicum check")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/electronicum-quality", time.Now().Format("20060102-150405"))
     26 	t.Chdir(t.TempDir())
     27 	for _, phase := range []string{"warm", "cold"} {
     28 		t.Run(phase, func(t *testing.T) {
     29 			world := newServerForTest(t)
     30 			var target *character
     31 			for _, actor := range world.characters {
     32 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     33 					target = actor
     34 				}
     35 			}
     36 			if target == nil {
     37 				t.Fatal("no Leet for the controlled shot")
     38 			}
     39 			if phase == "warm" {
     40 				for index := 0; index < 2; index++ {
     41 					player := world.newCharacter()
     42 					player.name = fmt.Sprint("Electronicum", index)
     43 					player.position = target.position
     44 					player.position[0] -= float32(index * 3)
     45 					player.position[2] -= 2
     46 					player.rotation = [4]float32{0, 0, 0, 1}
     47 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("electronicum-", index))))
     48 					if index == 0 {
     49 						player.level, player.profession, player.appearance, player.aggDef = 200, 6, 0x515, 75
     50 						player.stats.Abilities = characterAbilitiesStats{120, 120, 120, 120, 120, 120}
     51 						player.stats.Ranged.Pistol = 195
     52 						if maximum := player.trainingMaximum(112); maximum < 195 {
     53 							t.Fatal("controlled Pistol exceeds native training cap", maximum)
     54 						}
     55 						if pistol, _ := player.effectiveStat(112); pistol != 224 {
     56 							t.Fatal("controlled effective Pistol differs", pistol)
     57 						}
     58 					}
     59 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     60 					if err := world.saveUser(player); err != nil {
     61 						t.Fatal(err)
     62 					}
     63 				}
     64 				// Controlled contracts and target HP isolate delivery, not earned progression.
     65 				owner := world.characters[1]
     66 				for _, item := range []struct{ template, quality uint32 }{
     67 					{160288, 2}, {160289, 49}, {160289, 50}, {160288, 51}, {21609, 1},
     68 				} {
     69 					if granted, err := world.grantItem(owner, item.template); err != nil || !granted {
     70 						t.Fatal(granted, err)
     71 					}
     72 					owner.inventory.Items[len(owner.inventory.Items)-1].Quality = item.quality
     73 				}
     74 				owner.inventory.Items[4].Count = 100
     75 				if err := world.saveInventory(owner, owner.inventory); err != nil {
     76 					t.Fatal(err)
     77 				}
     78 				profile := *target.combatProfile
     79 				profile.Health = 10000
     80 				target.combatProfile, target.health = &profile, 10000
     81 				world.combatRoll = landedCombatRoll
     82 			}
     83 			owner, watcher := world.characters[1], world.characters[2]
     84 			// Keep the controlled target stationary and damage observable.
     85 			target.motion.roaming = false
     86 			target.regenerationAt = time.Now().Add(3 * time.Minute)
     87 			targetID := target.id
     88 			shutdown := startTestService(t, world)
     89 			mux := http.NewServeMux()
     90 			mux.Handle("/", world.httpHandler())
     91 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     92 				owner, watcher := world.characters[1], world.characters[2]
     93 				target := world.characters[targetID]
     94 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory,
     95 					"target": target.id, "health": target.health, "fighting": owner.fighting, "nanoInitiative": owner.equipmentBonus(149), "programming": owner.equipmentBonus(160), "ncu": owner.maximumNCU()})
     96 			}))
     97 			host := httptest.NewServer(mux)
     98 			t.Cleanup(host.Close)
     99 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    100 			defer cancel()
    101 
    102 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_electronicum.mjs"), host.URL, filepath.Join(output, phase))
    103 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    104 			err := command.Run()
    105 			host.Close()
    106 			if err := shutdown(); err != nil {
    107 				t.Fatal(err)
    108 			}
    109 			t.Logf("Browser artifacts: %s", output)
    110 
    111 			if err != nil {
    112 				t.Fatal(err)
    113 			}
    114 			expected := []ownedItem{
    115 				{ID: 1, Type: 51018, Template: 160288, Quality: 2, Slot: 64, Count: 1},
    116 				{ID: 2, Type: 51018, Template: 160289, Quality: 49, Slot: 65, Count: 1},
    117 				{ID: 3, Type: 51018, Template: 160289, Quality: 50, Slot: 6, Count: 1, Ammo: 19},
    118 				{ID: 4, Type: 51018, Template: 160288, Quality: 51, Slot: 67, Count: 1},
    119 				{ID: 5, Type: 51005, Template: 21609, Quality: 1, Slot: 68, Count: 80},
    120 			}
    121 			if !reflect.DeepEqual(owner.inventory.Items, expected) || owner.inventory.Credits != 0 ||
    122 				len(watcher.inventory.Items) != 0 || watcher.inventory.Credits != 0 || newTestInventoryServer(t).nextItem != 5 {
    123 				t.Fatal("native wear, shot or cold recovery lost an instance, round or cash")
    124 			}
    125 		})
    126 		if t.Failed() {
    127 			return
    128 		}
    129 	}
    130 }