cmvr-visual/backend/tests/test_continuity_projection.py

145 lines
5.6 KiB
Python
Raw Normal View History

from app.domain.continuity import upgrade_story_spec, compile_story_timing
from app.domain.stories import parse_story_spec
def test_upgrade_preserves_legacy_shape_and_trim():
raw = {"shots": [{"id": "1" * 32}, {"id": "2" * 32}]}
old = parse_story_spec(raw).model_dump(mode="json")
upgraded = upgrade_story_spec(old)
assert "continuity" not in old["shots"][0]
upgraded["shots"][1]["continuity"]["transition"] = "continue"
upgraded["shots"][1]["edit"]["dedup_frames"] = 1
upgraded["render"]["total_frames"] = None
timing = compile_story_timing(upgraded)
assert timing["total_frames"] == 239
assert timing["shots"][1]["source_end_frame"] == 120
def test_thousand_shot_upgrade_and_role_capacity():
from app.schemas.editors import SaveEditor, parse_editor_manifest
from app.domain.editors import validate_manifest, collect_manifest_asset_ids
ids = [format(i, "032x") for i in range(1000)]
spec = upgrade_story_spec({"shots": [{"id": sid} for sid in ids]})
binding = {
"source": "manual",
"locked": True,
"kind": "image",
"asset_id": "target",
"sha256": "a" * 64,
}
m = dict(
schema_version=2,
source_kind="story",
spec=spec,
shot_ids=ids,
bindings={
f"{role}:{sid}": binding
for role in ["frame", "end_frame", "join_anchor"]
for sid in ids
},
)
parsed = SaveEditor(expected_version=1, manifest=m)
saved = validate_manifest(parsed.manifest)
assert parse_editor_manifest(saved).schema_version == 2
assert collect_manifest_asset_ids(saved) == {"target"}
assert saved["bindings"]["join_anchor:" + ids[-1]]["locked"]
def test_versions_hashes_native_lengths_and_continue_edges():
import pytest
from app.domain.stories import compile_story
from app.schemas.editors import parse_editor_manifest
shot = {"id": "a" * 32, "visual_prompt": "daylight"}
old = parse_story_spec({"shots": [shot]})
new = upgrade_story_spec(old)
assert (
compile_story(old, {})["shots"][0]["context_hash"]
!= compile_story(new, {})["shots"][0]["context_hash"]
)
new["shots"][0]["video_parameters"] = {"profile": "minimax-h3-fl2va-bf16-v2"}
assert parse_story_spec(new).shots[0].video_parameters.source_frames == 124
new["shots"][0]["video_parameters"]["source_frames"] = 128
with pytest.raises(ValueError):
parse_story_spec(new)
new["shots"][0].pop("video_parameters")
new["shots"][0]["continuity"]["transition"] = "continue"
with pytest.raises(ValueError):
parse_story_spec(new)
for parse in (parse_story_spec, parse_editor_manifest):
with pytest.raises(ValueError):
parse({"schema_version": 999})
async def test_v4_author_intent_survives_mode_roundtrip(system):
client = system.client
spec = upgrade_story_spec({"shots": [{"id": "3" * 32, "visual_prompt": "Station"}]})
shot = spec["shots"][0]
shot["continuity"]["end_prompt"] = "Train stops"
shot["video_parameters"] = {"profile": "minimax-h3-fl2va-bf16-v2", "source_frames": 141}
response = await client.post("/api/stories", json={"name": "V4 roundtrip", "spec": spec})
assert response.status_code in (200, 201), response.text
draft = response.json()
url = "/api/editors/" + draft["id"] + "/mode"
for version, mode in enumerate(("auto", "manual", "auto"), start=1):
response = await client.post(
url,
json={"expected_version": version, "mode": mode},
headers={"Idempotency-Key": "v4-mode-" + str(version)},
)
assert response.status_code == 200, response.text
manifest = response.json()["manifest"]
assert manifest["schema_version"] == 2
assert manifest["spec"] == draft["spec"]
import pytest
@pytest.mark.parametrize("change", ["trim", "replace", "clear", "same"])
async def test_manual_anchor_follows_video_and_trim(monkeypatch, change):
from copy import deepcopy
from types import SimpleNamespace
from app.domain.editors import validate_manifest, collect_manifest_asset_ids
from app.services.editor_modes import sync_manual_patch, sync_manual_selection
sid = "3" * 32
spec = upgrade_story_spec({"shots": [{"id": sid}]})
video = dict(asset_id="video", sha256="a" * 64, kind="video", source="manual")
anchor = dict(asset_id="anchor", sha256="b" * 64, kind="image", source="generated")
m = validate_manifest(
dict(
schema_version=2,
editing_mode="manual",
source_kind="story",
spec=spec,
shot_ids=[sid],
bindings={"video:" + sid: video, "join_anchor:" + sid: anchor},
)
)
draft = SimpleNamespace(editor_manifest=m, spec=spec)
refs = set()
async def sync_refs(session, draft, updated):
refs.update(collect_manifest_asset_ids(updated))
monkeypatch.setattr("app.services.editors.sync_editor_refs", sync_refs)
if change == "trim":
revised = deepcopy(spec)
revised["shots"][0]["edit"]["duration_frames"] = 96
revised["render"]["total_frames"] = 96
await sync_manual_patch(None, draft, revised)
else:
asset = SimpleNamespace(
id="video" if change == "same" else "replacement", sha256="a" * 64, kind="video"
)
await sync_manual_selection(
None,
draft,
SimpleNamespace(kind="video", shot_id=sid),
None if change == "clear" else asset,
)
assert ("join_anchor:" + sid in draft.editor_manifest["bindings"]) == (change == "same")
assert ("anchor" in refs) == (change == "same")