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

120 lines
7.7 KiB
C++

#include "world_translation.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include <chrono>
#include <iostream>
using namespace gateway;
static unsigned checks=0;
static void check(bool value,const char* message="source stand/cast pipeline assertion"){++checks;if(!value)throw Error(message);}
template<class F> static void rejects(F fn){try{fn();}catch(const Error&){return;}throw Error("expected stand/cast rejection");}
static Bytes cast_request(Guid128 target={}){
Guid128 cast{};cast[0]=7;Bytes out=pack_guid(cast);le(out,0,1);
for(unsigned i=0;i<3;++i){le(out,0,4);}
le(out,2457,4);le(out,0,4);le(out,0,4);le(out,target==Guid128{}?0:2,4);
append(out,pack_guid(target));append(out,pack_guid(Guid128{}));append(out,pack_guid(Guid128{}));
le(out,0,2);le(out,0,4);le(out,0,4);append(out,pack_guid(Guid128{}));
for(unsigned i=0;i<3;++i){le(out,0,4);}
le(out,0,1);le(out,0,1);return out;
}
static NativeWorldTranslation world(){
NativeWorldTranslation w{};w.selected.guid=4520;w.realm=70;w.self_created=true;
w.location=WorldLocation{0,{1,2,3,0}};w.source_health=100;w.source_stand=1;w.spells.insert(2457);return w;
}
static void stand(NativeWorldTranslation& w){
auto p=w.gameplay(0x3e0036,Bytes{0});check(p.opcode==0x101 && p.body==Bytes({0,0,0,0}));
w.gameplay_sent(0x3e0036,Bytes{0});
}
static void queue(NativeWorldTranslation& w,const Bytes& body){
const auto p=w.gameplay(0x3e0176,body);check(w.defer_standing_cast(0x3e0176,body,p));
check(!w.pending_cast && !w.standing_cast_request());
}
int main(){try{
const auto body=cast_request();auto w=world();stand(w);queue(w,body);
rejects([&]{w.defer_standing_cast(0x3e0176,body,w.gameplay(0x3e0176,body));});
auto ack=w.translate({0x29d,Bytes{0}},1,0,nullptr);
Reader reply(ack);check(reply.le(4)==1 && reply.le(4)==0x4701c9 && reply.le(4)==5);
check(reply.le(1)==0 && reply.le(4)==0);reply.end();
auto p=w.standing_cast_request();check(p && p->opcode==0x12e);
Bytes expected;le(expected,2457,4);le(expected,0,2);check(p->body==expected);
// Sizing/repeated polling has no cipher/pending state change; commit once.
check(w.standing_cast_request()->body==expected && !w.pending_cast);
w.standing_cast_sent();check(w.pending_cast && !w.standing_cast && !w.standing_cast_request());
rejects([&]{w.standing_cast_sent();});
{auto q=world();q.source_stand=0;stand(q);check(!q.defer_standing_cast(0x3e0176,body,q.gameplay(0x3e0176,body)));}
{auto q=world();q.source_stand.reset();stand(q);check(!q.defer_standing_cast(0x3e0176,body,q.gameplay(0x3e0176,body)));}
{auto q=world();stand(q);queue(q,body);auto request=forever_spell_request(body);auto cancel=pack_guid(request.cast);le(cancel,2457,4);
check(q.cancel_standing_cast(0x3e0179,cancel));q.stand_state_received(0);check(!q.standing_cast_request());}
{auto q=world();stand(q);queue(q,body);q.stand_state_received(1);check(!q.standing_cast);}
for(unsigned mode=0;mode<7;++mode){auto q=world();stand(q);queue(q,body);q.stand_state_received(0);
if(mode==0)q.location->map=1;
if(mode==1)q.selected.guid=4521;
if(mode==2)q.self_created=false;
if(mode==3)q.source_health=0;
if(mode==4)q.logout_complete=true;
if(mode==5)q.standing_cast->deadline=std::chrono::steady_clock::now()-std::chrono::seconds(1);
if(mode==6)q.pending_teleport=NearTeleport{};
check(!q.standing_cast_request() && !q.standing_cast);
}
{auto q=world();auto guid=forever_player_guid(4519,70);auto& actor=q.entities.actors[4519];
actor.modern=guid;actor.sent=true;actor.generation=1;const auto target=cast_request(guid);stand(q);queue(q,target);
q.stand_state_received(0);actor.generation=2;check(!q.standing_cast_request() && !q.standing_cast);}
{auto q=world();stand(q);queue(q,body);LegacyObjectUpdate create;create.guid=4520;create.kind=2;create.type=4;
create.fields[turtle_entity_fields::UNIT_FIELD_BYTES_1]=0;q.observe_death_updates({create});check(!q.standing_cast && q.source_stand==0);}
// Source SendInterrupted broadcasts a complete caster GUID + spell ID.
// Another caster/spell must not cancel this local deferred standing cast.
auto interrupted=[](uint64_t caster,uint32_t spell){Bytes b;le(b,caster,8);le(b,spell,4);return b;};
auto resumes=[&](NativeWorldTranslation& q){
check(q.standing_cast.has_value(),"unrelated or malformed interrupt retired standing cast");
check(q.standing_cast->original==body && q.standing_cast->packet.body==expected,
"interrupt changed deferred cast identity or payload");
q.translate({0x29d,Bytes{0}},1,0,nullptr);
auto ready=q.standing_cast_request();
check(ready && ready->opcode==0x12e && ready->body==expected && !q.pending_cast,
"valid stand acknowledgement cannot resume preserved cast");
};
for(bool acknowledged:{false,true}){
for(auto [caster,spell]:{std::pair<uint64_t,uint32_t>{4521,2457},{4521,999},{4520,999}}){
auto q=world();stand(q);queue(q,body);if(acknowledged)q.stand_state_received(0);
check(q.translate({0x2a6,interrupted(caster,spell)},1,0,nullptr)==Bytes(4,0),
"unobserved unrelated interrupt emitted native event");resumes(q);
}
{auto q=world();stand(q);queue(q,body);if(acknowledged)q.stand_state_received(0);
check(q.translate({0x2a6,interrupted(4520,2457)},1,0,nullptr)==Bytes(4,0),
"deferred unsent cast invented native failure identity");
check(!q.standing_cast,"matching source interruption did not retire deferred cast");
q.stand_state_received(0);check(!q.standing_cast_request(),"stand acknowledgement revived interrupted cast");}
}
const auto complete=interrupted(4520,2457);
for(size_t n=0;n<=13;++n){if(n==12)continue;
auto q=world();stand(q);queue(q,body);auto bad=complete;bad.resize(n);
rejects([&]{q.translate({0x2a6,bad},1,0,nullptr);});resumes(q);
}
// SendCastResult: success is exactly spell+status0; failure status2 has
// a reason and only that reason's optional arguments. No active cast is
// required to validate framing before touching a deferred stand request.
std::vector<Bytes> cast_results;
{Bytes b;le(b,2457,4);le(b,0,1);cast_results.push_back(b);}
for(auto reason:{35u,93u,94u,25u}){Bytes b;le(b,2457,4);le(b,2,1);le(b,reason,1);
if(reason==93 || reason==94)le(b,123,4);
if(reason==25){le(b,2,4);le(b,16,4);le(b,32,4);}
cast_results.push_back(b);
}
auto bad_result=[&](const Bytes& bad){
auto q=world();stand(q);queue(q,body);
bool rejected=false;try{q.translate({0x130,bad},1,0,nullptr);}catch(const Error&){rejected=true;}
check(rejected,"malformed cast result was accepted without active pending cast");
resumes(q);
auto idle=world();rejects([&]{idle.translate({0x130,bad},1,0,nullptr);});
};
for(const auto& full:cast_results){
for(size_t n=0;n<full.size();++n){auto bad=full;bad.resize(n);bad_result(bad);}
auto trailing=full;trailing.push_back(0);bad_result(trailing);
// Preserve existing handling of genuine complete results. This unit
// changes framing atomicity, not the local valid-result cast policy.
auto q=world();stand(q);queue(q,body);
check(q.translate({0x130,full},1,0,nullptr)==Bytes(4,0),"complete uncorrelated result gained native failure");
check(!q.standing_cast,"valid-result retirement behavior changed");
}
for(auto status:{1u,3u}){Bytes bad;le(bad,2457,4);le(bad,status,1);bad_result(bad);}
std::cout<<"source stand/cast acknowledgement lifecycle PASS: "<<checks<<" checks\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}