Files
WoW-Hybrid-gateway/tests/character_select_ready_ui_test.py
2026-11-19 02:25:06 +02:00

163 lines
8.1 KiB
Python

"""Enter World stays closed while the character list refreshes; no client.
The helper is exercised on its own. When the pinned original character-select
file is available (it is client data and not part of this repository), the
transformed file is loaded in plain Lua with stubbed engine functions and the
original functions themselves are driven: permission check, Enter World and the
CHARACTER_LIST_UPDATE branch.
"""
from pathlib import Path
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'ops'))
import character_select_ready_ui as ui
ORIGINAL = Path(os.environ.get('GW_CHARACTER_SELECT_ORIGINAL', ROOT / (
'runtime/continuation-20261113/codex/PTR-feedback-disable/native70334-ui/' + ui.SOURCE_REFERENCE)))
LUA = shutil.which('lua5.1') or shutil.which('luajit') or shutil.which('lua')
HELPER_CASES = r'''
local logged = {};
C_Log = {LogMessage = function(text) logged[#logged + 1] = text; end};
dofile(arg[1]);
local shown = true;
local frame = {waitingforCharacterList = true, IsShown = function() return shown; end};
assert(CharacterListReady.Held(nil), "no screen object is held");
assert(CharacterListReady.Held({}), "an unset flag is held");
assert(CharacterListReady.Held(frame) and #logged == 0, "a permission poll is held without a log line");
assert(CharacterListReady.Held(frame, true) and logged[1] == "GW_CHARSELECT_ENTER_HELD waiting_list=1", "a held attempt is logged");
assert(CharacterListReady.Received(frame, 3) == false and logged[2] == "GW_CHARSELECT_LIST_UPDATE shown=1 count=3", "a list on the shown screen ends the wait");
frame.waitingforCharacterList = false;
assert(not CharacterListReady.Held(frame, true) and #logged == 2, "ready screen is open and silent");
shown = false;
assert(CharacterListReady.Received(frame, 3) == true and logged[3] == "GW_CHARSELECT_LIST_UPDATE shown=0 count=3", "a list on the hidden screen keeps the wait");
assert(CharacterListReady.Received(nil) == true, "no screen object keeps the wait");
C_Log = nil;
assert(CharacterListReady.Held(frame, true) == false and CharacterListReady.Received(frame, 1) == true, "works without the log interface");
print("helper ok");
'''
ORIGINAL_CASES = r'''
-- Every unknown global is a callable, indexable placeholder; the functions
-- the three paths depend on are given real answers below.
local any = setmetatable({}, {});
getmetatable(any).__index = function() return any; end;
getmetatable(any).__call = function() return any; end;
setmetatable(_G, {__index = function() return any; end});
local logged = {};
C_Log = {LogMessage = function(text) logged[#logged + 1] = text; end};
dofile(arg[1]);
local no = function() return false; end;
local hidden = {IsShown = no};
local entered, lists = 0, 0;
EnterWorld = function() entered = entered + 1; end;
UpdateCharacterList = function() lists = lists + 1; end;
UpdateAddonButton = function() end;
CharacterSelect_SetSelectedCharacterName = function() end;
-- Paid-service and promotion follow-ups of the same event branch are not under test.
CharacterServicesMaster_OnCharacterListUpdate = function() end;
CharacterSelectUtil = {IsAccountLocked = no};
CharacterSelectListUtil = {SaveCharacterOrder = function() end, BuildCharIndexToIDMapping = function() end,
GetCharIDFromIndex = function() return 1; end};
C_GameRules = {GetActiveGameMode = function() return 0; end, GetGameModeGlueScreenName = function() return nil; end};
Enum = {GameMode = {Plunderstorm = 1}};
local characters = 2;
GetNumCharacters = function() return characters; end;
GetCharacterSelection = function() return 1; end;
GetCharacterGUID = function() return "Player-1-00000001"; end;
GetServiceCharacterInfo = function() return {}; end;
GetBasicCharacterInfo = function() return {fullName = "Fixture"}; end;
GetCharacterTimerunningSeasonID = function() return nil; end;
AccountReactivationInProgressDialog, GoldReactivateConfirmationDialog, TokenReactivateConfirmationDialog = hidden, hidden, hidden;
CharSelectServicesFlowFrame = {ShouldDisableButtons = no};
Kiosk = {IsEnabled = no};
IsNameReservationOnly, IsCharacterTimerunning, IsRPEBoostEligible, IsKioskGlueEnabled = no, no, no, no;
PromotionFrame_AwaitingPromotion, StaticPopup_Hide, PlaySound, StopGlueAmbience = no, no, no, no;
local shown = true;
CharacterSelect = setmetatable({waitingforCharacterList = true, selectedIndex = 1,
IsShown = function() return shown; end}, {__index = CharacterSelectFrameMixin});
local function allowed() return (CharacterSelect_AllowedToEnterWorld()); end
-- 1. Waiting for the list: every route is closed although the engine still
-- reports characters and a selection (the list from before the logout).
assert(allowed() == false, "permission is closed while the list is awaited");
CharacterSelect_EnterWorld();
assert(entered == 0 and logged[#logged] == "GW_CHARSELECT_ENTER_HELD waiting_list=1", "Enter World is not sent while the list is awaited");
-- 2. The list arrives on the shown screen through the original event branch.
CharacterSelect:OnEvent("CHARACTER_LIST_UPDATE", 2);
assert(CharacterSelect.waitingforCharacterList == false and lists == 1, "the original branch ends the wait and rebuilds the rows");
assert(allowed() == true, "permission opens with the new list");
CharacterSelect_EnterWorld();
assert(entered == 1, "Enter World is sent once");
-- 3. The screen asks again (as its own request wrapper does): closed again.
CharacterSelect.waitingforCharacterList = true;
assert(allowed() == false, "a new request closes Enter World again");
-- 4. Left the screen; a late list answer must not open it.
shown = false;
CharacterSelect:OnEvent("CHARACTER_LIST_UPDATE", 2);
assert(CharacterSelect.waitingforCharacterList == true and allowed() == false, "a list received while hidden keeps Enter World closed");
CharacterSelect_EnterWorld();
assert(entered == 1, "no second Enter World");
-- 5. Shown again and answered: open; the original conditions still apply.
shown = true;
CharacterSelect:OnEvent("CHARACTER_LIST_UPDATE", 2);
assert(allowed() == true, "open after the answer on the shown screen");
characters = 0;
CharacterSelect.waitingforCharacterList = false;
assert(allowed() == false, "the original empty-list condition is unchanged");
print("original ok");
'''
def run_lua(script, argument):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'cases.lua'
path.write_text(script, encoding='utf-8')
return subprocess.run([LUA, str(path), str(argument)], capture_output=True, text=True)
class CharacterSelectReady(unittest.TestCase):
def test_other_input_is_refused(self):
with self.assertRaises(ValueError):
ui.transform(b'function CharacterSelect_EnterWorld()\nend\n')
with self.assertRaises(ValueError):
ui.apply('function CharacterSelect_EnterWorld()\nend\n')
@unittest.skipUnless(LUA, 'no Lua interpreter')
def test_helper(self):
result = run_lua(HELPER_CASES, ui.HELPER)
self.assertEqual((result.returncode, result.stdout.strip()), (0, 'helper ok'), result.stderr)
@unittest.skipUnless(LUA and ORIGINAL.is_file(), 'pinned original character-select file not available')
def test_original_functions(self):
source = ORIGINAL.read_bytes()
result = ui.transform(source)
self.assertEqual(result.count(b'\r\n'), result.count(b'\n'))
self.assertEqual(result.count(b'CharacterListReady.Held(CharacterSelect'), 2)
self.assertEqual(result.count(b'CharacterListReady.Received(self, listSize)'), 1)
# Apart from the helper and the three hooks the file is the original.
body = result[result.index(b'CHARACTER_FACING_INCREMENT = 2;'):]
original_lines, changed = source.split(b'\r\n'), body.split(b'\r\n')
self.assertEqual(len(changed) - len(original_lines), 8)
self.assertEqual(len(set(changed) - set(original_lines)), 3)
with tempfile.TemporaryDirectory() as directory:
staged = Path(directory) / 'characterselect.lua'
staged.write_bytes(result)
run = run_lua(ORIGINAL_CASES, staged)
self.assertEqual((run.returncode, run.stdout.strip()), (0, 'original ok'), run.stderr)
if __name__ == '__main__':
unittest.main()