2026-03-07 15:33:29 +08:00
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
|
from typing import cast
|
2026-02-01 17:28:27 +08:00
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
from src.core.api_format.conversion.internal_video import InternalVideoPollResult, VideoStatus
|
2026-03-07 15:33:29 +08:00
|
|
|
|
from src.models.database import VideoTask
|
2026-04-03 14:59:58 +08:00
|
|
|
|
from src.services.request.execution_runtime_client import ExecutionRuntimeSyncResult
|
2026-03-21 12:57:09 +08:00
|
|
|
|
from src.services.task.video.poller_adapter import VideoPollContext, VideoTaskPollerAdapter
|
2026-02-01 17:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
|
async def test_poll_task_status_routes_gemini_video_to_gemini(
|
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
|
) -> None:
|
2026-02-02 03:16:52 +08:00
|
|
|
|
adapter = VideoTaskPollerAdapter()
|
2026-02-01 17:28:27 +08:00
|
|
|
|
|
2026-03-07 15:33:29 +08:00
|
|
|
|
task = cast(
|
|
|
|
|
|
VideoTask,
|
|
|
|
|
|
SimpleNamespace(
|
|
|
|
|
|
endpoint_id="e1",
|
|
|
|
|
|
key_id="k1",
|
|
|
|
|
|
provider_api_format="gemini:video",
|
|
|
|
|
|
external_task_id="operations/123",
|
|
|
|
|
|
),
|
2026-02-01 17:28:27 +08:00
|
|
|
|
)
|
2026-03-31 19:19:04 +08:00
|
|
|
|
prepared_ctx = VideoPollContext(
|
|
|
|
|
|
task_id="task-1",
|
|
|
|
|
|
external_task_id="operations/123",
|
|
|
|
|
|
provider_api_format="gemini:video",
|
|
|
|
|
|
base_url="https://example.com",
|
|
|
|
|
|
upstream_key="decrypted",
|
|
|
|
|
|
headers={"authorization": "Bearer x"},
|
|
|
|
|
|
poll_count=0,
|
|
|
|
|
|
retry_count=0,
|
|
|
|
|
|
poll_interval_seconds=15,
|
|
|
|
|
|
max_poll_count=10,
|
|
|
|
|
|
current_status=VideoStatus.PROCESSING.value,
|
2026-02-01 17:28:27 +08:00
|
|
|
|
)
|
2026-03-31 19:19:04 +08:00
|
|
|
|
poll_http = AsyncMock(return_value=InternalVideoPollResult(status=VideoStatus.PROCESSING))
|
|
|
|
|
|
monkeypatch.setattr(adapter, "prepare_poll_context", AsyncMock(return_value=prepared_ctx))
|
|
|
|
|
|
monkeypatch.setattr(adapter, "poll_task_http", poll_http)
|
2026-02-01 17:28:27 +08:00
|
|
|
|
|
2026-02-02 03:16:52 +08:00
|
|
|
|
result = await adapter._poll_task_status(MagicMock(), task)
|
2026-02-01 17:28:27 +08:00
|
|
|
|
assert result.status == VideoStatus.PROCESSING
|
2026-03-31 19:19:04 +08:00
|
|
|
|
poll_http.assert_awaited_once_with(prepared_ctx)
|
2026-03-11 15:05:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
|
async def test_update_task_after_poll_skips_terminal_cancelled_task(
|
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
finalize = AsyncMock()
|
|
|
|
|
|
adapter = VideoTaskPollerAdapter(finalize_video_task_fn=finalize)
|
|
|
|
|
|
|
|
|
|
|
|
cancelled_task = SimpleNamespace(
|
|
|
|
|
|
id="t1",
|
|
|
|
|
|
status=VideoStatus.CANCELLED.value,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
session = MagicMock()
|
|
|
|
|
|
session.__enter__.return_value = session
|
|
|
|
|
|
session.__exit__.return_value = None
|
|
|
|
|
|
session.get.return_value = cancelled_task
|
|
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("src.services.task.video.poller_adapter.create_session", lambda: session)
|
|
|
|
|
|
|
|
|
|
|
|
await adapter.update_task_after_poll(
|
|
|
|
|
|
task_id="t1",
|
|
|
|
|
|
result=InternalVideoPollResult(status=VideoStatus.COMPLETED),
|
|
|
|
|
|
ctx=None,
|
|
|
|
|
|
redis_client=None,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
finalize.assert_not_awaited()
|
|
|
|
|
|
session.commit.assert_not_called()
|
2026-03-21 12:57:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
|
async def test_video_poller_try_rust_payload_passes_proxy_snapshot(
|
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
|
) -> None:
|
2026-04-03 14:59:58 +08:00
|
|
|
|
from src.services.request import execution_runtime_client as runtime_mod
|
2026-03-21 12:57:09 +08:00
|
|
|
|
from src.services.task.video import poller_adapter as mod
|
|
|
|
|
|
|
|
|
|
|
|
adapter = VideoTaskPollerAdapter()
|
|
|
|
|
|
monkeypatch.setattr(mod.config, "executor_backend", "rust")
|
|
|
|
|
|
|
|
|
|
|
|
proxy_snapshot = object()
|
|
|
|
|
|
captured: dict[str, object] = {}
|
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
|
async def _fake_execute_sync_json(self: object, plan: object) -> ExecutionRuntimeSyncResult:
|
2026-03-21 12:57:09 +08:00
|
|
|
|
captured["plan"] = plan
|
2026-04-03 14:59:58 +08:00
|
|
|
|
return ExecutionRuntimeSyncResult(
|
|
|
|
|
|
status_code=200,
|
|
|
|
|
|
response_json={"id": "op_1", "done": False},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
|
runtime_mod.ExecutionRuntimeClient,
|
|
|
|
|
|
"execute_sync_json",
|
|
|
|
|
|
_fake_execute_sync_json,
|
|
|
|
|
|
)
|
2026-03-21 12:57:09 +08:00
|
|
|
|
|
|
|
|
|
|
ctx = VideoPollContext(
|
|
|
|
|
|
task_id="task-1",
|
|
|
|
|
|
external_task_id="vid_1",
|
|
|
|
|
|
provider_api_format="openai:video",
|
|
|
|
|
|
base_url="https://api.openai.com",
|
|
|
|
|
|
upstream_key="upstream",
|
|
|
|
|
|
headers={"authorization": "Bearer test"},
|
|
|
|
|
|
poll_count=0,
|
|
|
|
|
|
retry_count=0,
|
|
|
|
|
|
poll_interval_seconds=15,
|
|
|
|
|
|
max_poll_count=10,
|
|
|
|
|
|
current_status=VideoStatus.PROCESSING.value,
|
|
|
|
|
|
proxy_snapshot=proxy_snapshot,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
payload = await adapter._try_rust_poll_payload(
|
|
|
|
|
|
ctx=ctx,
|
|
|
|
|
|
url="https://api.openai.com/v1/videos/vid_1",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert payload == {"id": "op_1", "done": False}
|
|
|
|
|
|
assert getattr(captured["plan"], "method") == "GET"
|
|
|
|
|
|
assert getattr(captured["plan"], "proxy") is proxy_snapshot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
|
async def test_video_poller_openai_poll_prefers_rust_payload(
|
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
adapter = VideoTaskPollerAdapter()
|
|
|
|
|
|
|
|
|
|
|
|
rust_poll = AsyncMock(return_value={"id": "vid_1", "status": "processing"})
|
|
|
|
|
|
monkeypatch.setattr(adapter, "_try_rust_poll_payload", rust_poll)
|
|
|
|
|
|
|
|
|
|
|
|
normalized = InternalVideoPollResult(status=VideoStatus.PROCESSING, progress_percent=42)
|
|
|
|
|
|
normalizer = MagicMock(return_value=normalized)
|
|
|
|
|
|
monkeypatch.setattr(adapter._openai_normalizer, "video_poll_to_internal", normalizer)
|
|
|
|
|
|
|
|
|
|
|
|
ctx = VideoPollContext(
|
|
|
|
|
|
task_id="task-1",
|
|
|
|
|
|
external_task_id="vid_1",
|
|
|
|
|
|
provider_api_format="openai:video",
|
|
|
|
|
|
base_url="https://api.openai.com",
|
|
|
|
|
|
upstream_key="upstream",
|
|
|
|
|
|
headers={"authorization": "Bearer test"},
|
|
|
|
|
|
poll_count=0,
|
|
|
|
|
|
retry_count=0,
|
|
|
|
|
|
poll_interval_seconds=15,
|
|
|
|
|
|
max_poll_count=10,
|
|
|
|
|
|
current_status=VideoStatus.PROCESSING.value,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
result = await adapter._poll_openai_with_context(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
assert result is normalized
|
|
|
|
|
|
normalizer.assert_called_once_with({"id": "vid_1", "status": "processing"})
|
|
|
|
|
|
rust_poll.assert_awaited_once()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-03-31 19:19:04 +08:00
|
|
|
|
async def test_video_poller_openai_poll_requires_rust_executor_when_payload_missing(
|
2026-03-21 12:57:09 +08:00
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
adapter = VideoTaskPollerAdapter()
|
|
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(adapter, "_try_rust_poll_payload", AsyncMock(return_value=None))
|
|
|
|
|
|
|
|
|
|
|
|
ctx = VideoPollContext(
|
|
|
|
|
|
task_id="task-2",
|
|
|
|
|
|
external_task_id="vid_2",
|
|
|
|
|
|
provider_api_format="openai:video",
|
|
|
|
|
|
base_url="https://api.openai.com",
|
|
|
|
|
|
upstream_key="upstream",
|
|
|
|
|
|
headers={"authorization": "Bearer test"},
|
|
|
|
|
|
poll_count=0,
|
|
|
|
|
|
retry_count=0,
|
|
|
|
|
|
poll_interval_seconds=15,
|
|
|
|
|
|
max_poll_count=10,
|
|
|
|
|
|
current_status=VideoStatus.PROCESSING.value,
|
|
|
|
|
|
proxy_config={"enabled": True, "url": "http://proxy.test:8080"},
|
|
|
|
|
|
delegate_config={"node_id": "node-1", "tunnel": True},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-31 19:19:04 +08:00
|
|
|
|
with pytest.raises(Exception) as exc_info:
|
|
|
|
|
|
await adapter._poll_openai_with_context(ctx)
|
2026-03-21 12:57:09 +08:00
|
|
|
|
|
2026-03-31 19:19:04 +08:00
|
|
|
|
assert "Rust executor" in str(exc_info.value)
|