"""Blender regression fixtures for debris, protected head details and failed limbs."""
import bpy,sys,json
from pathlib import Path
from mathutils import Vector
sys.path.insert(0,str(Path(__file__).parent))
from fragments import clean

def cube(name,location,scale):
 bpy.ops.mesh.primitive_cube_add(size=1,location=location);o=bpy.context.object;o.name=name;o.scale=scale;bpy.ops.object.transform_apply(location=False,rotation=False,scale=True);return o

def fixture(extra):
 bpy.ops.wm.read_factory_settings(use_empty=True)
 arm=bpy.data.armatures.new('Rig');rig=bpy.data.objects.new('Rig',arm);bpy.context.collection.objects.link(rig);bpy.context.view_layer.objects.active=rig;rig.select_set(True);bpy.ops.object.mode_set(mode='EDIT');bone=arm.edit_bones.new('Head');bone.head=(0,-.8,.6);bone.tail=(0,-1,.6);bpy.ops.object.mode_set(mode='OBJECT')
 body=cube('Body',(0,0,.5),(1,2,1));parts=[body]
 if extra=='debris':parts.append(cube('Debris',(.1,0,-.1),(.01,.02,.02)))
 if extra=='thin':parts.append(cube('Shard',(.2,0,-.1),(.15,.1,.0002)))
 if extra=='head':parts.append(cube('Eye',(0,-1.015,.6),(.03,.02,.03)))
 if extra=='limb':parts.append(cube('DetachedLeg',(1.3,0,.1),(.3,.4,.4)))
 if extra=='inner':parts.append(cube('Inner',(0,0,.5),(.98,1.98,.98)))
 data,_=clean(parts,rig);return data
results={k:fixture(k) for k in ['debris','thin','head','limb','inner']}
assert results['debris']['pass'] and results['debris']['removed_vertices']>0
assert results['thin']['pass'] and results['thin']['removed_vertices']>0
assert results['head']['pass'] and results['head']['removed_vertices']==0
assert not results['limb']['pass'] and results['limb']['remaining_detached_non_head']==1
assert results['inner']['pass'] and results['inner']['removed_vertices']>0
print('FRAGMENT FIXTURES PASS: debris, thin shard, near-head eye, rejected detached limb, enclosed shell')
if '--' in sys.argv:Path(sys.argv[-1]).write_text(json.dumps(results,indent=2))
