aorecomp

Anarchy Online classic in the browser

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


server/trade_restoration_test.go (5644B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"reflect"
      7 	"slices"
      8 	"testing"
      9 )
     10 
     11 func TestShopRestoresOffersIntoNativeFirstFreeSlots(t *testing.T) {
     12 	for _, scenario := range []struct {
     13 		name   string
     14 		offer  []uint32
     15 		remove bool
     16 		want   []ownedItem
     17 	}{
     18 		{"cancel noncontiguous", []uint32{64, 70}, false, []ownedItem{
     19 			{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     20 			{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 65, Count: 1},
     21 		}},
     22 		{"cancel trade order", []uint32{70, 64}, false, []ownedItem{
     23 			{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 65, Count: 1},
     24 			{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     25 		}},
     26 		{"remove then cancel", []uint32{64, 70}, true, []ownedItem{
     27 			{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 65, Count: 1},
     28 			{ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     29 		}},
     30 	} {
     31 		t.Run(scenario.name, func(t *testing.T) {
     32 			world, conn := newShopWorld(t)
     33 			player, client := world.characters[1], world.clients[1]
     34 			if granted, err := world.grantItem(player, 209283); err != nil || !granted {
     35 				t.Fatalf("grant: %v %v", granted, err)
     36 			}
     37 			player.inventory.Items[1].Slot = 70
     38 			if err := world.saveInventory(player, player.inventory); err != nil {
     39 				t.Fatal(err)
     40 			}
     41 			for _, slot := range scenario.offer {
     42 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, slot})[20:]); err != nil {
     43 					t.Fatal(err)
     44 				}
     45 			}
     46 
     47 			if scenario.remove {
     48 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 5, [2]uint32{50000, 1}, [2]uint32{108, 1})[20:]); err != nil {
     49 					t.Fatal(err)
     50 				}
     51 				if client.shop.items[0].Slot != 65 || player.inventory.Items[1].Slot != 64 {
     52 					t.Fatal("returned item collided with another offer's reservation")
     53 				}
     54 			}
     55 			conn.packets = nil
     56 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
     57 				t.Fatal(err)
     58 			}
     59 			restored := newTestInventoryServer(t).characters[1].inventory
     60 
     61 			if client.shop != nil || !reflect.DeepEqual(player.inventory.Items, scenario.want) || !reflect.DeepEqual(restored.Items, scenario.want) || restored.Credits != 100 {
     62 				t.Fatalf("restored memory=%+v disk=%+v", player.inventory, restored)
     63 			}
     64 			if len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][33:37]) != 2 {
     65 				t.Fatal("native cancel acknowledgement missing")
     66 			}
     67 		})
     68 	}
     69 }
     70 
     71 func TestShopFullInventoryCanRemoveReofferAndSellChargedItems(t *testing.T) {
     72 	world, _ := newShopWorld(t)
     73 	player, client := world.characters[1], world.clients[1]
     74 	for range 29 {
     75 		if granted, err := world.grantItem(player, 85366); err != nil || !granted {
     76 			t.Fatalf("grant charged kit: %v %v", granted, err)
     77 		}
     78 	}
     79 	for _, slot := range []uint32{64, 93} {
     80 		if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, slot})[20:]); err != nil {
     81 			t.Fatal(err)
     82 		}
     83 	}
     84 
     85 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 5, [2]uint32{50000, 1}, [2]uint32{108, 1})[20:]); err != nil {
     86 		t.Fatal(err)
     87 	}
     88 	if client.shop.items[0].Slot != 93 || player.inventory.Items[29].Slot != 64 {
     89 		t.Fatal("full inventory did not reserve the remaining offer away from the restored kit")
     90 	}
     91 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
     92 		t.Fatal(err)
     93 	}
     94 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
     95 		t.Fatal(err)
     96 	}
     97 	restored := newTestInventoryServer(t).characters[1].inventory
     98 
     99 	if client.shop != nil || restored.Credits != 706 || len(restored.Items) != 28 || !reflect.DeepEqual(restored, player.inventory) ||
    100 		slices.ContainsFunc(restored.Items, func(item ownedItem) bool {
    101 			return item.ID == 1 || item.ID == 30 || item.Count != 25 || item.Slot < 65 || item.Slot > 92
    102 		}) {
    103 		t.Fatalf("full inventory reoffer sold the wrong items or changed surviving charges: %+v", restored)
    104 	}
    105 }
    106 
    107 func TestShopRestorationFailureRetainsDraftAndCanRetry(t *testing.T) {
    108 	world, conn := newShopWorld(t)
    109 	player, client := world.characters[1], world.clients[1]
    110 	player.inventory.Items[0].Slot = 70
    111 	if err := world.saveInventory(player, player.inventory); err != nil {
    112 		t.Fatal(err)
    113 	}
    114 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 70})[20:]); err != nil {
    115 		t.Fatal(err)
    116 	}
    117 	before, err := readTestItemStore(world)
    118 	if err != nil {
    119 		t.Fatal(err)
    120 	}
    121 	if _, err := world.db.Exec("CREATE TRIGGER fail_restore BEFORE INSERT ON item_locations BEGIN SELECT RAISE(ABORT, 'restore failure'); END"); err != nil {
    122 		t.Fatal(err)
    123 	}
    124 	conn.packets = nil
    125 	request := shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]
    126 
    127 	failed := world.handleTrade(client, request)
    128 	after, err := readTestItemStore(world)
    129 	if err != nil {
    130 		t.Fatal(err)
    131 	}
    132 
    133 	if failed == nil || !bytes.Equal(before, after) || player.inventory.Items[0].Slot != 70 || client.shop == nil || client.shop.items[0].Slot != 70 || len(conn.packets) != 0 {
    134 		t.Fatal("failed restoration published a layout or cleared its retryable draft")
    135 	}
    136 	if _, err := world.db.Exec("DROP TRIGGER fail_restore"); err != nil {
    137 		t.Fatal(err)
    138 	}
    139 	if err := world.handleTrade(client, request); err != nil {
    140 		t.Fatal(err)
    141 	}
    142 	if client.shop != nil || newTestInventoryServer(t).characters[1].inventory.Items[0].Slot != 64 {
    143 		t.Fatal("restoration retry did not commit")
    144 	}
    145 }