aorecomp

Anarchy Online classic in the browser

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


server/item_sources_test.go (11980B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"context"
      7 	"encoding/json"
      8 	"image/png"
      9 	"net/http/httptest"
     10 	"os"
     11 	"os/exec"
     12 	"path/filepath"
     13 	"runtime"
     14 	"slices"
     15 	"strconv"
     16 	"strings"
     17 	"testing"
     18 	"time"
     19 )
     20 
     21 func TestItemSourcesIncludeShopsLootCraftingAndStarterFamilies(t *testing.T) {
     22 	sources := newTestServer(t).itemCatalog().families
     23 	armor := sources[itemFamily(85595)]
     24 	if !slices.ContainsFunc(armor, func(source *itemSource) bool {
     25 		return source.kind == "Shop" && source.quality == [2]uint32{5, 5} && len(source.locations) > 0
     26 	}) {
     27 		t.Fatal("high endpoint did not find the QL5 armor sellers")
     28 	}
     29 	if !slices.ContainsFunc(sources[itemFamily(42640)], func(source *itemSource) bool {
     30 		return source.kind == "Loot" && source.chance == 50 && len(source.locations) > 0
     31 	}) {
     32 		t.Fatal("Monster Parts lost its mapped 50% monster drops")
     33 	}
     34 	if !slices.ContainsFunc(sources[itemFamily(158416)], func(source *itemSource) bool { return source.kind == "Crafted" }) {
     35 		t.Fatal("NCU Hacker Interface lost its recipe")
     36 	}
     37 	if !slices.ContainsFunc(sources[itemFamily(121567)], func(source *itemSource) bool { return source.kind == "Starter equipment" }) {
     38 		t.Fatal("starter pistol lost its issuance source")
     39 	}
     40 }
     41 
     42 func TestItemSourcePagesAreReadOnlyAndFilterFamilies(t *testing.T) {
     43 	world := newTestServer(t)
     44 	handler := adminTestHandler(world)
     45 	for _, path := range []string{"/admin/items/catalog/85595", "/admin/shops/99491"} {
     46 		page := httptest.NewRecorder()
     47 		handler.ServeHTTP(page, httptest.NewRequest("GET", path, nil))
     48 		if page.Code != 200 || !strings.Contains(page.Body.String(), "admin/map?playfield=") || !strings.Contains(page.Body.String(), "class='item-icon'") || strings.Contains(page.Body.String(), "method='post'") && strings.Contains(page.Body.String(), "name='data'") {
     49 			t.Fatal(path, "lost read-only source data or icons")
     50 		}
     51 		post := httptest.NewRecorder()
     52 		handler.ServeHTTP(post, httptest.NewRequest("POST", path, strings.NewReader("")))
     53 		if post.Code != 405 {
     54 			t.Fatal(path, "allows mutations", post.Code)
     55 		}
     56 	}
     57 	for _, filter := range []string{"shops", "no-shops", "unknown"} {
     58 		page := httptest.NewRecorder()
     59 		handler.ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/catalog?catalog=85595&source="+filter, nil))
     60 		found := strings.Contains(page.Body.String(), "value='85595'>Create")
     61 		if page.Code != 200 || found != (filter == "shops") {
     62 			t.Fatal("family source filter failed", filter, page.Code)
     63 		}
     64 	}
     65 }
     66 
     67 func TestItemCatalogLootFilterIncludesShopAndLootOnlyFamilies(t *testing.T) {
     68 	world := newTestServer(t)
     69 	handler := adminTestHandler(world)
     70 	for _, scenario := range []struct {
     71 		template string
     72 		found    bool
     73 	}{
     74 		{"42640", true},  // Monster Parts are loot-only.
     75 		{"85595", true},  // The high endpoint shares the gloves' shops and boss drops.
     76 		{"95577", false}, // Lock Picks are sold, but have no configured drops.
     77 		{"209491", true}, // Inferred Subway flea salvage.
     78 	} {
     79 		page := httptest.NewRecorder()
     80 		handler.ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/catalog?source=loot&catalog="+scenario.template, nil))
     81 
     82 		found := strings.Contains(page.Body.String(), "value='"+scenario.template+"'>Create")
     83 		if page.Code != 200 || found != scenario.found || !strings.Contains(page.Body.String(), "<option value='loot' selected>Available as loot</option>") {
     84 			t.Fatal("loot filter returned the wrong family or lost its selection", scenario.template, page.Code)
     85 		}
     86 		if found && !strings.Contains(page.Body.String(), "Loot:") {
     87 			t.Fatal("catalog row omitted its loot source", scenario.template)
     88 		}
     89 	}
     90 }
     91 
     92 func TestItemCatalogCategoryCombinesWithNameAndSource(t *testing.T) {
     93 	handler := adminTestHandler(newTestServer(t))
     94 	for _, scenario := range []struct {
     95 		template, category, source, label string
     96 		found                             bool
     97 	}{
     98 		{"123742", "1", "loot", "Weapon", true},
     99 		{"123742", "2", "loot", "Armor", false},
    100 		{"70562", "2", "shops", "Armor", true},
    101 		{"85595", "2", "loot", "Armor", true},
    102 		{"136626", "0", "loot", "Miscellaneous", true},
    103 		{"95577", "0", "loot", "Miscellaneous", false},
    104 	} {
    105 		page := httptest.NewRecorder()
    106 		handler.ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/catalog?catalog="+scenario.template+"&category="+scenario.category+"&source="+scenario.source, nil))
    107 
    108 		if page.Code != 200 || strings.Contains(page.Body.String(), "value='"+scenario.template+"'>Create") != scenario.found {
    109 			t.Fatal("combined category/name/source filter returned the wrong item", scenario)
    110 		}
    111 		if !strings.Contains(page.Body.String(), "<option value='"+scenario.category+"' selected>"+scenario.label+"</option>") || !strings.Contains(page.Body.String(), "category="+scenario.category+"&amp;") {
    112 			t.Fatal("category selection or sorting link lost the active filter", scenario)
    113 		}
    114 	}
    115 }
    116 
    117 func TestShopSourceMapUsesOutdoorEntrancesAndLocationLinks(t *testing.T) {
    118 	world := newTestServer(t)
    119 	machines := gamedata.ShopMachines
    120 	t.Cleanup(func() { gamedata.ShopMachines = machines })
    121 	gamedata.ShopMachines = map[uint32]gamedata.ShopMachine{
    122 		1: {Template: 99491, Playfield: 1183, Position: [3]float32{180, 5, 135}},
    123 		2: {Template: 99491, Playfield: 950, Position: [3]float32{50, 14, 60}},
    124 		3: {Template: 99491, Playfield: 1183, Position: [3]float32{190, 5, 140}},
    125 	}
    126 	world.zoneLinks = map[string]map[uint32]zoneLinks{"target": {
    127 		705: {Doors: []zoneDoor{
    128 			{Destination: 1183, Position: [3]float32{100, 20, 200}},
    129 			{Destination: 1183, Position: [3]float32{300, 20, 400}},
    130 		}},
    131 		710:  {Doors: []zoneDoor{{Destination: 1184, Position: [3]float32{500, 20, 600}}}},
    132 		1184: {Doors: []zoneDoor{{Destination: 1183}}},
    133 		1183: {Doors: []zoneDoor{{Destination: 1184}}},
    134 	}}
    135 	handler := adminTestHandler(world)
    136 
    137 	for _, path := range []string{"/admin/shops/99491", "/admin/items/catalog/95577"} {
    138 		page := httptest.NewRecorder()
    139 		handler.ServeHTTP(page, httptest.NewRequest("GET", path, nil))
    140 
    141 		if path == "/admin/items/catalog/95577" {
    142 			for _, link := range []string{
    143 				"admin/map?playfield=705&amp;x=100&amp;z=200",
    144 				"admin/map?playfield=705&amp;x=300&amp;z=400",
    145 				"admin/map?playfield=710&amp;x=500&amp;z=600",
    146 				"admin/map?playfield=950&amp;x=50&amp;z=60",
    147 			} {
    148 				if !strings.Contains(page.Body.String(), "href='"+link+"'") {
    149 					t.Fatal("missing possible-source location link", link)
    150 				}
    151 			}
    152 			for _, removed := range []string{"Acquisition locations", "admin/map?playfield=1183"} {
    153 				if strings.Contains(page.Body.String(), removed) {
    154 					t.Fatal("item page repeats the location list or retains an indoor machine link", removed)
    155 				}
    156 			}
    157 			_, data, found := strings.Cut(page.Body.String(), "data-item-sources>")
    158 			data, _, _ = strings.Cut(data, "</script>")
    159 			var locations []sourceMapLocation
    160 			if !found || json.Unmarshal([]byte(data), &locations) != nil {
    161 				t.Fatal("missing item source overview")
    162 			}
    163 			var placements []mapEntrance
    164 			for _, location := range locations {
    165 				if strings.Contains(location.Name, "(machine ") {
    166 					if !location.Vending || location.Loot {
    167 						t.Fatal("vendor location lost its source type", location)
    168 					}
    169 					placements = append(placements, mapEntrance{location.Playfield, location.Position})
    170 				}
    171 			}
    172 			if !slices.Equal(placements, []mapEntrance{
    173 				{705, [3]float32{100, 20, 200}},
    174 				{705, [3]float32{300, 20, 400}},
    175 				{710, [3]float32{500, 20, 600}},
    176 			}) {
    177 				t.Fatal("overview must deduplicate shared entrances and include only overworld positions", placements)
    178 			}
    179 			continue
    180 		}
    181 
    182 		_, data, found := strings.Cut(page.Body.String(), "data-item-sources>")
    183 		data, _, _ = strings.Cut(data, "</script>")
    184 		var locations []sourceMapLocation
    185 		if page.Code != 200 || !found || json.Unmarshal([]byte(data), &locations) != nil {
    186 			t.Fatal("missing source map", path, page.Code)
    187 		}
    188 		var placements []mapEntrance
    189 		for _, location := range locations {
    190 			if strings.Contains(location.Name, "machine 1)") || strings.Contains(location.Name, "machine 2)") {
    191 				placements = append(placements, mapEntrance{location.Playfield, location.Position})
    192 			}
    193 			if strings.Contains(location.Name, "machine 1)") && !strings.Contains(location.Name, "entrance") {
    194 				t.Fatal("indoor vendor link does not identify its shop entrance")
    195 			}
    196 		}
    197 		if !slices.Equal(placements, []mapEntrance{
    198 			{705, [3]float32{100, 20, 200}},
    199 			{705, [3]float32{300, 20, 400}},
    200 			{710, [3]float32{500, 20, 600}},
    201 			{950, [3]float32{50, 14, 60}},
    202 		}) {
    203 			t.Fatal("wrong shared/nested entrances or outdoor vendor position", path, placements)
    204 		}
    205 		if !strings.Contains(page.Body.String(), "<a href='admin/map?playfield=705&amp;x=100&amp;z=200' data-map-source='") {
    206 			t.Fatal("missing clickable location link", path)
    207 		}
    208 		for _, control := range []string{"data-map-find", "data-map-view", "data-map-offline", "data-map-npcs", "data-map-warps", "data-map-center"} {
    209 			if strings.Contains(page.Body.String(), control) {
    210 				t.Fatal("acquisition map retains general map controls", path, control)
    211 			}
    212 		}
    213 	}
    214 }
    215 
    216 func TestAdminItemIconUsesOriginalPNG(t *testing.T) {
    217 	world := newTestServer(t)
    218 	response := httptest.NewRecorder()
    219 	// Use the shipped armor icon and its original green transparency key.
    220 	icon := itemStat(gamedata.ItemTemplates[85595], 79)
    221 	request := httptest.NewRequest("GET", "/admin/item-icons/"+strconv.FormatUint(uint64(icon), 10), nil)
    222 	request.SetPathValue("icon", strconv.FormatUint(uint64(icon), 10))
    223 	world.itemIconPage(response, request)
    224 	decoded, err := png.Decode(bytes.NewReader(response.Body.Bytes()))
    225 	if response.Code != 200 || response.Header().Get("Content-Type") != "image/png" || err != nil {
    226 		t.Fatal("original icon was not served", response.Code, err)
    227 	}
    228 	if decoded.Bounds().Dx() != 48 || decoded.Bounds().Dy() != 48 {
    229 		t.Fatal("unexpected original icon dimensions", decoded.Bounds())
    230 	}
    231 	_, _, _, alpha := decoded.At(0, 0).RGBA()
    232 	if alpha != 0 {
    233 		t.Fatal("original green background is not transparent")
    234 	}
    235 }
    236 
    237 func TestItemSourceMapBrowser(t *testing.T) {
    238 	if os.Getenv("AO_GAME_TEST") != "1" {
    239 		t.Skip("set AO_GAME_TEST=1 for acquisition map browser verification")
    240 	}
    241 	world := newTestServer(t)
    242 	world.baseURL = "/share/test/"
    243 	host := httptest.NewServer(adminTestHandler(world))
    244 	defer host.Close()
    245 	_, source, _, _ := runtime.Caller(0)
    246 	root := filepath.Dir(filepath.Dir(source))
    247 	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    248 	defer cancel()
    249 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_item_sources.mjs"), host.URL+world.baseURL)
    250 	command.Dir = root
    251 
    252 	output, err := command.CombinedOutput()
    253 
    254 	if err != nil {
    255 		t.Fatalf("%v\n%s", err, output)
    256 	}
    257 	t.Log(string(output))
    258 }
    259 
    260 func TestItemSourceLocationsArePagedWithoutChangingTheCachedCatalog(t *testing.T) {
    261 	handler := adminTestHandler(newTestServer(t))
    262 	path := "/admin/items/catalog/42640"
    263 	first := adminRequest(handler, "GET", path, "", nil)
    264 	second := adminRequest(handler, "GET", path+"?page=2", "", nil)
    265 	last := adminRequest(handler, "GET", path+"?page=999999", "", nil)
    266 	reopened := adminRequest(handler, "GET", path, "", nil)
    267 
    268 	const link = "<li><a href='admin/map?playfield="
    269 	for _, response := range []*httptest.ResponseRecorder{first, second} {
    270 		if response.Code != 200 || strings.Count(response.Body.String(), link) != 1000 {
    271 			t.Fatal("expected one page of acquisition locations", response.Code)
    272 		}
    273 	}
    274 	if !strings.Contains(first.Body.String(), "?page=2'>Next") || !strings.Contains(second.Body.String(), "?page=1'>Previous") ||
    275 		!strings.Contains(last.Body.String(), ">Previous</a>") || strings.Contains(last.Body.String(), ">Next</a>") {
    276 		t.Fatal("source location navigation does not reach the final page")
    277 	}
    278 	if count := strings.Count(last.Body.String(), link); count == 0 || count > 1000 {
    279 		t.Fatal("invalid number of locations on the final page", count)
    280 	}
    281 	if !bytes.Equal(first.Body.Bytes(), reopened.Body.Bytes()) {
    282 		t.Fatal("browsing another page changed the cached source locations")
    283 	}
    284 }