Repository navigation
Expand file tree
/
Copy pathcodingplan_test.go
More file actions
367 lines (337 loc) · 12.8 KB
/
Copy pathcodingplan_test.go
File metadata and controls
367 lines (337 loc) · 12.8 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"encoding/json"
"testing"
)
func mustJSON(t *testing.T, s string) map[string]any {
t.Helper()
var m map[string]any
if err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("测试 JSON 解析失败:%v", err)
}
return m
}
// ── 智谱:unit 字段分桶 ──
// cc-switch #3036 真实案例:周期末尾周桶比 5h 桶更早重置,
// 按 nextResetTime 排序必然把两桶标反,unit 字段必须优先。
func TestZhipuUnit分类_周桶先重置不翻转(t *testing.T) {
data := mustJSON(t, `{
"limits": [
{ "type": "TOKENS_LIMIT", "unit": 6, "number": 7, "percentage": 42.0, "nextResetTime": 1000003600000 },
{ "type": "TOKENS_LIMIT", "unit": 3, "number": 5, "percentage": 1.0, "nextResetTime": 1000018000000 }
]}`)
q5, q7 := parseZhipuTiers(data)
if q5 == nil || q7 == nil {
t.Fatalf("期望两个桶,got 5h=%v weekly=%v", q5, q7)
}
if q5.UsedPercent != 1.0 || q7.UsedPercent != 42.0 {
t.Errorf("分桶错误:5h=%.1f(期望 1.0),周=%.1f(期望 42.0)", q5.UsedPercent, q7.UsedPercent)
}
}
// 老套餐(2026-02-12 前订阅)只回 1 条 TOKENS_LIMIT,自然降级为仅 5h 桶。
func TestZhipu老套餐单条降级FiveHour(t *testing.T) {
data := mustJSON(t, `{
"limits": [
{ "type": "TOKENS_LIMIT", "percentage": 2.0, "nextResetTime": 1774967594803 },
{ "type": "TIME_LIMIT", "percentage": 0.0 }
]}`)
q5, q7 := parseZhipuTiers(data)
if q5 == nil || q7 != nil {
t.Fatalf("期望仅 5h 桶,got 5h=%v weekly=%v", q5, q7)
}
if q5.UsedPercent != 2.0 {
t.Errorf("5h 百分比 = %.1f,期望 2.0", q5.UsedPercent)
}
}
// 5h 桶 0% 时可能没有 nextResetTime:无 reset 的条目必须归 5h 桶,
// 不能按 reset 升序把周桶误判为 5h。
func TestZhipu无Reset条目归FiveHour(t *testing.T) {
data := mustJSON(t, `{
"limits": [
{ "type": "TOKENS_LIMIT", "percentage": 25.0, "nextResetTime": 2000000000000 },
{ "type": "TOKENS_LIMIT", "percentage": 0.0 }
]}`)
q5, q7 := parseZhipuTiers(data)
if q5 == nil || q7 == nil {
t.Fatalf("期望两个桶,got 5h=%v weekly=%v", q5, q7)
}
if q5.UsedPercent != 0.0 || q5.ResetsAt != "" {
t.Errorf("5h 桶应为 0%% 且无重置时间,got %.1f / %q", q5.UsedPercent, q5.ResetsAt)
}
if q7.UsedPercent != 25.0 || q7.ResetsAt == "" {
t.Errorf("周桶应为 25%% 带重置时间,got %.1f / %q", q7.UsedPercent, q7.ResetsAt)
}
}
// unit 缺失/不认识时兜底:按 reset 升序(无 reset 优先)填空缺槽位。
func TestZhipuUnit缺失按Reset升序兜底(t *testing.T) {
data := mustJSON(t, `{
"limits": [
{ "type": "tokens_limit", "percentage": 44.0, "nextResetTime": 1000000000000 },
{ "type": "Tokens_Limit", "percentage": 53.0, "nextResetTime": 2000000000000 }
]}`)
q5, q7 := parseZhipuTiers(data)
if q5 == nil || q7 == nil {
t.Fatalf("期望两个桶")
}
if q5.UsedPercent != 44.0 || q7.UsedPercent != 53.0 {
t.Errorf("兜底排序错误:5h=%.1f 周=%.1f", q5.UsedPercent, q7.UsedPercent)
}
}
// type 大小写不敏感;非 TOKENS_LIMIT 条目忽略。
func TestZhipu忽略非TokensLimit条目(t *testing.T) {
data := mustJSON(t, `{ "limits": [{ "type": "TIME_LIMIT", "percentage": 5.0 }] }`)
q5, q7 := parseZhipuTiers(data)
if q5 != nil || q7 != nil {
t.Errorf("不应产生任何桶")
}
}
// percentage 为字符串等异常形态时按 0 处理且不崩溃。
func TestZhipu异常Percentage按零处理(t *testing.T) {
data := mustJSON(t, `{
"limits": [
{ "type": "TOKENS_LIMIT", "percentage": "invalid", "nextResetTime": 1000000000000 },
{ "type": "TOKENS_LIMIT", "percentage": null, "nextResetTime": 2000000000000 }
]}`)
q5, q7 := parseZhipuTiers(data)
if q5 == nil || q7 == nil {
t.Fatalf("期望两个桶")
}
if q5.UsedPercent != 0 || q7.UsedPercent != 0 {
t.Errorf("异常 percentage 应按 0 处理")
}
}
// base_url 路由:bigmodel.cn → 国内站;z.ai / 其他 → 国际站。
func TestZhipuBase路由(t *testing.T) {
cases := []struct{ in, want string }{
{"https://open.bigmodel.cn/api/paas/v4", "https://open.bigmodel.cn"},
{"https://Open.BigModel.cn/api/paas/v4", "https://open.bigmodel.cn"},
{"https://api.z.ai/api/paas/v4", "https://api.z.ai"},
{"https://example.com/zhipu", "https://api.z.ai"},
}
for _, c := range cases {
if got := zhipuQuotaBase(c.in); got != c.want {
t.Errorf("zhipuQuotaBase(%q) = %q,期望 %q", c.in, got, c.want)
}
}
}
// ── Kimi:limit/remaining 推百分比 ──
func TestKimi用量与重置时间(t *testing.T) {
body := mustJSON(t, `{
"limits": [{ "type": "FIVE_HOUR", "detail": { "limit": 100, "remaining": 40, "resetTime": 1774967594803 } }],
"usage": { "limit": 1000, "remaining": "350", "resetTime": "2026-09-10T00:00:00Z" }
}`)
q5, q7 := parseKimiTiers(body)
if q5 == nil || q7 == nil {
t.Fatalf("期望两个桶")
}
if q5.UsedPercent != 60 {
t.Errorf("5h 已用 = %.1f,期望 60", q5.UsedPercent)
}
if q7.UsedPercent != 65 {
t.Errorf("周已用 = %.1f,期望 65(remaining 为字符串也应解析)", q7.UsedPercent)
}
if q7.ResetsAt != "2026-09-10T00:00:00Z" {
t.Errorf("字符串 resetTime 应原样透传,got %q", q7.ResetsAt)
}
}
// remaining > limit(异常数据)时已用截断为 0,不出现负百分比。
func TestKimi负用量截断为零(t *testing.T) {
body := mustJSON(t, `{ "usage": { "limit": 100, "remaining": 120 } }`)
_, q7 := parseKimiTiers(body)
if q7 == nil || q7.UsedPercent != 0 {
t.Errorf("负用量应截断为 0")
}
}
// ── MiniMax:剩余百分比反转 + 周桶激活判定 ──
func TestMiniMax两桶从剩余百分比反推(t *testing.T) {
body := mustJSON(t, `{
"model_remains": [
{ "model_name": "general", "current_interval_remaining_percent": 98.0,
"current_weekly_remaining_percent": 95.0, "current_weekly_status": 1,
"end_time": 1780329600000, "weekly_end_time": 1780848000000 },
{ "model_name": "video", "current_interval_remaining_percent": 100.0 }
]}`)
q5, q7 := parseMiniMaxTiers(body)
if q5 == nil || q7 == nil {
t.Fatalf("期望两个桶")
}
if q5.UsedPercent != 2 || q7.UsedPercent != 5 {
t.Errorf("已用 = %.1f / %.1f,期望 2 / 5", q5.UsedPercent, q7.UsedPercent)
}
if q5.ResetsAt == "" || q7.ResetsAt == "" {
t.Errorf("重置时间不应为空")
}
}
// 无周限额套餐:current_weekly_status=3(剩余恒 100%),不应展示假 0% 周桶。
func TestMiniMax无周限额跳过周桶(t *testing.T) {
body := mustJSON(t, `{
"model_remains": [{
"model_name": "general",
"current_interval_remaining_percent": 99,
"current_weekly_status": 3,
"current_weekly_remaining_percent": 100,
"end_time": 1780365600000,
"weekly_end_time": 1780848000000
}]}`)
q5, q7 := parseMiniMaxTiers(body)
if q5 == nil || q7 != nil {
t.Fatalf("期望仅 5h 桶,got 5h=%v weekly=%v", q5, q7)
}
if q5.UsedPercent != 1 {
t.Errorf("5h 已用 = %.1f,期望 1", q5.UsedPercent)
}
}
// 缺 general 桶(只有 video / 空数组 / 缺字段)不崩溃、不产生桶。
func TestMiniMax缺General返回空(t *testing.T) {
for _, s := range []string{
`{ "model_remains": [{ "model_name": "video" }] }`,
`{ "model_remains": [] }`,
`{}`,
} {
if q5, q7 := parseMiniMaxTiers(mustJSON(t, s)); q5 != nil || q7 != nil {
t.Errorf("%s 不应产生桶", s)
}
}
}
// ── ZenMux:0-1 小数 ×100 + USD 额度 ──
func TestZenMux小数百分比与USD(t *testing.T) {
body := mustJSON(t, `{
"success": true,
"data": {
"quota_5_hour": { "usage_percentage": 0.42, "resets_at": "2026-09-06T12:00:00Z",
"used_value_usd": 3.2, "max_value_usd": 7.5 },
"quota_7_day": { "usage_percentage": 0.55, "resets_at": "2026-09-10T00:00:00Z",
"used_value_usd": 22.1, "max_value_usd": 40 },
"plan": { "tier": "pro" },
"account_status": "active"
}}`)
data := jObj(body, "data")
q5o := jObj(data, "quota_5_hour")
q7o := jObj(data, "quota_7_day")
if pct, _ := jNum(q5o, "usage_percentage"); pct*100 != 42 {
t.Errorf("5h 已用 = %v,期望 42", pct*100)
}
if got := quotaUSDLabel(q5o); got != "$3.20 / $7.50" {
t.Errorf("USD 文本 = %q", got)
}
if got := quotaUSDLabel(q7o); got != "$22.10 / $40.00" {
t.Errorf("USD 文本 = %q", got)
}
}
// ── DeepSeek:余额解析与币种挑选 ──
// 官方示例形态:金额为字符串数字、is_available 布尔、赠金/充值拆分。
func TestDeepSeek余额解析(t *testing.T) {
body := mustJSON(t, `{
"is_available": true,
"balance_infos": [{
"currency": "CNY",
"total_balance": "110.00",
"granted_balance": "10.00",
"topped_up_balance": "100.00"
}]}`)
b := parseDeepSeekBalance(body)
if b == nil {
t.Fatalf("期望余额结构")
}
if b.Currency != "CNY" || !b.Available {
t.Errorf("币种/可用态解析错误:%q / %v", b.Currency, b.Available)
}
if b.Total != 110 || b.Granted != 10 || b.ToppedUp != 100 {
t.Errorf("金额解析错误:total=%v granted=%v toppedUp=%v", b.Total, b.Granted, b.ToppedUp)
}
}
// 双币种时优先 CNY;仅 USD 账户取 USD 条目。
func TestDeepSeek优先CNY币种(t *testing.T) {
body := mustJSON(t, `{
"is_available": true,
"balance_infos": [
{ "currency": "USD", "total_balance": "5.50", "granted_balance": "0", "topped_up_balance": "5.50" },
{ "currency": "CNY", "total_balance": "39.80", "granted_balance": "0", "topped_up_balance": "39.80" }
]}`)
if b := parseDeepSeekBalance(body); b == nil || b.Currency != "CNY" || b.Total != 39.8 {
t.Errorf("双币种应优先 CNY,got %+v", b)
}
onlyUSD := mustJSON(t, `{
"is_available": false,
"balance_infos": [{ "currency": "USD", "total_balance": "5.50" }]}`)
b := parseDeepSeekBalance(onlyUSD)
if b == nil || b.Currency != "USD" || b.Available {
t.Errorf("仅 USD 时应取 USD 且 is_available=false,got %+v", b)
}
}
// balance_infos 缺失 / 空数组 / 全为非对象时不崩溃,返回 nil 由上层报错。
func TestDeepSeek缺BalanceInfos不崩溃(t *testing.T) {
for _, s := range []string{`{}`, `{ "balance_infos": [] }`, `{ "balance_infos": [42] }`} {
if b := parseDeepSeekBalance(mustJSON(t, s)); b != nil {
t.Errorf("%s 不应产生余额结构", s)
}
}
}
// ── 供应商注册表 ──
// 六家供应商全部注册且有默认显示名;查询函数非空。
func Test供应商注册表全覆盖(t *testing.T) {
for _, p := range []string{
codingPlanZhipu, codingPlanKimi, codingPlanMiniMax, codingPlanZenMux, codingPlanDeepseek, codingPlanOpenCode,
} {
provider, ok := codingPlanProviders[p]
if !ok {
t.Errorf("供应商 %q 未注册", p)
continue
}
if provider.label == "" || provider.query == nil {
t.Errorf("供应商 %q 注册不完整:label/query 为空", p)
}
}
}
// ── OpenCode Go:rolling/weekly/monthly 三窗口 ──
// 官方响应形态:percent 为 0-100 整数,resetsAt 为 ISO 8601;
// status=rate-limited 只表示窗口触顶,仍应按用量展示。
func TestOpenCode三窗口解析(t *testing.T) {
body := mustJSON(t, `{
"usage": {
"rolling": { "status": "ok", "percent": 100, "resetsAt": "2026-11-16T08:00:00Z" },
"weekly": { "status": "ok", "percent": 99, "resetsAt": "2026-11-20T10:00:00Z" },
"monthly": { "status": "rate-limited", "percent": 100, "resetsAt": "2026-11-30T00:00:00Z" }
}}`)
usage, err := parseOpenCodeUsage(body)
if err != nil {
t.Fatalf("解析失败:%v", err)
}
if usage.FiveHour == nil || usage.Weekly == nil || usage.Monthly == nil {
t.Fatalf("期望三个窗口,got 5h=%v weekly=%v monthly=%v", usage.FiveHour, usage.Weekly, usage.Monthly)
}
if usage.FiveHour.UsedPercent != 100 || usage.Weekly.UsedPercent != 99 || usage.Monthly.UsedPercent != 100 {
t.Errorf("百分比解析错误:%v / %v / %v",
usage.FiveHour.UsedPercent, usage.Weekly.UsedPercent, usage.Monthly.UsedPercent)
}
if usage.FiveHour.ResetsAt != "2026-11-16T08:00:00Z" {
t.Errorf("resetsAt 应原样透传,got %q", usage.FiveHour.ResetsAt)
}
}
// percent 缺失或非数字的窗口视为无该桶,其余窗口照常展示。
func TestOpenCode缺失窗口跳过(t *testing.T) {
body := mustJSON(t, `{
"usage": {
"rolling": { "status": "ok", "percent": "42", "resetsAt": "2026-11-16T08:00:00Z" },
"weekly": { "status": "ok" },
"monthly": { "status": "ok", "percent": "invalid" }
}}`)
usage, err := parseOpenCodeUsage(body)
if err != nil {
t.Fatalf("解析失败:%v", err)
}
if usage.FiveHour == nil || usage.FiveHour.UsedPercent != 42 {
t.Errorf("rolling 字符串百分比应解析为 42,got %+v", usage.FiveHour)
}
if usage.Weekly != nil || usage.Monthly != nil {
t.Errorf("缺失 percent 的窗口不应产生桶")
}
}
// usage 字段缺失 / 三窗口全缺失时返回错误,由上层转 error 态。
func TestOpenCode缺Usage返回错误(t *testing.T) {
for _, s := range []string{`{}`, `{ "usage": {} }`, `{ "usage": { "weekly": { "status": "ok" } } }`} {
if _, err := parseOpenCodeUsage(mustJSON(t, s)); err == nil {
t.Errorf("%s 应返回解析错误", s)
}
}
}