aorecomp

Anarchy Online classic in the browser

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


server/control_inventory_test.go (9496B)


      1 package main
      2 
      3 import (
      4 	"net/http"
      5 	"net/http/httptest"
      6 	"net/url"
      7 	"regexp"
      8 	"strings"
      9 	"testing"
     10 )
     11 
     12 // Inventory assertions check text and structure; icon rendering has separate coverage.
     13 func withoutItemIcons(markup string) string {
     14 	return regexp.MustCompile(`<img class='item-icon'[^>]*>|<span class='item-icon'[^>]*></span>`).ReplaceAllString(markup, "")
     15 }
     16 
     17 func newControlInventoryWorld(t *testing.T) *server {
     18 	t.Helper()
     19 	useNPCData(t, testNPCData())
     20 	world := newTestServer(t)
     21 	if response := postControlForm(world, "/admin/players", url.Values{"name": {"Explorer"}}); response.Code != http.StatusSeeOther {
     22 		t.Fatal(response.Body.String())
     23 	}
     24 	for _, template := range []string{"121567", "85557", "99228", "121567"} {
     25 		if response := postControlForm(world, "/admin/items", url.Values{"template": {template}}); response.Code != http.StatusSeeOther {
     26 			t.Fatal(response.Body.String())
     27 		}
     28 	}
     29 	for _, placement := range [][2]string{{"1", "0:64"}, {"2", "0:18"}, {"3", "0:65"}, {"4", "3:0"}} {
     30 		response := postControlForm(world, "/admin/items/"+placement[0], url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {placement[1]}})
     31 		if response.Code != http.StatusSeeOther {
     32 			t.Fatal(response.Body.String())
     33 		}
     34 	}
     35 	return world
     36 }
     37 
     38 func TestControlCharacterFormSeparatesEquipment(t *testing.T) {
     39 	useNPCData(t, testNPCData())
     40 	world := newTestServer(t)
     41 	postControlForm(world, "/admin/players", url.Values{"name": {"Explorer"}})
     42 	postControlForm(world, "/admin/items", url.Values{"template": {"85557"}})
     43 
     44 	response := postControlForm(world, "/admin/players/1", playerFixtureForm("equipment: {clothes_head: 1}\n"))
     45 	restarted := newServerForTest(t)
     46 	page := httptest.NewRecorder()
     47 	adminTestHandler(restarted).ServeHTTP(page, httptest.NewRequest("GET", "/admin/players/1", nil))
     48 
     49 	if response.Code != http.StatusSeeOther {
     50 		t.Fatalf("top-level equipment save: %d %s", response.Code, response.Body.String())
     51 	}
     52 	fields := playerPageValues(t, page.Body.String())
     53 	count := 0
     54 	for name := range fields {
     55 		if strings.HasPrefix(name, "equipment.") {
     56 			count++
     57 		}
     58 	}
     59 	if count != 43 || fields.Get("equipment.clothes_head") != "1" {
     60 		t.Fatal("equipment form is incomplete")
     61 	}
     62 
     63 	if len(restarted.characters[1].inventory.Items) != 1 || restarted.characters[1].inventory.Items[0].Slot != 18 {
     64 		t.Fatal("the equipped instance did not survive restart")
     65 	}
     66 }
     67 
     68 func TestControlInventoryTablesShowInstancesContainersAndEmptySlots(t *testing.T) {
     69 	world := newControlInventoryWorld(t)
     70 	world.baseURL = "/share/test/"
     71 
     72 	page := httptest.NewRecorder()
     73 	adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/share/test/admin/players/1", nil))
     74 
     75 	body := withoutItemIcons(page.Body.String())
     76 	for _, expected := range []string{
     77 		"<base href='/share/test/'>", "<form class='player-editor'", "<h2>Inventory</h2>", "<h2>Equipment</h2>", "<h2>Bank</h2>",
     78 		"<tr><td>inventory_slot_1</td><td></td><td><a href='admin/items/1'>Solar-Powered Pistol (ID 1)</a></td></tr>",
     79 		"<tr><td>clothes_head</td><td></td><td><a href='admin/items/2'>Worn Metaplast Armor Helmet (ID 2)</a></td></tr>",
     80 		"<h2>Container <a href='admin/items/3'>Small Backpack (ID 3)</a></h2>",
     81 		"<tr><td>slot_1</td><td></td><td><a href='admin/items/4'>Solar-Powered Pistol (ID 4)</a></td></tr>",
     82 		"<tr><td>slot_21</td><td></td><td>Empty</td></tr>",
     83 		"<tr><td>slot_102</td><td></td><td>Empty</td></tr>",
     84 	} {
     85 		if !strings.Contains(body, expected) {
     86 			t.Errorf("missing %s", expected)
     87 		}
     88 	}
     89 	if strings.Count(body, "<td>Empty</td>") != 192 || strings.Index(body, "<td>inventory_slot_2</td>") > strings.Index(body, "<td>inventory_slot_10</td>") {
     90 		t.Fatal("slot tables omit empty slots or use lexical slot order")
     91 	}
     92 }
     93 
     94 func TestControlBackpackPagesLinkContentsAndParentContainer(t *testing.T) {
     95 	world := newControlInventoryWorld(t)
     96 
     97 	bag, child := httptest.NewRecorder(), httptest.NewRecorder()
     98 	adminTestHandler(world).ServeHTTP(bag, httptest.NewRequest("GET", "/admin/items/3", nil))
     99 	adminTestHandler(world).ServeHTTP(child, httptest.NewRequest("GET", "/admin/items/4", nil))
    100 
    101 	if !strings.Contains(withoutItemIcons(bag.Body.String()), "<h2>Contents</h2>") ||
    102 		!strings.Contains(withoutItemIcons(bag.Body.String()), "<tr><td>slot_1</td><td></td><td><a href='admin/items/4'>Solar-Powered Pistol (ID 4)</a></td></tr>") ||
    103 		!strings.Contains(withoutItemIcons(bag.Body.String()), "<tr><td>slot_21</td><td></td><td>Empty</td></tr>") ||
    104 		!strings.Contains(withoutItemIcons(bag.Body.String()), "<a href='admin/players/1'>Explorer (1)</a>") {
    105 		t.Fatal("backpack page lost its owner or readable contents")
    106 	}
    107 	if !strings.Contains(withoutItemIcons(child.Body.String()), "<a href='admin/items/3'>Container 3, slot_1</a>") {
    108 		t.Fatal("contained item has no link to its parent backpack")
    109 	}
    110 }
    111 
    112 func TestControlEquipmentClearUnassignsWithoutLosingItems(t *testing.T) {
    113 	world := newControlInventoryWorld(t)
    114 
    115 	response := postControlForm(world, "/admin/players/1", playerFixtureForm("equipment: {clothes_head: null}\n"))
    116 	restarted := newServerForTest(t)
    117 	var placements int
    118 	err := restarted.db.QueryRow("SELECT count(*) FROM item_locations").Scan(&placements)
    119 
    120 	if response.Code != http.StatusSeeOther || err != nil || placements != 3 || len(restarted.items) != 4 || len(restarted.characters[1].inventory.Items) != 2 {
    121 		t.Fatalf("clear changed unrelated items: %d, placements %d, %v", response.Code, placements, err)
    122 	}
    123 	page := httptest.NewRecorder()
    124 	adminTestHandler(restarted).ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/2", nil))
    125 	if !strings.Contains(page.Body.String(), "<th scope='row'>Owner</th><td>Unassigned</td>") || len(restarted.characters[1].inventory.Containers[3]) != 1 {
    126 		t.Fatal("cleared armor remained assigned or backpack contents changed")
    127 	}
    128 }
    129 
    130 func TestControlEquipmentRejectsInvalidPlacementsAtomically(t *testing.T) {
    131 	for name, document := range map[string]string{
    132 		"ordinary slot in equipment":  "equipment: {inventory_slot_1: 2}\n",
    133 		"equipment slot in inventory": "inventory: {slots: {clothes_head: 2}}\n",
    134 		"duplicate instance":          "inventory: {slots: {inventory_slot_3: 2}}\nequipment: {clothes_head: 2}\n",
    135 		"missing instance":            "equipment: {clothes_head: 999}\n",
    136 		"incompatible slot":           "equipment: {clothes_feet: 2}\n",
    137 		"contained instance":          "equipment: {weapons_right_hand: 4}\n",
    138 	} {
    139 		t.Run(name, func(t *testing.T) {
    140 			world := newControlInventoryWorld(t)
    141 
    142 			response := postControlForm(world, "/admin/players/1", playerFixtureForm("name: Changed\n"+document))
    143 			restarted := newServerForTest(t)
    144 
    145 			if response.Code != http.StatusBadRequest || world.characters[1].name != "Explorer" || restarted.characters[1].name != "Explorer" {
    146 				t.Fatalf("invalid placement changed profile: %d %s", response.Code, response.Body.String())
    147 			}
    148 			for _, expected := range [][4]uint32{{1, 1, 0, 64}, {2, 1, 0, 18}, {3, 1, 0, 65}, {4, 0, 3, 0}} {
    149 				var player, bag, slot uint32
    150 				err := restarted.db.QueryRow("SELECT coalesce(player_id, 0), coalesce(backpack_id, 0), slot FROM item_locations WHERE item_id=?", expected[0]).Scan(&player, &bag, &slot)
    151 				if err != nil || player != expected[1] || bag != expected[2] || slot != expected[3] {
    152 					t.Fatalf("invalid placement changed item %d: %d %d %d, %v", expected[0], player, bag, slot, err)
    153 				}
    154 			}
    155 		})
    156 	}
    157 }
    158 
    159 func TestControlEquipmentCannotTakeAnotherPlayersItem(t *testing.T) {
    160 	world := newControlInventoryWorld(t)
    161 	postControlForm(world, "/admin/players", url.Values{"name": {"Other"}})
    162 	postControlForm(world, "/admin/items", url.Values{"template": {"85557"}})
    163 	assigned := postControlForm(world, "/admin/items/5", url.Values{"action": {"assign"}, "owner": {"2"}, "destination": {"0:18"}})
    164 	if assigned.Code != http.StatusSeeOther {
    165 		t.Fatal(assigned.Body.String())
    166 	}
    167 
    168 	response := postControlForm(world, "/admin/players/1", playerFixtureForm("equipment: {clothes_head: 5}\n"))
    169 	var owner, slot uint32
    170 	err := world.db.QueryRow("SELECT player_id, slot FROM item_locations WHERE item_id=5").Scan(&owner, &slot)
    171 
    172 	if response.Code != http.StatusBadRequest || err != nil || owner != 2 || slot != 18 {
    173 		t.Fatalf("edit moved another player's armor: %d, owner %d, slot %d, %v", response.Code, owner, slot, err)
    174 	}
    175 }
    176 
    177 func TestControlInventoryTablesIncludeReclaimAndItsBackpackContents(t *testing.T) {
    178 	world := newControlInventoryWorld(t)
    179 	prepared := postControlForm(world, "/admin/players/1", url.Values{
    180 		"inventory.slots.inventory_slot_2": {""},
    181 		"reclaim.0.id":                     {"3"},
    182 		"reclaim.0.original_slot":          {"inventory_slot_2"},
    183 		"reclaim.0.available_at":           {"2026-10-23T00:00:00Z"},
    184 	})
    185 	if prepared.Code != http.StatusSeeOther {
    186 		t.Fatal(prepared.Code, prepared.Body.String())
    187 	}
    188 
    189 	page := httptest.NewRecorder()
    190 	adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/admin/players/1", nil))
    191 
    192 	if !strings.Contains(withoutItemIcons(page.Body.String()), "<h2>Reclaim</h2>") ||
    193 		!strings.Contains(withoutItemIcons(page.Body.String()), "<tr><td>inventory_slot_2</td><td></td><td><a href='admin/items/3'>Small Backpack (ID 3)</a></td></tr>") ||
    194 		!strings.Contains(withoutItemIcons(page.Body.String()), "<h2>Container <a href='admin/items/3'>Small Backpack (ID 3)</a></h2>") ||
    195 		!strings.Contains(withoutItemIcons(page.Body.String()), "<tr><td>slot_1</td><td></td><td><a href='admin/items/4'>Solar-Powered Pistol (ID 4)</a></td></tr>") {
    196 		t.Fatal("reclaim hides an assigned item or its backpack contents")
    197 	}
    198 }