from copy import deepcopy from sqlalchemy import select from app.models import Asset, WorkflowDraft, WorkflowRun, WorkflowStep from app.services.dialogue import apply_pending_result from test_dialogue_workflow import asset, wav from test_native_dialogue_workflow import capabilities, native_story, _drive, _start async def test_remix_with_missing_source_is_non_executable(system): did, draft = await native_story(system) url = f"/api/editors/{did}" body = dict(expected_version=draft["version"], output_goal="mix_film") response = await system.client.post(url + "/plan", json=body) assert response.status_code == 200, response.text plan = response.json() assert not plan["steps"] and any( i["unit_id"] == draft["manifest"]["shot_ids"][0] for i in plan["issues"] ) rejected = await system.client.post( url + "/runs", json={**body, "expected_plan_hash": plan["plan_hash"]}, headers={"Idempotency-Key": "missing-source-remix"}, ) assert rejected.status_code == 422 assert not system.provider.submitted_payloads async def test_mix_revision_recovers_without_prior_mix_and_uses_new_gain_offline(system, tmp_path): capabilities(system) did, draft = await native_story(system) rid = await _start(system, did, draft["version"]) _, _, state, error, _ = await _drive(system, rid, tmp_path) assert state == "succeeded", error await apply_pending_result(system.app.state.db, rid, system.settings) url = f"/api/editors/{did}" old_srt = (await system.client.get(f"{url}/runs/{rid}/captions.srt")).text original_film = (await system.client.get(f"/api/stories/{did}/film")).json()["asset"]["id"] async with system.app.state.db.session() as s: owner = (await s.get(WorkflowDraft, did)).owner_id # 老混音回执缺失,但已选的镜头、净音轨和原成片仍在。 old = await s.scalar( select(WorkflowStep).where( WorkflowStep.run_id == rid, WorkflowStep.step_key == "dialogue_mix" ) ) old.result_json = {} bgm = await asset(s, system, owner, "audio", wav(24000), duration=0.5) aid = bgm.id await s.commit() async def offline(): raise AssertionError("重混音不应依赖推理服务") system.settings.native_dialogue_enabled = False system.provider.upstream_capabilities = offline editor = (await system.client.get(url)).json() m = deepcopy(editor["manifest"]) original_cues = deepcopy(m["captions"]) m["audio"]["layers"] = [ dict(id="f" * 32, asset_id=aid, anchor="absolute", role="bgm", gain_db=-15.5) ] saved = await system.client.post( url + "/save", json=dict(expected_version=editor["version"], manifest=m), headers={"Idempotency-Key": "gain"}, ) assert saved.status_code == 200, saved.text body = dict(expected_version=saved.json()["version"], output_goal="mix_film") plan = await system.client.post(url + "/plan", json=body) assert plan.status_code == 200, plan.text plan = plan.json() assert not plan["issues"] and {s["kind"] for s in plan["steps"]} == { "audio.normalize", "audio.mix", "captions.compile", "video.compose", }, plan caption_only = ( await system.client.post(url + "/plan", json={**body, "output_goal": "caption_film"}) ).json() assert not caption_only["steps"] and any( i["code"] == "caption_mix_unavailable" for i in caption_only["issues"] ) started = await system.client.post( url + "/runs", json={**body, "expected_plan_hash": plan["plan_hash"]}, headers={"Idempotency-Key": "only-remix"}, ) assert started.status_code == 202, started.text new = started.json()["run"]["id"] result, _, state, error, calls = await _drive(system, new, tmp_path) assert state == "succeeded", error assert calls == {"audio.normalize": 1, "audio.mix": 1, "video.compose": 1} assert result["captions"] == original_cues assert (await system.client.get(f"{url}/runs/{new}/captions.srt")).text == old_srt assert (await system.client.get(f"{url}/runs/{rid}/captions.srt")).text == old_srt async with system.app.state.db.session() as s: run = await s.get(WorkflowRun, new) mix = await s.scalar( select(WorkflowStep).where(WorkflowStep.run_id == new, WorkflowStep.kind == "audio.mix") ) (segment,) = [x for x in mix.resolved_input_json["input"]["segments"] if x["role"] == "bgm"] assert segment["gain_db"] == -15.5 assert mix.result_json["sample_count"] == run.prepared_plan["timing"]["total_frames"] * 2000 assert ( run.current_output_asset_id != original_film and (await s.get(Asset, original_film)).ready ) # 同一版可安全重混音的镜头分别移除净音轨、损坏已存时基,任何一种都必须停在计划。 from app.services.mix_revision import plan_remix async with system.app.state.db.session() as session: d = await session.get(WorkflowDraft, did) sid = m["shot_ids"][0] for problem in ("audio", "timing"): bad = deepcopy(m) if problem == "audio": del bad["bindings"]["native_audio:" + sid] else: bad["bindings"]["native_alignment:" + sid]["provenance"][ "audio_offset_samples" ] += 2000 blocked = await plan_remix(session, d, bad, body, system.settings) assert not blocked["steps"] and not blocked["_recipes"] expected = "mix_source_missing" if problem == "audio" else "mix_timing_invalid" assert any(i["code"] == expected and i["unit_id"] == sid for i in blocked["issues"])