"""Verify the packaged runtime, including its bundled FFmpeg, from any cwd.""" import json from pathlib import Path import subprocess import tempfile import traceback from PIL import Image import imageio_ffmpeg from core import FrameConfig, Options, export_frames, inspect_media def run_self_test(report_path, app_class): checks = [] app = None try: with tempfile.TemporaryDirectory(prefix='animation_tool_') as temp: root = Path(temp) images = [Image.new('RGBA', (160, 80), (i * 70, 100, 150, 255)) for i in range(3)] for image in images: image.putpixel((0, 0), (0, 0, 0, 0)) gif = root / '测试动画.gif' images[0].save(gif, save_all=True, append_images=images[1:], duration=100, disposal=2) info = inspect_media(gif) if info.count != 3: raise RuntimeError('GIF frame count mismatch') options = Options(count=2, prefix='role') files = export_frames(gif, root / 'gif', info, options, config=FrameConfig('Role', -51, -85)) data = json.loads((root / 'gif/role.json').read_text(encoding='utf-8')) if len(files) != 4 or data['HEAD_POS'] != [-65, -5] or data['L']['Cx'] != -109: raise RuntimeError('Mirrored config or negative HEAD_POS mismatch') with Image.open(files[0]) as image: if image.getpixel((0, 0))[3] != 0: raise RuntimeError('GIF transparency mismatch') checks.append('GIF, transparency, mirrored offsets and negative HEAD_POS') files = export_frames(gif, root / 'single', info, options, config=FrameConfig('Role', -51, -85, mirror=False)) if [p.name for p in files] != ['role_0_0.png', 'role_0_1.png']: raise RuntimeError('Single-direction naming mismatch') checks.append('Single-direction PNG and JSON export') webm = root / '测试视频.webm' subprocess.run([imageio_ffmpeg.get_ffmpeg_exe(), '-v', 'error', '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-s', '160x80', '-r', '10', '-i', 'pipe:0', '-c:v', 'libvpx-vp9', '-lossless', '1', str(webm)], input=b''.join(im.convert('RGB').tobytes() for im in images), check=True, capture_output=True, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)) video_info = inspect_media(webm) files = export_frames(webm, root / 'webm', video_info, options) if video_info.count != 3 or len(files) != 2: raise RuntimeError('Bundled FFmpeg WebM decode mismatch') checks.append('Bundled FFmpeg WebM encode, decode and PNG export') app = app_class() app.withdraw() app.update() if not app.mirror_directions.get(): raise RuntimeError('Default mirror setting mismatch') app.destroy() app = None checks.append('Packaged Tk GUI initialization') result = {'ok': True, 'checks': checks} except Exception: result = {'ok': False, 'checks': checks, 'error': traceback.format_exc()} finally: if app is not None: app.destroy() Path(report_path).write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') return 0 if result['ok'] else 1