aorecomp

Anarchy Online classic in the browser

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


server/stack_resync_quality_test.go (2796B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"slices"
      8 	"testing"
      9 )
     10 
     11 func TestStackSplitRefusalPreservesSelectedEndpoints(t *testing.T) {
     12 	for _, family := range [][2]uint32{{25812, 25811}, {85366, 85367}, {25820, 25821}} {
     13 		for _, anchor := range family {
     14 			for _, quality := range []uint32{1, 2, 100, 101, 199, 200} {
     15 				t.Run(fmt.Sprintf("%d/QL%d", anchor, quality), func(t *testing.T) {
     16 					world, output := newEquipmentWorld(t)
     17 					player, client := world.characters[1], world.clients[1]
     18 					if granted, err := world.grantItem(player, anchor); err != nil || !granted {
     19 						t.Fatal(granted, err)
     20 					}
     21 					player.inventory.Items[1].Quality, player.inventory.Items[1].Count = quality, 3
     22 					if err := world.saveInventory(player, player.inventory); err != nil {
     23 						t.Fatal(err)
     24 					}
     25 					if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment == nil {
     26 						t.Fatal("weapon did not begin its admitted move", err)
     27 					}
     28 					pending := *client.equipment
     29 					before := slices.Clone(player.inventory.Items)
     30 					persisted, err := readTestItemStore(world)
     31 					if err != nil {
     32 						t.Fatal(err)
     33 					}
     34 					auditCheckpointWrites(t, world)
     35 					output.packets = nil
     36 
     37 					err = world.handleInventory(client, stackActionPacket(1, 52, 65, 1)[20:])
     38 					after, readErr := readTestItemStore(world)
     39 
     40 					if err != nil || readErr != nil || !bytes.Equal(persisted, after) || !slices.Equal(player.inventory.Items, before) ||
     41 						*client.equipment != pending || world.nextItem != 2 {
     42 						t.Fatal("refused split changed ownership or the admitted weapon move", err, readErr)
     43 					}
     44 					if writes := checkpointWrites(t, world); len(writes) != 0 {
     45 						t.Fatal("refused split wrote durable state", writes)
     46 					}
     47 					low, high := family[0], family[1]
     48 					if quality == 1 {
     49 						high = low
     50 					} else if quality == 200 {
     51 						low = high
     52 					}
     53 					var additions int
     54 					for _, packet := range output.packets {
     55 						if binary.BigEndian.Uint32(packet[16:20]) != 0x052e2f0c {
     56 							continue
     57 						}
     58 						additions++
     59 						if binary.BigEndian.Uint32(packet[29:33]) != low || binary.BigEndian.Uint32(packet[33:37]) != high ||
     60 							binary.BigEndian.Uint32(packet[37:41]) != quality || binary.BigEndian.Uint32(packet[41:45]) != 3 {
     61 							t.Fatal("refused split lost its selected endpoint contract")
     62 						}
     63 					}
     64 					if additions != 1 {
     65 						t.Fatal("refused split did not restore exactly one stack")
     66 					}
     67 					world.advanceEquipment(pending.readyAt)
     68 					cold := newTestInventoryServer(t)
     69 					before[0].Slot = 6
     70 					if !slices.Equal(cold.characters[1].inventory.Items, before) || cold.nextItem != 2 {
     71 						t.Fatal("admitted completion or cold recovery lost the selected stack")
     72 					}
     73 				})
     74 			}
     75 		}
     76 	}
     77 }