github-actions[bot] commited on
Commit
2d81900
·
1 Parent(s): 40e4107

deploy: backend from e7369e1

Browse files
Files changed (2) hide show
  1. main.py +7 -1
  2. tests/test_cost_calculator.py +17 -3
main.py CHANGED
@@ -1208,9 +1208,15 @@ async def global_exception_handler(request: Request, exc: Exception):
1208
  headers={"X-Request-ID": request_id},
1209
  )
1210
 
 
 
 
 
 
 
1211
  app.add_middleware(
1212
  CORSMiddleware,
1213
- allow_origins=["*"],
1214
  allow_credentials=True,
1215
  allow_methods=["*"],
1216
  allow_headers=["*"],
 
1208
  headers={"X-Request-ID": request_id},
1209
  )
1210
 
1211
+ _cors_origins = [
1212
+ origin.strip()
1213
+ for origin in os.getenv("CORS_ORIGINS", "http://localhost:5173,http://localhost:4173").split(",")
1214
+ if origin.strip()
1215
+ ]
1216
+
1217
  app.add_middleware(
1218
  CORSMiddleware,
1219
+ allow_origins=_cors_origins,
1220
  allow_credentials=True,
1221
  allow_methods=["*"],
1222
  allow_headers=["*"],
tests/test_cost_calculator.py CHANGED
@@ -5,16 +5,30 @@ import os
5
  from unittest.mock import patch
6
  from datetime import datetime, timezone
7
 
 
 
8
  sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
 
10
  from services.cost_calculator import calculate_feature_cost, calculate_full_price_cost
11
  from config.ai_pricing import get_active_pricing, DEEPSEEK_PRICING
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  class TestCalculateFeatureCostPromoActive:
15
  """Tests when promotional pricing is active (before 2026-05-31)."""
16
 
17
- def test_basic_calculation(self):
18
  result = calculate_feature_cost(
19
  "deepseek-v4-pro",
20
  cache_hit_tokens=1_000_000,
@@ -28,7 +42,7 @@ class TestCalculateFeatureCostPromoActive:
28
  expected_total = 0.003625 + 0.435 + 0.87
29
  assert abs(result["total_usd"] - expected_total) < 1e-5
30
 
31
- def test_zero_tokens(self):
32
  result = calculate_feature_cost("deepseek-v4-pro", 0, 0, 0)
33
  assert result["total_usd"] == 0.0
34
  assert result["cache_hit_cost"] == 0.0
@@ -36,7 +50,7 @@ class TestCalculateFeatureCostPromoActive:
36
  assert result["output_cost"] == 0.0
37
  assert result["is_promotional"] is True
38
 
39
- def test_only_cache_hits(self):
40
  result = calculate_feature_cost("deepseek-v4-pro", 5_000_000, 0, 0)
41
  expected = (5_000_000 / 1_000_000) * 0.003625
42
  assert abs(result["total_usd"] - expected) < 1e-5
 
5
  from unittest.mock import patch
6
  from datetime import datetime, timezone
7
 
8
+ import pytest
9
+
10
  sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
 
12
  from services.cost_calculator import calculate_feature_cost, calculate_full_price_cost
13
  from config.ai_pricing import get_active_pricing, DEEPSEEK_PRICING
14
 
15
 
16
+ ACTIVE_PROMO_TIME = datetime(2026, 5, 1, 0, 0, 0, tzinfo=timezone.utc)
17
+
18
+
19
+ @pytest.fixture
20
+ def active_promo_datetime():
21
+ """Keep promotional-price assertions independent of the wall clock."""
22
+ with patch("config.ai_pricing.datetime") as mock_dt:
23
+ mock_dt.now.return_value = ACTIVE_PROMO_TIME
24
+ mock_dt.side_effect = datetime
25
+ yield mock_dt
26
+
27
+
28
  class TestCalculateFeatureCostPromoActive:
29
  """Tests when promotional pricing is active (before 2026-05-31)."""
30
 
31
+ def test_basic_calculation(self, active_promo_datetime):
32
  result = calculate_feature_cost(
33
  "deepseek-v4-pro",
34
  cache_hit_tokens=1_000_000,
 
42
  expected_total = 0.003625 + 0.435 + 0.87
43
  assert abs(result["total_usd"] - expected_total) < 1e-5
44
 
45
+ def test_zero_tokens(self, active_promo_datetime):
46
  result = calculate_feature_cost("deepseek-v4-pro", 0, 0, 0)
47
  assert result["total_usd"] == 0.0
48
  assert result["cache_hit_cost"] == 0.0
 
50
  assert result["output_cost"] == 0.0
51
  assert result["is_promotional"] is True
52
 
53
+ def test_only_cache_hits(self, active_promo_datetime):
54
  result = calculate_feature_cost("deepseek-v4-pro", 5_000_000, 0, 0)
55
  expected = (5_000_000 / 1_000_000) * 0.003625
56
  assert abs(result["total_usd"] - expected) < 1e-5