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

113 lines
7.1 KiB
Lua

-- Focused check of the transformed GlueParent Source realm-choice routing.
-- Usage: luajit tests/source_realm_glue_poll_test.lua <transformed glueparent.lua>
local path = assert(arg[1], "transformed glueparent.lua required");
nativeConnected = false;
local nativeAuroraState = 2;
local disconnects = 0;
-- Permissive stubs: unknown globals resolve to inert callables/tables.
local function inert() return setmetatable({}, {__index = function() return inert(); end, __call = function() end}); end
setmetatable(_G, {__index = function(_, k) return inert(); end});
local snapshot = {abi=1, size=64, generation=3, endpoint_kind=1, state=1, error=0, auth_result=0,
discovery_result=0, server_proof_verified=1, realm_count=0, selected_index=-1, transport_prepared=0,
cancel=0, genuine_discovery=1, auth_port=3724, copied_realms=0};
local polls = {};
CreateFrame = function() local f = {}; function f:SetScript(name, fn) if name == "OnUpdate" then polls[#polls + 1] = fn; end end; return setmetatable(f, {__index = function() return function() end; end}); end
C_Login = setmetatable({
-- Numeric Bridge API (no Lua allocation in the foreign DLL).
GetTurtleRealmSnapshot = function(q, r, c)
local header = {"abi", "size", "generation", "endpoint_kind", "state", "error", "auth_result",
"discovery_result", "server_proof_verified", "realm_count", "selected_index", "transport_prepared",
"cancel", "auth_port", "copied_realms", "genuine_discovery", "source_login_readiness"};
if q == 0 then if r == 0 then return 0; end; return snapshot[header[r]] or 0; end
local row = snapshot["r" .. r]; if not row then return -4006; end
if q == 1 then
local f = ({"type", "flags", "characters", "category", "wire_id", "population_bits"})[c + 1];
if f == "population_bits" then return 0x3f800000; end
return row[f];
end
local text = q == 2 and row.name or row.address; local v = 0;
for i = 2, 0, -1 do local b = text:byte(c * 3 + i + 1) or 0; v = v * 256 + b; end
return v;
end,
RequestTurtleRealm = function() return 0; end,
GetState = function() return {auroraState = nativeAuroraState, connectedToWoW = nativeConnected}; end,
DisconnectFromServer = function() disconnects=disconnects+1;nativeAuroraState=0;end,
GetLastError = function() return nil; end,
GetLogonQueueInfo = function() return false; end,
}, {__index = function() return function() end; end});
LE_AURORA_STATE_WAITING_FOR_NETWORK, LE_AURORA_STATE_CONNECTING, LE_AURORA_STATE_CONNECTED, LE_AURORA_STATE_NONE = 1, 2, 3, 0;
local shown, hidden = {}, {};
StaticPopup_Show = function(which, text, _, _, _, onHide) shown[#shown + 1] = {which, text, onHide=onHide}; end
StaticPopup_Hide = function(which) hidden[#hidden + 1] = which; end
GlueParent = {currentScreen = "login"};
local chunk = assert(loadfile(path)); chunk();
assert(#polls >= 1, "owned Source poll installed");
local poll = polls[#polls];
-- Spy the original decision entry points the poll must re-run.
local ensured, updated = 0, 0;
local realEnsure, realUpdate = GlueParent_EnsureValidScreen, GlueParent_UpdateDialogs;
assert(type(realEnsure) == "function" and type(realUpdate) == "function");
GlueParent_EnsureValidScreen = function() ensured = ensured + 1; end
GlueParent_UpdateDialogs = function() updated = updated + 1; end
poll(nil, 1.0);
assert(ensured == 0 and updated == 0, "no re-run while Source is not pending");
snapshot.state, snapshot.realm_count, snapshot.copied_realms = 5, 1, 1;
snapshot.r0 = {name="Octo", address="92.114.107.62:8090", type=1, flags=0, characters=0, category=1, wire_id=1};
poll(nil, 0.2); assert(ensured == 0, "throttled below 0.5s");
poll(nil, 0.4); assert(ensured == 1 and updated == 1, "pending flip re-runs screen and dialogs");
assert(GlueParent_IsScreenValid("superdistrict") == true and GlueParent_IsScreenValid("login") == false);
assert(GlueParent_GetBestScreen() == "superdistrict");
poll(nil, 1.0); assert(ensured == 1, "no repeat while pending is unchanged");
-- While pending, the original dialog decision hides the Connecting popup.
realUpdate();
assert(#hidden >= 1 and hidden[#hidden] == "CANCEL", "Connecting popup hidden while pending");
for _, s in ipairs(shown) do assert(s[1] ~= "CANCEL", "no Connecting popup while pending"); end
-- Once a realm is chosen the realm screen is held while that realm connects
-- (the original choice would fall back to Login and disconnect the socket).
snapshot.state, snapshot.selected_index, snapshot.transport_prepared = 2, 0, 1;
poll(nil, 1.0); assert(ensured == 1, "chosen realm still connecting: screen held");
assert(GlueParent_GetBestScreen() == "superdistrict" and GlueParent_IsScreenValid("login") == false, "no Login fallback while connecting");
-- When the native connection is up the original flow resumes.
nativeConnected = true;
poll(nil, 1.0); assert(ensured == 2 and updated == 2, "native connection re-runs original decisions");
assert(GlueParent_GetBestScreen() ~= "superdistrict");
realUpdate();
assert(shown[#shown] and shown[#shown][1] == "CANCEL", "original Connecting popup restored");
-- Back uses the native disconnect. The selected realm can remain in Source's
-- snapshot until the next poll, while the native login page is already valid.
nativeConnected=false;nativeAuroraState=LE_AURORA_STATE_NONE;
poll(nil,1.0);
assert(GlueParent_GetBestScreen()=="login" and GlueParent_IsScreenValid("login"),
"Back from an established realm must return to the native login page");
-- A failed authentication never became a pending realm choice. The new
-- failure-generation edge must rerun dialogs even though IsHolding is false.
nativeConnected=false;nativeAuroraState=LE_AURORA_STATE_CONNECTING;
snapshot.generation,snapshot.state,snapshot.error,snapshot.auth_result=4,1,0,0;
snapshot.server_proof_verified,snapshot.genuine_discovery=0,0;
snapshot.realm_count,snapshot.copied_realms=0,0;
snapshot.selected_index,snapshot.transport_prepared=-1,0;
poll(nil,1);local previousEnsured,previousUpdated=ensured,updated;
snapshot.state,snapshot.error,snapshot.auth_result=3,1,-2;
poll(nil,1);
assert(ensured==previousEnsured+1 and updated==previousUpdated+1,"failed generation reruns original decisions");
local previousShown=#shown;realUpdate();
assert(disconnects==1 and #shown==previousShown+1 and shown[#shown][1]=="OKAY","real Source failure uses original disconnect and native OKAY");
assert(hidden[#hidden]=="CANCEL" and not shown[#shown][2]:find("password",1,true),"no stuck Connecting or guessed password diagnosis");
local acknowledge=shown[#shown].onHide;assert(type(acknowledge)=="function");
poll(nil,1);realUpdate();assert(disconnects==1 and #shown==previousShown+1,"same generation presents once");
acknowledge();poll(nil,1);realUpdate();assert(disconnects==1 and #shown==previousShown+1,"acknowledged failure stays dismissed");
snapshot.generation,snapshot.state,snapshot.error,snapshot.auth_result=5,1,0,0;
nativeAuroraState=LE_AURORA_STATE_CONNECTING;poll(nil,1);realUpdate();
assert(shown[#shown][1]=="CANCEL" and disconnects==1,"new genuine attempt resumes original Connecting flow");
print("PASS source_realm_glue_poll_test");