Files
WoW-Hybrid-gateway/tests/native_packet_dispatch_counters_test.py
2026-11-23 03:28:32 +02:00

97 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""Exercise production world dispatch with an in-memory metadata sink.
Only the journal dependency is substituted; all dispatch and specialist calls
are the production code. No game, network connection, or user files are used.
"""
import argparse
from pathlib import Path
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
SINK = r'''
#pragma once
#include <cstdint>
#include <span>
#include <tuple>
#include <vector>
namespace counter_fixture {
enum class Stream {Unhandled,Error};
inline std::vector<std::tuple<uint32_t,uint32_t>> packets;
struct Sink {
void begin(uint16_t) const {}
template<class T> void known(uint64_t,const T&) const {}
bool definition(uint32_t,uint32_t,bool,std::span<const uint8_t>) const {return true;}
void packet(Stream stream,uint32_t opcode,uint32_t size) const {if(stream==Stream::Unhandled)packets.emplace_back(opcode,size);}
};
inline Sink journal(){return {};}
}
'''
DRIVER = r'''
#include "world_translation.hpp"
#include "counter_sink.hpp"
#include <iostream>
#include <stdexcept>
#include <zlib.h>
using namespace gateway;
void check(bool ok){if(!ok)throw std::runtime_error("dispatch counter classification differs");}
int main(){try {
NativeWorldTranslation w{};
w.realm=70;w.selected.guid=4509;w.selected.cls=1;w.selected.race=1;
w.location=WorldLocation{0,{1,2,3,0}};w.self_created=true;
auto run=[&](uint32_t opcode,Bytes body){return w.translate({opcode,std::move(body)},0x46010046,0,nullptr);};
// Main handler, including an authoritative empty cancel acknowledgement.
run(0x4f,{});check(counter_fixture::packets.empty());
// Genuine unknown packets count once, even when their output is empty.
run(0x7fff,{1,2});
check(counter_fixture::packets==std::vector<std::tuple<uint32_t,uint32_t>>{{0x7fff,2}});
// No actor has been presented: these real downstream routes emit nothing.
Bytes id;le(id,4510,8);auto motion=pack_guid(id);
append(motion,turtle_ground_movement(GroundMovement{4510,0,1,{1,2,3,0}}));
run(0xda,motion);run(0xee,motion);
Bytes records;le(records,motion.size()+2,1);le(records,0xda,2);append(records,motion);
uLongf length=compressBound(records.size());Bytes packed(length);
check(compress2(packed.data(),&length,records.data(),records.size(),Z_DEFAULT_COMPRESSION)==Z_OK);
packed.resize(length);Bytes compressed;le(compressed,records.size(),4);append(compressed,packed);
run(0x2fb,compressed);
// Source not-found metadata replies are recognized, without fake rows.
Bytes missing;le(missing,0x80000001,4);run(0x58,missing);run(0x61,missing);
check(counter_fixture::packets.size()==1);
// The same packet outside an admitted world has no specialist dispatch.
w.self_created=false;run(0x61,missing);
check(counter_fixture::packets.back()==std::tuple<uint32_t,uint32_t>{0x61,4});
check(counter_fixture::packets.size()==2);
std::cout<<"PASS unknown/main/downstream movement/compression/item/creature counter boundaries\n";
return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}
'''
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('build', type=Path)
args = parser.parse_args()
build = args.build.resolve()
evidence = ROOT/'runtime/continuation-20261113/codex/Accepted-work-recovery-20261118'
evidence.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(dir=evidence) as directory:
path = Path(directory)
source = (ROOT/'native/world_translation.cpp').read_text()
source = source.replace('#include "diagnostics_journal.hpp"', '#include "counter_sink.hpp"')
source = source.replace('gw_diagnostics::', 'counter_fixture::')
(path/'world.cpp').write_text(source)
(path/'counter_sink.hpp').write_text(SINK)
(path/'driver.cpp').write_text(DRIVER)
exe = path/'dispatch-counters'
subprocess.run(['c++', '-std=c++20', '-O0', '-Wall', '-Wextra', '-Wpedantic', '-Werror',
'-I'+str(ROOT/'native'), '-I'+str(ROOT/'include'),
str(path/'world.cpp'), str(path/'driver.cpp'),
str(build/'libgateway_native.so'), str(build/'libgateway_protocol.a'),
'-Wl,-rpath,'+str(build), '-lcrypto', '-lz', '-o', str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == '__main__':
main()