server/admin_live_test.go (6078B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "math" 6 "net/http" 7 "net/url" 8 "slices" 9 "testing" 10 "time" 11 ) 12 13 func TestAdminCarriedItemChangesKeepSessionAndMovement(t *testing.T) { 14 world := newControlInventoryWorld(t) 15 player := world.characters[1] 16 connection := &combatInterestConn{} 17 client := &worldClient{id: 1, ready: true, conn: connection, target: true} 18 world.clients[1] = client 19 player.movement = heldPlayerMovement(player, 1, time.Now()) 20 movement := player.movement 21 created := postControlForm(world, "/admin/items", url.Values{"template": {"121567"}}) 22 if created.Code != http.StatusSeeOther { 23 t.Fatal(created.Body) 24 } 25 assigned := postControlForm(world, "/admin/items/5", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"0:93"}}) 26 if assigned.Code != http.StatusSeeOther || world.clients[1] != client || !client.ready || player.movement != movement || len(connection.packets) != 2 || binary.BigEndian.Uint32(connection.packets[1][16:20]) != 0x47537a24 { 27 t.Fatal("assignment reloaded the player or lost its saved slot", assigned.Code, assigned.Body, connection.packets) 28 } 29 restarted := newTestInventoryServer(t) 30 if !slices.Contains(restarted.characters[1].inventory.Items, ownedItem{ID: 5, Type: 51018, Template: 121567, Quality: 1, Slot: 93, Count: 1}) { 31 t.Fatal("assignment did not persist") 32 } 33 34 connection.packets = nil 35 removed := postControlForm(world, "/admin/players/1", playerFixtureForm("inventory:\n slots: {inventory_slot_30: null}\n")) 36 restarted = newTestInventoryServer(t) 37 if removed.Code != http.StatusSeeOther || world.clients[1] != client || !client.ready || player.movement != movement || len(connection.packets) != 2 { 38 t.Fatal("removal reloaded the player", removed.Code, removed.Body, connection.packets) 39 } 40 for _, state := range []*server{world, restarted} { 41 if len(state.characters[1].inventory.Items) != 3 || state.items[5].Template != 121567 { 42 t.Fatal("removal lost the unassigned instance", state.characters[1].inventory.Items) 43 } 44 } 45 for _, packet := range connection.packets { 46 if opcode := binary.BigEndian.Uint32(packet[16:20]); opcode == 0x43197d22 || opcode == 0x3c { 47 t.Fatal("inventory edit sent a reconnect") 48 } 49 } 50 connection.packets = nil 51 if _, err := world.db.Exec("CREATE TRIGGER reject_inventory BEFORE UPDATE ON characters BEGIN SELECT RAISE(FAIL, 'test inventory'); END"); err != nil { 52 t.Fatal(err) 53 } 54 rejected := postControlForm(world, "/admin/items/5", url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {"0:93"}}) 55 if rejected.Code != http.StatusInternalServerError || len(player.inventory.Items) != 3 || world.clients[1] != client || !client.ready || len(connection.packets) != 0 { 56 t.Fatal("rejected assignment changed live state", rejected.Code, rejected.Body) 57 } 58 } 59 60 func TestAdminSameZoneTeleportPreservesConnectionAndNotifiesObservers(t *testing.T) { 61 useNPCData(t, testNPCData()) 62 world := newTestServer(t) 63 player, observer := world.newCharacter(), world.newCharacter() 64 world.terrain[950] = &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 128, scale: 1, heights: []byte{0, 64, 128, 0, 64, 128, 0, 64, 128}} 65 ownerConnection, observerConnection := &combatInterestConn{}, &combatInterestConn{} 66 client := &worldClient{id: player.id, ready: true, conn: ownerConnection, target: true, npcs: make(map[uint32]bool)} 67 world.clients[player.id] = client 68 world.clients[observer.id] = &worldClient{id: observer.id, ready: true, conn: observerConnection, target: true, players: map[uint32]bool{player.id: true}} 69 player.rotation = [4]float32{0, 0.70710677, 0, 0.70710677} 70 player.movement = heldPlayerMovement(player, 1, time.Now()) 71 rotation := player.rotation 72 73 response := postControlForm(world, "/admin/players/1/teleport", url.Values{"playfield": {"950"}, "x": {"52"}, "z": {"56"}}) 74 restarted := newTestInventoryServer(t) 75 76 if response.Code != http.StatusSeeOther || world.clients[1] != client || !client.ready || !player.movement.active() || player.position != [3]float32{52, 26.5, 56} || restarted.characters[1].position != player.position { 77 t.Fatalf("same-zone teleport: status=%d connected=%t ready=%t moving=%t position=%v saved=%v rotation=%v expected=%v: %s", response.Code, world.clients[1] == client, client.ready, player.movement.active(), player.position, restarted.characters[1].position, player.rotation, rotation, response.Body) 78 } 79 for axis, value := range player.rotation { 80 if math.Abs(float64(value-rotation[axis])) > 0.000001 { 81 t.Fatal("teleport changed facing", player.rotation, rotation) 82 } 83 } 84 for _, connection := range []*combatInterestConn{ownerConnection, observerConnection} { 85 moved := false 86 for _, packet := range connection.packets { 87 opcode := binary.BigEndian.Uint32(packet[16:20]) 88 if opcode == 0x43197d22 || opcode == 0x3c { 89 t.Fatal("same-zone teleport sent a reconnect") 90 } 91 moved = moved || opcode == 0x54111123 92 } 93 if !moved { 94 t.Fatal("client did not receive the new pose") 95 } 96 } 97 } 98 99 func TestAdminRejectedSameZoneTeleportLeavesSessionUntouched(t *testing.T) { 100 useNPCData(t, testNPCData()) 101 world := newTestServer(t) 102 player := world.newCharacter() 103 if err := world.saveUser(player); err != nil { 104 t.Fatal(err) 105 } 106 before := player.position 107 world.terrain[950] = &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 128, scale: 1, heights: []byte{0, 64, 128, 0, 64, 128, 0, 64, 128}} 108 connection := &combatInterestConn{} 109 client := &worldClient{id: 1, ready: true, conn: connection} 110 world.clients[1] = client 111 if _, err := world.db.Exec("CREATE TRIGGER reject_position BEFORE UPDATE ON characters BEGIN SELECT RAISE(FAIL, 'test position'); END"); err != nil { 112 t.Fatal(err) 113 } 114 115 response := postControlForm(world, "/admin/players/1/teleport", url.Values{"playfield": {"950"}, "x": {"52"}, "z": {"56"}}) 116 117 if response.Code != http.StatusInternalServerError || player.position != before || world.clients[1] != client || !client.ready || len(connection.packets) != 0 { 118 t.Fatal("rejected teleport changed live state", response.Code, response.Body) 119 } 120 }