style: auto-format with black + isort + ruff + prettier [skip ci-format-check]
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 3s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m12s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m22s
AI Code Review / AI Code Review (pull_request) Successful in 6m28s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m40s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 4m42s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 2m58s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 5m30s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 5m42s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 6m27s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate - Security (pull_request) Successful in 12m27s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 2s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 6m45s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 25s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 40s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 3s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m12s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m22s
AI Code Review / AI Code Review (pull_request) Successful in 6m28s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m40s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 4m42s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 2m58s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 5m30s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 5m42s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 6m27s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate - Security (pull_request) Successful in 12m27s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 2s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 6m45s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 25s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 40s
This commit is contained in:
@@ -14,7 +14,6 @@ import logging
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from app.auth import AuthenticatedUser, get_current_user
|
||||
from packages.middleware.points_gate import points_gate
|
||||
from app.dependencies import get_db_session
|
||||
from app.schemas.ai_avatar_render import (
|
||||
AiAvatarRenderJobResponse,
|
||||
@@ -30,6 +29,8 @@ from app.services.ai_avatar_render_service import (
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from packages.middleware.points_gate import points_gate
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -8,7 +8,6 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from app.auth import AuthenticatedUser, get_current_user
|
||||
from packages.middleware.points_gate import points_gate
|
||||
from app.core.storage import get_storage_service
|
||||
from app.core.task_enqueue import (
|
||||
GLOBAL_PENDING_LIMIT,
|
||||
@@ -44,6 +43,7 @@ from packages.application import (
|
||||
GetGenerationTaskUseCase,
|
||||
ListGeneratedVideosByTaskUseCase,
|
||||
)
|
||||
from packages.middleware.points_gate import points_gate
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ from typing import Any
|
||||
|
||||
from app.api.routes._helpers import check_project_access
|
||||
from app.auth import AuthenticatedUser, get_current_user
|
||||
from packages.middleware.points_gate import points_gate
|
||||
from app.core.storage import OSSStorageService, get_storage_service
|
||||
from app.core.task_enqueue import (
|
||||
GLOBAL_PENDING_LIMIT,
|
||||
@@ -43,6 +42,7 @@ from packages.application import (
|
||||
ListGeneratedVideosByTaskUseCase,
|
||||
)
|
||||
from packages.domain.smart_match import smart_select_assets
|
||||
from packages.middleware.points_gate import points_gate
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -91,13 +91,16 @@ def points_gate(
|
||||
is_async = asyncio.iscoroutinefunction(func)
|
||||
|
||||
if is_async:
|
||||
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
if not _pg_enabled(): # noqa: F821
|
||||
return await func(*args, **kwargs)
|
||||
return await _pg_execute( # noqa: F821
|
||||
func, args, kwargs, scene_key, per_unit, unit_field, quantity_field, True
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
if not _pg_enabled(): # noqa: F821
|
||||
return func(*args, **_pg_filter(func, kwargs)) # noqa: F821
|
||||
@@ -126,8 +129,7 @@ def points_gate(
|
||||
# 为稳妥起见,直接把 wrapper code 的 co_names 映射到新名——复杂度过高,
|
||||
# 这里采用「确保短名没冲突」策略:如果冲突就抛异常让开发者改名。
|
||||
raise RuntimeError(
|
||||
f"points_gate: name collision in {func.__module__}.{func.__name__}: "
|
||||
f"'{k}' already defined"
|
||||
f"points_gate: name collision in {func.__module__}.{func.__name__}: " f"'{k}' already defined"
|
||||
)
|
||||
merged_globals[k] = v
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""AI数字人渲染 积分扣点单元测试 (#1895 P2 step 2.6)"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
@@ -17,17 +18,19 @@ def _enable(monkeypatch):
|
||||
class TestAiAvatarRenderPoints:
|
||||
def test_ai_digital_human_per_unit(self):
|
||||
from packages.domain.points_rules import calculate_points_cost
|
||||
|
||||
cost = calculate_points_cost("ai_digital_human", is_member=False, duration_minutes=1)
|
||||
assert cost >= 15
|
||||
|
||||
def test_decorator_attached(self):
|
||||
from app.api.routes.ai_avatar_render import create_render_job
|
||||
|
||||
assert hasattr(create_render_job, "__wrapped__"), "missing @points_gate"
|
||||
|
||||
def test_insufficient_raises_402(self):
|
||||
from fastapi import HTTPException
|
||||
from app.api.routes.ai_avatar_render import create_render_job
|
||||
from app.schemas.ai_avatar_render import CreateAiAvatarRenderRequest
|
||||
from fastapi import HTTPException
|
||||
|
||||
db = MagicMock()
|
||||
cu = MagicMock()
|
||||
|
||||
@@ -583,7 +583,6 @@ def _disable_points_gate(monkeypatch):
|
||||
yield
|
||||
|
||||
|
||||
|
||||
def _make_user(user_id="test_user_001"):
|
||||
"""构造 mock AuthenticatedUser"""
|
||||
mock_user = MagicMock()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""视频预览生成 积分扣点单元测试 (#1895 P2 step 2.5)"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
@@ -17,13 +18,14 @@ def _enable(monkeypatch):
|
||||
class TestGenerationPreviewPoints:
|
||||
def test_ai_video_cost(self):
|
||||
from packages.domain.points_rules import calculate_points_cost
|
||||
|
||||
assert calculate_points_cost("ai_video", is_member=False) == 4
|
||||
assert calculate_points_cost("ai_video", is_member=True, member_type="monthly") == 2
|
||||
|
||||
def test_insufficient_raises_402(self):
|
||||
from fastapi import HTTPException
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
from app.schemas.generation_task import CreatePreviewGenerationTaskRequest
|
||||
from fastapi import HTTPException
|
||||
|
||||
db = MagicMock()
|
||||
cu = MagicMock()
|
||||
@@ -38,7 +40,9 @@ class TestGenerationPreviewPoints:
|
||||
MS.return_value = svc
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
create_preview_generation_task(
|
||||
request=req, authenticated_user=cu, db=db,
|
||||
request=req,
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
generation_task_repository=MagicMock(),
|
||||
asset_repo=MagicMock(),
|
||||
)
|
||||
@@ -46,4 +50,5 @@ class TestGenerationPreviewPoints:
|
||||
|
||||
def test_decorator_attached(self):
|
||||
from app.api.routes.generation_preview import create_preview_generation_task
|
||||
|
||||
assert hasattr(create_preview_generation_task, "__wrapped__"), "missing @points_gate"
|
||||
|
||||
@@ -6,6 +6,7 @@ from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
import packages.middleware.points_gate as _pg_module
|
||||
from packages.application.generation_tasks import (
|
||||
CreateGenerationTaskCommand,
|
||||
CreateGenerationTaskUseCase,
|
||||
@@ -17,8 +18,6 @@ from packages.application.generation_tasks import (
|
||||
)
|
||||
from packages.domain import GenerationTask
|
||||
|
||||
import packages.middleware.points_gate as _pg_module
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _disable_points_gate(monkeypatch):
|
||||
@@ -27,7 +26,6 @@ def _disable_points_gate(monkeypatch):
|
||||
yield
|
||||
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_repo():
|
||||
return MagicMock()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""视频生成 积分扣点单元测试 (#1895 P2 step 2.4)"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
@@ -17,19 +18,21 @@ def _enable(monkeypatch):
|
||||
class TestGenerationTasksPoints:
|
||||
def test_ai_video_base_cost(self):
|
||||
from packages.domain.points_rules import calculate_points_cost
|
||||
|
||||
assert calculate_points_cost("ai_video", is_member=False) == 4
|
||||
assert calculate_points_cost("ai_video", is_member=True, member_type="monthly") == 2
|
||||
|
||||
def test_ai_video_quantity_scales(self):
|
||||
from packages.domain.points_rules import calculate_points_cost
|
||||
|
||||
c1 = calculate_points_cost("ai_video", is_member=False, quantity=1)
|
||||
c3 = calculate_points_cost("ai_video", is_member=False, quantity=3)
|
||||
assert c3 > c1
|
||||
|
||||
def test_insufficient_raises_402(self):
|
||||
from fastapi import HTTPException
|
||||
from app.api.routes.generation_tasks import create_generation_task
|
||||
from app.schemas.generation_task import CreateGenerationTaskRequest
|
||||
from fastapi import HTTPException
|
||||
|
||||
db = MagicMock()
|
||||
cu = MagicMock()
|
||||
@@ -44,12 +47,17 @@ class TestGenerationTasksPoints:
|
||||
MS.return_value = svc
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
create_generation_task(
|
||||
request=req, authenticated_user=cu, db=db,
|
||||
generation_task_repository=MagicMock(), project_repository=MagicMock(),
|
||||
asset_library_repository=MagicMock(), asset_repository=MagicMock(),
|
||||
request=req,
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
generation_task_repository=MagicMock(),
|
||||
project_repository=MagicMock(),
|
||||
asset_library_repository=MagicMock(),
|
||||
asset_repository=MagicMock(),
|
||||
)
|
||||
assert ei.value.status_code == 402
|
||||
|
||||
def test_decorator_attached(self):
|
||||
from app.api.routes.generation_tasks import create_generation_task
|
||||
|
||||
assert hasattr(create_generation_task, "__wrapped__"), "missing @points_gate"
|
||||
|
||||
@@ -99,7 +99,7 @@ class TestLipsyncPointsDeduction:
|
||||
# ── 直接调用 create_lipsync_job 覆盖扣点/402/退费分支 ──
|
||||
import importlib
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
import packages.middleware.points_gate as _pg_module
|
||||
|
||||
@@ -113,9 +113,16 @@ def _do_enable(monkeypatch):
|
||||
def _body(**kw):
|
||||
b = MagicMock()
|
||||
defaults = dict(
|
||||
video_url="http://x/v.mp4", audio_url=None, audio_duration=None,
|
||||
sentence_timings=None, voice_id=None, script_text="你好世界",
|
||||
speed=1.0, emotion="", enable_video_loop=False, project_id=None,
|
||||
video_url="http://x/v.mp4",
|
||||
audio_url=None,
|
||||
audio_duration=None,
|
||||
sentence_timings=None,
|
||||
voice_id=None,
|
||||
script_text="你好世界",
|
||||
speed=1.0,
|
||||
emotion="",
|
||||
enable_video_loop=False,
|
||||
project_id=None,
|
||||
)
|
||||
defaults.update(kw)
|
||||
for k, v in defaults.items():
|
||||
@@ -135,13 +142,16 @@ class TestLipsyncEndpointPoints:
|
||||
def test_insufficient_raises_402(self, monkeypatch):
|
||||
_do_enable(monkeypatch)
|
||||
from app.api.routes.lipsync import create_lipsync_job
|
||||
|
||||
db = MagicMock()
|
||||
svc = MagicMock()
|
||||
ps = MagicMock()
|
||||
ps.deduct_points.return_value = {"success": False, "balance": 0}
|
||||
fs = MagicMock(points_enabled=True)
|
||||
with patch("app.api.routes.lipsync.PointsService", return_value=ps), \
|
||||
patch("app.api.routes.lipsync.settings", fs):
|
||||
with (
|
||||
patch("app.api.routes.lipsync.PointsService", return_value=ps),
|
||||
patch("app.api.routes.lipsync.settings", fs),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
create_lipsync_job(body=_body(script_text="你" * 500), current_user=_cu(), db=db, svc=svc)
|
||||
assert ei.value.status_code == 402
|
||||
@@ -149,14 +159,17 @@ class TestLipsyncEndpointPoints:
|
||||
def test_value_error_refunds(self, monkeypatch):
|
||||
_do_enable(monkeypatch)
|
||||
from app.api.routes.lipsync import create_lipsync_job
|
||||
|
||||
db = MagicMock()
|
||||
svc = MagicMock()
|
||||
svc.create_job.side_effect = ValueError("bad input")
|
||||
ps = MagicMock()
|
||||
ps.deduct_points.return_value = {"success": True, "balance": 99}
|
||||
fs = MagicMock(points_enabled=True)
|
||||
with patch("app.api.routes.lipsync.PointsService", return_value=ps), \
|
||||
patch("app.api.routes.lipsync.settings", fs):
|
||||
with (
|
||||
patch("app.api.routes.lipsync.PointsService", return_value=ps),
|
||||
patch("app.api.routes.lipsync.settings", fs),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
create_lipsync_job(body=_body(), current_user=_cu(), db=db, svc=svc)
|
||||
assert ei.value.status_code == 400
|
||||
@@ -166,14 +179,17 @@ class TestLipsyncEndpointPoints:
|
||||
_do_enable(monkeypatch)
|
||||
from app.api.routes.lipsync import create_lipsync_job
|
||||
from app.services.mediakit_client import MediaKitError
|
||||
|
||||
db = MagicMock()
|
||||
svc = MagicMock()
|
||||
svc.create_job.side_effect = MediaKitError("fail", code="InvalidInput")
|
||||
ps = MagicMock()
|
||||
ps.deduct_points.return_value = {"success": True, "balance": 99}
|
||||
fs = MagicMock(points_enabled=True)
|
||||
with patch("app.api.routes.lipsync.PointsService", return_value=ps), \
|
||||
patch("app.api.routes.lipsync.settings", fs):
|
||||
with (
|
||||
patch("app.api.routes.lipsync.PointsService", return_value=ps),
|
||||
patch("app.api.routes.lipsync.settings", fs),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
create_lipsync_job(body=_body(), current_user=_cu(), db=db, svc=svc)
|
||||
assert ei.value.status_code == 400
|
||||
@@ -182,14 +198,17 @@ class TestLipsyncEndpointPoints:
|
||||
def test_generic_exception_refunds(self, monkeypatch):
|
||||
_do_enable(monkeypatch)
|
||||
from app.api.routes.lipsync import create_lipsync_job
|
||||
|
||||
db = MagicMock()
|
||||
svc = MagicMock()
|
||||
svc.create_job.side_effect = RuntimeError("boom")
|
||||
ps = MagicMock()
|
||||
ps.deduct_points.return_value = {"success": True, "balance": 99}
|
||||
fs = MagicMock(points_enabled=True)
|
||||
with patch("app.api.routes.lipsync.PointsService", return_value=ps), \
|
||||
patch("app.api.routes.lipsync.settings", fs):
|
||||
with (
|
||||
patch("app.api.routes.lipsync.PointsService", return_value=ps),
|
||||
patch("app.api.routes.lipsync.settings", fs),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
create_lipsync_job(body=_body(), current_user=_cu(), db=db, svc=svc)
|
||||
assert ei.value.status_code == 400
|
||||
@@ -198,7 +217,9 @@ class TestLipsyncEndpointPoints:
|
||||
def test_audio_duration_estimation(self, monkeypatch):
|
||||
_do_enable(monkeypatch)
|
||||
from app.api.routes.lipsync import create_lipsync_job
|
||||
|
||||
from packages.domain.points_rules import calculate_points_cost
|
||||
|
||||
db = MagicMock()
|
||||
svc = MagicMock()
|
||||
job = SimpleNamespace(id="job-1", status="queued")
|
||||
@@ -206,11 +227,15 @@ class TestLipsyncEndpointPoints:
|
||||
ps = MagicMock()
|
||||
ps.deduct_points.return_value = {"success": True, "balance": 99}
|
||||
fs = MagicMock(points_enabled=True)
|
||||
with patch("app.api.routes.lipsync.PointsService", return_value=ps), \
|
||||
patch("app.api.routes.lipsync.settings", fs):
|
||||
with (
|
||||
patch("app.api.routes.lipsync.PointsService", return_value=ps),
|
||||
patch("app.api.routes.lipsync.settings", fs),
|
||||
):
|
||||
create_lipsync_job(
|
||||
body=_body(audio_url="http://x/a.mp3", audio_duration=180, script_text=None),
|
||||
current_user=_cu(), db=db, svc=svc,
|
||||
current_user=_cu(),
|
||||
db=db,
|
||||
svc=svc,
|
||||
)
|
||||
# 180 seconds -> 3 minutes; assert deduct called with cost >= 15*3
|
||||
args = ps.deduct_points.call_args[0]
|
||||
|
||||
@@ -58,14 +58,15 @@ class TestEstimateMinutes:
|
||||
|
||||
|
||||
class TestTtsSynthesizePointsDeduction:
|
||||
def _setup(self, text="你好", deduct_success=True, balance=0,
|
||||
start_synth_raises=None, send_task_raises=None):
|
||||
def _setup(self, text="你好", deduct_success=True, balance=0, start_synth_raises=None, send_task_raises=None):
|
||||
db = MagicMock()
|
||||
cu = _make_cu()
|
||||
repo = MagicMock()
|
||||
import enum
|
||||
|
||||
class _S(enum.Enum):
|
||||
processing = "processing"
|
||||
|
||||
job = SimpleNamespace(id="job-1", status=_S.processing, metadata={})
|
||||
uc = MagicMock()
|
||||
uc.execute.return_value = job
|
||||
@@ -82,18 +83,23 @@ class TestTtsSynthesizePointsDeduction:
|
||||
return db, cu, repo, uc, wf, vc_repo, svc, fake_settings, job
|
||||
|
||||
def test_insufficient_raises_402(self):
|
||||
db, cu, repo, uc, wf, vc_repo, svc, fs, _ = self._setup(
|
||||
text="你好" * 200, deduct_success=False, balance=0)
|
||||
db, cu, repo, uc, wf, vc_repo, svc, fs, _ = self._setup(text="你好" * 200, deduct_success=False, balance=0)
|
||||
from app.api.routes.tts import synthesize
|
||||
with patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc), \
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf), \
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.tts.settings", fs):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc),
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf),
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc),
|
||||
patch("app.api.routes.tts.settings", fs),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
synthesize(
|
||||
request=_make_request(text="你好" * 200),
|
||||
authenticated_user=cu, db=db, repository=repo,
|
||||
cosyvoice_service=MagicMock(), voice_clone_repo=vc_repo,
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice_service=MagicMock(),
|
||||
voice_clone_repo=vc_repo,
|
||||
)
|
||||
assert ei.value.status_code == 402
|
||||
assert ei.value.detail["code"] == "INSUFFICIENT_POINTS"
|
||||
@@ -101,15 +107,21 @@ class TestTtsSynthesizePointsDeduction:
|
||||
def test_success_deducts_points(self):
|
||||
db, cu, repo, uc, wf, vc_repo, svc, fs, job = self._setup()
|
||||
from app.api.routes.tts import synthesize
|
||||
with patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc), \
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf), \
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.tts.celery_app.send_task") as _st, \
|
||||
patch("app.api.routes.tts.settings", fs):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc),
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf),
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc),
|
||||
patch("app.api.routes.tts.celery_app.send_task") as _st,
|
||||
patch("app.api.routes.tts.settings", fs),
|
||||
):
|
||||
resp = synthesize(
|
||||
request=_make_request(text="测试"),
|
||||
authenticated_user=cu, db=db, repository=repo,
|
||||
cosyvoice_service=MagicMock(), voice_clone_repo=vc_repo,
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice_service=MagicMock(),
|
||||
voice_clone_repo=vc_repo,
|
||||
)
|
||||
svc.deduct_points.assert_called_once()
|
||||
assert resp.job_id == job.id
|
||||
@@ -117,35 +129,48 @@ class TestTtsSynthesizePointsDeduction:
|
||||
def test_synthesis_failure_refunds(self):
|
||||
db, cu, repo, uc, wf, vc_repo, svc, fs, _ = self._setup(start_synth_raises=RuntimeError("boom"))
|
||||
from app.api.routes.tts import synthesize
|
||||
with patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc), \
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf), \
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.tts.celery_app.send_task"), \
|
||||
patch("app.api.routes.tts.settings", fs):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc),
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf),
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc),
|
||||
patch("app.api.routes.tts.celery_app.send_task"),
|
||||
patch("app.api.routes.tts.settings", fs),
|
||||
):
|
||||
synthesize(
|
||||
request=_make_request(text="测试"),
|
||||
authenticated_user=cu, db=db, repository=repo,
|
||||
cosyvoice_service=MagicMock(), voice_clone_repo=vc_repo,
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice_service=MagicMock(),
|
||||
voice_clone_repo=vc_repo,
|
||||
)
|
||||
assert svc.refund_points.called
|
||||
|
||||
def test_celery_send_failure_refunds(self):
|
||||
db, cu, repo, uc, wf, vc_repo, svc, fs, _ = self._setup(send_task_raises=RuntimeError("celery down"))
|
||||
from app.api.routes.tts import synthesize
|
||||
with patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc), \
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf), \
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.tts.celery_app.send_task", side_effect=RuntimeError("celery down")), \
|
||||
patch("app.api.routes.tts.settings", fs):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.tts.CreateTTSJobUseCase", return_value=uc),
|
||||
patch("app.api.routes.tts.TTSWorkflowService", return_value=wf),
|
||||
patch("app.api.routes.tts.PointsService", return_value=svc),
|
||||
patch("app.api.routes.tts.celery_app.send_task", side_effect=RuntimeError("celery down")),
|
||||
patch("app.api.routes.tts.settings", fs),
|
||||
):
|
||||
synthesize(
|
||||
request=_make_request(text="测试"),
|
||||
authenticated_user=cu, db=db, repository=repo,
|
||||
cosyvoice_service=MagicMock(), voice_clone_repo=vc_repo,
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice_service=MagicMock(),
|
||||
voice_clone_repo=vc_repo,
|
||||
)
|
||||
assert svc.refund_points.called
|
||||
|
||||
def test_member_cheaper(self):
|
||||
from packages.domain.points_rules import calculate_points_cost
|
||||
|
||||
cf = calculate_points_cost("ai_voice", is_member=False, duration_minutes=2)
|
||||
cm = calculate_points_cost("ai_voice", is_member=True, member_type="monthly", duration_minutes=2)
|
||||
assert cm < cf
|
||||
@@ -172,29 +197,48 @@ class TestVoiceClonePreviewPoints:
|
||||
def test_insufficient_raises_402(self):
|
||||
db, cu, repo, uc, cosy, svc, fs = self._setup(deduct_success=False, balance=0)
|
||||
from app.api.routes.voice_clones import get_voice_clone_preview
|
||||
with patch("app.api.routes.voice_clones.GetVoiceCloneUseCase", return_value=uc), \
|
||||
patch("app.api.routes.voice_clones.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.voice_clones.settings", fs), \
|
||||
patch("app.api.routes.voice_clones._clone_preview_cache", {}):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.voice_clones.GetVoiceCloneUseCase", return_value=uc),
|
||||
patch("app.api.routes.voice_clones.PointsService", return_value=svc),
|
||||
patch("app.api.routes.voice_clones.settings", fs),
|
||||
patch("app.api.routes.voice_clones._clone_preview_cache", {}),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
get_voice_clone_preview(
|
||||
clone_id="c1", text="你好", speed=1.0, emotion="",
|
||||
authenticated_user=cu, db=db, repository=repo, cosyvoice=cosy,
|
||||
clone_id="c1",
|
||||
text="你好",
|
||||
speed=1.0,
|
||||
emotion="",
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice=cosy,
|
||||
)
|
||||
assert ei.value.status_code == 402
|
||||
|
||||
def test_synth_cosyvoice_error_refunds_and_raises_502(self):
|
||||
from packages.application.cosyvoice_service import CosyVoiceError
|
||||
|
||||
db, cu, repo, uc, cosy, svc, fs = self._setup(synth_raises=CosyVoiceError("fail"))
|
||||
from app.api.routes.voice_clones import get_voice_clone_preview
|
||||
with patch("app.api.routes.voice_clones.GetVoiceCloneUseCase", return_value=uc), \
|
||||
patch("app.api.routes.voice_clones.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.voice_clones.settings", fs), \
|
||||
patch("app.api.routes.voice_clones._clone_preview_cache", {}):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.voice_clones.GetVoiceCloneUseCase", return_value=uc),
|
||||
patch("app.api.routes.voice_clones.PointsService", return_value=svc),
|
||||
patch("app.api.routes.voice_clones.settings", fs),
|
||||
patch("app.api.routes.voice_clones._clone_preview_cache", {}),
|
||||
):
|
||||
with pytest.raises(HTTPException) as ei:
|
||||
get_voice_clone_preview(
|
||||
clone_id="c1", text="你好", speed=1.0, emotion="",
|
||||
authenticated_user=cu, db=db, repository=repo, cosyvoice=cosy,
|
||||
clone_id="c1",
|
||||
text="你好",
|
||||
speed=1.0,
|
||||
emotion="",
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice=cosy,
|
||||
)
|
||||
assert ei.value.status_code == 502
|
||||
assert svc.refund_points.called
|
||||
@@ -202,13 +246,22 @@ class TestVoiceClonePreviewPoints:
|
||||
def test_success_returns_audio(self):
|
||||
db, cu, repo, uc, cosy, svc, fs = self._setup()
|
||||
from app.api.routes.voice_clones import get_voice_clone_preview
|
||||
with patch("app.api.routes.voice_clones.GetVoiceCloneUseCase", return_value=uc), \
|
||||
patch("app.api.routes.voice_clones.PointsService", return_value=svc), \
|
||||
patch("app.api.routes.voice_clones.settings", fs), \
|
||||
patch("app.api.routes.voice_clones._clone_preview_cache", {}):
|
||||
|
||||
with (
|
||||
patch("app.api.routes.voice_clones.GetVoiceCloneUseCase", return_value=uc),
|
||||
patch("app.api.routes.voice_clones.PointsService", return_value=svc),
|
||||
patch("app.api.routes.voice_clones.settings", fs),
|
||||
patch("app.api.routes.voice_clones._clone_preview_cache", {}),
|
||||
):
|
||||
resp = get_voice_clone_preview(
|
||||
clone_id="c1", text="你好", speed=1.0, emotion="",
|
||||
authenticated_user=cu, db=db, repository=repo, cosyvoice=cosy,
|
||||
clone_id="c1",
|
||||
text="你好",
|
||||
speed=1.0,
|
||||
emotion="",
|
||||
authenticated_user=cu,
|
||||
db=db,
|
||||
repository=repo,
|
||||
cosyvoice=cosy,
|
||||
)
|
||||
svc.deduct_points.assert_called_once()
|
||||
assert resp.audio_url.startswith("http")
|
||||
|
||||
Reference in New Issue
Block a user