113 lines
6.1 KiB
Python
113 lines
6.1 KiB
Python
|
|
import copy
|
||
|
|
import pytest
|
||
|
|
from pydantic import ValidationError
|
||
|
|
from app.timeline import WorkflowSpec, compile_timeline
|
||
|
|
|
||
|
|
|
||
|
|
def spec():
|
||
|
|
return {'shots': [{'key': f'shot_{i}', 'prompt': f'镜头{i}', 'parameters': {'seed': i}} for i in range(1,4)], 'profile': 'landscape_1080_v1'}
|
||
|
|
|
||
|
|
|
||
|
|
def test_exact_keys_and_unknown_fields():
|
||
|
|
body = spec(); body['shots'][1]['key'] = 'shot_1'
|
||
|
|
with pytest.raises(ValidationError): WorkflowSpec.model_validate(body)
|
||
|
|
body = spec(); body['render'] = {'filter': 'movie=http://bad'}
|
||
|
|
with pytest.raises(ValidationError): WorkflowSpec.model_validate(body)
|
||
|
|
body = spec(); body['shots'][0]['parameters']['width'] = 256
|
||
|
|
with pytest.raises(ValidationError): WorkflowSpec.model_validate(body)
|
||
|
|
|
||
|
|
|
||
|
|
def test_freezes_profiles_and_frame_boundaries():
|
||
|
|
body = spec(); body['render'] = {'subtitles': [{'start_frame':119, 'end_frame':241, 'text':'跨镜头'}]}
|
||
|
|
plan = compile_timeline(body, {})
|
||
|
|
assert plan.generation_parameters['width'] == 1280
|
||
|
|
assert plan.generation_parameters['upscale_height'] == 1056
|
||
|
|
assert plan.canvas == (1920,1080)
|
||
|
|
assert plan.frames == 360 and plan.clip_frames == 120
|
||
|
|
body['profile'] = 'portrait_1080_v1'
|
||
|
|
plan = compile_timeline(body, {})
|
||
|
|
assert plan.canvas == (1080,1920)
|
||
|
|
assert plan.generation_parameters['upscale_width'] == 1056
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize('offset,valid', [(120,True),(121,False)])
|
||
|
|
def test_explicit_audio_cut_and_offset(offset, valid):
|
||
|
|
body = spec(); body['render'] = {'audio': {'asset_id':'audio','source_start_ms':0,'source_end_ms':10000,'offset_frame':offset,'short_policy':'silence'}}
|
||
|
|
if valid:
|
||
|
|
compile_timeline(body, {'audio': {'kind':'audio','duration_seconds':10}})
|
||
|
|
else:
|
||
|
|
with pytest.raises(ValidationError): WorkflowSpec.model_validate(body)
|
||
|
|
|
||
|
|
|
||
|
|
def test_audio_source_bounds_and_subtitle_controls():
|
||
|
|
body = spec(); body['render'] = {'audio': {'asset_id':'audio','source_start_ms':3000,'source_end_ms':18000,'offset_frame':0}}
|
||
|
|
with pytest.raises(ValueError): compile_timeline(body, {'audio': {'kind':'audio','duration_seconds':17}})
|
||
|
|
compile_timeline(body, {'audio': {'kind':'audio','duration_seconds':18}})
|
||
|
|
body = spec(); body['render'] = {'subtitles':[{'start_frame':0,'end_frame':360,'text':r'{\an8}bad'}]}
|
||
|
|
with pytest.raises(ValidationError): WorkflowSpec.model_validate(body)
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_media_short_source_rejected_before_encoding(tmp_path,monkeypatch):
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from app import media
|
||
|
|
from app.media import MediaError
|
||
|
|
plan=compile_timeline(spec(),{})
|
||
|
|
paths=[]
|
||
|
|
for i in range(3):
|
||
|
|
p=tmp_path/f'{i}.mp4';p.write_bytes(b'not decoded');paths.append(p)
|
||
|
|
async def inspect(*args):return {'frames':119,'fps':24,'width':1920,'height':1056,'duration':119/24}
|
||
|
|
monkeypatch.setattr(media,'inspect_video_track',inspect)
|
||
|
|
with pytest.raises(MediaError,match='120'):
|
||
|
|
await media.compose_workflow(plan,tmp_path/'work',None,settings=SimpleNamespace(workspace_root=tmp_path),sources=paths)
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_media_fixed_frame_argv_and_audio_offset(tmp_path,monkeypatch):
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from app import media
|
||
|
|
body=spec();body['render']={'audio':{'asset_id':'audio','source_start_ms':3000,'source_end_ms':13000,'offset_frame':120},'subtitles':[{'start_frame':119,'end_frame':241,'text':'镜头'}]}
|
||
|
|
plan=compile_timeline(body,{'audio':{'kind':'audio','duration_seconds':13}})
|
||
|
|
paths=[]
|
||
|
|
for i in range(3):
|
||
|
|
p=tmp_path/f'{i}.mp4';p.write_bytes(b'x');paths.append(p)
|
||
|
|
audio=tmp_path/'audio.wav';audio.write_bytes(b'x');calls=[]
|
||
|
|
async def track(path,settings):return {'frames':360 if path.name=='workflow.mp4' else 121,'fps':24,'width':1920,'height':1080 if path.name=='workflow.mp4' else 1056,'duration':15}
|
||
|
|
async def inspect(*args):return {'kind':'audio','duration_seconds':13}
|
||
|
|
async def process(argv,**kwargs):calls.append(argv);__import__('pathlib').Path(argv[-1]).write_bytes(b'x');return b''
|
||
|
|
monkeypatch.setattr(media,'inspect_video_track',track);monkeypatch.setattr(media,'inspect_media',inspect);monkeypatch.setattr(media,'run_process',process)
|
||
|
|
result=await media.compose_workflow(plan,tmp_path/'work',None,settings=SimpleNamespace(workspace_root=tmp_path,ffmpeg_bin='ffmpeg',max_output_bytes=1000000),sources=paths,audio=audio)
|
||
|
|
assert len(calls)==4
|
||
|
|
assert all('120'==c[c.index('-frames:v')+1] for c in calls[:3])
|
||
|
|
assert calls[-1][calls[-1].index('-frames:v')+1]=='360'
|
||
|
|
assert any('adelay=5000' in value and 'atrim=start=3.000:end=13.000' in value for value in calls[-1])
|
||
|
|
assert result.result['frames']==360
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_upstream_capabilities_cache_and_control_envelope(monkeypatch,tmp_path):
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from app.providers import Provider
|
||
|
|
p=Provider(SimpleNamespace(request_timeout_seconds=5,inference_base_url='http://inference',inference_api_key=''))
|
||
|
|
calls=[]
|
||
|
|
async def call(method,url,**kwargs):
|
||
|
|
calls.append((method,url,kwargs))
|
||
|
|
return {'protocol_version':2,'features':{'scheduling':True,'admission_pause':True}} if method=='GET' else {'desired_state':'pause'}
|
||
|
|
monkeypatch.setattr(p,'_json',call)
|
||
|
|
try:
|
||
|
|
a=await p.upstream_capabilities();b=await p.upstream_capabilities()
|
||
|
|
assert a['admission_policy'] is True and a==b and len(calls)==1
|
||
|
|
await p.control('a'*32,'pause','cmd-1',expected_version=2,expected_execution_id='e'*32)
|
||
|
|
assert calls[-1][0]=='PUT' and calls[-1][2]['payload']=={'desired_state':'pause','command_id':'cmd-1','expected_version':2,'expected_execution_id':'e'*32}
|
||
|
|
finally:await p.aclose()
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_phase_callback_is_context_local():
|
||
|
|
import asyncio
|
||
|
|
from app.providers import current_phase_callback, enter_phase
|
||
|
|
seen=[]
|
||
|
|
async def one(name):
|
||
|
|
async def hook(key,pool):seen.append((name,key,pool))
|
||
|
|
token=current_phase_callback.set(hook)
|
||
|
|
try:await asyncio.sleep(0);await enter_phase('sample_frames','cpu_media')
|
||
|
|
finally:current_phase_callback.reset(token)
|
||
|
|
await asyncio.gather(one('first'),one('second'))
|
||
|
|
assert sorted(seen)==[('first','sample_frames','cpu_media'),('second','sample_frames','cpu_media')]
|