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

144 lines
6.2 KiB
Python

"""Q105 lifecycle checks using the real district, exit and glue factories."""
import importlib.util
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location(
"realm_exit_preference_fixture", ROOT / "tests/source_realm_district_preference_test.py")
preference_fixture = importlib.util.module_from_spec(spec)
spec.loader.exec_module(preference_fixture)
# Reuse only the existing external provider/frame setup, not its test execution.
DISTRICT_SETUP = preference_fixture.LUA.split(
"-- First visit requires manual selection and a confirmed character list.")[0]
DISTRICT = r'''
C_Login.GetState = function() return {connectedToWoW=false}; end;
local backendCancels = 0;
provider.Cancel = function() backendCancels=backendCancels+1; return true; end;
local exit = assert(loadfile(arg[2]))()(provider, preference);
-- Positive control: the very same manual choice and successful list can save.
local control, cf = session();
assert(control.Refresh(cf) and control.Confirm(cf, "source:1"));
status.state,status.selected_index,status.transport_prepared = 2,1,1;
control.CharacterListReceived();
assert(saved == "v2|Octo%20PvE|5" and writes == 1);
-- Exercise both actual Cancel branches: pending and already connecting.
for _, connecting in ipairs({false,true}) do
saved = nil;
status.generation = status.generation+1;
status.state,status.selected_index,status.transport_prepared = 5,-1,0;
local district, shown = session();
assert(district.Refresh(shown));
local priorWrites, priorRequests = writes, #requests;
assert(district.Confirm(shown, "source:1"));
assert(#requests == priorRequests+1 and writes == priorWrites);
status.state,status.selected_index,status.transport_prepared = 2,1,1;
if connecting then assert(district.Refresh(shown)); end;
-- Real Q105 helper clears the account preference; real district Cancel
-- must discard its closure-owned pendingChoice even on the early return.
assert(exit() and saved == nil and writes == priorWrites+1);
local cancels = backendCancels;
local accepted = district.Cancel(shown);
assert(accepted == not connecting);
assert(backendCancels == cancels+(connecting and 0 or 1));
district.CharacterListReceived();
district.CharacterListReceived();
assert(saved == nil and writes == priorWrites+1,
"delayed list repersisted the cancelled manual choice");
-- Cancellation must not disable persistence for a fresh manual login.
status.generation = status.generation+1;
status.state,status.selected_index,status.transport_prepared = 5,-1,0;
local fresh, ff = session();
priorRequests = #requests;
assert(fresh.Refresh(ff) and #requests == priorRequests);
assert(fresh.Confirm(ff, "source:0"));
status.state,status.selected_index,status.transport_prepared = 2,0,1;
fresh.CharacterListReceived();
assert(saved == "v2|Other|6" and writes == priorWrites+2);
end;
print("PASS real district pending/connecting cancellation and delayed-list persistence");
'''
GLUE = r'''
local make = assert(loadfile(arg[1]))();
local now, disconnects, connected = 100,0,false;
function GetTime() return now; end;
C_Login = {
GetState=function() return {connectedToWoW=connected}; end,
DisconnectFromServer=function() disconnects=disconnects+1; connected=false; end,
};
local status;
local source = {
Refresh=function() return true; end,
GetStatus=function() return true; end,
GetLoginStatus=function() return status; end,
};
for _, state in ipairs({5,2}) do
status = {generation=10, state=state, server_proof_verified=1,
genuine_discovery=1, auth_result=0, discovery_result=0, error=0,
selected_index=state==5 and -1 or 0,
transport_prepared=state==5 and 0 or 1, cancel=0};
local control, exiting = make(source,true),make(source,true);
control.Refresh(); exiting.Refresh();
assert(control.IsHolding() and exiting.IsHolding());
-- Native disconnect alone does not change the retained source snapshot.
local before = disconnects;
C_Login.DisconnectFromServer(); control.Refresh();
assert(disconnects == before+1 and control.IsHolding(),
"control failed to expose stale source-generation hold");
-- Exact owned notification used before the native disconnect by callers.
local SourceAccountBack = exiting.OnAccountBack;
SourceAccountBack();
C_Login.DisconnectFromServer();
exiting.Refresh();
assert(disconnects == before+2 and not exiting.IsHolding()
and not exiting.IsPending(), "retired generation retained source screen");
now=now+1; exiting.Refresh();
assert(not exiting.IsHolding(), "same stale snapshot rearmed hold");
assert(status.generation==10 and status.state==state and status.cancel==0,
"fixture must retain the stale backend snapshot unchanged");
-- A genuine later login remains eligible; retirement is not global.
status.generation=11;
exiting.Refresh();
assert(exiting.IsHolding(), "new login generation incorrectly retired");
end;
print("PASS real glue account-back retirement across pending/chosen stale snapshots");
'''
class RealmExitLifecycle(unittest.TestCase):
def run_lua(self, script, *inputs):
lua = shutil.which("lua") or shutil.which("lua5.1")
self.assertIsNotNone(lua, "Lua interpreter required")
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary) / "lifecycle.lua"
path.write_text(script)
result = subprocess.run([lua, str(path), *map(str, inputs)],
text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("PASS real", result.stdout)
def test_manual_choice_cancel_delayed_list_does_not_resave(self):
self.run_lua(DISTRICT_SETUP + DISTRICT,
ROOT / "compat/glue/source_realm_district_adapter.lua",
ROOT / "compat/glue/source_realm_exit.lua")
def test_account_back_retires_stale_pending_and_connecting_snapshot(self):
self.run_lua(GLUE, ROOT / "compat/glue/source_realm_glue_adapter.lua")
if __name__ == "__main__":
unittest.main()