server/item_import_test.go (4857B)
1 package main 2 3 import ( 4 "net/http" 5 "net/url" 6 "reflect" 7 "testing" 8 ) 9 10 func TestRoutedPlayerEditorTransfersUnassignedBagAtomically(t *testing.T) { 11 fixture := newControlInventoryWorld(t) 12 second := fixture.newCharacter() 13 second.name, second.playfield = "Second", 800 14 if err := fixture.saveUser(second); err != nil { 15 t.Fatal(err) 16 } 17 fixture.world = newRoutedTestWorld(t, fixture) 18 defer fixture.world.stop() 19 handler := adminTestHandler(fixture) 20 firstOwner, err := fixture.world.owner(1) 21 if err != nil { 22 t.Fatal(err) 23 } 24 secondOwner, err := fixture.world.owner(2) 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 cleared := adminRequest(handler, "POST", "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_1: null, inventory_slot_2: null}\n").Encode(), nil) 30 first := &zoneTransferQuery{player: 1} 31 if err := firstOwner.call(first); err != nil { 32 t.Fatal(err) 33 } 34 if cleared.Code != http.StatusSeeOther || !reflect.DeepEqual(first.items, []uint32{2}) { 35 t.Fatal("source retained unassigned records", cleared.Code, cleared.Body.String(), first.items) 36 } 37 if _, err := fixture.db.Exec("CREATE TRIGGER reject_import BEFORE INSERT ON item_locations WHEN NEW.player_id=2 AND NEW.item_id=1 BEGIN SELECT RAISE(FAIL,'claim rejected'); END"); err != nil { 38 t.Fatal(err) 39 } 40 document := playerFixtureForm("inventory:\n slots: {inventory_slot_1: 3, inventory_slot_2: 1}\n").Encode() 41 failed := adminRequest(handler, "POST", "/admin/players/2", document, nil) 42 destination := &zoneTransferQuery{player: 2} 43 if err := secondOwner.call(destination); err != nil { 44 t.Fatal(err) 45 } 46 var claimed int 47 if err := fixture.db.QueryRow("SELECT COUNT(*) FROM item_locations WHERE player_id=2").Scan(&claimed); err != nil { 48 t.Fatal(err) 49 } 50 if failed.Code != http.StatusInternalServerError || claimed != 0 || len(destination.items) != 0 { 51 t.Fatal("failed import published partial ownership", failed.Code, failed.Body.String(), claimed, destination.items) 52 } 53 if _, err := fixture.db.Exec("DROP TRIGGER reject_import"); err != nil { 54 t.Fatal(err) 55 } 56 accepted := adminRequest(handler, "POST", "/admin/players/2", document, nil) 57 if err := secondOwner.call(destination); err != nil { 58 t.Fatal(err) 59 } 60 if accepted.Code != http.StatusSeeOther || !reflect.DeepEqual(destination.items, []uint32{1, 3, 4}) || destination.locations[4] != (itemPlacement{Backpack: 3}) { 61 t.Fatal("bag import lost identity or contents", accepted.Code, accepted.Body.String(), destination) 62 } 63 stolen := adminRequest(handler, "POST", "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_1: 3}\n").Encode(), nil) 64 if stolen.Code != http.StatusBadRequest { 65 t.Fatal("another zone's assigned bag was accepted", stolen.Code, stolen.Body.String()) 66 } 67 } 68 69 func TestRoutedItemAssignmentClaimsOneOwnerAcrossConcurrentZones(t *testing.T) { 70 fixture := newControlInventoryWorld(t) 71 second := fixture.newCharacter() 72 second.name, second.playfield = "Second", 800 73 if err := fixture.saveUser(second); err != nil { 74 t.Fatal(err) 75 } 76 fixture.world = newRoutedTestWorld(t, fixture) 77 defer fixture.world.stop() 78 handler := adminTestHandler(fixture) 79 cleared := adminRequest(handler, "POST", "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_2: null}\n").Encode(), nil) 80 if cleared.Code != http.StatusSeeOther { 81 t.Fatal(cleared.Code, cleared.Body.String()) 82 } 83 84 results := make(chan int, 2) 85 for _, owner := range []string{"1", "2"} { 86 go func() { 87 response := adminRequest(handler, "POST", "/admin/items/3", url.Values{"action": {"assign"}, "owner": {owner}, "destination": {"0:65"}}.Encode(), nil) 88 results <- response.Code 89 }() 90 } 91 first, secondStatus := <-results, <-results 92 if (first == http.StatusSeeOther) == (secondStatus == http.StatusSeeOther) { 93 t.Fatal("concurrent claims did not produce exactly one owner", first, secondStatus) 94 } 95 for _, status := range []int{first, secondStatus} { 96 if status != http.StatusSeeOther && status != http.StatusBadRequest && status != http.StatusInternalServerError { 97 t.Fatal("unexpected claim response", status) 98 } 99 } 100 var ownerID, bag uint32 101 if err := fixture.db.QueryRow("SELECT player_id FROM item_locations WHERE item_id=3").Scan(&ownerID); err != nil { 102 t.Fatal(err) 103 } 104 if err := fixture.db.QueryRow("SELECT backpack_id FROM item_locations WHERE item_id=4").Scan(&bag); err != nil || bag != 3 { 105 t.Fatal("child lost its parent", bag, err) 106 } 107 for _, id := range []uint32{1, 2} { 108 owner, err := fixture.world.owner(id) 109 if err != nil { 110 t.Fatal(err) 111 } 112 query := &zoneTransferQuery{player: id} 113 if err := owner.call(query); err != nil { 114 t.Fatal(err) 115 } 116 _, hasBag := query.locations[3] 117 _, hasChild := query.locations[4] 118 if hasBag != (id == ownerID) || hasChild != hasBag { 119 t.Fatal("live owners disagree with SQLite", ownerID, id, query.locations) 120 } 121 } 122 }