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

124 lines
6.7 KiB
Python

import struct
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
import m2_sequence_contract as msc # noqa: E402
def _model(rows, extra=b'', durations=None):
seq = b''
for n, (animation, variation, flags, vnext, anext) in enumerate(rows):
rec = bytearray(64)
struct.pack_into('<HH', rec, 0, animation, variation)
struct.pack_into('<I', rec, 4, durations[n] if durations else 1000)
struct.pack_into('<I', rec, 12, flags)
struct.pack_into('<hH', rec, 60, vnext, anext)
seq += rec
body = bytearray(b'MD20' + bytes(304 - 4))
struct.pack_into('<II', body, 28, len(rows), len(body))
body += seq
return b'MD21' + struct.pack('<I', len(body)) + bytes(body) + extra
class SequenceContractTest(unittest.TestCase):
def test_converted_table_normalized_to_native_contract(self):
# Converted shape: Stand x2 with variation 0 twice, aliasNext 0 everywhere.
raw = _model([(0, 0, 0x20, 2, 0), (26, 0, 0x20, -1, 0), (0, 0, 0x20, -1, 0)])
out, audit = msc.normalize_m2_sequences(raw)
got = [(s['animation'], s['variation'], s['variation_next'], s['alias_next']) for s in msc.sequence_table(out)]
self.assertEqual(got, [(0, 0, 2, 0), (26, 0, -1, 1), (0, 1, -1, 2)])
self.assertEqual(audit['changed_sequences'], [1, 2])
self.assertEqual(len(out), len(raw))
self.assertEqual(msc.normalize_m2_sequences(out)[1]['changed_sequences'], [])
def test_native_alias_pair_preserved(self):
# Native 197004 shape: Stand(0) <-> alias(1, flag 0x40).
raw = _model([(0, 0, 0x20, -1, 1), (147, 0, 0x60, -1, 0), (148, 0, 0xa1, -1, 2)])
self.assertEqual(msc.normalize_m2_sequences(raw)[0], raw)
def test_zero_duration_repaired(self):
# Converted kodorider shape: Source single-pose records have duration 0;
# an alias takes its target's duration (native 40147/40147).
raw = _model([(0, 0, 0x20, -1, 1), (52, 0, 0x60, -1, 0), (11, 0, 0x20, -1, 2)], durations=[2667, 0, 0])
out, audit = msc.normalize_m2_sequences(raw)
self.assertEqual([s['duration'] for s in msc.sequence_table(out)], [2667, 2667, msc.MIN_DURATION])
self.assertEqual(audit['zero_durations'], [1, 2])
self.assertEqual(msc.normalize_m2_sequences(out)[0], out)
def test_coincident_key_times_made_distinct(self):
# kodorider Stand: two Source keys past the window, both clamped to its end.
self.assertEqual(msc.distinct_key_times((2667, 2667), 2667), ([0], [2667]))
self.assertEqual(msc.distinct_key_times((0, 0, 0, 500), 1000), ([2, 3], [0, 500]))
self.assertEqual(msc.distinct_key_times((0, 400, 400, 900), 1000), ([0, 1, 2, 3], [0, 400, 401, 900]))
self.assertEqual(msc.distinct_key_times((0, 400, 400, 401), 1000), ([0, 1, 3], [0, 400, 401]))
self.assertEqual(msc.distinct_key_times((0, 300, 1000), 1000), ([0, 1, 2], [0, 300, 1000]))
def test_track_keys_deduped_in_place(self):
raw = bytearray(_model([(0, 0, 0x20, -1, 0)], durations=[2667]))
base = 8
def put(data):
at = len(raw) - base; raw.extend(data); return at
times = put(struct.pack('<2I', 2667, 2667)); values = put(struct.pack('<6f', 1, 2, 3, 4, 5, 6))
slots_t = put(struct.pack('<II', 2, times)); slots_v = put(struct.pack('<II', 2, values))
bone = bytearray(88); struct.pack_into('<Hh4I', bone, 16, 1, -1, 1, slots_t, 1, slots_v)
struct.pack_into('<II', raw, base + 44, 1, put(bone))
struct.pack_into('<I', raw, 4, len(raw) - base)
out, audit = msc.dedupe_track_keys(bytes(raw))
self.assertEqual(audit, dict(key_arrays=1, keys_removed=1))
self.assertEqual(struct.unpack_from('<I', out, base + slots_t)[0], 1)
self.assertEqual(struct.unpack_from('<I', out, base + slots_v)[0], 1)
self.assertEqual(struct.unpack_from('<I', out, base + times)[0], 2667)
self.assertEqual(struct.unpack_from('<3f', out, base + values), (1, 2, 3))
self.assertEqual(msc.dedupe_track_keys(out)[0], out)
def test_external_anim_renumbering_held(self):
raw = _model([(0, 0, 0x00, -1, 0), (0, 0, 0x00, -1, 1)], b'AFID' + struct.pack('<I', 8) + bytes(8))
with self.assertRaises(ValueError):
msc.normalize_m2_sequences(raw)
if __name__ == '__main__':
unittest.main()
class StandInsertionTest(unittest.TestCase):
def _crate(self):
# Converted Crate02 shape: 148,146,147,149 inline, one bone with a per-sequence translation track.
rows = [(148, 0, 0x21, -1, 0), (146, 0, 0x21, -1, 1), (147, 0, 0x21, -1, 2), (149, 0, 0x21, -1, 3)]
body = bytearray(_model(rows)[8:])
struct.pack_into('<I', body, 4, 272)
lookup = len(body); body += struct.pack('<150h', *([-1] * 146 + [1, 2, 0, 3]))
struct.pack_into('<II', body, 36, 150, lookup)
keys = len(body); body += struct.pack('<I', 7) * 4
subs = len(body); body += b''.join(struct.pack('<II', 1, keys + 4 * i) for i in range(4))
bone = bytearray(88); struct.pack_into('<hHhH', bone, 0, -1, 0, -1, 0)
struct.pack_into('<HhIIII', bone, 16, 0, -1, 4, subs, 4, subs)
struct.pack_into('<Hh', bone, 36, 0, -1); struct.pack_into('<Hh', bone, 56, 0, -1)
bones = len(body); body += bone
struct.pack_into('<II', body, 44, 1, bones)
return b'MD21' + struct.pack('<I', len(body)) + bytes(body)
def test_crate_gets_native_aliased_stand(self):
out, audit = msc.insert_stand(self._crate())
self.assertEqual((audit['aliased_animation'], audit['stand_index'], audit['extended_tracks']), (147, 4, 1))
table = msc.sequence_table(out)
self.assertEqual([s['animation'] for s in table], [148, 146, 147, 149, 0])
self.assertEqual((table[2]['flags'], table[2]['alias_next']), (0x61, 4))
self.assertEqual((table[4]['flags'], table[4]['alias_next'], table[4]['variation_next']), (0x20, 2, -1))
body = out[8:]
lc, lo = struct.unpack_from('<II', body, 36)
self.assertEqual(struct.unpack_from('<h', body, lo)[0], 4)
bones = struct.unpack_from('<I', body, 48)[0]
n, subs = struct.unpack_from('<II', body, bones + 20)
self.assertEqual((n, struct.unpack_from('<II', body, subs + 32)), (5, struct.unpack_from('<II', body, subs + 16)))
self.assertEqual(msc.normalize_m2_sequences(out)[1]['changed_sequences'], [])
self.assertEqual(msc.insert_stand(out)[1], dict(changed=False))
def test_priority_and_single_sequence(self):
table = [dict(animation=a) for a in (4, 26, 5)]
self.assertEqual(msc.stand_source_index(table), 1)
self.assertEqual(msc.stand_source_index([dict(animation=169)]), 0)