Files
WoW-Hybrid-gateway/tests/area_trigger_metadata_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

117 lines
5.8 KiB
Python

"""Original AreaTrigger rows through the existing encoder/composer/lookup.
This grants no Source trigger alias, ActionSet constructor or runtime activation.
"""
import argparse
import copy
import hashlib
import json
from pathlib import Path
import struct
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'ops'))
sys.path.insert(0, str(ROOT / 'tests'))
import spell_passive_compat as encoder
import item_sparse_compat as composer
from map_area_native_encoder_test import decode_original_body, compare_original_row
def run(inputs, work, contract_path):
work.mkdir(parents=True, exist_ok=False)
contract = json.loads(contract_path.read_text())
expected = [tuple(field) for field in contract['fields']]
schema = encoder.fields(ROOT.parent / 'tools/WoWDBDefs/definitions/AreaTrigger.dbd', 'AreaTrigger')
assert schema == expected
table, layout, pin = encoder.SUPPLEMENTAL_TABLES['AreaTrigger']
assert (f'{table:08X}', f'{layout:08X}', pin) == (contract['table_hash'], contract['layout_hash'], contract['field_sequence_sha256'])
encoder.db2_header(ROOT.parent / 'servers/trinity-forever/install/bin/dbc/enUS/AreaTrigger.db2', table, layout)
originals = json.loads(inputs.read_text())
assert len(originals) == 343 and len({r['ID'] for r in originals}) == 343
bodies, rows = {}, []
for original in originals:
body = encoder.encode(original, schema, table='AreaTrigger')
compare_original_row(original, decode_original_body(body, expected, original['ID']), expected)
assert len(body) == 57
# Pos is followed by the inline ID; it must survive the package too.
assert struct.unpack_from('<i', body, 12)[0] == original['ID']
bodies[original['ID']] = body
rows.append(dict(table_hash=f'0x{table:08x}', table_name='AreaTrigger', id=original['ID'],
record=original, record_status='valid', wire_hex=body.hex(),
sha256=hashlib.sha256(body).hexdigest()))
first = originals[0]
invalid = []
row = copy.deepcopy(first); del row['AreaTriggerActionSetID']; invalid.append((row, schema, 'AreaTrigger'))
row = copy.deepcopy(first); row['ContinentID'] = 32768; invalid.append((row, schema, 'AreaTrigger'))
row = copy.deepcopy(first); row['Radius'] = float('nan'); invalid.append((row, schema, 'AreaTrigger'))
row = copy.deepcopy(first); row['ID'] = 0; invalid.append((row, schema, 'AreaTrigger'))
invalid.extend([(first, schema[:-1], 'AreaTrigger'), (first, schema, 'Map')])
for row, fields, table_name in invalid:
try:
encoder.encode(row, fields, table=table_name)
except ValueError:
pass
else:
raise AssertionError('Unsafe original trigger encoding admitted')
push, header, pages = composer._package_data(rows)
assert sum(p['count'] for p in pages) == 343
assert sum(p['wire_bytes'] - 8 for p in pages) == 343 * (21 + 57)
(work / 'original-triggers.hpp').write_text(header)
source = work / 'original-trigger-lookup.cpp'
source.write_text('''#include <cstdio>
#include "original-triggers.hpp"
int main() {
using namespace gateway::item_sparse_compat;
for (const auto& row : Records) {
const auto actual = find(row.table, row.id);
const uint32_t prefix[] = {row.table, row.id, static_cast<uint32_t>(actual.size())};
if (std::fwrite(prefix, sizeof(prefix), 1, stdout) != 1 ||
std::fwrite(actual.data(), 1, actual.size(), stdout) != actual.size()) return 2;
}
if (!find(0x1A5081E1, 0).empty() || !find(0xBD84CD62, 60).empty()) return 3;
return 0;
}
''')
executable = work / 'original-trigger-lookup'
command = ['clang++', '-std=c++20', '-O0', '-Wall', '-Wextra', '-Werror', str(source), '-o', str(executable)]
(work / 'compile-command.json').write_text(json.dumps(command, indent=2) + '\n')
with (work / 'compile-private.log').open('w') as log:
subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, check=True)
binary = subprocess.run([str(executable)], capture_output=True, check=True).stdout
actual, offset = {}, 0
while offset < len(binary):
native_table, ident, size = struct.unpack_from('<III', binary, offset)
offset += 12
assert native_table == table and size == 57 and ident not in actual
actual[ident] = binary[offset:offset + size]
offset += size
assert offset == len(binary) and actual == bodies
(work / 'original343-records.json').write_text(json.dumps(rows, indent=2) + '\n')
(work / 'original343-pages.json').write_text(json.dumps(dict(push_id=push, pages=pages), indent=2) + '\n')
return dict(status='PASS_ORIGINAL_AREATRIGGER_ENCODER_COMPOSER_CPP_LOOKUP', rows=343,
fields_per_row=15, body_bytes=57, original_inline_IDs_preserved=343,
sphere_rows=sum(r['ShapeType'] == 0 for r in originals),
box_rows=sum(r['ShapeType'] == 1 for r in originals), refusals=len(invalid),
source_constructed_rows=0, actionset_roles_granted=False,
native_client_or_runtime_application=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--inputs', type=Path, required=True)
parser.add_argument('--work', type=Path, required=True)
parser.add_argument('--contract', type=Path, required=True)
parser.add_argument('--result', type=Path, required=True)
args = parser.parse_args()
try:
result = run(args.inputs, args.work, args.contract)
except Exception as error:
args.result.write_text(json.dumps(dict(status='FAIL_NEW_AREATRIGGER_BOUNDARY',
error_type=type(error).__name__, error=str(error)), indent=2) + '\n')
raise
args.result.write_text(json.dumps(result, indent=2) + '\n')
args.result.chmod(0o600)
print(result['status'])