258 lines
16 KiB
Python
258 lines
16 KiB
Python
"""资源库 v2:跨字段检索 / 筛选 / 排序 / 分页、素材墙、引用统计、疑似重复、批量操作。
|
||
|
||
这些能力是「资源库对多资源管理不便」的服务端底座,页面侧(网格 / 列表 / 媒体墙
|
||
三种视图 + 多选批量)全部依赖这里的返回形状,因此契约要锁死。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from tests.conftest import _make_image_png
|
||
|
||
|
||
def _upload(api, owner_type, owner_id, *, kind="portrait", name="定妆图", size=240, color="0x224466"):
|
||
return api.post(
|
||
f"/api/platform/{'characters' if owner_type == 'human' else owner_type}/{owner_id}/media",
|
||
files={"file": (f"{name}.png", _make_image_png(size, size, color), "image/png")},
|
||
data={"kind": kind, "name": name},
|
||
)["result"]
|
||
|
||
|
||
def _seed(api):
|
||
"""三类型母版 + 素材 + 标签 + 一次项目引入 + 一对同名重复。"""
|
||
project_id = api.project(name="检索验收项目")
|
||
human_a = api.post("/api/platform/characters", json={
|
||
"name": "紫小慧", "role": "主角", "appearance": "松散黑色大波浪,白色短袖",
|
||
"tags": ["都市", "写实"]})["result"]
|
||
human_b = api.post("/api/platform/characters", json={
|
||
"name": "陈澈", "role": "配角", "appearance": "短寸黑发,洗白牛仔外套",
|
||
"tags": ["都市"]})["result"]
|
||
human_dup = api.post("/api/platform/characters", json={"name": "紫小慧 "})["result"]
|
||
scene = api.post("/api/platform/scenes", json={
|
||
"name": "雨夜面馆", "prompt": "暖黄灯光的街边面馆", "lighting": "暖黄"})["result"]
|
||
voice = api.post("/api/platform/voices", json={
|
||
"name": "低沉男声", "provider": "mock", "provider_voice_id": "v-low", "gender": "male"})["result"]
|
||
bare = api.post("/api/platform/characters", json={"name": "无素材路人"})["result"]
|
||
|
||
_upload(api, "human", human_a["id"], name="紫小慧定妆图")
|
||
_upload(api, "human", human_a["id"], kind="turnaround", name="紫小慧三视图", color="0x993355")
|
||
_upload(api, "human", human_a["id"], name="紫小慧定妆图", color="0x1177aa") # 同名→版本 v2
|
||
_upload(api, "human", human_b["id"], name="陈澈定妆图", size=120, color="0x226699")
|
||
_upload(api, "scene", scene["id"], kind="scene_image", name="面馆全景", size=320, color="0x336655")
|
||
api.post("/api/platform/import", json={"project_id": project_id, "asset_type": "human",
|
||
"asset_id": human_a["id"]})
|
||
return {"project_id": project_id, "human_a": human_a, "human_b": human_b,
|
||
"human_dup": human_dup, "scene": scene, "voice": voice, "bare": bare}
|
||
|
||
|
||
def test_platform_asset_search_filters_sort_and_pagination(api):
|
||
ids = _seed(api)
|
||
everything = api.get("/api/platform/assets")
|
||
assert {row["id"] for row in everything["human"]} >= {ids["human_a"]["id"], ids["human_b"]["id"]}
|
||
assert everything["filtered"] == everything["stats"]["masters"] == len(everything["items"])
|
||
assert {s["value"] for s in everything["sorts"]} == {"updated", "created", "name",
|
||
"media", "size", "usage"}
|
||
|
||
# 跨字段搜索:命中提示词、素材名都要能捞出来,且带上 matched 母版
|
||
by_appearance = api.get("/api/platform/assets", params={"q": "大波浪"})
|
||
assert [r["id"] for r in by_appearance["items"]] == [ids["human_a"]["id"]]
|
||
by_media_name = api.get("/api/platform/assets", params={"q": "面馆全景"})
|
||
assert [r["id"] for r in by_media_name["items"]] == [ids["scene"]["id"]]
|
||
assert api.get("/api/platform/assets", params={"q": "不存在的关键字"})["filtered"] == 0
|
||
|
||
# 标签 / 素材类型 / 有无素材 / 类型筛选
|
||
assert {r["id"] for r in api.get("/api/platform/assets", params={"tag": "都市"})["items"]} == {
|
||
ids["human_a"]["id"], ids["human_b"]["id"]}
|
||
assert all("scene_image" in [m["kind"] for m in r["assets"]]
|
||
for r in api.get("/api/platform/assets", params={"kind": "scene_image"})["items"])
|
||
without = api.get("/api/platform/assets", params={"has_media": "without"})
|
||
assert ids["bare"]["id"] in {r["id"] for r in without["items"]}
|
||
assert all(r["media_count"] == 0 for r in without["items"])
|
||
assert ids["bare"]["id"] not in {r["id"] for r in
|
||
api.get("/api/platform/assets", params={"has_media": "with"})["items"]}
|
||
assert all(r["owner_type"] == "voice"
|
||
for r in api.get("/api/platform/assets", params={"owner_type": "voice"})["items"])
|
||
|
||
# 状态筛选:停用后只在 inactive 出现
|
||
api.patch(f"/api/platform/characters/{ids['human_b']['id']}", json={"is_active": False})
|
||
assert {r["id"] for r in api.get("/api/platform/assets", params={"status": "inactive"})["items"]} == {
|
||
ids["human_b"]["id"]}
|
||
assert ids["human_b"]["id"] not in {r["id"] for r in
|
||
api.get("/api/platform/assets", params={"status": "active"})["items"]}
|
||
|
||
# 排序:素材数 / 被引用项目数
|
||
by_media = api.get("/api/platform/assets", params={"sort": "media", "owner_type": "human"})["items"]
|
||
counts = [r["media_count"] for r in by_media]
|
||
assert counts == sorted(counts, reverse=True)
|
||
by_usage = api.get("/api/platform/assets", params={"sort": "usage"})["items"]
|
||
assert by_usage[0]["id"] == ids["human_a"]["id"]
|
||
assert by_usage[0]["usage_count"] == 1
|
||
assert by_usage[0]["usage"][0]["project_name"] == "检索验收项目"
|
||
assert api.get("/api/platform/assets", params={"sort": "nonsense"})["filtered"] > 0 # 未知排序不炸
|
||
|
||
# 分页:page_size=2 时 pages 与 slice 必须自洽,越界页回退末页
|
||
page1 = api.get("/api/platform/assets", params={"page": 1, "page_size": 2})
|
||
assert len(page1["items"]) == 2 and page1["page"] == 1
|
||
assert page1["pages"] == (page1["filtered"] + 1) // 2
|
||
last = api.get("/api/platform/assets", params={"page": page1["pages"], "page_size": 2})
|
||
tail = api.get("/api/platform/assets", params={"page": 999, "page_size": 2})
|
||
assert [r["id"] for r in tail["items"]] == [r["id"] for r in last["items"]]
|
||
assert tail["page"] == page1["pages"]
|
||
ids_p1 = {r["id"] for r in page1["items"]}
|
||
assert ids_p1.isdisjoint({r["id"] for r in last["items"]}) or page1["pages"] == 1
|
||
|
||
# 统计口径:素材总数/版本数/体积/分类型
|
||
stats = api.get("/api/platform/assets")["stats"]
|
||
# 4 条素材、5 个版本(同名同资产位再上传只涨版本不新增行)
|
||
assert stats["media_count"] == 4 and stats["versions"] == 5 and stats["without_media"] == 3 # 空壳母版:重名副本 / 无素材路人 / 未挂音色样本的声线
|
||
assert stats["by_kind"] == {"portrait": 2, "turnaround": 1, "scene_image": 1}
|
||
assert stats["media_bytes"] > 0
|
||
# 兼容旧口径:三分类列表 + counts 仍在
|
||
legacy = api.get("/api/platform/assets")
|
||
assert sum(legacy["counts"].values()) == legacy["filtered"]
|
||
|
||
|
||
def test_platform_duplicate_detection(api):
|
||
ids = _seed(api)
|
||
payload = api.get("/api/platform/assets")
|
||
groups = payload["duplicates"]
|
||
group = next(g for g in groups if g["owner_type"] == "human" and g["key"] == "紫小慧")
|
||
assert {i["asset_id"] for i in group["ids"]} == {ids["human_a"]["id"], ids["human_dup"]["id"]}
|
||
assert payload["stats"]["duplicate_groups"] >= 1
|
||
assert payload["stats"]["duplicate_masters"] >= 2
|
||
only = api.get("/api/platform/assets", params={"duplicate_only": True})["items"]
|
||
assert {r["id"] for r in only} == {ids["human_a"]["id"], ids["human_dup"]["id"]}
|
||
flagged = next(r for r in only if r["id"] == ids["human_a"]["id"])
|
||
assert flagged["duplicate_of"] == [ids["human_dup"]["id"]]
|
||
# 同名但跨类型不算重复
|
||
assert all(g["owner_type"] == "human" or len(g["ids"]) < 2 for g in groups)
|
||
|
||
|
||
def test_platform_media_wall(api):
|
||
ids = _seed(api)
|
||
wall = api.get("/api/platform/media")
|
||
assert wall["total"] == wall["stats"]["count"] == 4
|
||
assert {i["owner_name"] for i in wall["items"]} >= {"紫小慧", "雨夜面馆"}
|
||
assert all(i["url"].startswith("/media/platform/") and i["kind_label"] for i in wall["items"])
|
||
assert wall["stats"]["multi_version"] == 1 # 只有「紫小慧定妆图」有 v2
|
||
assert wall["stats"]["by_kind"] == {"portrait": 2, "turnaround": 1, "scene_image": 1}
|
||
assert {k["value"] for k in wall["kinds"]} == {"portrait", "turnaround", "scene_image"}
|
||
|
||
# 类型 / 归属 / 搜索 / 多版本 过滤
|
||
assert all(i["kind"] == "turnaround" for i in
|
||
api.get("/api/platform/media", params={"kind": "turnaround"})["items"])
|
||
owner_rows = api.get("/api/platform/media", params={"owner_type": "scene"})["items"]
|
||
assert [i["owner_id"] for i in owner_rows] == [ids["scene"]["id"]]
|
||
mine = api.get("/api/platform/media",
|
||
params={"owner_type": "human", "owner_id": ids["human_a"]["id"]})["items"]
|
||
assert len(mine) == 2 and all(i["owner_id"] == ids["human_a"]["id"] for i in mine)
|
||
# 只给 owner_id 时按「任意类型的同号母版」检索——前端必须带上 owner_type
|
||
spanning = api.get("/api/platform/media", params={"owner_id": ids["human_a"]["id"]})["items"]
|
||
assert len(spanning) >= len(mine) and {i["owner_type"] for i in spanning} != {"human"}
|
||
assert api.get("/api/platform/media", params={"q": "三视图"})["total"] == 1
|
||
multi = api.get("/api/platform/media", params={"multi_version_only": True})["items"]
|
||
assert [i["name"] for i in multi] == ["紫小慧定妆图"] and multi[0]["version"] == 2
|
||
|
||
# 排序与分页
|
||
biggest = api.get("/api/platform/media", params={"sort": "size"})["items"]
|
||
assert [i["size_bytes"] for i in biggest] == sorted((i["size_bytes"] for i in biggest), reverse=True)
|
||
by_name = api.get("/api/platform/media", params={"sort": "name"})["items"]
|
||
assert [i["name"] for i in by_name] == sorted((i["name"] for i in by_name), key=str.lower)
|
||
paged = api.get("/api/platform/media", params={"page_size": 2, "page": 2})
|
||
assert len(paged["items"]) == 2 and paged["pages"] == 2 and paged["page"] == 2
|
||
assert api.get("/api/platform/media", params={"page_size": 2, "page": 9})["page"] == 2
|
||
assert api.get("/api/platform/media", params={"page_size": 0})["total"] == 4 # 不分页时返回全量
|
||
|
||
|
||
def test_platform_batch_tag_delete_and_import(api):
|
||
ids = _seed(api)
|
||
refs = [{"owner_type": "human", "asset_id": ids["human_a"]["id"]},
|
||
{"owner_type": "human", "asset_id": ids["human_b"]["id"]},
|
||
{"owner_type": "scene", "asset_id": ids["scene"]["id"]}]
|
||
|
||
tagged = api.post("/api/platform/assets/batch-tag",
|
||
json={"items": refs, "add": [" 批量整理 ", "批量整理"], "remove": []})["result"]
|
||
assert tagged["count"] == 3
|
||
assert tagged["items"][0]["tags"].count("批量整理") == 1 # 去空白 + 去重
|
||
assert {r["id"] for r in api.get("/api/platform/assets", params={"tag": "批量整理"})["items"]} == {
|
||
ids["human_a"]["id"], ids["human_b"]["id"], ids["scene"]["id"]}
|
||
removed_tag = api.post("/api/platform/assets/batch-tag",
|
||
json={"items": refs[:1], "add": [], "remove": ["批量整理"]})["result"]
|
||
assert "批量整理" not in removed_tag["items"][0]["tags"]
|
||
|
||
# 批量引入到项目:成功项落地,坏引用只进 failures 不炸整批
|
||
imported = api.post("/api/platform/assets/batch-import", json={
|
||
"project_id": ids["project_id"],
|
||
"items": refs + [{"owner_type": "human", "asset_id": 999999}]})["result"]
|
||
assert imported["imported_count"] == 3
|
||
assert len(imported["failures"]) == 1 and imported["failures"][0]["asset_id"] == 999999
|
||
project = api.get(f"/api/projects/{ids['project_id']}")["result"]
|
||
assert {ids["human_a"]["id"], ids["human_b"]["id"], ids["scene"]["id"]} <= {
|
||
row["platform_source"]["id"] for row in project["characters"] + project["scenes"]
|
||
if row.get("platform_source")}
|
||
usage = api.get("/api/platform/assets", params={"sort": "usage"})["items"]
|
||
assert next(r for r in usage if r["id"] == ids["human_b"]["id"])["usage_count"] == 1
|
||
|
||
# 批量删素材:文件真删、统计随之下降
|
||
media_ids = [i["id"] for i in api.get(
|
||
"/api/platform/media",
|
||
params={"owner_type": "human", "owner_id": ids["human_a"]["id"]})["items"]]
|
||
assert len(media_ids) == 2
|
||
gone = api.post("/api/platform/media/batch-delete",
|
||
json={"media_ids": media_ids[:1] + [999999]})["result"]
|
||
assert gone["deleted_count"] == 1 and gone["files_removed"] >= 1
|
||
assert len(gone["failures"]) == 1 and gone["failures"][0]["media_id"] == 999999
|
||
assert api.get("/api/platform/media",
|
||
params={"owner_type": "human", "owner_id": ids["human_a"]["id"]})["total"] == 1
|
||
|
||
# 批量删母版:连带素材与文件一起回收
|
||
before = api.get("/api/platform/media")["total"]
|
||
deleted = api.post("/api/platform/assets/batch-delete", json={"items": [
|
||
{"owner_type": "human", "asset_id": ids["human_dup"]["id"]},
|
||
{"owner_type": "human", "asset_id": ids["bare"]["id"]},
|
||
{"owner_type": "voice", "asset_id": 999999}]})["result"]
|
||
assert deleted["deleted_count"] == 2 and len(deleted["failures"]) == 1
|
||
assert deleted["deleted"][0]["files_removed"] >= 0
|
||
assert api.get("/api/platform/media")["total"] == before
|
||
assert ids["human_dup"]["id"] not in {r["id"] for r in api.get("/api/platform/assets")["human"]}
|
||
|
||
|
||
def test_timeline_version_activate_and_delete(api, studio, script_text, make_image_png):
|
||
from tests.test_delivery import _prepare
|
||
pid, _ = _prepare(api, studio, script_text, make_image_png)
|
||
|
||
first = api.post(f"/api/projects/{pid}/timeline", json={"name": "第一版"})["result"]["timeline"]
|
||
second = api.post(f"/api/projects/{pid}/timeline", json={"name": "第二版"})["result"]["timeline"]
|
||
versions = api.get(f"/api/projects/{pid}/timelines")["timelines"]
|
||
assert [v["version"] for v in versions] == [second["version"], first["version"]]
|
||
assert [v["is_current"] for v in versions] == [True, False]
|
||
|
||
# 读取时可按版本定位,跨项目版本要 404
|
||
historic = api.get(f"/api/projects/{pid}/timeline", params={"timeline_id": first["id"]})["timeline"]
|
||
assert historic["name"] == "第一版"
|
||
other = api.project(name="另一个项目")
|
||
api.get(f"/api/projects/{other}/timeline", params={"timeline_id": first["id"]}, expect=404)
|
||
|
||
# 切换当前版本
|
||
activated = api.post(f"/api/projects/{pid}/timelines/{first['id']}/activate")["result"]
|
||
assert activated["is_current"] is True and activated["version"] == first["version"]
|
||
flags = {v["id"]: v["is_current"] for v in api.get(f"/api/projects/{pid}/timelines")["timelines"]}
|
||
assert flags == {first["id"]: True, second["id"]: False}
|
||
assert api.get(f"/api/projects/{pid}/timeline")["timeline"]["id"] == first["id"]
|
||
assert any(e["action"] == "timeline.activated" for e in api.get("/api/events")["events"])
|
||
|
||
# 删除非当前版本:当前标记不动;删光到只剩 1 个时禁止再删
|
||
api.delete(f"/api/projects/{pid}/timelines/{second['id']}")
|
||
left = api.get(f"/api/projects/{pid}/timelines")["timelines"]
|
||
assert [v["id"] for v in left] == [first["id"]] and left[0]["is_current"] is True
|
||
api.delete(f"/api/projects/{pid}/timelines/{first['id']}", expect=400)
|
||
assert api.get(f"/api/projects/{pid}/timelines")["timelines"]
|
||
|
||
# 删除当前版本:自动把剩下的最新版本设为当前,避免项目没有时间线
|
||
third = api.post(f"/api/projects/{pid}/timeline", json={"name": "第三版"})["result"]["timeline"]
|
||
api.post(f"/api/projects/{pid}/timelines/{first['id']}/activate")
|
||
dropped = api.delete(f"/api/projects/{pid}/timelines/{first['id']}")["result"]
|
||
assert dropped["remaining"] == 1 and dropped["current_version"] == third["version"]
|
||
current = {v["id"]: v["is_current"] for v in api.get(f"/api/projects/{pid}/timelines")["timelines"]}
|
||
assert sum(1 for v in current.values() if v) == 1
|
||
assert api.get(f"/api/projects/{pid}/timeline")["timeline"]["id"] == third["id"]
|
||
api.delete(f"/api/projects/{pid}/timelines/999999", expect=404)
|