282 lines
14 KiB
Python
282 lines
14 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from tests.conftest import _make_image_png
|
|
from tests.test_delivery import _make_wav
|
|
|
|
|
|
def test_platform_assets_upload_import_publish_and_generate(api):
|
|
project_id = api.project()
|
|
character = api.post("/api/platform/characters", json={
|
|
"name": "林澈", "project_id": project_id, "role": "主角", "appearance": "银色短发,深蓝机甲",
|
|
"persona_prompt": "冷静果决的领航员", "style_version": "v3",
|
|
})["result"]
|
|
# 平台母版创建时始终全局;请求里误带 project_id 也不能把母版绑定到项目。
|
|
assert character["project_id"] is None
|
|
assert character["project_name"] == ""
|
|
voice = api.post("/api/platform/voices", json={
|
|
"name": "低沉男声", "provider": "mock", "provider_voice_id": "v-low",
|
|
"gender": "male", "version": "v2",
|
|
})["result"]
|
|
api.patch(f"/api/platform/characters/{character['id']}", json={"default_voice_id": voice["id"]})
|
|
scene = api.post("/api/platform/scenes", json={
|
|
"name": "星舰舰桥", "prompt": "环形舰桥,全息星图", "lighting": "冷蓝",
|
|
})["result"]
|
|
|
|
uploaded = api.post(
|
|
f"/api/platform/characters/{character['id']}/media",
|
|
files={"file": ("portrait.png", _make_image_png(320, 320, "0x224466"), "image/png")},
|
|
data={"kind": "portrait", "name": "林澈定妆图"},
|
|
)["result"]
|
|
assert uploaded["url"].startswith("/media/platform/")
|
|
|
|
generated = api.generate(
|
|
f"/api/platform/{'scene'}/{scene['id']}/generate",
|
|
{"kind": "scene_image", "provider": "mock", "model": "mock-image-1",
|
|
"prompt": "冷蓝全息舰桥", "width": 320, "height": 180},
|
|
)
|
|
assert generated["metadata"]["generated"] is True
|
|
|
|
imported = api.post("/api/platform/import", json={
|
|
"project_id": project_id, "asset_type": "human", "asset_id": character["id"],
|
|
})["result"]
|
|
project = api.get(f"/api/projects/{project_id}")["result"]
|
|
local = next(row for row in project["characters"] if row["id"] == imported["id"])
|
|
assert local["platform_source"]["id"] == character["id"]
|
|
assert local["platform_source"]["version"] == "v3"
|
|
assert local["default_voice_id"]
|
|
assert local["assets"] and local["assets"][0]["metadata"]["platform_media_id"] == uploaded["id"]
|
|
|
|
scene_imported = api.post("/api/platform/import", json={
|
|
"project_id": project_id, "asset_type": "scene", "asset_id": scene["id"],
|
|
})["result"]
|
|
assert scene_imported["id"] in {row["id"] for row in api.get(f"/api/projects/{project_id}")["result"]["scenes"]}
|
|
|
|
local_new = api.post(f"/api/projects/{project_id}/characters", json={
|
|
"name": "项目独有角色", "appearance": "红色风衣",
|
|
})["result"]
|
|
published = api.post("/api/platform/publish", json={
|
|
"project_id": project_id, "asset_type": "human", "asset_id": local_new["id"],
|
|
})["result"]
|
|
platform = api.get("/api/platform/assets")
|
|
published_row = next(row for row in platform["human"] if row["id"] == published["id"])
|
|
assert published_row["project_id"] is None
|
|
|
|
assist = api.post(f"/api/platform/characters/{character['id']}/text-assist", json={
|
|
"instruction": "强调面部辨识度",
|
|
})["result"]
|
|
assert assist["provider"] == "mock" and len(assist["text"]) > 20
|
|
|
|
|
|
def test_platform_media_can_be_deleted_individually(api):
|
|
project_id = api.project()
|
|
character = api.post("/api/platform/characters", json={
|
|
"name": "单媒体删除", "project_id": project_id,
|
|
})["result"]
|
|
first = api.post(
|
|
f"/api/platform/characters/{character['id']}/media",
|
|
files={"file": ("first.png", _make_image_png(180, 180, "0x335577"), "image/png")},
|
|
data={"kind": "portrait", "name": "第一张"},
|
|
)["result"]
|
|
second = api.post(
|
|
f"/api/platform/characters/{character['id']}/media",
|
|
files={"file": ("second.png", _make_image_png(180, 180, "0x557733"), "image/png")},
|
|
data={"kind": "expression", "name": "第二张"},
|
|
)["result"]
|
|
|
|
from app.services import storage
|
|
|
|
second_path = storage.to_absolute(second["path"])
|
|
assert second_path.exists()
|
|
deleted = api.delete(f"/api/platform/media/{second['id']}")
|
|
assert deleted["deleted"] == second["id"]
|
|
assert deleted["files_removed"] >= 1
|
|
assert not second_path.exists()
|
|
rows = api.get("/api/platform/assets")["human"]
|
|
assets = next(row["assets"] for row in rows if row["id"] == character["id"])
|
|
assert [item["id"] for item in assets] == [first["id"]]
|
|
|
|
|
|
def test_deleting_voice_reference_clears_stale_binding(api, db):
|
|
from app.models import PlatformVoice
|
|
|
|
voice = api.post("/api/platform/voices", json={"name": "临时参考声线"})["result"]
|
|
reference = api.post(
|
|
f"/api/platform/voice/{voice['id']}/media",
|
|
files={"file": ("reference.wav", _make_wav(0.4), "audio/wav")},
|
|
data={"kind": "reference", "name": "参考音色"},
|
|
)["result"]
|
|
row = db.get(PlatformVoice, voice["id"])
|
|
row.provider_voice_id = reference["path"]
|
|
db.commit()
|
|
|
|
api.delete(f"/api/platform/media/{reference['id']}")
|
|
|
|
db.expire_all()
|
|
assert db.get(PlatformVoice, voice["id"]).provider_voice_id == ""
|
|
|
|
|
|
def test_platform_image_generation_accepts_owned_reference_media(api):
|
|
character = api.post("/api/platform/characters", json={
|
|
"name": "参考图角色", "appearance": "银发,黑色战术服",
|
|
})["result"]
|
|
reference = api.post(
|
|
f"/api/platform/characters/{character['id']}/media",
|
|
files={"file": ("reference.png", _make_image_png(320, 320, "0x335577"), "image/png")},
|
|
data={"kind": "reference", "name": "参考定妆"},
|
|
)["result"]
|
|
generated = api.generate(
|
|
f"/api/platform/human/{character['id']}/generate",
|
|
{"kind": "portrait", "provider": "mock", "model": "mock-image-1",
|
|
"prompt": "保持参考图中的五官与服装", "width": 320, "height": 320,
|
|
"reference_media_ids": [reference["id"]]},
|
|
)
|
|
|
|
assert generated["metadata"]["reference_media_ids"] == [reference["id"]]
|
|
|
|
|
|
def test_platform_voice_generation_uses_uploaded_reference(api, db, monkeypatch):
|
|
from app.api import platform as platform_api
|
|
from app.models import PlatformVoice, VoiceAsset
|
|
from app.providers.mock import MockVoiceProvider
|
|
|
|
project_id = api.project()
|
|
voice = api.post("/api/platform/voices", json={
|
|
"name": "IndexTTS 参考音色", "gender": "female", "version": "v1",
|
|
})["result"]
|
|
assert voice["provider"] == ""
|
|
|
|
reference = api.post(
|
|
f"/api/platform/voice/{voice['id']}/media",
|
|
files={"file": ("reference.wav", _make_wav(0.4), "audio/wav")},
|
|
data={"kind": "reference", "name": "参考音色"},
|
|
)["result"]
|
|
api.post("/api/providers", json={
|
|
"provider": "comfyui", "model": "indextts-test", "display_name": "IndexTTS2 测试",
|
|
"media_type": "tts", "adapter": "comfyui_tts", "enabled": True,
|
|
"modes": ["voice", "voice_clone"], "config": {"reference_audio": ""},
|
|
})["id"]
|
|
|
|
captured: dict = {}
|
|
|
|
def fake_generate(_db, request, provider, model):
|
|
captured.update({"request": request, "provider": provider, "model": model})
|
|
return MockVoiceProvider().synthesize(request)
|
|
|
|
monkeypatch.setattr(platform_api.model_registry, "is_available", lambda *_args, **_kwargs: True)
|
|
monkeypatch.setattr(platform_api.model_registry, "generate_voice", fake_generate)
|
|
generated = api.generate(
|
|
f"/api/platform/voice/{voice['id']}/generate",
|
|
{"kind": "voice_sample", "provider": "comfyui", "model": "indextts-test",
|
|
"sample_text": "参考音色已经接通。", "reference_media_id": reference["id"]},
|
|
)
|
|
assert generated["kind"] == "voice_sample"
|
|
assert captured["request"].provider_voice_id.endswith(reference["path"])
|
|
assert Path(captured["request"].provider_voice_id).is_absolute()
|
|
|
|
db.expire_all()
|
|
saved = db.get(PlatformVoice, voice["id"])
|
|
assert saved.provider == "comfyui"
|
|
assert saved.provider_voice_id == reference["path"]
|
|
|
|
imported = api.post("/api/platform/import", json={
|
|
"project_id": project_id, "asset_type": "voice", "asset_id": voice["id"],
|
|
})["result"]
|
|
db.expire_all()
|
|
local = db.get(VoiceAsset, imported["id"])
|
|
assert local.provider == "comfyui"
|
|
assert local.provider_voice_id.startswith(f"{project_id}/reference/")
|
|
|
|
|
|
def test_publish_is_idempotent_for_same_name_master(api):
|
|
"""反向上架同名母版时复用既有行并升版,不再产生重名母版。"""
|
|
project_id = api.project()
|
|
first = api.post(f"/api/projects/{project_id}/characters", json={
|
|
"name": "重复上架角色", "appearance": "灰蓝工装", "role": "主角", "style_version": "v1",
|
|
})["result"]
|
|
api.post(
|
|
f"/api/projects/{project_id}/characters/{first['id']}/assets",
|
|
files={"file": ("kf.png", _make_image_png(240, 320, "0x113355"), "image/png")},
|
|
data={"kind": "reference", "name": "参考图"},
|
|
)
|
|
|
|
a = api.post("/api/platform/publish", json={
|
|
"project_id": project_id, "asset_type": "human", "asset_id": first["id"],
|
|
})["result"]
|
|
assert a["reused"] is False
|
|
|
|
def _masters():
|
|
return [r for r in api.get("/api/platform/assets")["human"] if r["name"] == "重复上架角色"]
|
|
|
|
assert len(_masters()) == 1
|
|
|
|
# 再次上架同一角色 → 命中同名母版,复用同一行并让素材升版,而不是新建母版
|
|
b = api.post("/api/platform/publish", json={
|
|
"project_id": project_id, "asset_type": "human", "asset_id": first["id"],
|
|
})["result"]
|
|
assert b["id"] == a["id"] and b["reused"] is True
|
|
assert b["media_version"] == 2 and len(_masters()) == 1
|
|
|
|
# 显式要求新建时才允许出现第二份
|
|
c = api.post("/api/platform/publish", json={
|
|
"project_id": project_id, "asset_type": "human", "asset_id": first["id"], "reuse": False,
|
|
})["result"]
|
|
assert c["id"] != a["id"] and c["reused"] is False and len(_masters()) == 2
|
|
|
|
|
|
def test_project_creation_can_import_library_masters(api):
|
|
"""新建项目时就能勾选资源库母版。
|
|
|
|
以前必须先建项目、再跳到资源库页面逐条「引入到项目」,新项目的第一镜总是
|
|
零素材起步(没有定妆图、没有场景母图、没有音色样本)。现在建项目请求带上
|
|
platform_assets 一起提交,并且:人物要连默认声线一起落地并重映射到项目内、
|
|
同一个声线被引用两次不能变成两条、坏引用只降级不阻断建项目。
|
|
"""
|
|
voice = api.post("/api/platform/voices", json={
|
|
"name": "清亮女声", "provider": "mock", "provider_voice_id": "v-female",
|
|
"gender": "female"})["result"]
|
|
character = api.post("/api/platform/characters", json={
|
|
"name": "紫小慧", "role": "主角", "appearance": "齐肩短发,白色连衣裙"})["result"]
|
|
api.patch(f"/api/platform/characters/{character['id']}", json={"default_voice_id": voice["id"]})
|
|
api.post(f"/api/platform/characters/{character['id']}/media",
|
|
files={"file": ("portrait.png", _make_image_png(320, 320, "0x224466"), "image/png")},
|
|
data={"kind": "portrait", "name": "紫小慧定妆图"})
|
|
scene = api.post("/api/platform/scenes", json={
|
|
"name": "海边别墅", "description": "前卫建筑,光线充足"})["result"]
|
|
|
|
created = api.post("/api/projects", json={
|
|
"name": "带资源起步", "resolution": "480p", "fps": 24, "default_provider": "mock",
|
|
"platform_assets": [
|
|
{"owner_type": "human", "asset_id": character["id"]},
|
|
{"owner_type": "scene", "asset_id": scene["id"]},
|
|
{"owner_type": "voice", "asset_id": voice["id"]}, # 与人物默认声线同源:只能有一条
|
|
{"owner_type": "voice", "asset_id": 987654}, # 不存在的母版
|
|
]})["result"]
|
|
|
|
imported = {(x["asset_type"], x["platform_source_id"]): x for x in created["imported_assets"]}
|
|
assert set(imported) == {("human", character["id"]), ("scene", scene["id"]),
|
|
("voice", voice["id"])}
|
|
assert imported[("human", character["id"])]["media_count"] == 1
|
|
assert [f["asset_id"] for f in created["import_failures"]] == [987654]
|
|
assert created["import_failures"][0]["detail"] == "平台资产不存在"
|
|
|
|
project = api.get(f"/api/projects/{created['id']}")["result"]
|
|
assert len(project["characters"]) == 1 and len(project["scenes"]) == 1
|
|
assert len(project["voices"]) == 1, "同一母版的声线被引用两次不该复制成两条"
|
|
local = project["characters"][0]
|
|
assert local["default_voice_id"] == project["voices"][0]["id"], "默认声线必须重映射到项目内的副本"
|
|
assert local["assets"] and local["assets"][0]["url"].startswith("/media/"), "定妆图要跟着人物进来"
|
|
assert local["platform_source"]["id"] == character["id"]
|
|
# 引入的是副本:母版改名不能回头改写已有项目
|
|
api.patch(f"/api/platform/characters/{character['id']}", json={"name": "紫小慧(改名)"})
|
|
assert api.get(f"/api/projects/{created['id']}")["result"]["characters"][0]["name"] == "紫小慧"
|
|
|
|
|
|
def test_project_creation_without_assets_is_unchanged(api):
|
|
"""没勾资源库时建项目行为不变:不引入、不报错、不带空键。"""
|
|
created = api.post("/api/projects", json={"name": "纯净起步"})["result"]
|
|
assert created["imported_assets"] == [] and created["import_failures"] == []
|
|
project = api.get(f"/api/projects/{created['id']}")["result"]
|
|
assert not project["characters"] and not project["scenes"] and not project["voices"]
|