"""图2 节点 4~9:任务清单 → 入队 → 限流派发 → 生成 → 保存产物 → 质检。 用 inline 执行器 + mock 适配器(真实 ffmpeg 产物),因此这一条用例 等价于「在没有 Redis、没有供应商密钥的机器上跑完整条流水线」。 """ from __future__ import annotations from pathlib import Path import pytest from app.constants import CameraMovement, canvas_for from app.providers.base import VideoGenRequest from app.providers.mock import MockVideoProvider from app.services import ffmpeg as ff from tests.conftest import absolute def _import_two_shots(api, studio, script_text, make_image_png): pid = studio["project_id"] api.post(f"/api/projects/{pid}/import", data={"script_text": script_text, "default_duration": 2}, files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")), ("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))]) return pid def test_plan_task_list_reports_cost_and_voiceovers(api, studio, script_text, make_image_png): pid = _import_two_shots(api, studio, script_text, make_image_png) plan = api.post(f"/api/projects/{pid}/tasklist", json={"include_voiceover": True})["plan"] assert plan["queued_count"] == 2 and plan["voiceover_count"] == 2 assert plan["blocked_count"] == 0 and plan["skipped_count"] == 0 assert plan["currency"] == "CNY" assert {s["provider"] for s in plan["shots"]} == {"mock"} assert all(s["action"] == "enqueue" for s in plan["shots"]) assert [v["chars"] for v in plan["voiceovers"]] == [5, 6] # 只生成清单不入队 assert api.get(f"/api/projects/{pid}/queue")["counts"] == {} def test_enqueue_dispatch_generate_qc_end_to_end(api, studio, script_text, make_image_png): pid = _import_two_shots(api, studio, script_text, make_image_png) enqueued = api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": True, "priority": 5, "actor": "测试导演"}) created = enqueued["result"]["created"] assert created["count"] >= 2 and created["skipped"] == [] assert enqueued["summary"]["stats"]["tasks"] == created["count"] assert enqueued["queue"]["counts"].get("queued", 0) >= 2 shot_ids = [s["id"] for s in api.get(f"/api/projects/{pid}/shots")["shots"]] dispatched = api.post(f"/api/projects/{pid}/dispatch", json={"limit": 10}) assert dispatched["result"]["dispatched"]["count"] >= 1 drained = api.drain(pid)["result"]["drained"] assert drained["completed"] >= 1 and drained["rounds"] >= 1 assert drained["remaining"] == 0 and drained["timed_out"] is False shots = api.get(f"/api/projects/{pid}/shots")["shots"] for shot in shots: assert shot["status"] in ("generated", "qc_passed"), shot["status"] assert shot["output_exists"] is True and shot["output_url"].startswith("/media/") assert absolute(shot["output_path"]).exists() and absolute(shot["output_path"]).stat().st_size > 1024 assert shot["output_duration"] and shot["output_duration"] > 1.0 assert shot["provider_job_id"].startswith("mock-") assert shot["stage"] in ("quality_check", "review") # 生成成功后自动挂上质检任务并给出评分 qc = api.get(f"/api/projects/{pid}/qc")["qc"] assert qc["checked"] == 2 and qc["passed"] == 2 and qc["average_score"] >= 70 assert all(row["score"] is not None for row in qc["shots"]) tasks = api.get("/api/tasks", params={"project_id": pid}) assert tasks["counts"].get("succeeded", 0) >= 2 assert tasks["counts"].get("failed", 0) == 0 kinds = {t["kind"] for t in tasks["tasks"]} assert {"generate_shot", "qc_shot", "voiceover"} <= kinds one = next(t for t in tasks["tasks"] if t["kind"] == "generate_shot") assert one["attempts"] >= 1 and one["elapsed_ms"] and one["elapsed_ms"] >= 0 assert api.get(f"/api/tasks/{one['id']}")["task"]["status"] == "succeeded" assert one["execution_backend"] == "inline" and len(one["error_history"]) == 0 # 产物入库并计入生成记录(成本 mock 为 0,但记录必须存在) generations = api.get(f"/api/projects/{pid}/generations") assert generations["count"] >= 2 costs = api.get(f"/api/projects/{pid}/costs")["cost"] assert costs["total_cost"] == 0.0 and costs["generations"]["count"] >= 2 workflow = {n["key"]: n for n in api.get(f"/api/projects/{pid}/summary")["result"]["workflow"]} assert workflow["generate"]["state"] == "done" and workflow["save_output"]["state"] == "done" assert workflow["quality_check"]["state"] == "done" assert workflow["review"]["state"] in ("pending", "active") events = api.get(f"/api/projects/{pid}/events")["events"] assert all(e["message"] and e["created_at"] for e in events) types = {e["action"] for e in events} assert {"task.succeeded", "shot.generated"} <= types def test_approve_shots_advances_review_stage(api, studio, script_text, make_image_png): pid = _import_two_shots(api, studio, script_text, make_image_png) api.post(f"/api/projects/{pid}/tasks", json={}) api.drain(pid) shots = api.get(f"/api/projects/{pid}/shots")["shots"] first = api.post(f"/api/projects/{pid}/shots/{shots[0]['id']}/approve", json={"notes": "构图与运动都合格", "reviewer": "审核老师"})["result"]["shot"] assert first["status"] == "approved" and first["review_state"] == "pass" assert api.get(f"/api/projects/{pid}/qc")["qc"]["shots"][0]["review_state"] == "pass" api.post(f"/api/projects/{pid}/shots/{shots[1]['id']}/approve", json={}) summary = api.get(f"/api/projects/{pid}/summary")["summary"] assert summary["stats"]["shots_approved"] == 2 assert summary["stats"]["progress_ratio"] == 1.0 manual = api.get(f"/api/projects/{pid}/reviews", params={"kind": "manual"})["reviews"] assert len(manual) == 2 and {r["verdict"] for r in manual} == {"pass"} assert {r["reviewer"] for r in manual} == {"审核老师", "studio"} auto = api.get(f"/api/projects/{pid}/reviews", params={"kind": "auto"})["reviews"] assert len(auto) == 2 and all(r["score"] is not None for r in auto) def test_reject_with_new_prompt_regenerates_the_shot(api, studio, script_text, make_image_png): pid = _import_two_shots(api, studio, script_text, make_image_png) api.post(f"/api/projects/{pid}/tasks", json={}) api.drain(pid) shot = api.get(f"/api/projects/{pid}/shots")["shots"][0] before = (shot["output_path"], shot["provider_job_id"]) rejected = api.post(f"/api/projects/{pid}/shots/{shot['id']}/reject", json={"notes": "主体漂移", "prompt": "角色五官与定妆图保持一致,镜头更稳", "regenerate": True, "reviewer": "审核老师"}) assert rejected["result"]["shot"]["status"] == "rejected" assert rejected["result"]["requeue"]["created"]["count"] >= 1 api.drain(pid) after = next(s for s in api.get(f"/api/projects/{pid}/shots")["shots"] if s["id"] == shot["id"]) assert after["status"] in ("generated", "qc_passed") assert after["params"]["prompt_override"].startswith("角色五官") assert "角色五官" in after["prompt"] assert after["output_path"] != before[0] or after["provider_job_id"] != before[1] verdicts = [r["verdict"] for r in api.get(f"/api/projects/{pid}/reviews")["reviews"]] assert "fail" in verdicts def test_voiceover_produces_playable_audio_and_subtitles(api, studio, script_text, make_image_png): pid = _import_two_shots(api, studio, script_text, make_image_png) created = api.post(f"/api/projects/{pid}/voiceover", json={"only_missing": True})["result"] assert created["count"] == 2 api.drain(pid) shots = api.get(f"/api/projects/{pid}/shots")["shots"] for shot in shots: assert shot["voiceover_url"].startswith("/media/") assert absolute(shot["voiceover_path"]).exists() assert shot["voiceover_duration_ms"] and shot["voiceover_duration_ms"] > 200 again = api.post(f"/api/projects/{pid}/voiceover", json={"only_missing": True})["result"] assert again["count"] == 0 def test_camera_movement_reaches_video_adapter(api, studio, script_text, make_image_png): """镜头上的运镜必须作为结构化入参抵达适配器。 之前 build_video_request 只把 params_json 透传给供应商,景别/机位/运镜仅存在于 提示词文本里;读 request.params 的适配器(mock 的 _motion_filter、带 camera control 的供应商)因此永远拿不到运镜,出片一律是固定机位。 """ pid = _import_two_shots(api, studio, script_text, make_image_png) shots = api.get(f"/api/projects/{pid}/shots")["shots"] assert [s["camera_movement"] for s in shots] == ["push_in", "pull_out"] api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False, "priority": 5}) api.post(f"/api/projects/{pid}/dispatch", json={"limit": 10}) api.drain(pid) rows = api.get(f"/api/projects/{pid}/generations")["generations"] video = {g["shot_id"]: g for g in rows if g["kind"] == "generate_shot"} assert len(video) == 2, video movements = set() for shot in api.get(f"/api/projects/{pid}/shots")["shots"]: meta = video[shot["id"]].get("provider_metadata") or {} assert meta.get("camera_movement") == shot["camera_movement"], (shot, meta) movements.add(meta.get("camera_movement")) assert movements == {"push_in", "pull_out"}, "两镜运镜不同,产物留痕必须不同" @pytest.mark.parametrize("movement", CameraMovement.ALL) def test_every_camera_movement_renders(tmp_path, movement): """每一种运镜都必须能在本机真实 ffmpeg 上出片。 回归背景:crop 的 w/h 只在 configure 期求值、不支持变量 t, 旧的缩放类运镜把 `ih/(1.35-0.04*t)` 写进 crop w,真实 ffmpeg 直接 `Error when evaluating the expression` → 产物为空、运镜静默丢失。 同时覆盖「运镜参数必须随 VideoGenRequest.params 下发到适配器」。 """ out = tmp_path / f"{movement}.mp4" result = MockVideoProvider().generate(VideoGenRequest( prompt="运镜回归", duration_seconds=1.0, aspect_ratio="16:9", resolution="480p", fps=12, mode="t2v", params={"camera_movement": movement}, output_path=str(out), label=f"shot-{movement}")) probed = ff.probe(Path(result.file_path)) expect_w, expect_h = canvas_for("480p", "16:9") assert out.exists() and out.stat().st_size > 512, f"{movement} 产物为空" assert (probed.width, probed.height) == (expect_w, expect_h) assert abs(probed.duration - 1.0) <= 0.25 assert result.metadata["camera_movement"] == movement assert result.metadata["canvas"] == [expect_w, expect_h] def test_qc_drain_returns_score_written_by_worker_session(api, studio, script_text, make_image_png): """drain 结束后必须回读到线程池会话刚写入的质检结果。 回归(验收项目 6 · 镜头 4):`POST /shots/{sid}/qc?drain=true` 在 drain 之后 直接 `db.get(Shot, sid)`,而质检任务由内联线程池的**独立会话**落库; 请求会话的 identity map 还缓存着质检前的旧对象,于是库里明明已经 100 分, 接口却返回 score=None / qc={},前端与自动化都会误判「质检没跑」。 """ from app.db import SessionLocal from app.models import Shot pid = _import_two_shots(api, studio, script_text, make_image_png) api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False}) api.drain(pid) rows = api.get(f"/api/projects/{pid}/shots")["shots"] shot_id = rows[0]["id"] with SessionLocal() as writer: # 独立会话:模拟「刚出片、质检未落库」 row = writer.get(Shot, shot_id) row.qc_score = None row.qc_json = {} writer.commit() result = api.post(f"/api/projects/{pid}/shots/{shot_id}/qc?drain=true")["result"] assert result["drain"]["remaining"] == 0, result["drain"] assert result["score"] is not None and result["score"] >= 70, result assert result["qc"].get("passed") is True, result["qc"] # 技术指标恒为这 7 项;画面质检只往后追加 visual.* / visual_error, # 它改的是混合总分,不该混进技术清单里稀释权重。 checks = result["qc"]["checks"] tech = [c for c in checks if not str(c["key"]).startswith("visual")] visual = [c for c in checks if str(c["key"]).startswith("visual")] assert [c["key"] for c in tech] == ["integrity", "duration", "resolution", "fps", "bitrate", "black", "freeze"], tech assert all(c["weight"] == 0 for c in visual), visual overview = api.get(f"/api/projects/{pid}/qc")["qc"] line = next(x for x in overview["shots"] if x["shot_id"] == shot_id) assert line["passed"] is True and line["score"] == result["score"], (line, result["score"]) def test_existing_output_is_not_regenerated_without_redo_intent(api, studio, script_text, make_image_png): """RUN #39 现场:LLM 重跑「生成镜头」这一步时,把 4 个已经质检合格的镜头 又排了一遍生成——因为「产物已存在但尚未人工审核通过」也被当成待生成。 质检/审核是后面步骤的事,这里必须跳过;只有明确的「重做」才重出画面。""" pid = _import_two_shots(api, studio, script_text, make_image_png) api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": True}) api.post(f"/api/projects/{pid}/dispatch", json={"limit": 10}) assert api.drain(pid)["result"]["drained"]["remaining"] == 0 plan = api.post(f"/api/projects/{pid}/tasklist", json={"include_voiceover": True})["plan"] assert plan["queued_count"] == 0, plan["shots"] assert plan["skipped_count"] == 2, plan["shots"] assert all("产物已存在" in s["reason"] for s in plan["shots"]), plan["shots"] assert plan["voiceover_count"] == 0 and all( v["action"] == "skip" for v in plan["voiceovers"]), plan["voiceovers"] # 入队也不会凭空造任务 again = api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": True}) assert again["result"]["created"]["count"] == 0 assert len(again["result"]["created"]["skipped"]) == 2, again["result"]["created"] # 编导说「重做」:force=True 时照旧重新排队 forced = api.post(f"/api/projects/{pid}/tasklist", json={"force": True})["plan"] assert forced["queued_count"] == 2, forced["shots"] def test_rejected_shot_with_output_is_regenerated(api, studio, script_text, make_image_png): """反向:人工审核不合格的镜头即使有产物也必须重出,不能被「已有产物」挡住。""" pid = _import_two_shots(api, studio, script_text, make_image_png) api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": True}) api.post(f"/api/projects/{pid}/dispatch", json={"limit": 10}) api.drain(pid) shots = api.get(f"/api/projects/{pid}/shots")["shots"] sid = shots[0]["id"] api.post(f"/api/projects/{pid}/shots/{sid}/reject", json={"notes": "人物穿帮", "regenerate": False}) plan = api.post(f"/api/projects/{pid}/tasklist", json={})["plan"] todo = {row["shot_id"]: row for row in plan["shots"]} assert todo[sid]["action"] == "enqueue", todo[sid] assert plan["queued_count"] == 1 and plan["skipped_count"] == 1, plan["shots"]