"""Trusted selection uses actual frozen assets; no generation, device or gateway.""" import importlib.util import json from pathlib import Path import tempfile import unittest from unittest.mock import patch import zipfile ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location('character_assets', ROOT/'scripts/select-character-assets.py') select = importlib.util.module_from_spec(spec) spec.loader.exec_module(select) ASSETS = ROOT/'app/src/main/assets' class CharacterProfilesTests(unittest.TestCase): def full(self, profile='main', **changes): flags = dict(emotion_sha=select.catalog()['mediaBundles']['black-correct03']['indexSha256'], user_assets=True, mouth=True, startup=True) flags.update(changes) return select.selection(ASSETS, profile, **flags) def test_identities_are_distinct_media_is_identical(self): main, lvbu = self.full(), self.full('lvbu-test') self.assertEqual(main['files'], lvbu['files']) self.assertEqual(lvbu['file_count'], 4051) self.assertEqual((main['persona'], lvbu['persona']), ('default', 'lvbu')) self.assertNotEqual(main['application_id'], lvbu['application_id']) self.assertEqual((main['media_identity'], lvbu['media_identity']), ('a01', 'a01')) self.assertNotIn('voice_id', json.dumps(select.catalog())) def test_complete_inventory_is_exact_code33(self): selected = self.full()['files'] with zipfile.ZipFile(ROOT/'artifacts/lan-voice-v0714-black-tulle-correct03-code33-arm64-debug.apk') as z: actual = {n[7:]: {'sha256': select.digest(z.read(n)), 'bytes': z.getinfo(n).file_size} for n in z.namelist() if n.startswith('assets/') and not n.endswith('/')} self.assertEqual(selected, actual) self.assertEqual(sum(n.startswith('avatar/a01-neutral-graph-v1/') for n in selected), 593) self.assertNotIn('avatar/a01-expression-action-v1/manifest.json', selected) def test_default_legacy_flags_preserve_108_files(self): selected = select.selection(ASSETS) self.assertEqual(selected['file_count'], 108) self.assertFalse(any('runtime-04/' in n for n in selected['files'])) self.assertFalse(any('startup-' in n or 'black-tulle' in n for n in selected['files'])) def test_unlisted_source_files_do_not_enter_selection(self): import shutil with tempfile.TemporaryDirectory() as folder: copied = Path(folder)/'assets' shutil.copytree(ASSETS, copied) (copied/'unapproved').mkdir() (copied/'unapproved/wrong-character.png').write_bytes(b'not media') self.assertEqual(select.selection(copied)['files'], select.selection(ASSETS)['files']) def test_wrong_persona_flags_or_pin_are_rejected(self): for profile, changes in [('unknown', {}), ('lvbu-test', {'startup': False}), ('lvbu-test', {'mouth': False}), ('lvbu-test', {'user_assets': False}), ('main', {'emotion_sha': '0'*64})]: with self.subTest(profile=profile, changes=changes), self.assertRaises(ValueError): self.full(profile, **changes) def test_index_bootstrap_and_poster_corruption_rejected(self): media = select.catalog()['mediaBundles']['black-correct03'] targets = [ASSETS/media['assetRoot']/'manifest.json', ASSETS/media['bootstrapRoot']/'manifest.json', ROOT/'app/src/main/res/drawable-nodpi'/ (media['posterResource']+'.png')] original = Path.read_bytes for target in targets: def corrupted(p): return b'corrupted' if p == target else original(p) with self.subTest(target=target.name), patch.object(Path, 'read_bytes', corrupted), self.assertRaises(ValueError): self.full() def test_selected_symlink_and_escape_rejected(self): original = Path.is_symlink target = ASSETS/'face_landmarker.task' with patch.object(Path, 'is_symlink', lambda p: p == target or original(p)), self.assertRaises(ValueError): self.full() for name in ('../x', '/x', 'x/../y', 'x//y', 'x\\y'): with self.subTest(name=name), self.assertRaises(ValueError): select.relative(name) def test_staging_cannot_overwrite_other_profile_or_build_root(self): result = self.full('lvbu-test') for dest in (ROOT/'app/build', ROOT/'app/build/generated/packagedAssets', ASSETS): with self.subTest(dest=dest), self.assertRaises(ValueError): select.stage(ASSETS, dest, result) def test_backup_lifecycle_component_contracts(self): profiles = select.catalog()['profiles'] self.assertTrue(profiles['main']['allowBackup']) self.assertTrue(profiles['main']['permissionConnectionRetention']) self.assertFalse(profiles['lvbu-test']['allowBackup']) self.assertFalse(profiles['lvbu-test']['permissionConnectionRetention']) text = (ROOT/'app/src/main/AndroidManifest.xml').read_text() self.assertIn('android:allowBackup="${characterAllowBackup}"', text) self.assertIn('android:name="com.gorios.lanvoice.MainActivity"', text) self.assertIn('android:name="com.gorios.lanvoice.CameraDiagnosticsActivity"', text) self.assertNotIn('android:name=".MainActivity"', text) build = (ROOT/'app/build.gradle').read_text() self.assertIn("build/characters/", build) self.assertIn("applicationId characterProfile.applicationId", build) activity=(ROOT/'app/src/main/java/com/gorios/lanvoice/MainActivity.kt').read_text() pause=activity[activity.index("override fun onPause()"):activity.index("override fun onStop()")] self.assertLess(pause.index("if(releaseSessionFirst"),pause.index("closeCamera()")) self.assertIn("if(!releaseSessionFirst",pause)