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

130 lines
5.2 KiB
Python

"""Offline selected-Glue browser routes; no game process or network requests."""
import os
from pathlib import Path
import re
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "ops"))
import octo_browser_routes as routes
SELECTED = Path(os.environ.get(
"GW_OCTO_BROWSER_ORIGINALS",
ROOT / "runtime/continuation-20261113/codex/"
"account-back-explicit-and-guild-20261119/selected",
))
LUA = shutil.which("lua5.1") or shutil.which("luajit") or shutil.which("lua")
def run_lua(script: str):
with tempfile.TemporaryDirectory() as directory:
fixture = Path(directory) / "fixture.lua"
fixture.write_text(script, encoding="utf-8")
return subprocess.run([LUA, str(fixture)], capture_output=True, text=True)
def lua_function(source: bytes, name: str) -> str:
text = source.decode("utf-8")
if name == "UpgradeAccount":
start = text.index("function UpgradeAccount()")
end = text.index("function MinutesToTime", start)
return text[start:end]
match = re.search(r"function " + re.escape(name) + r"\(\)\r?\n.*?\r?\nend", text, re.DOTALL)
assert match, name
return match.group(0)
class OctoBrowserRoutes(unittest.TestCase):
def test_wrong_selected_content_is_refused(self):
with self.assertRaises(ValueError):
routes.transform_character_select(b"function CharacterSelect_TechSupport() end")
with self.assertRaises(ValueError):
routes.transform_glue_parent(b"function UpgradeAccount() end")
@unittest.skipUnless((SELECTED / "characterselect.lua").is_file(), "selected source unavailable")
def test_character_select_exact_delta(self):
original = (SELECTED / "characterselect.lua").read_bytes()
changed = routes.transform_character_select(original)
restored = changed.replace(b'LaunchURL("https://octowow.st/bug-tracker");',
b"LaunchURL(TECH_SUPPORT_URL);")
restored = restored.replace(b'LaunchURL("https://octowow.st/");',
b"LaunchURL(AUTH_NO_TIME_URL);")
self.assertEqual(restored, original)
self.assertNotIn(b"LaunchURL(TECH_SUPPORT_URL);", changed)
self.assertNotIn(b"LaunchURL(AUTH_NO_TIME_URL);", changed)
@unittest.skipUnless(LUA and (SELECTED / "characterselect.lua").is_file(),
"Lua or selected source unavailable")
def test_character_select_buttons_open_only_octo(self):
changed = routes.transform_character_select((SELECTED / "characterselect.lua").read_bytes())
callbacks = "\n".join(lua_function(changed, name) for name in (
"CharacterSelect_TechSupport", "CharacterSelect_ManageAccount"))
script = r'''
local opened = {}
function LaunchURL(url) opened[#opened + 1] = url end
function PlaySound() end
SOUNDKIT = {GS_CHARACTER_SELECTION_ACCT_OPTIONS = 1}
TECH_SUPPORT_URL = "https://support.blizzard.com/"
AUTH_NO_TIME_URL = "https://account.battle.net/"
''' + callbacks + r'''
assert(#opened == 0)
CharacterSelect_TechSupport()
CharacterSelect_ManageAccount()
assert(#opened == 2)
assert(opened[1] == "https://octowow.st/bug-tracker")
assert(opened[2] == "https://octowow.st/")
'''
result = run_lua(script)
self.assertEqual(result.returncode, 0, result.stderr)
@unittest.skipUnless((SELECTED / "glueparent.lua").is_file(), "selected source unavailable")
def test_glue_parent_exact_delta(self):
original = (SELECTED / "glueparent.lua").read_bytes()
changed = routes.transform_glue_parent(original)
restored = changed.replace(b'LaunchURL("https://octowow.st/");',
b"LoadURLIndex(2);")
self.assertEqual(restored, original)
self.assertNotIn(b"LoadURLIndex(2);", changed)
@unittest.skipUnless(LUA and (SELECTED / "glueparent.lua").is_file(),
"Lua or selected source unavailable")
def test_upgrade_fallback_only_opens_octo(self):
changed = routes.transform_glue_parent((SELECTED / "glueparent.lua").read_bytes())
callback = lua_function(changed, "UpgradeAccount")
script = r'''
local opened, trial, purchasable, used_store = {}, false, false, 0
function LaunchURL(url) opened[#opened + 1] = url end
function LoadURLIndex() error("Unexpected vendor URL lookup") end
function PlaySound() end
function IsTrialAccount() return trial end
function ToggleStoreUI() used_store = used_store + 1 end
function StoreFrame_SetGamesCategory() end
WOW_GAMES_CATEGORY_ID = 1
SOUNDKIT = {GS_LOGIN_NEW_ACCOUNT = 1}
StoreInterfaceUtil = {OpenToSubscriptionProduct = function() used_store = used_store + 1 end}
C_StorePublic = {DoesGroupHavePurchaseableProducts = function() return purchasable end}
C_CatalogShop = {IsShop2Enabled = function() return false end}
''' + callback + r'''
assert(#opened == 0)
UpgradeAccount()
assert(#opened == 1 and opened[1] == "https://octowow.st/")
trial = true
UpgradeAccount()
assert(#opened == 1 and used_store == 1)
trial, purchasable = false, true
UpgradeAccount()
assert(#opened == 1 and used_store == 2)
'''
result = run_lua(script)
self.assertEqual(result.returncode, 0, result.stderr)
if __name__ == "__main__":
unittest.main()