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

192 lines
8.6 KiB
Python

"""Focused Lua 5.1 test for the active SuperDistrict realm preference route."""
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
LUA = r'''
local make = assert(loadfile(arg[1]))();
function StaticPopup_Show() end
C_Login = {GetSubRegions = function() return {}; end};
local saved, writes, reads, result = nil, 0, 0, true;
local key = string.rep("a",64);
local preference = {
Read = function(scope) assert(scope == key); reads = reads + 1; return saved; end,
Write = function(scope,value) assert(scope == key); saved = value; writes = writes + 1; end,
};
local function row(name, address, wire, offline)
return {name=name, sourceAddress=address, sourceWireID=wire,
populationState=offline and "OFFLINE" or "SOURCE", sourceType=0,
sourcePopulation="0.5", numCharacters=1};
end
local wanted = row("Octo PvE", "play.octowow.st:8085", 5);
local moved = row("Octo PvE", "192.0.2.25:8085", 5);
local other = row("Other", "play.octowow.st:8086", 6);
local rows = {[0]=other, [1]=wanted};
local status = {account_preference_key=key, generation=11, state=5, selected_index=-1, transport_prepared=0};
local requests = {};
local provider = {
Refresh = function() return true; end,
GetLoginStatus = function() return status; end,
GetRealmsInCategory = function(category)
assert(category == "octo-source"); return {0,1};
end,
GetRealmInfo = function(index) return rows[index]; end,
ConnectToRealm = function(index)
requests[#requests+1] = {index, status.generation};
return result, result and "requested" or "selection-held";
end,
Cancel = function() return true; end,
};
local function text(initial)
return {value=initial, SetText=function(self,v) self.value=v; end,
GetText=function(self) return self.value; end,
SetEnabled=function(self,v) self.enabled=v; end,
Hide=function(self) self.hidden=true; end,
SetShown=function(self,v) self.hidden=not v; end};
end
local function frame()
local value = {Title=text("Original title"), Description=text("Original description"),
SelectButton=text("Original select"), SubRegionDropdown=text(), districtButtons={},
ButtonLayout={Layout=function() end}};
value.districtButtonPool = {
ReleaseAll=function() end,
Acquire=function()
local button = {
Title=text(), Description=text(), Icon={GetTexture=function() end, SetTexture=function() end},
SetEnabled=function() end, SetDisabledVisuals=function() end,
SetMotionScriptsWhileDisabled=function() end, SetDisabledTooltip=function() end,
SetScript=function() end, Show=function() end,
};
return button;
end,
};
function value:UpdateSelection() end
return value;
end
local function session()
return make(provider, preference), frame();
end
-- First visit requires manual selection and a confirmed character list.
local first, firstFrame = session();
assert(first.Refresh(firstFrame) and #requests == 0);
assert(first.Confirm(firstFrame, "source:1") and #requests == 1 and requests[1][1] == 1);
assert(writes == 0 and saved == nil);
status.state, status.selected_index, status.transport_prepared = 2, 1, 1;
first.CharacterListReceived();
status.state, status.selected_index, status.transport_prepared = 5, -1, 0;
assert(writes == 1 and saved == "v2|Octo%20PvE|5");
-- Reordered current rows and a changed peer address still select the one
-- current authenticated row with the stable name and wire ID.
rows = {[0]=moved, [1]=other}; status.generation = 12;
local nextSession, nextFrame = session();
assert(nextSession.Refresh(nextFrame) and #requests == 2 and requests[2][1] == 0
and requests[2][2] == 12);
assert(nextFrame.SelectButton.enabled == false and #nextFrame.districtButtons == 0
and nextFrame.Description.value == "Connecting to Octo PvE...");
nextSession.Refresh(nextFrame); assert(#requests == 2 and writes == 1);
-- Changed names/wire IDs, offline or duplicate rows never auto request.
local function noAuto(currentRows, generation)
rows = currentRows; status.generation = generation;
local before = #requests;
local adapter, shown = session(); assert(adapter.Refresh(shown) and #requests == before);
end
noAuto({[0]=row("Renamed", "play.octowow.st:8087", 5), [1]=other}, 13);
noAuto({[0]=row("Octo PvE", "play.octowow.st:8085", 7), [1]=other}, 14);
noAuto({[0]=row("Octo PvE", "play.octowow.st:8085", 5, true), [1]=other}, 15);
noAuto({[0]=wanted, [1]=moved}, 16);
saved = "invalid"; noAuto({[0]=wanted, [1]=other}, 17);
-- A previously saved v1 value first accepts an exact current address; if
-- that address changed, a unique current name/wire ID match is usable.
saved = "v1|Octo%20PvE|play.octowow.st%3A8085|5";
rows = {[0]=moved, [1]=other}; status.generation = 18;
local before = #requests;
local migrated, migratedFrame = session();
assert(migrated.Refresh(migratedFrame) and #requests == before + 1
and requests[#requests][1] == 0 and saved == "v1|Octo%20PvE|play.octowow.st%3A8085|5" and writes == 1);
saved = "v1|Octo%20PvE|play.octowow.st%3A8085|5";
rows = {[0]=wanted, [1]=moved}; status.generation = 19;
before = #requests;
local exact, exactFrame = session();
assert(exact.Refresh(exactFrame) and #requests == before + 1
and requests[#requests][1] == 0 and writes == 1
and saved == "v1|Octo%20PvE|play.octowow.st%3A8085|5");
-- A refused auto request does not loop or change the remembered identity.
saved = "v2|Octo%20PvE|5";
rows = {[0]=wanted, [1]=other}; status.generation = 20; result = false;
local refused, refusedFrame = session();
before = #requests;
assert(refused.Refresh(refusedFrame) and #requests == before + 1);
refused.Refresh(refusedFrame); assert(#requests == before + 1 and writes == 1);
result = true;
-- A nil/empty machine saved variable can load after the Glue module. It must
-- not consume this generation's one automatic attempt before the value loads.
saved = nil; rows = {[0]=moved, [1]=other}; status.generation = 21;
local late, lateFrame = session(); before = #requests;
assert(late.Refresh(lateFrame) and #requests == before);
late.OnUpdate(lateFrame, 0.5); assert(#requests == before);
saved = "v2|Octo%20PvE|5";
late.OnUpdate(lateFrame, 0.5);
assert(#requests == before + 1 and requests[#requests][1] == 0);
status.selected_index, status.transport_prepared = 0, 1;
assert(late.Refresh(lateFrame) and lateFrame.Description.value == "Connecting to Octo PvE...");
status.selected_index, status.transport_prepared = -1, 0;
-- Without a preference, original manual choice is available; outside the
-- pending state Source does not own the SuperDistrict controls.
status.generation = 22;
local disabled = make(provider, nil); local disabledFrame = frame();
local beforeReads = reads;
assert(disabled.Refresh(disabledFrame) and reads == beforeReads);
assert(#disabledFrame.districtButtons == 2);
assert(disabled.Confirm(disabledFrame, "source:0") and requests[#requests][1] == 0);
-- Machine saved-variable read/write failures are inert, and refusal cannot
-- overwrite a remembered choice.
status.generation = 23;
local failingPreference = {
Read = function() error("saved variable read unavailable"); end,
Write = function() error("saved variable write unavailable"); end,
};
local guarded = make(provider, failingPreference); local guardedFrame = frame();
assert(guarded.Refresh(guardedFrame) and #guardedFrame.districtButtons == 2);
assert(guarded.Confirm(guardedFrame, "source:0"));
result = false; status.generation = 24;
local priorSaved, priorWrites = saved, writes;
local failedManual = make(provider, preference); local failedFrame = frame();
assert(failedManual.Refresh(failedFrame));
assert(failedManual.Confirm(failedFrame, "source:0"));
assert(saved == priorSaved and writes == priorWrites);
status.state = 2;
assert(disabled.Refresh(disabledFrame) == false);
print("PASS SuperDistrict current-row realm memory and safe fallback");
'''
class DistrictPreference(unittest.TestCase):
def test_active_source_realm_preference(self):
lua = shutil.which("lua") or shutil.which("lua5.1")
self.assertIsNotNone(lua, "Lua 5.1 interpreter required")
with tempfile.TemporaryDirectory() as temporary:
fixture = Path(temporary) / "district-preference.lua"
fixture.write_text(LUA)
result = subprocess.run(
[lua, str(fixture), str(ROOT / "compat/glue/source_realm_district_adapter.lua")],
text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("PASS SuperDistrict", result.stdout)
if __name__ == "__main__":
unittest.main()