cmvr-visual/backend/tests/test_story_render_policy.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

86 lines
4.5 KiB
Python

"""长镜头校验持有事务锁时,最终合成准入仍须读取真实且新鲜的远程策略。"""
import time
import pytest
from sqlalchemy import func, select
from test_story_studio import story_spec
@pytest.mark.asyncio
@pytest.mark.parametrize('refresh_result', ['ready', 'unavailable', 'invalid'])
async def test_render_refreshes_policy_after_slow_clip_validation(system, monkeypatch, refresh_result):
from app.domain.errors import MediaError
from app.models import Asset, QuotaReservation, ShotCandidate, ShotSelection, WorkflowDraft, WorkflowRun
from app.services.stories.common import current_hash
c = system.client
created = await c.post('/api/stories', json={'name': 'Slow clip validation', 'spec': story_spec()})
assert created.status_code == 201, created.text
draft = created.json()
owner = (await c.get('/api/auth/me')).json()['user']['id']
async with system.app.state.db.session() as s:
d = await s.get(WorkflowDraft, draft['id'])
asset = Asset(owner_id=owner, name='approved.mp4', kind='video', mime_type='video/mp4',
storage_key='approved.mp4', size_bytes=3, sha256='a' * 64,
width=1920, height=1080, duration_seconds=5, ready=True)
s.add(asset)
await s.flush()
context = await current_hash(s, d, '1' * 32, 'video', source_kind='manual', source_sha256=asset.sha256)
candidate = ShotCandidate(owner_id=owner, draft_id=d.id, shot_id='1' * 32, kind='video',
source_kind='manual', asset_id=asset.id, asset_sha256=asset.sha256,
context_hash=context, generation_hash='b' * 64,
created_revision=1, state='ready')
s.add(candidate)
await s.flush()
s.add(ShotSelection(owner_id=owner, draft_id=d.id, shot_id='1' * 32, kind='video',
candidate_id=candidate.id, asset_id=asset.id, context_hash=context,
decision_json={'asset_sha256':asset.sha256, 'source_kind':'manual'}))
await s.commit()
settings = system.settings
settings.inference_remote_files = True
settings._remote_policy_error = None
settings._remote_policy_observed_at = time.monotonic()
calls = []
async def slow_validation(*args, **kwargs):
# 模拟多片段真实解码耗时,不延长测试或修改事件循环的时钟。
settings._remote_policy_observed_at = time.monotonic() - 6
async def refresh():
calls.append('refresh')
if refresh_result == 'unavailable':
raise MediaError('upstream_unavailable', 'Unavailable', True)
settings._remote_policy_observed_at = time.monotonic()
settings._remote_policy_error = 'runtime_policy_invalid' if refresh_result == 'invalid' else None
return {'runtime_policy': {'configuration_error': settings._remote_policy_error}}
monkeypatch.setattr('app.services.stories.api.validate_video', slow_validation)
monkeypatch.setattr(system.provider, 'runtime_status', refresh, raising=False)
path = f"/api/stories/{draft['id']}/renders"
headers = {'Idempotency-Key':'slow-render'}
body = {'expected_version':1, 'execution_mode':'interactive'}
response = await c.post(path, headers=headers, json=body)
assert calls == ['refresh']
if refresh_result == 'ready':
assert response.status_code == 202, response.text
settings._remote_policy_observed_at = time.monotonic() - 6
replay = await c.post(path, headers=headers, json=body)
assert replay.status_code == 202 and replay.json() == response.json()
assert calls == ['refresh'] # 已接纳结果重放不依赖外部服务再次可用。
else:
assert response.status_code == 503, response.text
assert response.json()['detail']['code'] == 'runtime_policy_unavailable'
async with system.app.state.db.session() as s:
count = await s.scalar(select(func.count()).select_from(WorkflowRun))
assert count == (1 if refresh_result == 'ready' else 0)
reservations = await s.scalar(select(func.count()).select_from(QuotaReservation).where(
QuotaReservation.operation == 'story.render'))
assert reservations == (1 if refresh_result == 'ready' else 0)
if refresh_result != 'ready':
refresh_result = 'ready'
recovered = await c.post(path, headers=headers, json=body)
assert recovered.status_code == 202, recovered.text
assert calls == ['refresh', 'refresh']