Files
WoW-Hybrid-gateway/tests/keyring_inventory_test.cpp
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

77 lines
5.7 KiB
C++

#include "inventory_translation.hpp"
#include "serializer.h"
#include "gateway/keyring_slots.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include <iostream>
#include <source_location>
using namespace gateway;
namespace F=gateway::turtle_entity_fields;
static constexpr unsigned first=F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+24;
static void check(bool ok,std::source_location at=std::source_location::current()){
if(!ok)throw Error("keyring inventory fixture line "+std::to_string(at.line()));
}
static void pair(std::map<uint16_t,uint32_t>& fields,unsigned index,uint64_t guid){fields[index]=uint32_t(guid);fields[index+1]=uint32_t(guid>>32);}
static LegacyPacket update(uint64_t guid,uint8_t type,uint8_t kind,std::map<uint16_t,uint32_t> fields){
Bytes b;le(b,1,4);le(b,0,1);le(b,kind,1);Bytes id;le(id,guid,8);append(b,pack_guid(id));
if(kind==2||kind==3){le(b,type,1);le(b,0,1);}
const auto count=fields.rbegin()->first/32+1;le(b,count,1);std::vector<uint32_t> masks(count);
for(auto [index,value]:fields){(void)value;masks[index/32]|=1u<<(index%32);}
for(auto mask:masks)le(b,mask,4);for(auto [index,value]:fields){(void)index;le(b,value,4);}
return {0xa9,b};
}
static std::map<uint16_t,uint32_t> key_fields(const Bytes& input){
Reader r(input);r.le(8);r.le(2);r.le(4);auto count=r.le(4);std::map<uint16_t,uint32_t> result;
for(unsigned i=0;i<count;++i){r.le(8);auto type=r.le(1);r.le(1);auto fields=r.le(2);
for(unsigned j=0;j<fields;++j){auto index=r.le(2);auto value=r.le(4);if(type==4&&index>=first&&index<first+64)result[uint16_t(index)]=uint32_t(value);}
}r.end();return result;
}
int main(){try{
constexpr uint64_t self=42;NativeInventoryTranslation state;Bytes last;
auto codec=[&](uint32_t mode,const Bytes& input){if(mode==GW_CODEC_INVENTORY)last=input;return Bytes(6);};
auto send=[&](LegacyPacket packet){state.translate(packet,self,70,0,false,codec);};
std::array<uint64_t,32> ids{};std::map<uint16_t,uint32_t> slots;
for(unsigned i=0;i<32;++i){
ids[i]=(uint64_t(0x4000)<<48)|(1000+i);std::map<uint16_t,uint32_t> item{{F::OBJECT_FIELD_ENTRY,2592},{F::ITEM_FIELD_STACK_COUNT,2}};
pair(item,F::ITEM_FIELD_OWNER,self);pair(item,F::ITEM_FIELD_CONTAINED,self);send(update(ids[i],1,2,item));pair(slots,first+2*i,ids[i]);
}
send(update(self,4,0,slots));check(key_fields(last)==slots);
for(unsigned i=0;i<32;++i){
auto pos=state.position(255,uint8_t(112+i));check(pos.bag==255&&pos.slot==81+i&&pos.item==ids[i]);
Bytes destroy;le(destroy,1,4);le(destroy,255,1);le(destroy,112+i,1);
auto result=state.gameplay(0x3e016c,destroy);check(result.opcode==0x111&&result.body==Bytes({255,uint8_t(81+i),1,0,0,0}));
}
check(state.items.size()==32&&state.quest_item_counts(self).at(2592)==64);
auto held=[&](auto action){bool rejected=false;try{action();}catch(const Error&){rejected=true;}check(rejected);};
held([&]{state.position(255,111);});held([&]{state.position(255,144);});held([&]{state.position(254,112);});
// Normal root swaps/splits continue through the same Source validators.
auto swapped=state.gameplay(0x410007,Bytes{0,143,112});check(swapped.opcode==0x10d&&swapped.body==Bytes({81,112}));
Bytes split{0,255,112,255,143};le(split,1,4);auto split_result=state.gameplay(0x410008,split);check(split_result.opcode==0x10e&&split_result.body==Bytes({255,81,255,112,1}));
// Packed native item-use shape reused from the established inventory fixture.
Bytes use{255,112};append(use,pack_guid(state.owned_bindings().at(ids[0])));append(use,Bytes{0,0});
le(use,0,1);le(use,0,8);le(use,0,4);le(use,433,4);le(use,0,8);le(use,0,4);
append(use,Bytes(8));append(use,Bytes(8));append(use,Bytes(2));append(use,Bytes(12));append(use,Bytes(2));
auto used=state.use_item(use,{});check(used.opcode==0xab&&used.body==Bytes({255,81,0,0,0}));
auto wrong=use;wrong[1]=113;held([&]{state.use_item(wrong,{});});
// Actual partial low-word deltas retain the cached high words; no other
// keyring entry is included in this native change publication.
send(update(self,4,0,{{first,uint32_t(ids[31])},{first+62,uint32_t(ids[0])}}));
auto delta=key_fields(last);check(delta.size()==4&&delta.at(first+1)==uint32_t(ids[31]>>32)&&delta.at(first+63)==uint32_t(ids[0]>>32));
check(state.position(255,112).item==ids[31]&&state.position(255,143).item==ids[0]);
send(update(self,4,0,{{F::PLAYER_FIELD_PACK_SLOT_1,0},{F::PLAYER_FIELD_PACK_SLOT_1+1,0}}));check(key_fields(last).empty());
send(update(self,4,0,{{first+62,0},{first+63,0}}));check(key_fields(last).size()==2&&state.position(255,143).item==0);
Bytes empty_destroy;le(empty_destroy,1,4);append(empty_destroy,Bytes{255,143});held([&]{state.gameplay(0x3e016c,empty_destroy);});
// Referenced but absent/foreign owned objects never authorize operations.
state.items.erase(ids[31]);held([&]{state.position(255,112);});
// Full Source creates clear every omitted slot, including the previous
// nonzero references. The Source still owns progress/items; no local grant.
std::map<uint16_t,uint32_t> fresh;pair(fresh,first+62,ids[1]);send(update(self,4,2,fresh));
delta=key_fields(last);check(delta.size()==64);for(unsigned i=0;i<31;++i)check(delta.at(first+2*i)==0&&delta.at(first+2*i+1)==0);
check(state.position(255,112).item==0&&state.position(255,143).item==ids[1]);
send(update(self,4,2,{{F::PLAYER_FLAGS,0}}));
delta=key_fields(last);check(delta.size()==64);
for(auto [index,value]:delta){(void)index;check(value==0);}
check(state.position(255,143).item==0);
std::cout<<"All32 Source keyring routes, controls, partial GUID events, full-create clears and ownership holds PASS\n";
return 0;
}catch(const std::exception& error){std::cerr<<error.what()<<'\n';return 1;}}