cmvr-visual/qa/v3_live_acceptance.py

89 lines
6.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Live private-entry acceptance. Keys are stable; never prints login secrets."""
import json,time
from pathlib import Path
import requests
ROOT=Path(__file__).resolve().parents[1]
ENV=dict(line.split('=',1) for line in (ROOT/'.env.server').read_text(encoding='utf-8-sig').splitlines() if line and not line.startswith('#') and '=' in line)
BASE='http://127.0.0.1:8000/api'
OUT=ROOT/'qa/v3-live.json'
report={}
def save():OUT.write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
def login():
client=requests.Session()
response=client.post(BASE+'/auth/login',json={'username':ENV['ADMIN_USERNAME'],'password':ENV['ADMIN_PASSWORD']},timeout=15)
assert response.status_code==200,('login',response.status_code)
client.headers['X-CSRF-Token']=response.json()['csrf_token'];return client
def accepted(client,route,body,key):
r=client.post(BASE+route,json=body,headers={'Idempotency-Key':key},timeout=25)
assert r.status_code==202,(route,r.status_code)
return r.json()
def wait_job(client,id,seconds=300):
deadline=time.monotonic()+seconds
while time.monotonic()<deadline:
r=client.get(BASE+'/jobs/'+id,timeout=15);assert r.status_code==200
job=r.json()
if job['state'] in {'succeeded','failed','timed_out','cancelled','needs_attention'}:
assert job['state']=='succeeded',(id,job['state'],job.get('error',{}).get('code') if job.get('error') else None)
return job
time.sleep(1)
raise TimeoutError('Live job did not finish within acceptance deadline')
client=login()
try:
caps=client.get(BASE+'/capabilities',timeout=15).json()
assert caps['execution_policy']['batch_window_enabled'] is False
assert caps['execution_policy']['configuration_error'] is None
assert caps['workflow_limits']['max_shots']==120
report['execution_policy']=caps['execution_policy'];report['workflow_limits']=caps['workflow_limits']
spec=json.loads((ROOT/'frontend/public/examples/kunpeng-storyboard.json').read_text(encoding='utf-8'))
name='鲲鹏·20镜头100秒示例(未运行)'
drafts=client.get(BASE+'/workflows',params={'page_size':100},timeout=15).json()['items']
existing=next((d for d in drafts if d['name']==name),None)
if existing:draft=existing
else:
r=client.post(BASE+'/workflows',json={'name':name,'kind':'multi_shot','spec':spec},timeout=15)
assert r.status_code==201,('sample_draft',r.status_code);draft=r.json()
assert len(draft['spec']['shots'])==20 and draft['spec']['render']['total_frames']==2400
assert not draft['current_run_id']
report['sample_draft_id']=draft['id'];report['sample_draft_not_run']=True;save()
old=client.get(BASE+'/workflow-runs/5e9108a5d1bc4f989e11ffe7bd7e7707',timeout=15).json()
sources=[next(a for a in j['result_assets'] if a['kind']=='video')['id'] for j in sorted(old['jobs'],key=lambda j:j['step_key'] or '') if j['step_key'] in {'shot_1','shot_2','shot_3'}]
assert len(sources)==3
sources.append(sources[0])
short={'schema_version':2,'shots':[{'key':f'shot_{i+1}','prompt':'已有验收素材复用合成','parameters':{'seed':42},'duration_frames':frames} for i,frames in enumerate([24,120,120,120])],'profile':'landscape_1080_v1','render':{'fps':24,'total_frames':384,'fit':'contain','audio':None,'subtitles':[{'start_frame':0,'end_frame':24,'text':'多镜头长视频验收'},{'start_frame':361,'end_frame':384,'text':'超过15秒'}],'template_version':'plain-v1'}}
composition=accepted(client,'/jobs',{'task_type':'video.compose','input':{'asset_ids':sources},'parameters':{'workflow_spec':short,'clips':[{'asset_id':id} for id in sources]},'execution_mode':'batch'},'v3-live-16s-20261111')['jobs'][0]
narration=(ROOT/'frontend/public/examples/kunpeng-narration.txt').read_text(encoding='utf-8').strip()
voice=accepted(client,'/jobs',{'task_type':'audio.speech','input':{'prompt':narration},'parameters':{'voice':'zm_yunxi','speed':0.85},'execution_mode':'batch'},'v3-live-narration-20261111')['jobs'][0]
batch=accepted(client,'/batches',{'name':'开发全天配置验收','items':[{'kind':'job','task_type':'math.solve','input':{'prompt':'只输出一个数字:7加8等于多少?'},'parameters':{'max_tokens':32,'enable_thinking':False}}]},'v3-live-policy-batch-20261111')
report.update(composition_job_id=composition['id'],narration_job_id=voice['id'],math_batch_id=batch['batch']['id']);save()
finally:client.close()
# Closing the original HTTP client leaves all three persistent tasks running.
client=login()
try:
job=wait_job(client,report['composition_job_id'])
result=job['result'];assert result['frames']==384 and result['duration_seconds']==16 and result['clip_count']==4,result
asset=next(a for a in job['result_assets'] if a['kind']=='video')
r=client.patch(BASE+'/assets/'+asset['id'],json={'name':'多镜头16秒开发验收.mp4','tags':['开发验收','多镜头']},timeout=10);assert r.status_code==200
target=ROOT/'qa/generated/v3-long-video-16s.mp4';target.parent.mkdir(parents=True,exist_ok=True)
r=client.get(BASE+f"/assets/{asset['id']}/content",timeout=60);assert r.status_code==200;target.write_bytes(r.content)
report['composition']={'asset_id':asset['id'],'result':result,'bytes':len(r.content)};save()
job=wait_job(client,report['narration_job_id'])
asset=next(a for a in job['result_assets'] if a['kind']=='audio')
r=client.patch(BASE+'/assets/'+asset['id'],json={'name':'鲲鹏完整旁白(开发验收).wav','tags':['鲲鹏','旁白','开发验收']},timeout=10);assert r.status_code==200
report['narration']={'asset_id':asset['id'],'voice':'zm_yunxi','speed':0.85,'duration_seconds':asset['duration_seconds'],'bytes':asset['size_bytes'],'characters':len(narration)};save()
deadline=time.monotonic()+60
while time.monotonic()<deadline:
batch=client.get(BASE+'/batches/'+report['math_batch_id'],timeout=15).json()
if batch['batch']['state']=='succeeded':break
time.sleep(1)
assert batch['batch']['state']=='succeeded'
math=client.get(BASE+'/jobs/'+batch['items'][0]['target_id'],timeout=15).json()
assert '15' in json.dumps(math['result'],ensure_ascii=False)
report['math_after_http_close']={'job_id':math['id'],'succeeded':True,'answer':math['result']}
report['ready']=client.get(BASE+'/ready',timeout=15).json();assert all(report['ready']['checks'].values())
save();print(json.dumps(report,ensure_ascii=True))
finally:client.close()