Files
WoW-Hybrid-gateway/tests/area_parent_none_profile_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

157 lines
7.8 KiB
Python

"""Source404 parent predicates through encoded Area fields and a native rule.
Carrier rows are two unchanged original native examples, not invented new Area
rows. Parent bindings are intentionally translated synthetic inputs. The test
claims the producer/reference-rule boundary, never original game acceptance.
"""
import argparse
import copy
import json
import os
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 producer
from map_area_native_encoder_test import decode_original_body
def run(inputs, work):
work.mkdir()
cases = json.loads((inputs / 'new404-complete-source-and-parent-WMA-relations.json').read_text())
assert len(cases) == 404
originals = json.loads((inputs.parent / 'original-control-composition/original-five-records.json').read_text())
carriers = {row['id']: row['record'] for row in originals if row['table_name'] == 'AreaTable'}
assert set(carriers) == {1, 77}
explicit = json.loads((ROOT / 'tests/fixtures/map-area-native-encoder-schema.json').read_text())['AreaTable']['fields']
schema = producer.fields(ROOT.parent / 'tools/WoWDBDefs/definitions/AreaTable.dbd', 'AreaTable')
parents = sorted({case['source']['ParentAreaID'] for case in cases if case['source']['ParentAreaID']})
translated = {parent: 60000 + index for index, parent in enumerate(parents)}
assert max(translated.values()) <= 65535
assert all(a != b for a, b in translated.items())
expected, wire, roots, children = [], bytearray(), 0, 0
for case in cases:
source = case['source']
parent = source['ParentAreaID']
native_parent = translated[parent] if parent else 0
# Force both directions: root carriers initially have the child tag;
# child carriers initially lack it. Other flags and all fields survive.
baseline = carriers[1 if parent else 77]
original = copy.deepcopy(baseline)
projected = producer.area_parent_projection(parent, native_parent, baseline['Flags'],
profile=producer.AREA_CLASSIC_PROFILE)
none = producer.area_classic_optional_none_fields(profile=producer.AREA_CLASSIC_PROFILE)
row = dict(baseline, **projected, **none)
assert baseline == original
decoded = decode_original_body(producer.encode(row, schema, table='AreaTable'), explicit, row['ID'])
changed = {'ParentAreaID', 'Flags', *none}
for field in original:
if field not in changed:
assert decoded[field] == original[field]
assert (decoded['Flags'][0] & ~0x40000000) == (original['Flags'][0] & ~0x40000000)
assert decoded['Flags'][1] == original['Flags'][1]
# Source resolver: IsZone()==(ZoneId==0), direct parent otherwise.
# Translate that conceptual result with the explicit binding supplied.
expected.append(native_parent if parent else row['ID'])
roots += not bool(parent)
children += bool(parent)
wire.extend(struct.pack('<4Ii2I', row['ID'], decoded['ParentAreaID'],
decoded['Flags'][0], decoded['Flags'][1],
decoded['PvpCombatWorldStateID'],
decoded['WildBattlePetLevelMin'], decoded['WildBattlePetLevelMax']))
assert (roots, children) == (79, 325)
source = work / 'primary-native-area-rule.cpp'
source.write_text('''#include <cstdint>
#include <cstdio>
int main() {
uint32_t row[7];
while (std::fread(row, sizeof(row), 1, stdin) == 1) {
// Native TerrainInfo::GetZoneId: parent is effective only for IsSubzone.
const uint32_t zone = row[1] && (row[2] & 0x40000000u) ? row[1] : row[0];
const int32_t pvpWorldState = static_cast<int32_t>(row[4]);
uint32_t worldStateCalls = 0;
auto conditionalPvP = [&]() {
if (pvpWorldState == -1) return false;
++worldStateCalls;
return true;
};
const bool ordinaryPvP = false || false || conditionalPvP();
const bool battlefieldPvP = true || false || conditionalPvP();
const bool combatZonePvP = false || true || conditionalPvP();
// SelectWildBattlePetLevel keeps the default when the area minimum is0.
uint32_t level = 5, randomCalls = 0;
if (row[5] > 0) { ++randomCalls; level = row[5]; }
const uint32_t result[] = {zone, worldStateCalls, ordinaryPvP,
battlefieldPvP, combatZonePvP, level, randomCalls};
if (std::fwrite(result, sizeof(result), 1, stdout) != 1) return 2;
}
return std::ferror(stdin) ? 3 : 0;
}
''')
executable = work / 'primary-native-area-rule'
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.log').open('w') as log:
subprocess.run(command, check=True, stdout=log, stderr=subprocess.STDOUT)
raw = subprocess.run([str(executable)], input=bytes(wire), capture_output=True, check=True).stdout
assert len(raw) == len(cases) * 28
for index, expected_zone in enumerate(expected):
observed = struct.unpack_from('<7I', raw, index * 28)
assert observed == (expected_zone, 0, 0, 1, 1, 5, 0)
# Fail before projection when an explicit binding/profile/flag shape is
# missing. No fallback native parent or unreviewed profile is synthesized.
profile = producer.AREA_CLASSIC_PROFILE
bad = [(1, 0, [0, 0], profile), (0, 1, [0, 0], profile),
(1, None, [0, 0], profile), (-1, 0, [0, 0], profile),
(1, 65536, [0, 0], profile), (1, 10, [0], profile),
(1, 10, [0, 0x100000000], profile),
(1, 10, [0.0, 0], profile), (True, 10, [0, 0], profile),
(1, 10, [0, 0], 'unreviewed-mainline-area')]
refusals = 0
for parent, native_parent, flags, context in bad:
try:
producer.area_parent_projection(parent, native_parent, flags, profile=context)
except ValueError:
refusals += 1
else:
raise AssertionError('Unbound/unsafe Area relation admitted')
try:
producer.area_classic_optional_none_fields(profile='unreviewed-mainline-area')
except ValueError:
refusals += 1
else:
raise AssertionError('Unreviewed optional NONE profile admitted')
assert refusals == 11
return dict(status='PASS_SOURCE404_ENCODED_AREA_PARENT_NATIVE_RULE_NONE_PROFILE',
Source_predicates=404, roots=roots, children=children,
explicit_translated_parent_bindings=len(translated),
real_parent_bindings_admitted_by_this_fixture=0,
unreviewed_or_missing_binding_refusals=refusals,
preserved_unrelated_native_flags_and_fields=True,
conditional_PvP_lookup_calls=0, area_wild_level_random_calls=0,
wild_pet_default_level_preserved=True,
battlefield_and_combat_zone_PvP_preserved=True,
original_native_runtime_or_new_Area_rows_claimed=False,
old_Map55_row_or_SQL_checks_repeated=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('--result', type=Path, required=True)
args = parser.parse_args()
try:
result = run(args.inputs, args.work)
except Exception as error:
args.result.write_text(json.dumps(dict(status='FAIL_NEW_AREA_PARENT_PROFILE_BOUNDARY',
error_type=type(error).__name__, error=str(error)), indent=2) + '\n')
raise
args.result.write_text(json.dumps(result, indent=2) + '\n')
os.chmod(args.result, 0o600)
print(result['status'])