aorecomp

Anarchy Online classic in the browser

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


server/browser_mission_cash_test.go (3882B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"context"
      6 	"crypto/sha256"
      7 	"encoding/json"
      8 	"fmt"
      9 	"math"
     10 	"net/http"
     11 	"net/http/httptest"
     12 	"os"
     13 	"os/exec"
     14 	"path/filepath"
     15 	"runtime"
     16 	"testing"
     17 	"time"
     18 )
     19 
     20 func TestBrowserMissionCashResupply(t *testing.T) {
     21 	if os.Getenv("AO_GAME_TEST") != "1" {
     22 		t.Skip("set AO_GAME_TEST=1 for mission-funded resupply")
     23 	}
     24 	_, source, _, _ := runtime.Caller(0)
     25 	root := filepath.Dir(filepath.Dir(source))
     26 	output := filepath.Join(root, "local/alpha-mission-cash", time.Now().Format("20060102-150405"))
     27 	for _, shop := range missionCashShops {
     28 		t.Run(fmt.Sprint(shop.booth), func(t *testing.T) {
     29 			world, _ := newMissionCashWorld(t, shop.booth, 1, 6)
     30 			collectMissionCashObjective(t, world)
     31 			player := world.characters[1]
     32 			player.appearance = shop.appearance
     33 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("mission-cash")))
     34 			if err := world.saveUser(player); err != nil {
     35 				t.Fatal(err)
     36 			}
     37 
     38 			// Separate cold checkpoints cover hand-in and resupply without replaying travel.
     39 			// The fixture seeds the generation fee; all purchase funds come from hand-in.
     40 			for _, phase := range []string{"hand-in", "resupply"} {
     41 				position, rotation := gamedata.MissionBooths[shop.booth].Position, [4]float32{0, 0, 0, 1}
     42 				player.playfield = gamedata.MissionBooths[shop.booth].Playfield
     43 				if phase == "resupply" {
     44 					machine := gamedata.ShopMachines[shop.machine]
     45 					player.playfield, position, rotation = machine.Playfield, machine.Position, machine.Rotation
     46 				}
     47 				forward, _ := bodyAxes(rotation)
     48 				player.position = [3]float32{position[0] + 3*forward[0], position[1] + .5, position[2] + 3*forward[2]}
     49 				yaw := math.Atan2(float64(position[0]-player.position[0]), float64(position[2]-player.position[2]))
     50 				player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))}
     51 				if err := world.savePosition(player); err != nil {
     52 					t.Fatal(err)
     53 				}
     54 				if err := world.closeDatabase(); err != nil {
     55 					t.Fatal(err)
     56 				}
     57 				world = newServerForTest(t)
     58 				player = world.characters[1]
     59 				missionID := world.missionExpiry[0].id
     60 				shutdown := startTestService(t, world)
     61 				mux := http.NewServeMux()
     62 				mux.Handle("/", world.httpHandler())
     63 				mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     64 					player := world.characters[1]
     65 					open, purchases := false, 0
     66 					if client := world.clients[1]; client != nil && client.shop != nil {
     67 						open = true
     68 						for _, count := range client.shop.purchases {
     69 							purchases += int(count)
     70 						}
     71 					}
     72 					json.NewEncoder(w).Encode(map[string]any{"phase": phase, "booth": shop.booth, "machine": shop.machine,
     73 						"supplyCost": shop.supplyCost, "inventory": player.inventory, "completed": world.world.queryMissions(missionLookup{id: missionID})[0].completed,
     74 						"open": open, "purchases": purchases})
     75 				}))
     76 				host := httptest.NewServer(mux)
     77 				ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     78 				command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_mission_cash.mjs"), host.URL,
     79 					filepath.Join(output, fmt.Sprint(shop.booth), phase))
     80 				command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     81 				err := command.Run()
     82 				host.Close()
     83 				if err := shutdown(); err != nil {
     84 					t.Fatal(err)
     85 				}
     86 				cancel()
     87 				host.Close()
     88 				if err != nil {
     89 					t.Fatal(err)
     90 				}
     91 				world = newServerForTest(t)
     92 				player = world.characters[1]
     93 			}
     94 			cold := newTestInventoryServer(t).characters[1].inventory
     95 			if cold.Credits != 500-shop.supplyCost || cold.LastID != 8 || len(cold.Items) != 6 {
     96 				t.Fatal("completion-funded purchase changed during cold recovery", cold)
     97 			}
     98 			t.Logf("Browser artifacts: %s", filepath.Join(output, fmt.Sprint(shop.booth)))
     99 		})
    100 	}
    101 }