aorecomp

Anarchy Online classic in the browser

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


server/browser_scanner_refusal_test.go (2351B)


      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 TestBrowserScannerReclaimRefusal(t *testing.T) {
     19 	if os.Getenv("AO_GAME_TEST") != "1" {
     20 		t.Skip("set AO_GAME_TEST=1 for the focused scanner refusal check")
     21 	}
     22 
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/scanner-refusal", time.Now().Format("20060102-150405"))
     26 	world := newTestServer(t)
     27 	for _, availability := range []time.Duration{time.Hour, -time.Hour} {
     28 		player := world.newCharacter()
     29 		player.name, player.level, player.appearance = fmt.Sprint("Reclaim scanner ", player.id), 10, 0x515
     30 		player.playfield, player.position = 705, [3]float32{598, 16.9, 329}
     31 		player.rotation = [4]float32{0, 0.70710677, 0, 0.70710677}
     32 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("scanner-", player.id))))
     33 		if err := world.saveUser(player); err != nil {
     34 			t.Fatal(err)
     35 		}
     36 		if granted, err := world.grantItem(player, 42640); !granted || err != nil {
     37 			t.Fatal(granted, err)
     38 		}
     39 		inventory := player.inventory
     40 		inventory.Reclaim = []reclaimItem{{inventory.Items[0], time.Now().Add(availability).UTC()}}
     41 		inventory.Items = nil
     42 		if err := world.saveInventory(player, inventory); err != nil {
     43 			t.Fatal(err)
     44 		}
     45 	}
     46 	shutdown := startTestService(t, world)
     47 	mux := http.NewServeMux()
     48 	mux.Handle("/", world.httpHandler())
     49 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     50 		player := world.browserCharacter(r)
     51 		json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory,
     52 			"insurance": player.resurrectionTerminal, "insuredExperience": player.insuredExperience})
     53 	}))
     54 	host := httptest.NewServer(mux)
     55 	t.Cleanup(host.Close)
     56 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     57 	defer cancel()
     58 
     59 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_scanner_refusal.mjs"), host.URL, output)
     60 	command.Dir = root
     61 	command.Stdout, command.Stderr = os.Stdout, os.Stderr
     62 	err := command.Run()
     63 	host.Close()
     64 	if err := shutdown(); err != nil {
     65 		t.Fatal(err)
     66 	}
     67 
     68 	if err != nil {
     69 		t.Fatal(err)
     70 	}
     71 	t.Logf("Browser artifacts: %s", output)
     72 }