Files
wov-api/tests/test_apps_api.py
T

208 lines
7.0 KiB
Python

"""用户应用 API 测试。
覆盖已发布应用的上传建任务、进度查询、产物下载、失败重试以及
未发布/无版本工作流的拒绝逻辑。
"""
import json
from pathlib import Path
from fastapi.testclient import TestClient
from app.main import app
WORKSPACE = Path(__file__).resolve().parent.parent.parent
def _create_published_echo_workflow(client) -> str:
"""注册 Echo 节点并创建一个已发布的单节点工作流。"""
definition = {
"name": "echo-flow",
"version": 1,
"nodes": [
{
"id": "step",
"node_type": "echo",
"inputs": {"file_uri": "input.video_uri"},
}
],
"edges": [],
"entry_inputs": {"video_uri": "file"},
"final_outputs": {"result": "step.file_uri"},
}
manifest = json.loads(
(WORKSPACE / "wov-node-echo" / "node.manifest.json").read_text(encoding="utf-8")
)
assert client.post("/api/admin/nodes", json=manifest).status_code == 200
client.post(
"/api/admin/workflows",
json={
"id": "echo-app",
"name": "Echo App",
"description": "upload a file",
"definition": definition,
},
)
client.post("/api/admin/workflows/echo-app/publish")
return "echo-app"
def test_upload_run_progress_and_download() -> None:
"""验证上传文件建任务、手动执行、查询产物与下载的完整流程。"""
with TestClient(app) as client:
workflow_id = _create_published_echo_workflow(client)
apps = client.get("/api/apps")
assert apps.status_code == 200
assert any(item["id"] == workflow_id for item in apps.json())
uploaded = client.post(
f"/api/apps/{workflow_id}/runs",
files={"file": ("sample.txt", b"hello from upload", "text/plain")},
)
assert uploaded.status_code == 200
run_id = uploaded.json()["id"]
assert uploaded.json()["status"] == "QUEUED"
run = client.get(f"/api/runs/{run_id}")
assert run.status_code == 200
assert run.json()["input_uri"].endswith("sample.txt")
assert run.json()["artifacts"] == []
scheduler = app.state.scheduler
scheduler.execute_run(run_id)
completed = client.get(f"/api/runs/{run_id}")
assert completed.status_code == 200
assert completed.json()["status"] == "COMPLETED"
artifact_names = [item["name"] for item in completed.json()["artifacts"]]
assert "result" in artifact_names
artifacts = client.get(f"/api/runs/{run_id}/artifacts")
assert artifacts.status_code == 200
assert len(artifacts.json()) >= 1
downloaded = client.get(f"/api/runs/{run_id}/artifacts/result")
assert downloaded.status_code == 200
assert b"hello from upload" in downloaded.content
assert client.get(f"/api/runs/{run_id}/artifacts/missing").status_code == 404
assert client.get("/api/runs/missing").status_code == 404
assert client.get("/api/runs/missing/artifacts").status_code == 404
db = app.state.db
db.create_artifact(
{
"run_id": run_id,
"node_id": "step",
"name": "missing-file",
"uri": str(Path(__file__).resolve().parent / "not-exists.bin"),
"mime_type": "text/plain",
"size": 0,
}
)
assert client.get(f"/api/runs/{run_id}/artifacts/missing-file").status_code == 404
runs = client.get("/api/runs")
assert runs.status_code == 200
assert any(item["id"] == run_id for item in runs.json())
def test_upload_rejects_unpublished_workflow() -> None:
"""验证草稿或不存在的工作流不能被用户发起任务。"""
with TestClient(app) as client:
client.post(
"/api/admin/workflows",
json={
"id": "draft",
"name": "Draft",
"definition": {
"name": "Draft",
"version": 1,
"nodes": [],
"edges": [],
},
},
)
response = client.post(
"/api/apps/draft/runs",
files={"file": ("x.txt", b"x", "text/plain")},
)
assert response.status_code == 404
response = client.post(
"/api/apps/missing/runs",
files={"file": ("x.txt", b"x", "text/plain")},
)
assert response.status_code == 404
def test_upload_rejects_workflow_without_version() -> None:
"""验证已发布但没有任何版本的工作流返回 422。"""
with TestClient(app) as client:
db = app.state.db
db.upsert_workflow(
{"id": "empty", "name": "Empty", "published": 1, "latest_version": 0}
)
response = client.post(
"/api/apps/empty/runs",
files={"file": ("x.txt", b"x", "text/plain")},
)
assert response.status_code == 422
def test_retry_failed_run_requeues_and_reruns() -> None:
"""验证失败任务重试会清空旧产物并重新执行成功。"""
with TestClient(app) as client:
workflow_id = _create_published_echo_workflow(client)
uploaded = client.post(
f"/api/apps/{workflow_id}/runs",
files={"file": ("sample.txt", b"hello retry", "text/plain")},
)
run_id = uploaded.json()["id"]
db = app.state.db
db.update_run(
run_id,
status="FAILED",
error="boom",
updated_at="2026-01-01T00:00:00+00:00",
)
db.create_artifact(
{
"run_id": run_id,
"node_id": "step",
"name": "stale",
"uri": "stale.txt",
"mime_type": "text/plain",
"size": 1,
}
)
response = client.post(f"/api/runs/{run_id}/retry")
assert response.status_code == 200
assert response.json() == {"id": run_id, "status": "QUEUED"}
run = client.get(f"/api/runs/{run_id}").json()
assert run["status"] == "QUEUED"
assert run["error"] is None
assert run["artifacts"] == []
app.state.scheduler.execute_run(run_id)
completed = client.get(f"/api/runs/{run_id}").json()
assert completed["status"] == "COMPLETED"
assert any(item["name"] == "result" for item in completed["artifacts"])
def test_retry_rejects_non_failed_and_missing_runs() -> None:
"""验证只有 FAILED 状态且存在的任务才能重试。"""
with TestClient(app) as client:
workflow_id = _create_published_echo_workflow(client)
uploaded = client.post(
f"/api/apps/{workflow_id}/runs",
files={"file": ("sample.txt", b"x", "text/plain")},
)
run_id = uploaded.json()["id"]
assert client.post(f"/api/runs/{run_id}/retry").status_code == 422
assert client.post("/api/runs/missing/retry").status_code == 404