from __future__ import annotations import tempfile import unittest from pathlib import Path from backend.routes.cost_routes import get_cost_usage from backend.routes.cost_routes import check_cost_guard from backend.services.cost_guard_service import CostGuardService 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() conn = self.database.connect() try: conn.execute( """ INSERT INTO character_profiles ( profile_id, name, owner_platform, owner_user_id ) VALUES (?, ?, ?, ?) """, ("profile_001", "Cost Test", "astrbot", "user_001"), ) conn.commit() finally: conn.close() 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", "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", "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"], 1200) 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["total_tokens"], 1000) self.assertEqual(site_b["estimated_cost"], 0.045) def test_usage_summary_counts_identical_multi_image_request_usage_once(self): for index in range(3): self.image_repository.create( { "image_id": f"img_multi_{index}", "task_id": "task_multi", "profile_id": "profile_001", "image_uri": f"storage://images/multi_{index}.png", "model_route": "image_high", "metadata": { "provider_id": "site_b", "provider_model": "image2", "usage": {"total_tokens": 900, "input_tokens": 600, "output_tokens": 300}, }, } ) summary = CostService(self.database).usage_summary(profile_id="profile_001") self.assertEqual(summary["totals"]["image_count"], 3) self.assertEqual(summary["totals"]["task_count"], 1) self.assertEqual(summary["totals"]["total_tokens"], 900) self.assertEqual(summary["totals"]["input_tokens"], 600) self.assertEqual(summary["totals"]["output_tokens"], 300) def test_usage_summary_does_not_mislabel_missing_provider_as_mock(self): self.image_repository.create( { "image_id": "img_unknown_provider", "task_id": "task_unknown_provider", "profile_id": "profile_001", "image_uri": "storage://images/unknown.png", "model_route": "image_default", "metadata": {}, } ) summary = CostService(self.database).usage_summary(profile_id="profile_001") self.assertEqual(summary["groups"][0]["provider_id"], "unknown") 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") 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"]) if __name__ == "__main__": unittest.main()