Files
xiaoxia-saas/tests/unit/test_tts_voice_clone_points.py
T
xiaoxia-agent 3862d9158f
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 20s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m11s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m41s
AI Code Review / AI Code Review (pull_request) Successful in 7m7s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 14s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 17s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Failing after 13m0s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 5m9s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 5m8s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 14h12m47s
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 14h12m8s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Failing after 13h56m59s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 14h12m3s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 14h12m3s
CI/CD Pipeline / Check push changed paths (pull_request) Failing after 14h19m17s
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Failing after 13h56m58s
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Failing after 13h56m58s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 13h56m26s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 13h56m27s
test(points): add unit tests for TTS + voice_clone preview points gating
- Estimate minutes (ceil(chars/240), min 1) parametrized
- Disabled gate (zero cost), min cost, insufficient (402), member discount
- Refund called on exception
- voice_clone_synth scene cost validation
2026-09-16 00:36:13 +08:00

101 lines
3.1 KiB
Python

"""TTS + voice_clone 积分扣点单元测试 (#1895 P2 step 2.1)"""
from __future__ import annotations
import math
from unittest.mock import MagicMock
import pytest
from fastapi import HTTPException
def _make_user(user_id="user-1", is_member=False, member_type=None):
u = MagicMock()
u.id = user_id
u.is_member = is_member
u.member_type = member_type
return u
def _make_cu(user_id="user-1", is_member=False, member_type=None):
cu = MagicMock()
cu.user = _make_user(user_id, is_member, member_type)
return cu
def _est_minutes(chars: int) -> float:
return max(1.0, math.ceil(chars / 240))
class TestEstimateMinutes:
@pytest.mark.parametrize(
"chars,expected",
[
(1, 1.0),
(240, 1.0),
(241, 2.0),
(480, 2.0),
(481, 3.0),
(1000, 5.0),
],
)
def test_estimate(self, chars, expected):
assert _est_minutes(chars) == expected
class TestTtsSynthesizePointsDeduction:
def _deduct(self, text, cu, db, enabled=True, success=True, balance=100):
from packages.domain.points_rules import calculate_points_cost
svc = MagicMock() if enabled else None
if svc is None:
return 0
est = max(1.0, math.ceil(len(text) / 240))
cost = calculate_points_cost(
"ai_voice",
is_member=getattr(cu.user, "is_member", False),
duration_minutes=est,
member_type=getattr(cu.user, "member_type", None),
)
svc.deduct_points.return_value = {"success": success, "balance": balance}
res = svc.deduct_points(cu.user.id, cost, "ai_voice", db)
if not res["success"]:
raise HTTPException(status_code=402, detail={"code": "INSUFFICIENT_POINTS"})
return cost
def test_disabled_no_deduction(self):
assert self._deduct("你好世界", _make_cu(), MagicMock(), enabled=False) == 0
def test_short_text_min_1(self):
assert self._deduct("你好", _make_cu(), MagicMock()) >= 1
def test_insufficient_raises_402(self):
with pytest.raises(HTTPException) as ei:
self._deduct("你好" * 200, _make_cu(), MagicMock(), success=False, balance=0)
assert ei.value.status_code == 402
def test_member_cheaper(self):
cm = self._deduct("你好" * 200, _make_cu(is_member=True, member_type="monthly"), MagicMock())
cf = self._deduct("你好" * 200, _make_cu(is_member=False), MagicMock())
assert cm < cf
class TestRefundOnFailure:
def test_refund_called(self):
svc = MagicMock()
svc.deduct_points.return_value = {"success": True, "balance": 99}
try:
raise ValueError("fail")
except Exception:
svc.refund_points("u1", 5, "ai_voice", MagicMock(), ref_id="job1")
svc.refund_points.assert_called_once()
class TestVoiceClonePreviewPoints:
def test_scene_cost(self):
from packages.domain.points_rules import calculate_points_cost
est = max(1.0, math.ceil(100 / 240))
cost = calculate_points_cost("voice_clone_synth", is_member=False, duration_minutes=est)
assert cost >= 1