from pathlib import Path import hashlib import json import sys import tempfile import unittest from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] ORIGINAL = ROOT.parent/'Original' sys.path.insert(0, str(ROOT/'tools/extractors')) from baseball_resources import ARCHIVE_SHA256, VR07_SHA256, CANVASES, AIM_CANVASES, AIM_ARCHIVES, export_baseball from omt import FormatError from png import read_png @unittest.skipUnless(ORIGINAL.exists(), 'Original assets are local integration fixtures') class BaseballResourceTests(unittest.TestCase): def test_exact_original_pixels_and_provenance(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'baseball' manifest = export_baseball(ORIGINAL, output) self.assertEqual(manifest, json.loads((output/'baseball.json').read_text())) self.assertEqual((manifest['schema'], manifest['sourceSha256']), (1, ARCHIVE_SHA256)) self.assertEqual([e['id'] for e in manifest['textures']], [2, 3, 4, 5, 8]) for entry, expected in zip(manifest['textures'], CANVASES): self.assertEqual((entry['id'], entry['name'], entry['sourceOffset'], entry['sourceBytes'], entry['width'], entry['height'], entry['rgbaSha256']), expected) rgba = (output/entry['path']).read_bytes() self.assertEqual(hashlib.sha256(rgba).hexdigest(), entry['rgbaSha256']) self.assertEqual(read_png(output/entry['png'])['rgba'], rgba) def test_aim_strip_and_original_gameplay_glyphs_are_not_substituted(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'baseball' manifest = export_baseball(ORIGINAL, output) self.assertEqual(manifest['aimArchiveSha256'], AIM_ARCHIVES) for entry, expected in zip(manifest['aimTextures'], AIM_CANVASES): self.assertEqual((entry['database'], entry['id'], entry['width'], entry['height'], entry['rgbaSha256']), expected) rgba = (output/entry['path']).read_bytes() self.assertEqual(hashlib.sha256(rgba).hexdigest(), entry['rgbaSha256']) self.assertEqual(read_png(output/entry['png'])['rgba'], rgba) def test_tutorial_retains_six_duplicate_tags_and_exact_properties(self): with tempfile.TemporaryDirectory() as directory: manifest = export_baseball(ORIGINAL, Path(directory)/'baseball') tutorial = manifest['tutorial'] self.assertEqual(tutorial['sha256'], VR07_SHA256) targets = [e for e in tutorial['entities'] if e['kind'] == '3TAR'] self.assertEqual([e['offset'] for e in targets], [0x885, 0xAD6, 0xD27, 0xF78, 0x11C9, 0x141C]) for target in targets: values = {p['name']: p['value'] for p in target['properties']} self.assertEqual((values['ObjectTag'], values['SpriteDatabase'], values['SpriteIndex']), ('C3DMOVINGTARGET', 'sprites.omt', 135)) self.assertEqual((values['HitsRequired'], values['Speed'], values['NumPoints'], values['RespawnTime']), (3, 100.0, 10, 15.0)) source = next(p for p in target['properties'] if p['name'] == 'SpriteIndex') self.assertEqual(source['raw'], '00000087') self.assertGreater(source['offset'], target['offset']) def test_source_outputs_and_descendant_symlinks_rejected(self): for output in (ORIGINAL, ORIGINAL/'omt/baseball-generated', ORIGINAL.parent): with self.subTest(output=output), self.assertRaises(ValueError): export_baseball(ORIGINAL, output) with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'baseball' output.mkdir() target = Path(directory)/'unchanged' target.write_bytes(b'unchanged') (output/'canvas_2.rgba').symlink_to(target) with self.assertRaises(ValueError): export_baseball(ORIGINAL, output) self.assertEqual(target.read_bytes(), b'unchanged') def test_bad_pixels_rejected_before_output(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'baseball' with patch('baseball_resources.read_canvas', return_value=dict(width=32, height=32, rgba=b'\0'*4096)), \ self.assertRaises(FormatError): export_baseball(ORIGINAL, output) self.assertFalse(output.exists()) def test_bad_source_identity_rejected_before_decode(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'baseball' with patch('baseball_resources.digest', return_value='0'*64), \ patch('baseball_resources.read_canvas') as decoder, self.assertRaises(FormatError): export_baseball(ORIGINAL, output) decoder.assert_not_called() self.assertFalse(output.exists()) if __name__ == '__main__': unittest.main()