#!/usr/bin/env python3
import copy,importlib.util,json
from pathlib import Path
R=Path(__file__).resolve().parents[1];sp=importlib.util.spec_from_file_location('v',Path(__file__).with_name('validate_storyboard_package.py'));v=importlib.util.module_from_spec(sp);sp.loader.exec_module(v);base=json.loads((R/'fixtures/storyboard-package-test-fixture.json').read_text(encoding='utf-8'))
def run(n,m=None,x=None):
 d=copy.deepcopy(base)
 if m:m(d)
 e,w=v.validate(d);ok=(not e) if x is None else any(x in z for z in e);return {'name':n,'status':'PASS' if ok else 'FAIL','errors':e}
def f(path,val=None,delete=False):
 def m(d):
  o=d
  for k in path[:-1]:o=o[k] if not isinstance(k,int) else o[k]
  if delete:del o[path[-1]]
  else:o[path[-1]]=val
 return m
C=[
 run('positive'),
 run('reject_new_story',f(['creates_new_story_truth'],True),'new story truth'),
 run('source_hash',f(['source_contract','source_hash'],''),'source_hash'),
 run('reject_word_count_basis',f(['source_metrics','shot_count_basis'],'WORD_COUNT'),'cannot be derived'),
 run('reject_metric_misuse',lambda d:d['source_metrics']['usage'].append('SHOT_COUNT_GENERATION'),'metrics misuse'),
 run('require_clusters',f(['scenes',0,'beat_clusters'],[]),'beat_clusters'),
 run('cluster_source_coverage',f(['scenes',0,'beat_clusters',1,'source_refs'],['1-1.B02']),'beat clustering incomplete'),
 run('split_requires_reason',f(['scenes',0,'beat_clusters',1,'split_reasons'],[]),'split lacks reasons'),
 run('nmin_relation',f(['scenes',0,'shot_count_plan','necessary_shots_n_min'],4),'invalid N_MIN'),
 run('nsetup_relation',f(['scenes',0,'shot_count_plan','camera_setups_n_setup'],4),'invalid N_SETUP'),
 run('minimum_test_agreement',f(['scenes',0,'minimum_sufficient_test','proposed_n_min'],1),'minimum test'),
 run('require_pov_owner',f(['scenes',0,'pov_ownership','audience_follows'],''),'restricted POV'),
 run('illegal_expansion_class',f(['scenes',0,'production_expansion_ledger',0,'class'],'NEW_MEMORY'),'illegal production'),
 run('expansion_changes_story',f(['scenes',0,'production_expansion_ledger',0,'creates_new_story_fact'],True),'changes story truth'),
 run('evidence_overclaim',f(['scenes',0,'paired_learning','claim_limit'],'FULLY_UNDERSTOOD'),'evidence claim'),
 run('invalid_track',f(['scenes',0,'shots',0,'track'],'FINAL'),'invalid track'),
 run('safety_in_edit',f(['scenes',0,'shots',2,'planned_edit_use_s'],1.0),'safety-only'),
 run('unknown_setup',f(['scenes',0,'shots',0,'setup_id'],'BAD'),'unknown setup'),
 run('unknown_expansion',f(['scenes',0,'shots',0,'expansion_refs'],['BAD']),'unknown expansion'),
 run('new_dialogue',f(['scenes',0,'shots',1,'dialogue','new_text'],True),'new dialogue'),
 run('dialogue_source',f(['scenes',0,'shots',1,'dialogue','source_line_ref'],''),'dialogue missing'),
 run('movement_motivation',f(['scenes',0,'shots',1,'camera','movement_motivation'],''),'moving shot lacks'),
 run('axis_recovery',lambda d:d['scenes'][0]['shots'][0]['axis_and_screen_direction'].update({'axis_crossing':True,'axis_recovery':'NOT_REQUIRED'}),'axis crossing'),
 run('timing_over',f(['scenes',0,'shots',0,'timing_budget','occupied_s'],8),'timing exceeds'),
 run('capture_mismatch',f(['scenes',0,'shots',0,'timing_budget','allocated_capture_s'],5),'capture timing differs'),
 run('speech_time',f(['scenes',0,'shots',1,'timing_budget','dialogue_s'],0.1),'dialogue time below'),
 run('coverage_count',f(['scenes',0,'shot_count_plan','coverage_shots_n_coverage'],4),'N_COVERAGE'),
 run('edit_count',f(['scenes',0,'shot_count_plan','creative_edit_shots_n_edit'],3),'N_EDIT'),
 run('creative_unknown',lambda d:d['scenes'][0]['creative_cut_plan']['ordered_shot_ids'].append('BAD'),'creative cut references'),
 run('creative_uses_safety',f(['scenes',0,'creative_cut_plan','ordered_shot_ids'],['1-1-S01','1-1-S03']),'safety-only'),
 run('edit_duration',f(['scenes',0,'shots',0,'planned_edit_use_s'],4),'creative edit duration'),
 run('creative_beat_coverage',f(['scenes',0,'shots',1,'source_refs'],['1-1.B02']),'creative cut does not cover'),
 run('safety_enumeration',f(['scenes',0,'safety_coverage_plan','required_shot_ids'],['1-1-S01','1-1-S02']),'safety coverage plan'),
 run('information_ledger_complete',lambda d:d['scenes'][0]['information_ledger'].pop(),'information ledger'),
 run('setup_record_count',lambda d:d['scenes'][0]['camera_setups'].pop(),'N_SETUP'),
 run('required_reaction',lambda d:d['scenes'][0]['required_reactions'].append('R99'),'required reactions'),
 run('project_duration',f(['project_profile','target_edit_duration_s'],11),'project edit duration'),
 run('release_overclaim',f(['release_claim'],'FULLY_UNDERSTOOD_SHOT_ALGORITHM'),'static evidence')
]
st='PASS' if all(x['status']=='PASS' for x in C) else 'FAIL';print(json.dumps({'status':st,'passed':sum(x['status']=='PASS' for x in C),'total':len(C),'results':C},ensure_ascii=False,indent=2));raise SystemExit(0 if st=='PASS' else 1)
