"""镜头「审片预览」平台能力 + 模型配置两处静默坑位的回归测试。 对应验收中发现的两个问题: 1. 音画同出的模型(MiniMax H3)自带一条即兴音轨,评审在分镜/审核页听到的 是模型原声而不是平台配音,被误判成「配音错了」; 现在默认口径已反过来:有台词的镜头优先用模型原声,TTS 只在显式 dub 或 模型不会出声时兜底,所以本文件的配音用例都先 `_dub_mode()` 显式切换; 2. PATCH /api/providers/{id} 传部分 config 会把整份配置覆盖掉; config.megapixels 调小后画面静默降档,预检只比对声明档位所以不报警。 """ from __future__ import annotations import subprocess from pathlib import Path import pytest from app.services import ffmpeg as ff, storage from tests.test_delivery import _make_wav def _make_video_with_audio(path: Path, *, seconds: float = 2.0, freq: float = 180.0, size: str = "320x180", audio: bool = True) -> Path: """造一条真能解码、且带模型原声的镜头产物(模拟音画同出模型)。""" if not ff.available(): pytest.skip("本机缺少 ffmpeg,无法验证审片预览") path.parent.mkdir(parents=True, exist_ok=True) cmd = [ff.FFMPEG, "-v", "error", "-y", "-f", "lavfi", "-i", f"testsrc=size={size}:rate=24:duration={seconds}", "-f", "lavfi", "-i", f"sine=frequency={freq}:duration={seconds}", "-t", f"{seconds}", "-c:v", "libx264", "-preset", "veryfast", "-pix_fmt", "yuv420p"] cmd += ["-c:a", "aac", "-b:a", "96k", "-shortest"] if audio else ["-an"] cmd += [str(path)] subprocess.run(cmd, check=True, capture_output=True) return path @pytest.fixture def shot_with_output(api, studio): """一个「有模型产物(自带原声)+ 有平台配音」的镜头。""" pid = studio["project_id"] sid = api.post(f"/api/projects/{pid}/shots", json={"title": "雾大", "description": "浓雾渡口,老船夫按住船舷", "mode": "i2v", "duration": 2, "dialogue": "雾大,站稳再下船。"})["result"]["id"] shot = api.get(f"/api/projects/{pid}/shots/{sid}")["shot"] out = storage.project_dir(pid, "generated") / f"shot-native-{sid}.mp4" _make_video_with_audio(out) voice = storage.project_dir(pid, "voiceover") / f"shot-{sid}.wav" voice.write_bytes(_make_wav(1.2, 520.0)) return {"pid": pid, "sid": sid, "out": out, "voice": voice, "code": shot["code"]} def _attach(api, db, ctx): from app.models import Shot shot = db.get(Shot, ctx["sid"]) shot.output_path = storage.to_relative(ctx["out"]) shot.voiceover_path = storage.to_relative(ctx["voice"]) shot.voiceover_duration_ms = 1200 db.commit() def _dub_mode(api, ctx): """把镜头切到「平台配音」口径。 出厂默认已经改成模型原声优先(voice_source=native,原生音画同步的产物 不再被 TTS 盖掉),而这几条用例验证的正是「换嗓子」那条路:必须显式 dub, 否则测的是新默认而不是配音替换。 """ api.post(f"/api/projects/{ctx['pid']}/timeline", json={"only_approved": False, "settings": {"voice_source": "dub"}}) # ---------------------------------------------------------------- 审片预览 def test_preview_replaces_model_audio_with_platform_voiceover(api, db, shot_with_output): ctx = shot_with_output _attach(api, db, ctx) _dub_mode(api, ctx) before = api.get(f"/api/projects/{ctx['pid']}/shots/{ctx['sid']}")["shot"] assert before["native_audio"] is True and before["voiceover_ready"] is True assert before["preview_ready"] is False and before["preview_url"] == "" built = api.post(f"/api/projects/{ctx['pid']}/shots/{ctx['sid']}/preview", json={"force": False, "actor": "acceptance"}) assert built["ok"] is True and built["preview"]["reused"] is False assert built["preview"]["mode"] == "voiceover" preview_path = storage.to_absolute(built["preview"]["path"]) assert preview_path.exists() probed = ff.probe(preview_path) video = ff.probe(shot_with_output["out"]) assert probed.has_audio is True # 画面直接 copy:不重编码、不降档,尺寸与模型产物完全一致 assert (probed.width, probed.height) == (video.width, video.height) assert probed.codec == video.codec # 音轨与画面等长(尾部补静音),入点留白不会把画面截短 assert abs(probed.duration - video.duration) < 0.25 after = api.get(f"/api/projects/{ctx['pid']}/shots/{ctx['sid']}")["shot"] assert after["preview_ready"] is True and after["preview_url"].startswith("/media/") assert after["preview_url"].endswith(".mp4") # 分镜看板同样带出预览,列表页不用逐条再点详情 board = api.get(f"/api/projects/{ctx['pid']}/shots")["shots"] row = next(r for r in board if r["id"] == ctx["sid"]) assert row["preview_ready"] is True and row["preview_url"] == after["preview_url"] # 预览产物登记为镜头资产,走统一媒体抽象 asset = next(a for a in after["assets"] if a["kind"] == "preview_video") assert asset["path"] == built["preview"]["path"] def test_preview_is_idempotent_and_rebuilt_when_inputs_change(api, db, shot_with_output): ctx = shot_with_output _attach(api, db, ctx) _dub_mode(api, ctx) url = f"/api/projects/{ctx['pid']}/shots/{ctx['sid']}/preview" first = api.post(url, json={})["preview"] again = api.post(url, json={})["preview"] assert again["reused"] is True and again["path"] == first["path"] # 换配音 → 指纹变化,必须重新封装(否则改台词后听到的还是旧声音) ctx["voice"].write_bytes(_make_wav(1.6, 660.0)) rebuilt = api.post(url, json={})["preview"] assert rebuilt["reused"] is False and rebuilt["path"] != first["path"] forced = api.post(url, json={"force": True})["preview"] assert forced["reused"] is False batch = api.post(f"/api/projects/{ctx['pid']}/previews", json={}) assert batch["project_id"] == ctx["pid"] and batch["result"]["reused"] >= 1 def test_preview_falls_back_when_shot_has_no_voiceover(api, db, shot_with_output): ctx = shot_with_output _attach(api, db, ctx) _dub_mode(api, ctx) from app.models import Shot shot = db.get(Shot, ctx["sid"]) shot.voiceover_path = "" db.commit() result = api.post(f"/api/projects/{ctx['pid']}/shots/{ctx['sid']}/preview", json={}) assert result["ok"] is True and result["preview"]["mode"] == "native" detail = api.get(f"/api/projects/{ctx['pid']}/shots/{ctx['sid']}")["shot"] assert detail["preview_ready"] is False and detail["voiceover_ready"] is False def test_preview_without_output_is_a_clear_400(api, studio): pid = studio["project_id"] sid = api.post(f"/api/projects/{pid}/shots", json={"title": "还没生成", "description": "等待生成的镜头", "mode": "i2v", "duration": 2})["result"]["id"] body = api.call("POST", f"/api/projects/{pid}/shots/{sid}/preview", json={}, expect=400) assert "生成视频" in body["detail"] # ---------------------------------------------------------------- 配置覆盖坑 def test_provider_config_patch_merges_by_default(api): created = api.post("/api/providers", json={"provider": "localtest", "model": "cfg-merge", "media_type": "video", "adapter": "mock", "config": {"base_url": "http://127.0.0.1:9199", "workflow": "minimax_h3", "megapixels": 0.4}})["id"] api.patch(f"/api/providers/{created}", json={"config": {"megapixels": 0.98}}) config = api.get("/api/providers")["providers"] spec = next(p for p in config if p["id"] == created) assert spec["config"]["megapixels"] == pytest.approx(0.98) # 关键断言:只传一个键不得清空 base_url / workflow(旧实现会,且仍返回 200) assert spec["config"]["base_url"] == "http://127.0.0.1:9199" assert spec["config"]["workflow"] == "minimax_h3" api.patch(f"/api/providers/{created}", json={"config": {"only_key": 1}, "replace_config": True}) spec = next(p for p in api.get("/api/providers")["providers"] if p["id"] == created) assert spec["config"] == {"only_key": 1} def test_probe_reports_effective_canvas(api): created = api.post("/api/providers", json={"provider": "localtest", "model": "cfg-canvas", "media_type": "video", "adapter": "comfyui", "config": {"base_url": "http://127.0.0.1:1", "workflow": "minimax_h3", "megapixels": 0.4}})["id"] probe = api.get(f"/api/providers/{created}/probe") assert probe["canvas"]["16:9"] == [832, 480] # 声明 720p,实际只出 480p api.patch(f"/api/providers/{created}", json={"config": {"megapixels": 0.98}}) assert api.get(f"/api/providers/{created}/probe")["canvas"]["16:9"] == [1312, 736] def test_preflight_warns_when_effective_canvas_is_silently_downgraded(api, studio): pid = studio["project_id"] spec = api.post("/api/providers", json={"provider": "localtest", "model": "cfg-low", "media_type": "video", "adapter": "comfyui", "resolutions": ["480p", "720p"], "config": {"base_url": "http://127.0.0.1:1", "workflow": "minimax_h3", "megapixels": 0.4}})["id"] api.patch(f"/api/projects/{pid}", json={"resolution": "720p", "default_provider": "localtest", "default_model": "cfg-low"}) api.post(f"/api/projects/{pid}/shots", json={"title": "低像素", "description": "浓雾里的渡口灯火", "mode": "t2v", "duration": 6}) codes = {i["code"]: i for c in api.get(f"/api/projects/{pid}/preflight")["preflight"]["checks"] for i in c["issues"]} assert "resolution_effective_lower" in codes, codes assert "832x480" in codes["resolution_effective_lower"]["message"] # 把像素数调回真实 720p 档 → 警告消失(档位声明没变,只有配置变) api.patch(f"/api/providers/{spec}", json={"config": {"megapixels": 0.98}}) codes = {i["code"] for c in api.get(f"/api/projects/{pid}/preflight")["preflight"]["checks"] for i in c["issues"]} assert "resolution_effective_lower" not in codes