Files
WoW-Hybrid-gateway/tests/native_forced_reactions_test.cpp

38 lines
2.6 KiB
C++

#include "world_translation.hpp"
#include <algorithm>
#include <iostream>
using namespace gateway;
static unsigned checks;
static void check(bool ok){++checks;if(!ok)throw Error("forced reaction fixture");}
static int capture(uint32_t mode,const uint8_t* in,uint32_t size,uint32_t,uint8_t* out,uint32_t capacity,uint32_t* written){
if(mode!=GW_CODEC_FORCED_REACTIONS)return GW_CODEC_PACKET;
*written=size;if(capacity<size)return GW_CODEC_CAPACITY;std::copy(in,in+size,out);return GW_CODEC_OK;
}
static Bytes list(std::initializer_list<std::pair<uint32_t,uint32_t>> entries){
Bytes b;le(b,entries.size(),4);for(auto [f,r]:entries){le(b,f,4);le(b,r,4);}return b;
}
static Bytes only(const Bytes& wire){Reader r(wire);check(r.le(4)==1);check(r.le(4)==0x5e0000);auto b=r.take(r.le(4));r.end();return b;}
static ForcedReactions dto(const Bytes& b){Reader r(b);check(r.le(1)==1);check(r.le(2)==70);check(r.le(2)==0);check(r.le(8)==4514);ForcedReactions a;for(auto& x:a){x.faction=r.le(4);x.reaction=r.le(4);}r.end();return a;}
int main(){try{
NativeWorldTranslation w;w.realm=70;w.selected.guid=4514;w.selected.name="Synthetic";
auto translate=[&](uint32_t op,Bytes b){return w.translate({op,std::move(b)},0x46010046,0,capture);};
const auto two=list({{67,1},{1090,7}});
check(translate(0x2a5,two)==Bytes(4));check(w.forced_reactions_dirty); // retain until self exists
w.location=WorldLocation{0,{0,0,0,0}};w.self_created=true;
auto first=dto(only(translate(0x2a5,two)));
check(first[0].faction==67 && first[0].reaction==1);check(first[1].faction==1090 && first[1].reaction==7);
for(unsigned i=2;i<32;++i)check(!first[i].faction&&!first[i].reaction);
check(!w.forced_reactions_dirty && w.no_native_consumer_drops.empty());
for(const auto& bad:{Bytes{},Bytes{33,0,0,0},list({{0,1}}),list({{67,8}}),list({{67,1},{67,2}})}){
bool rejected=false;try{translate(0x2a5,bad);}catch(const Error&){rejected=true;}check(rejected);
check(w.forced_reactions->at(0).faction==67); // invalid replacement preserves old state
}
auto cleared=dto(only(translate(0x2a5,list({}))));
for(const auto& x:cleared)check(!x.faction&&!x.reaction);
check(!w.forced_reactions_dirty);
// Clear received before world entry must also replace a previously retained list.
w.self_created=false;check(translate(0x2a5,two)==Bytes(4));check(translate(0x2a5,list({}))==Bytes(4));
w.self_created=true;auto replay=dto(only(translate(0x330,{})));for(const auto& x:replay)check(!x.faction&&!x.reaction);
std::cout<<"PASS "<<checks<<" forced-reaction routing, retention, replacement and full 32-slot DTO checks\n";
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}