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 ase import expand_mesh, read_ase from entities import resolve_asset from omt import FormatError from png import read_png from scooter_resources import SOURCES, export_scooter @unittest.skipUnless(ORIGINAL.exists(), 'Original assets are local integration fixtures') class ScooterResourceTests(unittest.TestCase): def test_original_shape_material_links_and_pixels(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'scooter' manifest = export_scooter(ORIGINAL, output) self.assertEqual(manifest, json.loads((output/'scooter.json').read_text())) self.assertEqual(manifest['sources'], SOURCES) self.assertEqual([s['triangles'] for s in manifest['shapes']], [176, 56]) for identity, expected_canvas in ((0, 1), (1, 0)): scene = json.loads((output/f'shape_{identity}/scene.json').read_text()) self.assertEqual(len(scene['materials']), 1) self.assertEqual(scene['materials'][0]['texture'], dict(type='Canv', id=expected_canvas)) texture = scene['textures'][0] self.assertEqual(texture['id'], expected_canvas) image = read_png(output/f'shape_{identity}'/texture['png']) self.assertEqual(image['rgba'], (output/f'shape_{identity}'/texture['path']).read_bytes()) wheel = json.loads((output/'shape_1/scene.json').read_text()) self.assertIn(-1, [m['material'] for m in wheel['meshes']]) self.assertEqual((output/'jimycarl.rgba').read_bytes(), read_png(resolve_asset(ORIGINAL, 'png/jimycarl.png'))['rgba']) def test_jimmy_exact_base_topology_and_morph_positions(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'scooter' export_scooter(ORIGINAL, output) base = read_ase(resolve_asset(ORIGINAL, 'ASE/jimstop.ASE'))['objects'][0]['samples'][0] for stem in ('jimscooter', 'jimscooterstop'): source = read_ase(resolve_asset(ORIGINAL, f'ASE/{stem}.ASE')) animation = json.loads((output/stem/'animation.json').read_text()) self.assertEqual((animation['framesPerSecond'], animation['ticksPerFrame']), (10, 480)) self.assertEqual([f['time'] for f in animation['frames']], [0, 480]) self.assertEqual(animation['sha256'], source['sha256']) for index, sample in enumerate(source['objects'][0]['samples']): expected = expand_mesh(dict(sample, faces=base['faces'], uvs=base['uvs'], textureFaces=base['textureFaces'])) actual = json.loads((output/stem/animation['frames'][index]['path']).read_text()) self.assertEqual(actual['vertices'], [list(v) for v in expected['vertices']]) self.assertEqual(actual['uvs'], [list(v) for v in expected['uvs']]) def test_unbound_morphs_retain_source_161_ticks_and_no_guessed_uvs(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'scooter' manifest = export_scooter(ORIGINAL, output) for entry in manifest['unboundMorphs']: morph = json.loads((output/entry['path']).read_text()) self.assertEqual((morph['framesPerSecond'], morph['ticksPerFrame']), (30, 160)) samples = morph['objects'][0]['samples'] self.assertEqual([s['time'] for s in samples], [0, 161]) self.assertEqual([len(s['vertices']) for s in samples], [195, 195]) self.assertTrue(all(not s['uvs'] and not s['textureFaces'] for s in samples)) self.assertEqual(morph['sha256'], hashlib.sha256( resolve_asset(ORIGINAL, 'ASE/'+entry['source']).read_bytes()).hexdigest()) def test_original_subtrees_ancestors_and_symlinks_rejected(self): for output in (ORIGINAL, ORIGINAL/'png/generated-scooter', ORIGINAL.parent): with self.subTest(output=output), self.assertRaises(ValueError): export_scooter(ORIGINAL, output) with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'scooter' output.mkdir() outside = Path(directory)/'unchanged' outside.write_text('unchanged') for relative in ('scooter.json', 'shape_0', 'jimscooter', 'jimycarl.rgba'): (output/relative).symlink_to(outside) with self.subTest(relative=relative), self.assertRaises(ValueError): export_scooter(ORIGINAL, output) self.assertEqual(outside.read_text(), 'unchanged') (output/relative).unlink() def test_changed_source_identity_rejected_before_writes(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'scooter' with patch.dict(SOURCES, {'omt/scooter.omt': '0'*64}), self.assertRaises(FormatError): export_scooter(ORIGINAL, output) self.assertFalse(output.exists()) if __name__ == '__main__': unittest.main()