-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_limits.py
More file actions
151 lines (123 loc) · 4.32 KB
/
Copy pathtest_limits.py
File metadata and controls
151 lines (123 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from cursor_usage.report import render_limits
PRIMARY = {
"billingCycleStart": "1783126408000",
"billingCycleEnd": "1785804808000",
"planUsage": {
"totalSpend": 1047,
"includedSpend": 1047,
"remaining": 38953,
"limit": 40000,
"autoPercentUsed": 1.047,
"apiPercentUsed": 0,
"totalPercentUsed": 0.698,
},
"enabled": True,
}
FALLBACK = {
"billingCycleStart": "2026-07-04T00:53:28.000Z",
"billingCycleEnd": "2026-08-04T00:53:28.000Z",
"membershipType": "ultra",
"isUnlimited": False,
"individualUsage": {
"plan": {
"enabled": True,
"used": 1002,
"limit": 40000,
"remaining": 38998,
"autoPercentUsed": 1.002,
"apiPercentUsed": 0,
"totalPercentUsed": 0.668,
},
},
}
def test_primary_shape_renders_buckets_and_money():
out = render_limits(PRIMARY)
assert out is not None
assert "Composer / Auto" in out
assert "Other models" in out
assert "1.0% used" in out
assert "99.0% left" in out
assert "$10.47 of $400.00 used" in out
assert "($389.53 left)" in out
assert "resets 2026-08-04" in out
def test_fallback_shape_renders():
out = render_limits(FALLBACK)
assert out is not None
assert "resets 2026-08-04" in out
assert "$10.02 of $400.00" in out
def test_zero_percents_empty_bar():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
payload["planUsage"]["autoPercentUsed"] = 0
payload["planUsage"]["apiPercentUsed"] = 0
out = render_limits(payload)
assert "0.0% used" in out
assert "100.0% left" in out
assert "[..............................]" in out
def test_percent_over_100_full_bar():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
payload["planUsage"]["autoPercentUsed"] = 103.5
out = render_limits(payload)
assert "[##############################]" in out
assert "103.5% used" in out
assert "0.0% left" in out
def test_unlimited_no_bars():
payload = dict(PRIMARY)
payload["isUnlimited"] = True
out = render_limits(payload)
assert "unlimited usage" in out
assert "[" not in out.split("unlimited")[1]
def test_missing_plan_returns_none():
assert render_limits({"enabled": True}) is None
assert render_limits({}) is None
def test_enabled_false_returns_none():
payload = dict(PRIMARY)
payload["enabled"] = False
assert render_limits(payload) is None
payload2 = dict(FALLBACK)
payload2["individualUsage"] = dict(FALLBACK["individualUsage"])
payload2["individualUsage"]["plan"] = dict(
FALLBACK["individualUsage"]["plan"])
payload2["individualUsage"]["plan"]["enabled"] = False
assert render_limits(payload2) is None
def test_zero_limit_no_money_line():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
payload["planUsage"]["limit"] = 0
out = render_limits(payload)
assert out is not None
assert "$" not in out
def test_string_numbers_tolerated():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
payload["planUsage"]["totalSpend"] = "1047"
payload["planUsage"]["limit"] = "40000"
payload["planUsage"]["remaining"] = "38953"
out = render_limits(payload)
assert "$10.47 of $400.00 used" in out
def test_garbage_reset_date_omits_resets():
payload = dict(PRIMARY)
payload["billingCycleEnd"] = "not-a-date"
out = render_limits(payload)
assert out is not None
assert "resets" not in out
def test_derived_remaining_when_missing():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
del payload["planUsage"]["remaining"]
out = render_limits(payload)
assert "($389.53 left)" in out
def test_null_total_spend_falls_back_to_used():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
payload["planUsage"]["totalSpend"] = None
payload["planUsage"]["used"] = 1002
out = render_limits(payload)
assert "$10.02 of $400.00 used" in out
def test_null_remaining_derived_from_limit():
payload = dict(PRIMARY)
payload["planUsage"] = dict(PRIMARY["planUsage"])
payload["planUsage"]["remaining"] = None
out = render_limits(payload)
assert "($389.53 left)" in out