"""Synthetic unit tests for tools/rsc8_audio.py (ASSET-56). python -I tests/test_rsc8_audio.py Every byte is synthesized by the test. No game file, game audio, or game asset name is used. """ import contextlib import csv import io import json import os import struct import subprocess import sys import tempfile import unittest from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "rsc8_audio.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import rsc8_audio # noqa: E402 def _align16(n): return (n + 15) & ~15 def build_synthetic_rsc8( *, version=8001, stream_hash=0x20112233, frames=142, block_frames=None, tracks_before_anim=False, fps=60, track_count=90, inline_dict_name=False, extra_bool_prop=False, anim_payload_len=64, ): """Build a synthetically valid RSC8 v8001 facial/lip-sync clip matching the measured layout.""" if block_frames is None: block_frames = [frames] blk_cnt = len(block_frames) block_payloads = [anim_payload_len + bi * 16 for bi in range(blk_cnt)] block_sizes = [32 + pl for pl in block_payloads] tracks_bytes = track_count * 12 # Place tracks before anim blocks if requested (matches the 7 reordered real-corpus clips), # otherwise place anim blocks starting at virt+0xB0 followed by the sorted track directory. block_voffs = [] cur = 0xB0 if tracks_before_anim: tracks_voff = cur cur = _align16(cur + tracks_bytes) for bsz in block_sizes: block_voffs.append(cur) cur = _align16(cur + bsz) else: for bsz in block_sizes: block_voffs.append(cur) cur = _align16(cur + bsz) tracks_voff = cur cur = _align16(cur + tracks_bytes) props_voff = cur prop_count = 5 if extra_bool_prop else 4 props_tail_off = 0x70 + prop_count * 0x48 # Typed attributes: string (0x40), guid (0x20), optional bool (0x0C), fps int (0x0C), lead_in float (0x0C) attrs_size = 0x40 + 0x20 + (0x0C if extra_bool_prop else 0) + 0x0C + 0x0C props_size = props_tail_off + attrs_size clip_voff = _align16(props_voff + props_size) anim_dict_voff = clip_voff + 0xA0 anim_desc_voff = anim_dict_voff + 0x70 hash_map_voff = anim_desc_voff + 0x70 clip_map_voff = hash_map_voff + 0x60 tail_voff = clip_map_voff + 0x60 # Tail sub-allocations dict_str_voff = tail_voff + 0x00 # 0x20 bytes (heap dict name) anim_dict_str_voff = tail_voff + 0x20 # 0x20 bytes clip_str_voff = tail_voff + 0x40 # 0x30 bytes anim_name_voff = tail_voff + 0x70 # 0x20 bytes block_map_voff = tail_voff + 0x90 # 0x20 bytes arr70_voff = tail_voff + 0xB0 # 0x10 bytes (anim_dict_ptr, hash, 1) blk_arr_voff = arr70_voff + 0x10 # blk_cnt * 8 bytes (atArray) vlen = _align16(blk_arr_voff + blk_cnt * 8) virt = bytearray(vlen) def put_vptr(off, target): struct.pack_into(" 0 else 0.0 struct.pack_into(" 0: put_vptr(anim_desc_voff + 0x60, tracks_voff) struct.pack_into(" unsorted h0 = struct.unpack_from(" 99/60 = 1.65s) multi = build_synthetic_rsc8(block_frames=[64, 37], anim_payload_len=64) res_m = rsc8_audio.decode_rsc8(multi, stream_id=0x00112233) self.assertTrue(res_m["valid"], res_m["checks"]) self.assertEqual(res_m["anim"]["block_count"], 2) self.assertEqual(res_m["anim"]["frame_count"], 100) self.assertEqual(res_m["anim"]["max_block_size"], res_m["anim_desc"]["max_block_size"]) self.assertAlmostEqual(res_m["clip"]["duration"], 99 / 60.0, places=5) # Reordered sub-allocation: track directory placed at virt+0xB0, anim block after it reord = build_synthetic_rsc8(frames=120, tracks_before_anim=True) res_r = rsc8_audio.decode_rsc8(reord, stream_id=0x00112233) self.assertTrue(res_r["valid"], res_r["checks"]) self.assertEqual(res_r["anim_desc"]["track_ptr"], 0xB0) self.assertGreater(res_r["anim"]["blocks"][0]["voff"], 0xB0) # Flagged version 0x01001F41 (bit 24 set, base version 8001) flagged = build_synthetic_rsc8(version=0x01001F41, frames=79) res_f = rsc8_audio.decode_rsc8(flagged, stream_id=0x00112233) self.assertTrue(res_f["valid"], res_f["checks"]) self.assertEqual(res_f["version"], 0x01001F41) self.assertEqual(res_f["version_base"], 8001) self.assertEqual(res_f["version_flags"], 0x0100) # Non-deep mode omits skipped track and property-dependent duration checks rather than reporting False/True res_nd = rsc8_audio.decode_rsc8(multi, deep=False) self.assertTrue(res_nd["valid"], res_nd["checks"]) self.assertNotIn("clip_duration_matches_frames", res_nd["checks"]) self.assertNotIn("tracks_sorted", res_nd["checks"]) self.assertNotIn("tracks_schema_ok", res_nd["checks"]) # Non-60 fps clip (fps=30, 61 frames -> 2.0s) validates in both deep and shallow (--no-deep) modes clip_30fps = build_synthetic_rsc8(frames=61, fps=30) res_30_deep = rsc8_audio.decode_rsc8(clip_30fps, deep=True) res_30_shallow = rsc8_audio.decode_rsc8(clip_30fps, deep=False) self.assertTrue(res_30_deep["valid"], res_30_deep["checks"]) self.assertAlmostEqual(res_30_deep["clip"]["duration"], 2.0, places=5) self.assertTrue(res_30_shallow["valid"], res_30_shallow["checks"]) self.assertNotIn("clip_duration_matches_frames", res_30_shallow["checks"]) def test_reject_truncated_props_and_zero_fps(self): blob = bytearray(build_synthetic_rsc8(frames=60)) dec = rsc8_audio.decode_rsc8(bytes(blob)) # Shrink props_size so descriptors cannot fit struct.pack_into("> 6 h = (h + (h << 3)) & 0xFFFFFFFF h ^= h >> 11 return (h + (h << 15)) & 0xFFFFFFFF def build_synthetic_markers(records): """16-byte cue records: u32 name hash, f32 value, u32 sample offset, u32 extra. No header.""" return b"".join(struct.pack("