2026-11-10 21:36:09 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
|
|
|
import importlib
|
|
|
|
|
|
import json
|
|
|
|
|
|
import os
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
import shutil
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
import threading
|
|
|
|
|
|
import time
|
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
import wave
|
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
|
UPSTREAM_ID = "a" * 32
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProviderTests(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
|
|
|
|
self.addCleanup(self.temp.cleanup)
|
|
|
|
|
|
self.root = Path(self.temp.name).resolve()
|
2026-11-14 14:07:26 +08:00
|
|
|
|
(self.root / "assets").mkdir()
|
|
|
|
|
|
(self.root / "jobs").mkdir()
|
|
|
|
|
|
self.work = self.root / "jobs" / "attempt"
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.work.mkdir()
|
|
|
|
|
|
self.requests = []
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.reply = {"id": UPSTREAM_ID, "state": "queued"}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.status = 200
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.binary = b"actual-file"
|
2026-11-10 21:36:09 +08:00
|
|
|
|
owner = self
|
|
|
|
|
|
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
|
|
|
|
def log_message(self, *_):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def respond(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
data = self.rfile.read(int(self.headers.get("Content-Length", "0")))
|
|
|
|
|
|
owner.requests.append(
|
|
|
|
|
|
(
|
|
|
|
|
|
self.command,
|
|
|
|
|
|
self.path,
|
|
|
|
|
|
dict(self.headers),
|
|
|
|
|
|
json.loads(data) if data else None,
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
blob = (
|
|
|
|
|
|
owner.binary
|
|
|
|
|
|
if "/outputs/" in self.path or self.path.endswith("/audio/speech")
|
|
|
|
|
|
else json.dumps(owner.reply).encode()
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.send_response(owner.status)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.send_header("Content-Type", "application/json")
|
|
|
|
|
|
self.send_header("Content-Length", str(len(blob)))
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.end_headers()
|
|
|
|
|
|
self.wfile.write(blob)
|
|
|
|
|
|
|
|
|
|
|
|
do_GET = do_POST = respond
|
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.thread = threading.Thread(target=self.server.serve_forever, daemon=True)
|
|
|
|
|
|
self.thread.start()
|
|
|
|
|
|
self.addCleanup(self.server.server_close)
|
|
|
|
|
|
self.addCleanup(self.server.shutdown)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
url = f"http://127.0.0.1:{self.server.server_port}"
|
|
|
|
|
|
self.settings = SimpleNamespace(
|
|
|
|
|
|
inference_base_url=url,
|
|
|
|
|
|
inference_api_key="PRIVATE-KEY",
|
|
|
|
|
|
inference_api_key_file=None,
|
|
|
|
|
|
workspace_root=self.root,
|
|
|
|
|
|
asset_root=self.root / "assets",
|
|
|
|
|
|
job_work_root=self.root / "jobs",
|
|
|
|
|
|
gpu_status_path=self.root / "status.json",
|
|
|
|
|
|
gpu_scheduler_path=self.root / "scheduler.json",
|
|
|
|
|
|
qwen_base_url=url + "/v1",
|
|
|
|
|
|
qwen_model="existing-model",
|
|
|
|
|
|
qwen_api_key="QWEN-PRIVATE",
|
|
|
|
|
|
ffmpeg_bin=shutil.which("ffmpeg") or "ffmpeg",
|
|
|
|
|
|
ffprobe_bin=shutil.which("ffprobe") or "ffprobe",
|
|
|
|
|
|
tts_base_url="",
|
|
|
|
|
|
tts_model="",
|
|
|
|
|
|
tts_api_key="",
|
|
|
|
|
|
request_timeout_seconds=3,
|
|
|
|
|
|
max_output_bytes=1024 * 1024,
|
|
|
|
|
|
)
|
|
|
|
|
|
self.module = importlib.import_module("app.adapters.provider.client")
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.provider = self.module.Provider(self.settings)
|
|
|
|
|
|
self.addAsyncCleanup(self.provider.aclose)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.image = self.root / "assets" / "source.png"
|
|
|
|
|
|
Image.new("RGB", (320, 240), "red").save(self.image)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_video_payload_has_real_frame_limit_and_owned_image_path(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
payload = await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"video.generate",
|
|
|
|
|
|
{"prompt": "镜头缓慢移动", "image_asset_id": "owned"},
|
|
|
|
|
|
{},
|
|
|
|
|
|
{"owned": self.image},
|
|
|
|
|
|
self.work,
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(payload["input"]["image_path"], str(self.image))
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
(payload["parameters"]["num_frames"], payload["parameters"]["fps"]), (121, 24)
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(payload["parameters"]["num_inference_steps"], 50)
|
|
|
|
|
|
for parameters in (
|
|
|
|
|
|
{"num_frames": 365},
|
|
|
|
|
|
{"num_frames": 120},
|
|
|
|
|
|
{"fps": 8},
|
|
|
|
|
|
{"width": 1280, "height": 736},
|
|
|
|
|
|
{"unknown": True},
|
|
|
|
|
|
):
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.subTest(parameters=parameters), self.assertRaises(self.module.ProviderError):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"video.generate", {"prompt": "x"}, parameters, {}, self.work
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_submission_validation_rejects_unknown_fields_before_creating_job(self):
|
|
|
|
|
|
validate = self.module.validate_arguments
|
2026-11-14 14:07:26 +08:00
|
|
|
|
validate("video.generate", {"prompt": "x"}, {"num_frames": 121}, self.settings)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
for task, inputs, parameters in (
|
2026-11-14 14:07:26 +08:00
|
|
|
|
("image.generate", {"prompt": "x"}, {"steps": 10}),
|
|
|
|
|
|
("math.solve", {"prompt": "x"}, {"shell": "evil"}),
|
|
|
|
|
|
("media.structure", {"asset_id": "x"}, {"frame_count": 13}),
|
|
|
|
|
|
("audio.speech", {"prompt": "x"}, {"voice": "/tmp/foreign.npy"}),
|
|
|
|
|
|
("video.compose", {}, {"clips": [{"asset_id": "x"}], "filter": "unsafe"}),
|
|
|
|
|
|
("video.generate", {"prompt": "x"}, {"num_frames": 365}),
|
2026-11-10 21:36:09 +08:00
|
|
|
|
):
|
|
|
|
|
|
with self.subTest(task=task), self.assertRaises(self.module.ProviderError):
|
|
|
|
|
|
validate(task, inputs, parameters, self.settings)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unknown_inputs_and_foreign_paths_are_rejected(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
for inputs, assets in (
|
|
|
|
|
|
({"prompt": "x", "image_path": "/etc/passwd"}, {}),
|
|
|
|
|
|
({"prompt": "x", "image_asset_id": "x"}, {"x": self.root.parent / "secret.png"}),
|
|
|
|
|
|
):
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError) as caught:
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"video.generate", inputs, {}, assets, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertNotIn("passwd", caught.exception.message)
|
|
|
|
|
|
self.assertNotIn("secret.png", caught.exception.message)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"defect.generate", {"prompt": "scratch"}, {}, {}, self.work
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_image_and_defect_constraints_match_actual_runner(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
payload = await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"defect.generate",
|
|
|
|
|
|
{"prompt": "scratch", "image_asset_id": "x"},
|
|
|
|
|
|
{},
|
|
|
|
|
|
{"x": self.image},
|
|
|
|
|
|
self.work,
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(payload["parameters"]["controlnet_conditioning_scale"], 0.8)
|
|
|
|
|
|
for parameters in (
|
|
|
|
|
|
{"canny_low": 200, "canny_high": 100},
|
|
|
|
|
|
{"guidance_scale": float("nan")},
|
|
|
|
|
|
{"width": True},
|
|
|
|
|
|
{"num_inference_steps": 101},
|
|
|
|
|
|
{"mask_asset_id": "x"},
|
|
|
|
|
|
):
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.subTest(parameters=parameters), self.assertRaises(self.module.ProviderError):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"defect.generate",
|
|
|
|
|
|
{"prompt": "scratch", "image_asset_id": "x"},
|
|
|
|
|
|
parameters,
|
|
|
|
|
|
{"x": self.image},
|
|
|
|
|
|
self.work,
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_submit_preserves_immutable_key_and_error_never_leaks_secret(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
payload = {
|
|
|
|
|
|
"task_type": "image.generate",
|
|
|
|
|
|
"input": {"prompt": "x"},
|
|
|
|
|
|
"parameters": {},
|
|
|
|
|
|
"timeout_seconds": 3600,
|
|
|
|
|
|
}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
for _ in range(2):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.submit(payload, "same-key")
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
[request[2]["Idempotency-Key"] for request in self.requests], ["same-key", "same-key"]
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.assertEqual(self.requests[0][3], self.requests[1][3])
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.status, self.reply = 500, {"detail": "PRIVATE-KEY /workspace/private"}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError) as caught:
|
|
|
|
|
|
await self.provider.query(UPSTREAM_ID)
|
|
|
|
|
|
self.assertTrue(caught.exception.retriable)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertNotIn("PRIVATE", caught.exception.message)
|
|
|
|
|
|
self.assertNotIn("/workspace", caught.exception.message)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_replacing_http_client_affects_gpu_qwen_and_file_transfers(self):
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
await self.provider.client.aclose()
|
|
|
|
|
|
for marker in ("first", "replacement"):
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
|
|
|
|
async def respond(request):
|
|
|
|
|
|
calls.append((request.url.path, request.headers.get("Authorization")))
|
|
|
|
|
|
if request.url.path.endswith("/chat/completions"):
|
|
|
|
|
|
return httpx.Response(200, json={"choices": [{"message": {"content": marker}}]})
|
|
|
|
|
|
if "/outputs/" in request.url.path:
|
|
|
|
|
|
return httpx.Response(200, content=marker.encode())
|
|
|
|
|
|
return httpx.Response(200, json={"id": UPSTREAM_ID, "state": "queued"})
|
|
|
|
|
|
|
|
|
|
|
|
self.provider.client = httpx.AsyncClient(transport=httpx.MockTransport(respond))
|
|
|
|
|
|
try:
|
|
|
|
|
|
await self.provider.query(UPSTREAM_ID)
|
|
|
|
|
|
answer = await self.provider.run_cpu(
|
|
|
|
|
|
"math.solve", {"prompt": "2+2"}, {}, {}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
target = self.work / "download.bin"
|
|
|
|
|
|
await self.provider.download(UPSTREAM_ID, 0, target)
|
|
|
|
|
|
self.assertEqual(answer.result["text"], marker)
|
|
|
|
|
|
self.assertEqual(target.read_bytes(), marker.encode())
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
[key for _, key in calls],
|
|
|
|
|
|
["Bearer PRIVATE-KEY", "Bearer QWEN-PRIVATE", "Bearer PRIVATE-KEY"],
|
|
|
|
|
|
)
|
|
|
|
|
|
finally:
|
|
|
|
|
|
await self.provider.client.aclose()
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_replayed_submit_and_cancel_errors_are_sanitized(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.reply = {
|
|
|
|
|
|
"id": UPSTREAM_ID,
|
|
|
|
|
|
"state": "failed",
|
|
|
|
|
|
"error": "PRIVATE-KEY /workspace/private",
|
|
|
|
|
|
"last_preempted_reason": "PRIVATE /run/private",
|
|
|
|
|
|
}
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
"task_type": "image.generate",
|
|
|
|
|
|
"input": {"prompt": "x"},
|
|
|
|
|
|
"parameters": {},
|
|
|
|
|
|
"timeout_seconds": 3600,
|
|
|
|
|
|
}
|
|
|
|
|
|
for value in (
|
|
|
|
|
|
await self.provider.submit(payload, "fixed"),
|
|
|
|
|
|
await self.provider.cancel(UPSTREAM_ID),
|
|
|
|
|
|
):
|
|
|
|
|
|
self.assertNotIn("PRIVATE", json.dumps(value))
|
|
|
|
|
|
self.assertNotIn("/workspace", json.dumps(value))
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_download_is_real_atomic_and_bounded(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
target = self.work / "result.png"
|
2026-11-10 21:36:09 +08:00
|
|
|
|
await self.provider.download(UPSTREAM_ID, 0, target)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertEqual(target.read_bytes(), b"actual-file")
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.settings.max_output_bytes = 4
|
|
|
|
|
|
with self.assertRaises(self.module.ProviderError):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.download(UPSTREAM_ID, 1, self.work / "too-large.png")
|
|
|
|
|
|
self.assertFalse((self.work / "too-large.png").exists())
|
|
|
|
|
|
self.assertFalse(list(self.work.glob("*.part-*")))
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_private_key_file_is_bounded_and_not_symlink_followed(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
key = self.root / "private-key"
|
|
|
|
|
|
key.write_text("FILE-SECRET", encoding="utf-8")
|
|
|
|
|
|
if os.name != "nt":
|
2026-11-10 21:36:09 +08:00
|
|
|
|
key.chmod(0o600)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.settings.inference_api_key_file, self.settings.inference_api_key = key, ""
|
2026-11-10 21:36:09 +08:00
|
|
|
|
await self.provider.query(UPSTREAM_ID)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertEqual(self.requests[-1][2]["Authorization"], "Bearer FILE-SECRET")
|
|
|
|
|
|
key.write_text("x" * 5000)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError):
|
|
|
|
|
|
await self.provider.query(UPSTREAM_ID)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_math_and_visual_structure_return_real_text_json_files(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.reply = {"choices": [{"message": {"content": "答案为 4。"}, "finish_reason": "stop"}]}
|
|
|
|
|
|
result = await self.provider.run_cpu("math.solve", {"prompt": "2+2"}, {}, {}, self.work)
|
|
|
|
|
|
self.assertEqual(result.result, {"text": "答案为 4。"})
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.assertTrue(all(path.is_file() for path in result.outputs))
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertNotIn("reasoning_content", result.result)
|
|
|
|
|
|
self.reply = {
|
|
|
|
|
|
"choices": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"message": {
|
|
|
|
|
|
"content": '{"subjects":["红色画面"],"scene":"室内","ocr":[],"tags":["红色"]}'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
result = await self.provider.run_cpu(
|
|
|
|
|
|
"media.structure", {"asset_id": "x"}, {}, {"x": self.image}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(result.result["subjects"], ["红色画面"])
|
|
|
|
|
|
content = self.requests[-1][3]["messages"][-1]["content"]
|
|
|
|
|
|
self.assertTrue(content[0]["image_url"]["url"].startswith("data:image/jpeg;base64,"))
|
|
|
|
|
|
self.assertEqual(result.result["frames"][0]["timestamp_seconds"], 0)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.assertTrue(all(path.is_file() for path in result.outputs))
|
|
|
|
|
|
|
|
|
|
|
|
async def test_math_length_finish_is_failure_and_creates_no_answer_artifacts(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.reply = {
|
|
|
|
|
|
"choices": [{"message": {"content": "计算尚未完成:17*3="}, "finish_reason": "length"}]
|
|
|
|
|
|
}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError) as caught:
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.run_cpu(
|
|
|
|
|
|
"math.solve", {"prompt": "17*3?"}, {"max_tokens": 128}, {}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(caught.exception.code, "output_truncated")
|
|
|
|
|
|
self.assertIn("提高输出长度", caught.exception.message)
|
|
|
|
|
|
self.assertFalse((self.work / "answer.txt").exists())
|
|
|
|
|
|
self.assertFalse((self.work / "answer.json").exists())
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_visual_length_finish_is_failure_even_when_partial_output_parses_as_json(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.reply = {
|
|
|
|
|
|
"choices": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"message": {"content": '{"subjects":[],"scene":"部分内容","ocr":[],"tags":[]}'},
|
|
|
|
|
|
"finish_reason": "length",
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError) as caught:
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.run_cpu(
|
|
|
|
|
|
"media.structure", {"asset_id": "x"}, {}, {"x": self.image}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(caught.exception.code, "output_truncated")
|
|
|
|
|
|
self.assertFalse((self.work / "structure.json").exists())
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_visual_non_json_and_unconfigured_speech_are_explicit_failures(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.reply = {"choices": [{"message": {"content": "not json"}}]}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.run_cpu(
|
|
|
|
|
|
"media.structure", {"asset_id": "x"}, {}, {"x": self.image}, self.work
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.assertRaises(self.module.ProviderError) as caught:
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.run_cpu(
|
|
|
|
|
|
"audio.speech", {"prompt": "hello"}, {"voice": "zf_xiaobei"}, {}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(caught.exception.code, "capability_unavailable")
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_composition_rejects_url_filter_and_invalid_subtitles_before_ffmpeg(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
for parameters in (
|
|
|
|
|
|
{"clips": [{"asset_id": "x", "url": "http://evil"}]},
|
|
|
|
|
|
{"clips": [{"asset_id": "x"}], "filter": "movie=/etc/passwd"},
|
|
|
|
|
|
{"clips": [{"asset_id": "x", "start_seconds": 2, "end_seconds": 1}]},
|
|
|
|
|
|
{"clips": [{"asset_id": "x"}], "subtitles": [{"start": 0, "end": 1, "text": "{\\p1}"}]},
|
|
|
|
|
|
):
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with self.subTest(parameters=parameters), self.assertRaises(self.module.ProviderError):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
await self.provider.run_cpu(
|
|
|
|
|
|
"video.compose", {}, parameters, {"x": self.image}, self.work
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
async def test_capabilities_and_scheduler_do_not_overstate_or_disclose_pids(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
items = {item["task_type"]: item for item in self.module.capabilities(self.settings)}
|
|
|
|
|
|
self.assertFalse(items["audio.speech"]["available"])
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
items["video.generate"]["constraints"]["num_frames"], {"min": 5, "max": 361, "step": 4}
|
|
|
|
|
|
)
|
|
|
|
|
|
status = {
|
|
|
|
|
|
"schema_version": 1,
|
|
|
|
|
|
"generated_at": time.time(),
|
|
|
|
|
|
"error": None,
|
|
|
|
|
|
"container_id": "b" * 64,
|
|
|
|
|
|
"gpus": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"index": 2,
|
|
|
|
|
|
"uuid": "GPU-00000000-0000-0000-0000-000000000002",
|
|
|
|
|
|
"total_gib": 79.6,
|
|
|
|
|
|
"free_gib": 79.5,
|
|
|
|
|
|
"idle_since": time.time() - 20,
|
|
|
|
|
|
"own_pids": [],
|
|
|
|
|
|
"external_pids": [],
|
|
|
|
|
|
"baseline_pids": [2880],
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
}
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.settings.gpu_status_path.write_text(json.dumps(status))
|
|
|
|
|
|
scheduler = await self.provider.scheduler_status()
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertTrue(scheduler["available"])
|
|
|
|
|
|
self.assertTrue(scheduler["gpus"][0]["eligible"])
|
|
|
|
|
|
self.assertNotIn("2880", json.dumps(scheduler))
|
|
|
|
|
|
status["generated_at"] -= 10
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.settings.gpu_status_path.write_text(json.dumps(status))
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertFalse((await self.provider.scheduler_status())["available"])
|
2026-11-10 21:36:09 +08:00
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
|
@unittest.skipUnless(shutil.which("ffmpeg") and shutil.which("ffprobe"), "FFmpeg not installed")
|
2026-11-10 21:36:09 +08:00
|
|
|
|
async def test_real_compose_preserves_order_trims_and_video_asr_prepares_wav(self):
|
2026-11-14 14:07:26 +08:00
|
|
|
|
from app.adapters.media import run_process, inspect_media
|
|
|
|
|
|
|
|
|
|
|
|
video = self.root / "assets" / "tiny.mp4"
|
|
|
|
|
|
await run_process(
|
|
|
|
|
|
[
|
|
|
|
|
|
self.settings.ffmpeg_bin,
|
|
|
|
|
|
"-v",
|
|
|
|
|
|
"error",
|
|
|
|
|
|
"-f",
|
|
|
|
|
|
"lavfi",
|
|
|
|
|
|
"-i",
|
|
|
|
|
|
"color=c=blue:s=320x256:r=24:d=1",
|
|
|
|
|
|
"-f",
|
|
|
|
|
|
"lavfi",
|
|
|
|
|
|
"-i",
|
|
|
|
|
|
"sine=frequency=440:duration=1",
|
|
|
|
|
|
"-c:v",
|
|
|
|
|
|
"libx264",
|
|
|
|
|
|
"-pix_fmt",
|
|
|
|
|
|
"yuv420p",
|
|
|
|
|
|
"-c:a",
|
|
|
|
|
|
"aac",
|
|
|
|
|
|
"-shortest",
|
|
|
|
|
|
str(video),
|
|
|
|
|
|
],
|
|
|
|
|
|
timeout=30,
|
|
|
|
|
|
)
|
|
|
|
|
|
red = self.root / "assets" / "red.mp4"
|
|
|
|
|
|
await run_process(
|
|
|
|
|
|
[
|
|
|
|
|
|
self.settings.ffmpeg_bin,
|
|
|
|
|
|
"-v",
|
|
|
|
|
|
"error",
|
|
|
|
|
|
"-f",
|
|
|
|
|
|
"lavfi",
|
|
|
|
|
|
"-i",
|
|
|
|
|
|
"color=c=red:s=320x256:r=24:d=1",
|
|
|
|
|
|
"-c:v",
|
|
|
|
|
|
"libx264",
|
|
|
|
|
|
"-pix_fmt",
|
|
|
|
|
|
"yuv420p",
|
|
|
|
|
|
str(red),
|
|
|
|
|
|
],
|
|
|
|
|
|
timeout=30,
|
|
|
|
|
|
)
|
|
|
|
|
|
result = await self.provider.run_cpu(
|
|
|
|
|
|
"video.compose",
|
|
|
|
|
|
{},
|
|
|
|
|
|
{
|
|
|
|
|
|
"clips": [
|
|
|
|
|
|
{"asset_id": "v", "start_seconds": 0, "end_seconds": 0.25},
|
|
|
|
|
|
{"asset_id": "r", "start_seconds": 0.5, "end_seconds": 0.75},
|
|
|
|
|
|
],
|
|
|
|
|
|
"width": 320,
|
|
|
|
|
|
"height": 256,
|
|
|
|
|
|
"subtitles": [{"start": 0, "end": 0.4, "text": "Safe <title> & credits"}],
|
|
|
|
|
|
},
|
|
|
|
|
|
{"v": video, "r": red},
|
|
|
|
|
|
self.work,
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
info = await inspect_media(result.outputs[0], self.settings)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertEqual((info["width"], info["height"]), (320, 256))
|
|
|
|
|
|
self.assertAlmostEqual(info["duration_seconds"], 0.5, delta=0.15)
|
|
|
|
|
|
for timestamp, dominant in ((0.1, 2), (0.4, 0)):
|
|
|
|
|
|
frame = await run_process(
|
|
|
|
|
|
[
|
|
|
|
|
|
self.settings.ffmpeg_bin,
|
|
|
|
|
|
"-v",
|
|
|
|
|
|
"error",
|
|
|
|
|
|
"-ss",
|
|
|
|
|
|
str(timestamp),
|
|
|
|
|
|
"-i",
|
|
|
|
|
|
str(result.outputs[0]),
|
|
|
|
|
|
"-frames:v",
|
|
|
|
|
|
"1",
|
|
|
|
|
|
"-f",
|
|
|
|
|
|
"image2pipe",
|
|
|
|
|
|
"-c:v",
|
|
|
|
|
|
"png",
|
|
|
|
|
|
"pipe:1",
|
|
|
|
|
|
],
|
|
|
|
|
|
timeout=30,
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
with Image.open(BytesIO(frame)) as image:
|
2026-11-14 14:07:26 +08:00
|
|
|
|
color = image.convert("RGB").getpixel((10, 10))
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.assertGreater(color[dominant], 150)
|
|
|
|
|
|
self.assertLess(color[2 if dominant == 0 else 0], 50)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
payload = await self.provider.prepare_gpu_payload(
|
|
|
|
|
|
"audio.transcribe", {"audio_asset_id": "v"}, {}, {"v": video}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(Path(payload["input"]["audio_path"]).suffix, ".wav")
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
(await inspect_media(Path(payload["input"]["audio_path"]), self.settings))["kind"],
|
|
|
|
|
|
"audio",
|
|
|
|
|
|
)
|
|
|
|
|
|
self.reply = {
|
|
|
|
|
|
"choices": [
|
|
|
|
|
|
{"message": {"content": '{"subjects":[],"scene":"blue","ocr":[],"tags":[]}'}}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
structure = await self.provider.run_cpu(
|
|
|
|
|
|
"media.structure", {"asset_id": "v"}, {"frame_count": 2}, {"v": video}, self.work
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(len(structure.result["frames"]), 2)
|
|
|
|
|
|
self.assertAlmostEqual(structure.result["frames"][0]["timestamp_seconds"], 0.25, delta=0.05)
|
|
|
|
|
|
self.assertAlmostEqual(structure.result["frames"][1]["timestamp_seconds"], 0.75, delta=0.05)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.assertTrue(all(path.is_file() for path in structure.outputs))
|
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
|
@unittest.skipUnless(shutil.which("ffprobe"), "FFprobe not installed")
|
2026-11-10 21:36:09 +08:00
|
|
|
|
async def test_configured_speech_posts_only_allowed_voice_and_real_wav_output(self):
|
|
|
|
|
|
stream = BytesIO()
|
2026-11-14 14:07:26 +08:00
|
|
|
|
with wave.open(stream, "wb") as audio:
|
2026-11-10 21:36:09 +08:00
|
|
|
|
audio.setnchannels(1)
|
|
|
|
|
|
audio.setsampwidth(2)
|
|
|
|
|
|
audio.setframerate(16000)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
audio.writeframes(b"\x00\x00" * 1600)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.binary = stream.getvalue()
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.settings.tts_base_url = self.settings.inference_base_url + "/v1"
|
|
|
|
|
|
self.settings.tts_model = "kokoro-82m"
|
|
|
|
|
|
result = await self.provider.run_cpu(
|
|
|
|
|
|
"audio.speech", {"prompt": "你好"}, {"voice": "zf_xiaobei", "speed": 1.2}, {}, self.work
|
|
|
|
|
|
)
|
2026-11-10 21:36:09 +08:00
|
|
|
|
self.assertEqual(result.outputs[0].read_bytes(), self.binary)
|
2026-11-14 14:07:26 +08:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
self.requests[-1][3],
|
|
|
|
|
|
{
|
|
|
|
|
|
"model": "kokoro-82m",
|
|
|
|
|
|
"input": "你好",
|
|
|
|
|
|
"voice": "zf_xiaobei",
|
|
|
|
|
|
"speed": 1.2,
|
|
|
|
|
|
"response_format": "wav",
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExecutionContextTests(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
|
async def test_phase_context_is_task_scoped_and_shared_with_facade(self):
|
|
|
|
|
|
from app.adapters.provider import client
|
|
|
|
|
|
from app.domain.execution import current_phase_callback, enter_phase
|
|
|
|
|
|
from app.domain.errors import MediaError
|
|
|
|
|
|
from app.adapters import media
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIs(client.current_phase_callback, current_phase_callback)
|
|
|
|
|
|
self.assertIs(client.enter_phase, enter_phase)
|
|
|
|
|
|
self.assertIs(client.ProviderError, MediaError)
|
|
|
|
|
|
self.assertIs(media.MediaError, MediaError)
|
|
|
|
|
|
self.assertIs(media.CpuResult, client.CpuResult)
|
|
|
|
|
|
observed = []
|
|
|
|
|
|
|
|
|
|
|
|
async def run(label):
|
|
|
|
|
|
async def callback(phase, pool):
|
|
|
|
|
|
observed.append((label, phase, pool))
|
|
|
|
|
|
|
|
|
|
|
|
token = current_phase_callback.set(callback)
|
|
|
|
|
|
try:
|
|
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
|
await client.enter_phase(label, "cpu_media")
|
|
|
|
|
|
finally:
|
|
|
|
|
|
current_phase_callback.reset(token)
|
|
|
|
|
|
|
|
|
|
|
|
await asyncio.gather(run("one"), run("two"))
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
sorted(observed), [("one", "one", "cpu_media"), ("two", "two", "cpu_media")]
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertIsNone(current_phase_callback.get())
|
|
|
|
|
|
|
|
|
|
|
|
def test_execution_domain_can_import_without_http_database_or_adapters(self):
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
script = """import sys
|
|
|
|
|
|
from app.domain.execution import CpuResult, enter_phase
|
|
|
|
|
|
from app.domain.errors import MediaError
|
|
|
|
|
|
assert not any(name == 'httpx' or name.startswith(('sqlalchemy', 'app.adapters')) for name in sys.modules)
|
|
|
|
|
|
"""
|
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
|
[sys.executable, "-B", "-c", script],
|
|
|
|
|
|
cwd=Path(__file__).resolve().parents[1],
|
|
|
|
|
|
capture_output=True,
|
|
|
|
|
|
text=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|