aorecomp

Anarchy Online classic in the browser

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


server/browser_game_test.go (9066B)


      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 	"reflect"
     12 	"runtime"
     13 	"testing"
     14 	"time"
     15 )
     16 
     17 func TestBrowserStarterEquipment(t *testing.T) {
     18 	if os.Getenv("AO_GAME_TEST") != "1" {
     19 		t.Skip("set AO_GAME_TEST=1 to run the real WASM client in Chromium")
     20 	}
     21 
     22 	_, source, _, _ := runtime.Caller(0)
     23 	root := filepath.Dir(filepath.Dir(source))
     24 	output := filepath.Join(root, "local", "game-tests", "starter-"+time.Now().Format("20060102-150405"))
     25 
     26 	// Use production initialization and random rolls in a disposable world.
     27 	t.Chdir(t.TempDir())
     28 	world := newServerForTest(t)
     29 	shutdown := startTestService(t, world)
     30 	mux := http.NewServeMux()
     31 	mux.Handle("/", world.httpHandler())
     32 	mux.HandleFunc("GET /test-state", testBrowserHTTP(world, func(world *server, w http.ResponseWriter, r *http.Request) {
     33 		player := world.browserCharacter(r)
     34 		enemies := []map[string]any{}
     35 		corpses := []map[string]any{}
     36 		for _, enemy := range world.characters {
     37 			if enemy.corpse != nil && enemy.playfield == player.playfield {
     38 				corpses = append(corpses, map[string]any{"id": enemy.id,
     39 					"items": enemy.corpse.items, "credits": enemy.corpse.credits})
     40 			}
     41 			if enemy.npc && enemy.playfield == player.playfield && enemy.monsterData == 17655 && (enemy.level == 1 || enemy.level == 2) && enemy.health > 0 {
     42 				enemies = append(enemies, map[string]any{"id": enemy.id, "position": enemy.position,
     43 					"level": enemy.level, "health": enemy.health, "maximumHealth": enemy.maximumHealth()})
     44 			}
     45 		}
     46 		missions := []map[string]any{}
     47 		for _, mission := range world.world.queryMissions(missionLookup{exterior: 3000}) {
     48 			if mission.owner != player.id {
     49 				continue
     50 			}
     51 			missions = append(missions, map[string]any{"id": mission.id, "world": mission.world,
     52 				"objective": mission.objective, "completed": mission.completed,
     53 				"definition": mission.offer.definition, "layout": mission.layout})
     54 		}
     55 		var offer any
     56 		if client := world.clients[player.id]; client != nil && client.missionOffer != nil {
     57 			offer = client.missionOffer.definition
     58 		}
     59 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "health": player.health,
     60 			"maximumHealth": player.maximumHealth(), "nano": player.nano, "level": player.level, "experience": player.experience,
     61 			"treatmentLocked": time.Now().Before(player.skillLocks[124]),
     62 			"position":        player.position, "rotation": player.rotation, "stats": player.stats, "enemies": enemies, "corpses": corpses,
     63 			"playfield": player.playfield, "missions": missions, "offer": offer})
     64 	}))
     65 	host := httptest.NewServer(mux)
     66 	t.Cleanup(host.Close)
     67 
     68 	// Advance the ordinary world without changing characters, encounters or rolls.
     69 	ctx, cancel := context.WithTimeout(context.Background(), 90*time.Minute)
     70 	defer cancel()
     71 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_starter_equipment.mjs"), host.URL, output)
     72 	command.Dir = root
     73 	command.Stdout, command.Stderr = os.Stdout, os.Stderr
     74 	err := command.Run()
     75 	missions := world.world.queryMissions(missionLookup{exterior: 3000})
     76 	actors := make(map[uint32]*character)
     77 	listed, listErr := world.adminPlayers()
     78 	if listErr != nil {
     79 		t.Fatal(listErr)
     80 	}
     81 	for _, listedPlayer := range listed {
     82 		callTestZone(t, world, listedPlayer.ID, zoneTestCommand(func(state *server) error {
     83 			actors[listedPlayer.ID] = state.characters[listedPlayer.ID]
     84 			return nil
     85 		}))
     86 	}
     87 	host.Close()
     88 	if err := shutdown(); err != nil {
     89 		t.Fatal(err)
     90 	}
     91 	cancel()
     92 	t.Logf("Browser artifacts: %s", output)
     93 	if failure := os.MkdirAll(output, 0755); failure != nil {
     94 		t.Fatal(failure)
     95 	}
     96 	copyTestDatabase(t, filepath.Join(output, "world.sqlite"))
     97 	if err != nil {
     98 		t.Fatal(err)
     99 	}
    100 
    101 	restored := newServerForTest(t)
    102 	players := 0
    103 	for _, player := range actors {
    104 		if player.name != "Starterowner" && player.name != "Starterwatch" {
    105 			continue
    106 		}
    107 		players++
    108 		if player.health != player.maximumHealth() ||
    109 			player.playfield != restored.characters[player.id].playfield ||
    110 			player.position != restored.characters[player.id].position || player.rotation != restored.characters[player.id].rotation ||
    111 			!reflect.DeepEqual(player.inventory.Items, restored.characters[player.id].inventory.Items) ||
    112 			!reflect.DeepEqual(player.inventory.references(), restored.characters[player.id].inventory.references()) ||
    113 			!reflect.DeepEqual(player.inventory.LearnedNanos, restored.characters[player.id].inventory.LearnedNanos) ||
    114 			!reflect.DeepEqual(player.stats, restored.characters[player.id].stats) ||
    115 			player.health != restored.characters[player.id].health || player.experience != restored.characters[player.id].experience ||
    116 			player.inventory.Credits != restored.characters[player.id].inventory.Credits {
    117 			t.Fatalf("starter ownership changed across cold loading for %s: %+v", player.name, player.inventory)
    118 		}
    119 		if player.name == "Starterwatch" {
    120 			if player.level != 1 || len(player.inventory.Items) != 7 || player.inventory.Credits != 0 || player.experience != 0 || player.stats.ImprovementPoints != 1500 {
    121 				t.Fatalf("observer received another player's progress: %+v", player)
    122 			}
    123 			continue
    124 		}
    125 		wantIP := uint32(5006)
    126 		if len(missions) == 1 && missions[0].offer.definition.RewardItem == 122691 {
    127 			wantIP = 5003
    128 		}
    129 		if player.level != 2 || player.experience < 150 || (player.experience-150)%100 != 0 ||
    130 			player.stats.ImprovementPoints != wantIP || player.stats.Body.BodyDevelopment != 15 || player.stats.Melee.OneHandBlunt != 15 ||
    131 			player.stats.NanoAiding.FirstAid != 7 || player.maximumHealth() != 111 || player.aggDef != 100 || len(player.missions) != 0 ||
    132 			!reflect.DeepEqual(player.inventory.LearnedNanos, []uint32{43371}) {
    133 			t.Fatalf("ordinary starter mission, training or learning was not retained: %+v", player)
    134 		}
    135 	}
    136 	if len(restored.missionExpiry) != 1 || !restored.missionExpiry[0].completed {
    137 		t.Fatal("completed starter mission changed across cold loading")
    138 	}
    139 	live, cold := missions[0], restored.missionExpiry[0]
    140 	if live.id != cold.id || live.owner != cold.owner || live.world != cold.world || live.objective != cold.objective ||
    141 		!reflect.DeepEqual(live.layout, cold.layout) || live.offer.definition.RewardItem != cold.offer.definition.RewardItem ||
    142 		live.offer.definition.RewardQuality != cold.offer.definition.RewardQuality || live.offer.definition.Credits != cold.offer.definition.Credits {
    143 		t.Fatal("starter mission identity, layout or reward changed across cold loading")
    144 	}
    145 	var encounters, killed int
    146 	if err := restored.db.QueryRow("SELECT count(*),sum(killed) FROM mission_encounters").Scan(&encounters, &killed); err != nil {
    147 		t.Fatal(err)
    148 	}
    149 	if encounters != 5 || killed != 5 {
    150 		t.Fatalf("cold mission progress = %d encounters, %d killed; want 5/5", encounters, killed)
    151 	}
    152 	if players != 2 {
    153 		t.Fatalf("created %d ordinary characters, want 2", players)
    154 	}
    155 	testBrowserStarterCold(t, restored, root, filepath.Join(output, "cold"))
    156 }
    157 
    158 func TestBrowserStarterMissionTargets(t *testing.T) {
    159 	if os.Getenv("AO_GAME_TEST") != "1" {
    160 		t.Skip("set AO_GAME_TEST=1 to run the real WASM client in Chromium")
    161 	}
    162 
    163 	_, source, _, _ := runtime.Caller(0)
    164 	root := filepath.Dir(filepath.Dir(source))
    165 	output := filepath.Join(root, "local", "game-tests", "mission-targets-"+time.Now().Format("20060102-150405"))
    166 	world := newTestServer(t)
    167 	shutdown := startTestService(t, world)
    168 	mux := http.NewServeMux()
    169 	mux.Handle("/", world.httpHandler())
    170 	mux.HandleFunc("GET /test-state", testBrowserHTTP(world, func(world *server, w http.ResponseWriter, r *http.Request) {
    171 		player := world.browserCharacter(r)
    172 		json.NewEncoder(w).Encode(map[string]any{"playfield": player.playfield, "inventory": player.inventory})
    173 	}))
    174 	host := httptest.NewServer(mux)
    175 	t.Cleanup(host.Close)
    176 
    177 	// Advance the ordinary travel route without teleporting the character.
    178 	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
    179 	defer cancel()
    180 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_starter_mission_targets.mjs"), host.URL, output)
    181 	command.Dir = root
    182 	command.Stdout, command.Stderr = os.Stdout, os.Stderr
    183 	if err := command.Run(); err != nil {
    184 		t.Fatal(err)
    185 	}
    186 	actors := make(map[uint32]*character)
    187 	listed, listErr := world.adminPlayers()
    188 	if listErr != nil {
    189 		t.Fatal(listErr)
    190 	}
    191 	for _, listedPlayer := range listed {
    192 		callTestZone(t, world, listedPlayer.ID, zoneTestCommand(func(state *server) error {
    193 			actors[listedPlayer.ID] = state.characters[listedPlayer.ID]
    194 			return nil
    195 		}))
    196 	}
    197 	host.Close()
    198 	if err := shutdown(); err != nil {
    199 		t.Fatal(err)
    200 	}
    201 	t.Logf("Browser artifacts: %s", output)
    202 	for _, player := range actors {
    203 		if player.name == "Missionpick" {
    204 			if player.playfield != 3000 || len(player.inventory.Items) != 7 || player.inventory.Credits != 0 ||
    205 				player.level != 1 || player.experience != 0 || len(player.missions) != 0 {
    206 				t.Fatalf("native terminal inspection changed starter progress: %+v", player.userData())
    207 			}
    208 			return
    209 		}
    210 	}
    211 	t.Fatal("normally created character is missing")
    212 }