104 行
4 KiB
Python
104 行
4 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from backend.routes.cost_routes import get_cost_usage
|
|
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")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|