cmvr-visual/backend/tests/test_editor_names.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

156 lines
6.0 KiB
Python

import pytest
from copy import deepcopy
from sqlalchemy import select
from app.models import WorkflowDraft, WorkflowRun, Submission
async def create_editor(c):
d = (
await c.post(
"/api/stories",
json={
"name": "未命名故事",
"spec": {
"schema_version": 3,
"shots": [{"id": "1" * 32, "name": "冻结镜头名", "visual_prompt": "tree"}],
},
},
)
).json()
enabled = await c.post(
f"/api/editors/{d['id']}/mode",
json={"expected_version": 1, "mode": "auto"},
headers={"Idempotency-Key": "enable"},
)
assert enabled.status_code == 200, enabled.text
assert enabled.json()["capabilities"]["name_edit"] is True
assert enabled.json()["story"]["capabilities"]["name_edit"] is True
return enabled.json()
async def test_editor_name_manifest_atomic_cas_replay_and_run_frozen(system, other_client):
c = system.client
editor = await create_editor(c)
id = editor["draft_id"]
url = f"/api/editors/{id}"
m = deepcopy(editor["manifest"])
plan_body = {"expected_version": 2, "output_goal": "media", "targets": ["frame:" + ("1" * 32)]}
plan = await c.post(url + "/plan", json=plan_body)
assert plan.status_code == 200, plan.text
run = await c.post(
url + "/runs",
json={**plan_body, "expected_plan_hash": plan.json()["plan_hash"]},
headers={"Idempotency-Key": "run"},
)
assert run.status_code == 202, run.text
rid = run.json()["run"]["id"]
async with system.app.state.db.session() as s:
r = await s.get(WorkflowRun, rid)
frozen_spec = deepcopy(r.spec_snapshot)
frozen_plan = deepcopy(r.plan_snapshot)
frozen_hash = r.spec_hash
m["spec"]["shots"][0]["name"] = "编辑后的镜头名"
body = {"expected_version": 2, "manifest": m, "name": " 新故事名字 "}
saved = await c.post(url + "/save", json=body, headers={"Idempotency-Key": "rename"})
assert saved.status_code == 200, saved.text
assert (
saved.json()["name"] == "新故事名字"
and saved.json()["version"] == 3
and saved.json()["revision"] == 3
)
assert saved.json()["manifest"]["spec"]["shots"][0]["name"] == "编辑后的镜头名"
assert (
await c.post(url + "/save", json=body, headers={"Idempotency-Key": "rename"})
).json() == saved.json()
conflicting_key = await c.post(
url + "/save", json={**body, "name": "另一个名字"}, headers={"Idempotency-Key": "rename"}
)
assert conflicting_key.status_code == 409
assert (
await c.post(url + "/save", json=body, headers={"Idempotency-Key": "stale"})
).status_code == 409
assert (
await other_client.post(
url + "/save",
json={**body, "expected_version": 3},
headers={"Idempotency-Key": "foreign"},
)
).status_code == 404
async with system.app.state.db.session() as s:
r = await s.get(WorkflowRun, rid)
assert (
r.spec_snapshot == frozen_spec
and r.plan_snapshot == frozen_plan
and r.spec_hash == frozen_hash
)
assert r.spec_snapshot["shots"][0]["name"] == "冻结镜头名"
r.state = "succeeded"
from app.services.editors import apply_run_result
assert not await apply_run_result(s, r, system.settings)
d = await s.get(WorkflowDraft, id)
assert d.name == "新故事名字" and d.version == 3
@pytest.mark.parametrize("name", ["", " ", "x" * 256])
async def test_editor_name_invalid_is_atomic(system, name):
editor = await create_editor(system.client)
id = editor["draft_id"]
url = f"/api/editors/{id}"
m = deepcopy(editor["manifest"])
m["spec"]["shots"][0]["name"] = "must not save"
response = await system.client.post(
url + "/save",
json={"expected_version": 2, "manifest": m, "name": name},
headers={"Idempotency-Key": "invalid"},
)
assert response.status_code == 422, response.text
unchanged = (await system.client.get(url)).json()
assert unchanged["name"] == "未命名故事" and unchanged["version"] == 2
assert unchanged["manifest"] == editor["manifest"]
async def test_editor_name_omission_null_keeps_legacy_fingerprint(system):
c = system.client
editor = await create_editor(c)
id = editor["draft_id"]
url = f"/api/editors/{id}"
body = {"expected_version": 2, "manifest": editor["manifest"]}
saved = await c.post(url + "/save", json=body, headers={"Idempotency-Key": "legacy"})
assert saved.status_code == 200, saved.text
assert saved.json()["name"] == "未命名故事"
replay = await c.post(
url + "/save", json={**body, "name": None}, headers={"Idempotency-Key": "legacy"}
)
assert replay.status_code == 200, replay.text
assert replay.json()["version"] == 3
async with system.app.state.db.session() as s:
sub = await s.scalar(select(Submission).where(Submission.operation == "editor.save"))
assert "name" not in sub.request_json
async def test_editor_name_rolls_back_when_manifest_validation_fails(system):
c = system.client
editor = await create_editor(c)
id = editor["draft_id"]
url = f"/api/editors/{id}"
broken = deepcopy(editor["manifest"])
broken["spec"]["shots"][0]["id"] = "2" * 32
response = await c.post(
url + "/save",
json={"expected_version": 2, "manifest": broken, "name": "must rollback"},
headers={"Idempotency-Key": "invalid-manifest"},
)
assert response.status_code == 422, response.text
unchanged = (await c.get(url)).json()
assert unchanged["name"] == "未命名故事" and unchanged["version"] == 2
async with system.app.state.db.session() as s:
assert (
await s.scalar(
select(Submission).where(
Submission.operation == "editor.save", Submission.key == "invalid-manifest"
)
)
is None
)