"""Only actual TP2 generation in the chosen closure is subject to the six-second limit.""" from copy import deepcopy from types import SimpleNamespace from unittest.mock import AsyncMock import pytest from sqlalchemy import func, select from app.models import Asset, Job, WorkflowRun from app.services.orchestration import editor_engine from test_editor_engine import manifest from test_h3_tp2_product import TP2, certificate from test_h3_tp2_review_fixes import tp2_editor @pytest.mark.parametrize("goal", ["media", "film"]) async def test_certified_seven_second_tp2_plan_explains_limit_and_never_accepts_run(system, goal): id, m = await tp2_editor(system, n=1, source_kind="story") path = f"/api/editors/{id}" m["spec"]["shots"][0]["video_parameters"]["source_seconds"] = 7 saved = await system.client.post( path + "/save", json={"expected_version": 2, "manifest": m}, headers={"Idempotency-Key": "tp2-seven-seconds"}, ) assert saved.status_code == 200, saved.text assert saved.json()["manifest"]["spec"]["shots"][0]["video_parameters"]["source_seconds"] == 7 system.provider.upstream_capabilities = AsyncMock( return_value={"video_generation": {"tp2": certificate()}} ) body = {"expected_version": 3, "output_goal": goal, "targets": ["video:" + m["shot_ids"][0]]} plan = await system.client.post(path + "/plan", json=body) assert plan.status_code == 200, plan.text issues = plan.json()["issues"] assert any(i["code"] == "invalid_video_profile" and "1–6秒" in i["message"] for i in issues) assert ( next(s for s in plan.json()["steps"] if s["kind"] == "video.generate")["parameters"][ "num_frames" ] == 169 ) rejected = await system.client.post( path + "/runs", json={**body, "expected_plan_hash": plan.json()["plan_hash"]}, headers={"Idempotency-Key": "tp2-seven-run"}, ) assert rejected.status_code == 422, rejected.text async with system.app.state.db.session() as s: assert await s.scalar(select(func.count()).select_from(WorkflowRun)) == 0 assert await s.scalar(select(func.count()).select_from(Job)) == 0 async def test_seven_second_tp2_source_still_allows_unrelated_target_reuse_and_compose( system, monkeypatch, png_bytes ): id, m = await tp2_editor(system, n=1, source_kind="story") path = f"/api/editors/{id}" m["spec"]["shots"][0]["video_parameters"]["source_seconds"] = 7 uploaded = await system.client.post( "/api/assets", files={"file": ("old-video.png", png_bytes, "image/png")} ) assert uploaded.status_code == 201 aid = uploaded.json()["id"] async with system.app.state.db.session() as s: a = await s.get(Asset, aid) a.kind, a.width, a.height, a.duration_seconds = "video", 1344, 768, 10 sha = a.sha256 await s.commit() sid = m["shot_ids"][0] m["bindings"]["video:" + sid] = { "asset_id": aid, "sha256": sha, "kind": "video", "source": "manual", } monkeypatch.setattr("app.services.stories.candidates.validate_video", AsyncMock()) saved = await system.client.post( path + "/save", json={"expected_version": 2, "manifest": m}, headers={"Idempotency-Key": "tp2-seven-reuse"}, ) assert saved.status_code == 200, saved.text system.provider.upstream_capabilities = AsyncMock( side_effect=AssertionError("reuse must not probe TP2") ) for goal, targets in [("media", ["frame:" + sid]), ("media", ["video:" + sid]), ("film", [])]: body = {"expected_version": 3, "output_goal": goal, "targets": targets} plan = await system.client.post(path + "/plan", json=body) assert plan.status_code == 200 and not plan.json()["issues"], plan.text accepted = await system.client.post( path + "/runs", json={**body, "expected_plan_hash": plan.json()["plan_hash"]}, headers={"Idempotency-Key": goal + str(len(targets)) + str(targets)}, ) assert accepted.status_code == 202, accepted.text system.provider.upstream_capabilities.assert_not_awaited() read = await system.client.get(path) assert read.json()["manifest"]["spec"]["shots"][0]["video_parameters"]["source_seconds"] == 7 @pytest.mark.parametrize( "frames,valid", [(24, False), (124, False), (146, False), (169, False), (25, True), (145, True)] ) async def test_final_tp2_plan_validates_the_actual_requested_frames(monkeypatch, frames, valid): m = manifest(1) m["spec"]["shots"][0]["parameters"]["profile"] = TP2 recipes = editor_engine.build_recipes(m, {}, {"output_goal": "media"}, SimpleNamespace()) next(r for r in recipes if r["kind"] == "video.generate")["parameters"]["num_frames"] = frames monkeypatch.setattr(editor_engine, "build_recipes", lambda *_: deepcopy(recipes)) plan = await editor_engine.plan_editor( SimpleNamespace(get=AsyncMock(return_value=None)), SimpleNamespace(owner_id="owner", version=1), m, {"output_goal": "media"}, SimpleNamespace(), tp2_capability=AsyncMock(return_value=True), ) assert any(i["code"] == "invalid_video_profile" for i in plan["issues"]) is (not valid)