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

97 lines
4.4 KiB
Python

import ast
import hashlib
import os
from pathlib import Path
import re
import sys
import tempfile
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parent))
import native_codec_driver as driver
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / 'tests/native_serializer_test.py'
TREE = ast.parse(SOURCE.read_text())
LOAD = next(n for n in TREE.body if isinstance(n, ast.Assign) and
any(isinstance(t, ast.Name) and t.id == 'library' for t in n.targets))
PREFIX = '\n'.join(SOURCE.read_text().splitlines()[:LOAD.lineno - 1])
COMMAND = next(n.value for n in ast.walk(TREE) if isinstance(n, ast.Assign) and
any(isinstance(t, ast.Name) and t.id == 'command' for t in n.targets))
class IsolatedCodecPaths(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.addCleanup(self.directory.cleanup)
self.root = Path(self.directory.name)
self.cli = self.root / 'fresh-cli'
self.library = self.root / 'fresh-codec.so'
self.cli.write_bytes(b'offline-path-fixture')
self.library.write_bytes(b'offline-path-fixture')
old = self.root / 'runtime/codec-before-native-library'
old.parent.mkdir()
old.write_bytes(b'old-reference-must-not-be-selected')
self.old = old
def setup_only(self, cli=None, library=None):
environment = {'GW_TEST_CODEC_CLI': str(cli or self.cli),
'GW_TEST_CODEC_PATH': str(library or self.library)}
scope = {'__name__': 'offline_codec_path_fixture'}
with mock.patch.dict(os.environ, environment), mock.patch.object(driver, 'ROOT', self.root):
# Deliberately stop before load(): no ELF/CLI or source transport runs.
exec(compile(PREFIX, str(SOURCE), 'exec'), scope)
return scope
def test_explicit_library_cli_and_benchmark_reference_are_the_same_pair(self):
scope = self.setup_only()
self.assertEqual(scope['CODEC_CLI'], self.cli.resolve())
self.assertEqual(scope['CODEC_LIBRARY'], self.library.resolve())
scope['saved'] = self.old
command = eval(compile(ast.Expression(COMMAND), str(SOURCE), 'eval'), scope)
self.assertEqual(command, [str(self.cli.resolve())])
def test_explicit_missing_artifact_is_failure_not_optional_skip(self):
with self.assertRaisesRegex(RuntimeError, 'GW_TEST_CODEC_CLI'):
self.setup_only(cli=self.root / 'missing-cli')
with self.assertRaisesRegex(RuntimeError, 'GW_TEST_CODEC_PATH'):
self.setup_only(library=self.root / 'missing-codec')
def test_python_mode_binding_matches_append_only_native_dispatch_array(self):
text = (ROOT / 'native/serializer_modes.hpp').read_text()
start = text.index('{', text.index('gw_codec_modes'))
modes = re.findall(r'"([^"]*)"', text[start:text.rindex('}')])
self.assertEqual(driver.MODES, modes)
header = (ROOT / 'native/serializer.h').read_text()
body = header.split('enum gw_codec_mode {', 1)[1].split('};', 1)[0]
entries = re.sub(r'//[^\n]*', '', body).split(',')
symbols = []
for index, entry in enumerate(entries):
name, equal, value = entry.strip().partition('=')
if equal:
self.assertEqual(int(value, 0), index, name)
symbols.append(name.strip())
self.assertEqual(symbols[-1], 'GW_CODEC_MODE_COUNT')
self.assertEqual(len(symbols) - 1, len(modes))
self.assertEqual(len(set(modes)), len(modes))
# Preserve every public (index, enum, CLI spelling) from 0 through 68
# in the parent ABI 5f8c6208, even if both name lists are reordered together.
legacy = ''.join(f'{index} {symbols[index]} {modes[index]}\n' for index in range(69))
self.assertEqual(hashlib.sha256(legacy.encode()).hexdigest(),
'4091e8b1e401f7c4be517f2df3384fd5b0b3b1f96288680e5419916da598d0d2')
self.assertEqual(list(zip(symbols, modes))[68:71], [
('GW_CODEC_OWNED_STAT_PET_VALUES', '--owned-stat-pet-values'),
('GW_CODEC_GROUP_EVENT', '--group-event'),
('GW_CODEC_TRADE_EVENT', '--trade-event'),
])
self.assertEqual(list(zip(symbols, modes))[77:79], [
('GW_CODEC_AUCTION_PERSONAL', '--auction-personal'),
('GW_CODEC_COMMODITY_QUOTE', '--commodity-quote'),
])
if __name__ == '__main__':
unittest.main()