astrbot_plugin_imagestudio/tests/test_cost_service.py

146 行
5.5 KiB
Python

2026-07-06 02:34:43 +08:00
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from backend.routes.cost_routes import get_cost_usage
2026-07-06 02:40:18 +08:00
from backend.routes.cost_routes import check_cost_guard
from backend.services.cost_guard_service import CostGuardService
2026-07-06 02:34:43 +08:00
from backend.services.cost_service import CostService
from core.identity import RequestContext
from database.db_helper import Database, DatabaseSettings, SqliteSettings
from database.repositories.image_repository import ImageRepository
class CostServiceTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.database = Database(
DatabaseSettings(sqlite=SqliteSettings(path=str(Path(self.tmp.name) / "imagestudio.db")))
)
self.database.initialize()
self.image_repository = ImageRepository(self.database)
self.ctx = RequestContext.from_astrbot(username="admin", user_id="user_001", permissions={"can_write"})
def tearDown(self):
self.tmp.cleanup()
def test_usage_summary_groups_provider_usage_and_estimated_cost(self):
for index, tokens in enumerate((1000, 500), start=1):
self.image_repository.create(
{
"image_id": f"img_b_{index}",
"task_id": "task_b_1",
"profile_id": "profile_001",
"workflow_id": "workflow_001",
"node_id": "node_001",
"image_uri": f"storage://images/b_{index}.png",
"model_route": "image_high",
"metadata": {
"provider_id": "site_b",
"provider_model": "image2",
"usage": {"total_tokens": tokens, "input_tokens": tokens // 2, "output_tokens": tokens // 2},
},
}
)
self.image_repository.create(
{
"image_id": "img_a_1",
"task_id": "task_a_1",
"profile_id": "profile_001",
"workflow_id": "workflow_001",
"node_id": "node_002",
"image_uri": "storage://images/a_1.png",
"model_route": "image_low",
"metadata": {
"provider_id": "site_a",
"provider_model": "gpt-image-1.5",
"usage": {"total_tokens": 200},
},
}
)
summary = CostService(self.database).usage_summary(
profile_id="profile_001",
unit_prices={
"site_b": 0.045,
"site_a:gpt-image-1.5": {"per_image": 0.01},
},
)
self.assertEqual(summary["totals"]["image_count"], 3)
self.assertEqual(summary["totals"]["task_count"], 2)
self.assertEqual(summary["totals"]["total_tokens"], 1700)
self.assertEqual(summary["totals"]["estimated_cost"], 0.055)
site_b = next(group for group in summary["groups"] if group["provider_id"] == "site_b")
self.assertEqual(site_b["image_count"], 2)
self.assertEqual(site_b["task_count"], 1)
self.assertEqual(site_b["estimated_cost"], 0.045)
def test_cost_usage_route_requires_auth_and_accepts_price_table(self):
self.image_repository.create(
{
"image_id": "img_b_1",
"task_id": "task_b_1",
"image_uri": "storage://images/b_1.png",
"model_route": "image_high",
"metadata": {"provider_id": "site_b", "provider_model": "image2", "usage": {"total_tokens": 100}},
}
)
response = get_cost_usage(
self.ctx,
{"unit_prices": {"site_b": 0.045}},
CostService(self.database),
)
anonymous = get_cost_usage(RequestContext(), {}, CostService(self.database))
self.assertTrue(response["ok"])
self.assertEqual(response["data"]["totals"]["estimated_cost"], 0.045)
self.assertFalse(anonymous["ok"])
self.assertEqual(anonymous["error_code"], "UNAUTHORIZED")
2026-07-06 02:40:18 +08:00
def test_cost_guard_blocks_projected_budget_overrun(self):
self.image_repository.create(
{
"image_id": "img_b_1",
"task_id": "task_b_1",
"image_uri": "storage://images/b_1.png",
"model_route": "image_high",
"metadata": {"provider_id": "site_b", "provider_model": "image2", "usage": {"total_tokens": 100}},
}
)
guard = CostGuardService(CostService(self.database))
result = guard.check_generation_budget(
provider_id="site_b",
provider_model="image2",
model_route="image_high",
planned_task_count=1,
planned_image_count=1,
max_task_count=1,
max_estimated_cost=0.05,
unit_prices={"site_b": 0.045},
)
route_result = check_cost_guard(
self.ctx,
{
"provider_id": "site_b",
"provider_model": "image2",
"max_estimated_cost": 0.05,
"unit_prices": {"site_b": 0.045},
},
guard,
)
self.assertFalse(result["allowed"])
self.assertIn("task_budget_exceeded", result["violations"])
self.assertIn("cost_budget_exceeded", result["violations"])
self.assertTrue(route_result["ok"])
self.assertFalse(route_result["data"]["allowed"])
2026-07-06 02:34:43 +08:00
if __name__ == "__main__":
unittest.main()