server/item_store_test.go (7999B)
1 package main 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "strconv" 8 "strings" 9 "testing" 10 ) 11 12 func TestItemStoreConsumableChargesKeepIDUntilLastCharge(t *testing.T) { 13 for _, count := range []uint16{1, 2} { 14 t.Run(strconv.Itoa(int(count)), func(t *testing.T) { 15 world, _ := newNanoKitWorld(t) 16 player := world.characters[1] 17 player.inventory.Items[0].Count = count 18 if err := world.saveInventory(player, player.inventory); err != nil { 19 t.Fatal(err) 20 } 21 22 err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]) 23 restarted := newTestInventoryServer(t) 24 25 if err != nil || restarted.nextItem != 1 { 26 t.Fatalf("consumption failed or lost the ID counter: %v", err) 27 } 28 if count == 1 { 29 if len(restarted.items) != 0 || len(restarted.characters[1].inventory.Items) != 0 { 30 t.Fatal("last charge retained an item instance or slot reference") 31 } 32 } else if len(restarted.items) != 1 || restarted.items[1].Count != 1 || restarted.characters[1].inventory.Items[0].ID != 1 { 33 t.Fatal("using a charge changed identity or lost the remaining charge") 34 } 35 }) 36 } 37 } 38 39 func TestItemStoreLoadsUnassignedInstancesWithoutCharacters(t *testing.T) { 40 useNPCData(t, testNPCData()) 41 world := newTestServer(t) 42 43 response := postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 44 restarted := newServerForTest(t) 45 46 if response.Code != http.StatusSeeOther || len(restarted.items) != 1 || restarted.items[1].Template != 121567 || restarted.nextItem != 1 { 47 t.Fatalf("unassigned instance was lost: %d %s", response.Code, response.Body.String()) 48 } 49 } 50 51 func TestDatabaseRejectsSharedInstanceReferences(t *testing.T) { 52 useNPCData(t, testNPCData()) 53 world := newTestServer(t) 54 postControlForm(world, "/admin/players", url.Values{"name": {"First"}}) 55 postControlForm(world, "/admin/players", url.Values{"name": {"Second"}}) 56 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 57 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 58 postControlForm(world, "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_1: 1}")) 59 60 _, err := world.db.Exec("INSERT INTO item_locations (item_id, player_id, slot) VALUES (1, 2, 65)") 61 _, occupiedError := world.db.Exec("INSERT INTO item_locations (item_id, player_id, slot) VALUES (2, 1, 64)") 62 _, missingError := world.db.Exec("INSERT INTO item_locations (item_id, player_id, slot) VALUES (999, 2, 65)") 63 var owner uint32 64 if err := world.db.QueryRow("SELECT player_id FROM item_locations WHERE item_id=1").Scan(&owner); err != nil { 65 t.Fatal(err) 66 } 67 68 if err == nil || occupiedError == nil || missingError == nil || owner != 1 { 69 t.Fatal("database accepted duplicate ownership, an occupied slot or a missing item") 70 } 71 } 72 73 func TestControlItemAssignmentOffersOnlyFreeCompatibleSlots(t *testing.T) { 74 useNPCData(t, testNPCData()) 75 world := newTestServer(t) 76 postControlForm(world, "/admin/players", url.Values{"name": {"Explorer"}}) 77 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 78 postControlForm(world, "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_1: 1}\n")) 79 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 80 81 page := httptest.NewRecorder() 82 adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/2?owner=1", nil)) 83 occupied := postControlForm(world, "/admin/items/2", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"0:64"}}) 84 placed := postControlForm(world, "/admin/items/2", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"0:65"}}) 85 postControlForm(world, "/admin/items", url.Values{"template": {"99228"}}) 86 bag := postControlForm(world, "/admin/items/3", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"0:66"}}) 87 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 88 contained := postControlForm(world, "/admin/items/4", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"3:20"}}) 89 restarted := newServerForTest(t) 90 91 if page.Code != http.StatusOK || strings.Contains(page.Body.String(), "value='0:64'") || strings.Contains(page.Body.String(), "value='0:8'") || 92 !strings.Contains(page.Body.String(), "value='0:6'") || !strings.Contains(page.Body.String(), "value='0:65'") { 93 t.Fatal("assignment form included an occupied or incompatible slot") 94 } 95 if occupied.Code != http.StatusBadRequest || placed.Code != http.StatusSeeOther || bag.Code != http.StatusSeeOther || contained.Code != http.StatusSeeOther { 96 t.Fatalf("assignment status: %d %d %d %d: %s", occupied.Code, placed.Code, bag.Code, contained.Code, contained.Body.String()) 97 } 98 if len(restarted.characters[1].inventory.Items) != 3 || restarted.characters[1].inventory.Items[1].ID != 2 || restarted.characters[1].inventory.Items[1].Slot != 65 || 99 len(restarted.characters[1].inventory.Containers[3]) != 1 || restarted.characters[1].inventory.Containers[3][0] != (ownedItem{ID: 4, Type: 51018, Template: 121567, Quality: 1, Slot: 20, Count: 1}) { 100 t.Fatal("item or backpack assignment did not survive restart") 101 } 102 } 103 104 func TestControlBackpackRejectsNestedBackpacksAndSharedContents(t *testing.T) { 105 for _, contents := range []url.Values{{"slots.slot_1": {"1"}}, {"slots.slot_1": {"2"}}, {"slots.slot_1": {"3"}, "slots.slot_2": {"3"}}, {"slots.slot_22": {"3"}}} { 106 t.Run(contents.Encode(), func(t *testing.T) { 107 useNPCData(t, testNPCData()) 108 world := newTestServer(t) 109 postControlForm(world, "/admin/items", url.Values{"template": {"99228"}}) 110 postControlForm(world, "/admin/items", url.Values{"template": {"99228"}}) 111 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 112 113 response := postControlForm(world, "/admin/items/1", contents) 114 115 if response.Code != http.StatusBadRequest || world.items[1].Slots["slot_1"] != nil || world.nextItem != 3 { 116 t.Fatalf("invalid backpack content accepted: %d %s", response.Code, response.Body.String()) 117 } 118 }) 119 } 120 } 121 122 func TestControlConcurrentCreationAllocatesUniqueGlobalIDs(t *testing.T) { 123 useNPCData(t, testNPCData()) 124 world := newTestServer(t) 125 shutdown := startTestService(t, world) 126 responses := make(chan *httptest.ResponseRecorder, 8) 127 128 for range 8 { 129 go func() { 130 responses <- postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 131 }() 132 } 133 ids := make(map[string]bool) 134 for range 8 { 135 response := <-responses 136 if response.Code != http.StatusSeeOther { 137 t.Fatalf("creation failed: %d %s", response.Code, response.Body.String()) 138 } 139 ids[response.Header().Get("Location")] = true 140 } 141 if err := shutdown(); err != nil { 142 t.Fatal(err) 143 } 144 restarted := newServerForTest(t) 145 146 if len(ids) != 8 || len(restarted.items) != 8 || restarted.nextItem != 8 { 147 t.Fatal("concurrent creation reused an instance ID or lost a committed instance") 148 } 149 } 150 151 func TestControlConcurrentAssignmentCannotShareOneInstance(t *testing.T) { 152 useNPCData(t, testNPCData()) 153 world := newTestServer(t) 154 postControlForm(world, "/admin/players", url.Values{"name": {"First"}}) 155 postControlForm(world, "/admin/players", url.Values{"name": {"Second"}}) 156 postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 157 shutdown := startTestService(t, world) 158 responses := make(chan *httptest.ResponseRecorder, 2) 159 160 for _, owner := range []string{"1", "2"} { 161 go func() { 162 responses <- postControlForm(world, "/admin/items/1", url.Values{"action": {"assign"}, "owner": {owner}, "destination": {"0:64"}}) 163 }() 164 } 165 statuses := make(map[int]int) 166 for range 2 { 167 statuses[(<-responses).Code]++ 168 } 169 if err := shutdown(); err != nil { 170 t.Fatal(err) 171 } 172 restarted := newServerForTest(t) 173 174 if statuses[http.StatusSeeOther] != 1 || statuses[http.StatusBadRequest] != 1 || 175 len(restarted.characters[1].inventory.Items)+len(restarted.characters[2].inventory.Items) != 1 || len(restarted.items) != 1 { 176 t.Fatal("one instance was assigned to more than one character") 177 } 178 }