Files
WoW-Hybrid-gateway/tests/source_realm_change_test.py

119 lines
7.3 KiB
Python

"""Execute selected Change Realm/Back callbacks with the real remembered-realm adapter."""
from pathlib import Path
import importlib.util, os, subprocess, tempfile, unittest
ROOT=Path(__file__).resolve().parents[1]
def module(name,path):
spec=importlib.util.spec_from_file_location(name,path);m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m);return m
ui=module('realm105_ui',ROOT/'ops/source_realm_ui.py')
fixture=module('realm105_existing',ROOT/'tests/source_realm_district_preference_test.py')
INPUT=Path(os.environ.get('Q105_INPUTS','/mnt/Games/@project/build/realm105-change-20261128'))
CASES={'character':('CharacterSelectUtil.lua','function CharacterSelectUtil.ChangeRealm()','CharacterSelectUtil.ChangeRealm()'),
'realm':('RealmList.lua','function RealmList_OnCancel()','RealmList_OnCancel()'),
'district':('SuperDistrict.lua','function SuperDistrictMixin:OnCancel()','SuperDistrictMixin:OnCancel()')}
PRE=fixture.LUA.split('-- First visit requires manual selection')[0]
BODY=r'''
local disconnects, nativeChanges, legacyClears, retirements = 0,0,0,0;
function GlueParent_SourceAccountBack() retirements=retirements+1;end;
local nativeIdentity = {};
C_RealmList=nativeIdentity;
C_RealmList.RequestChangeRealmList=function() nativeChanges=nativeChanges+1;end;
C_Login.GetState=function()return {connectedToWoW=true};end;
C_Login.DisconnectFromServer=function()disconnects=disconnects+1;end;
local loginIdentity=C_Login;
function CharacterSelect_Exit() GlueParent_SourceAccountBack();C_Login.DisconnectFromServer();end;
CharacterSelectUtil={SetAutoSwitchRealm=function()end};
CharacterSelectListUtil={SaveCharacterOrder=function()end};
CharacterSelectCharacterFrame={ClearSearch=function()end};
SOUNDKIT={GS_CHARACTER_SELECTION_DEL_CHARACTER=1};function PlaySound()end;
EventRegistry={TriggerEvent=function()end};function ExitSuperDistrictSelection()end;
RealmListProvider={IsSource=function()return true;end};
SourceRealmDistrict={Cancel=function()return true;end};SuperDistrictMixin={};
local factoryFile=io.open(arg[2],'r');local SourceRealmExit;
if factoryFile then factoryFile:close();SourceRealmExit=assert(loadfile(arg[2]))()(provider,preference,function()legacyClears=legacyClears+1;end)
else SourceRealmExit=function()return false;end;end;
saved='v2|Octo%20PvE|5';rows={[0]=wanted,[1]=other};
local automatic,af=session();assert(automatic.Refresh(af) and #requests==1 and requests[1][1]==0);
status.state,status.selected_index,status.transport_prepared=2,0,1;
__CALLBACK__
__INVOKE__
assert(saved==nil,'Change Realm/Back retained saved realm');
assert(disconnects==1 and nativeChanges==0,'did not use clean native disconnect');
assert(legacyClears==1,'legacy remembered CVar not cleared');
assert(retirements==1,'source generation not retired before disconnect');
status.generation=12;status.state,status.selected_index,status.transport_prepared=5,-1,0;
local nextSession,nf=session();assert(nextSession.Refresh(nf) and #requests==1 and #nf.districtButtons==2,'list auto-joined after explicit exit');
assert(nextSession.Confirm(nf,'source:1') and #requests==2 and requests[2][1]==1);
assert(saved==nil,'remembered before character-list success');
status.state,status.selected_index,status.transport_prepared=2,1,1;nextSession.CharacterListReceived();
assert(saved=='v2|Other|6','different realm not remembered');
status.generation=13;status.state,status.selected_index,status.transport_prepared=5,-1,0;
local normal,normalFrame=session();assert(normal.Refresh(normalFrame) and #requests==3 and requests[3][1]==1,'normal auto-join lost');
assert(C_RealmList==nativeIdentity and C_Login==loginIdentity,'native API replaced');
print('PASS real callback: remembered autojoin, explicit exit, new choice and subsequent autojoin');
'''
def callback(text,anchor):
body=text[text.index(anchor):];return body[:body.index('\nend\n')+5]
class RealmChange(unittest.TestCase):
def test_callbacks_and_remembered_lifecycle(self):
for kind,(name,anchor,invoke) in CASES.items():
with self.subTest(kind=kind):
raw=(INPUT/name).read_bytes()
if hasattr(ui,'transform_realm_exit'):raw=ui.transform_realm_exit(raw,consumer=kind)
text=raw.decode().replace('\r\n','\n')
script=PRE+BODY.replace('__CALLBACK__',callback(text,anchor)).replace('__INVOKE__',invoke)
with tempfile.TemporaryDirectory() as d:
p=Path(d)/'test.lua';p.write_text(script)
r=subprocess.run(['lua',str(p),str(ROOT/'compat/glue/source_realm_district_adapter.lua'),str(ROOT/'compat/glue/source_realm_exit.lua')],capture_output=True,text=True)
self.assertEqual(r.returncode,0,r.stdout+r.stderr)
def test_emitted_numeric_backend_account_scope_and_fail_closed(self):
raw=(INPUT/'CharacterSelectUtil.lua').read_bytes()
text=ui.transform_realm_exit(raw,consumer='character').decode().replace('\r\n','\n')
prefix=text[:text.index('local s_vasQueueTimes = {};')]
setup=r"""
local a=string.rep('a',64);local b=string.rep('b',64);
g_octoLastRealmIdentity={[a]='v2|Alpha|1',[b]='v2|Beta|2'};
local values={1,64,7,0,2,0,0,0,1,1,0,1,0,3724,1,1,1};
local legacy,requests='',0;local torn=false;local generationReads=0;
C_Login={RequestTurtleRealm=function()requests=requests+1;end};
function C_Login.GetTurtleRealmSnapshot(q,i,f)
if q==0 then
if i==0 then generationReads=0;return 0;end;
if i==3 then generationReads=generationReads+1;if torn and generationReads>1 then return 8;end;end;
if i<=17 then return values[i];end;return tonumber(a:sub((i-18)*4+1,(i-18)*4+4),16);
end;
if q==1 then return ({0,0,1,0,1,0x3f000000})[f+1];end;
local v=q==2 and 'Alpha' or '192.0.2.1:8085';
return (v:byte(f*3+1)or 0)+(v:byte(f*3+2)or 0)*256+(v:byte(f*3+3)or 0)*65536;
end;
local login=C_Login;
function SetCVar(k,v)assert(k=='octoLastRealmIdentity' and v=='');legacy=legacy..'x';end;
"""
tail=r"""
assert(SourceRealmExit());assert(g_octoLastRealmIdentity[a]==nil and g_octoLastRealmIdentity[b]=='v2|Beta|2');
assert(legacy=='x' and C_Login==login and requests==0);
g_octoLastRealmIdentity[a]='v2|Alpha|1';torn=true;
assert(not SourceRealmExit() and g_octoLastRealmIdentity[a]=='v2|Alpha|1' and legacy=='x');
torn=false;values[9]=0;
assert(not SourceRealmExit() and g_octoLastRealmIdentity[a]=='v2|Alpha|1');
print('PASS emitted numeric snapshot, current account only, torn and unverified snapshots held');
"""
with tempfile.TemporaryDirectory() as d:
p=Path(d)/'prefix.lua';p.write_text(setup+prefix+tail)
r=subprocess.run(['lua',str(p)],capture_output=True,text=True)
self.assertEqual(r.returncode,0,r.stdout+r.stderr)
def test_pinned_inputs_and_full_lua_syntax(self):
for kind,(name,_,_) in CASES.items():
raw=(INPUT/name).read_bytes()
with self.assertRaisesRegex(ValueError,'Pinned'):
ui.transform_realm_exit(raw+b' ',consumer=kind)
candidate=ui.transform_realm_exit(raw,consumer=kind)
with self.assertRaisesRegex(ValueError,'Pinned'):
ui.transform_realm_exit(candidate,consumer=kind)
with tempfile.TemporaryDirectory() as d:
p=Path(d)/name;p.write_bytes(candidate)
r=subprocess.run(['luac','-p',str(p)],capture_output=True,text=True)
self.assertEqual(r.returncode,0,r.stderr)
if __name__=='__main__':unittest.main()