dsh-zcode-startplan 0.5.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1963 -672
- package/README.md +92 -472
- package/bin/cli.mjs +509 -450
- package/client.js +2459 -1002
- package/cordis.patch.yml +4 -2
- package/index.js +109 -11
- package/lib/adapter.js +476 -449
- package/lib/anthropic-body.js +513 -469
- package/lib/assemble.js +808 -542
- package/lib/auto-claim.js +229 -0
- package/lib/captcha/minter.js +61 -7
- package/lib/distribution-keys.js +202 -0
- package/lib/errors.js +281 -125
- package/lib/fallback.js +120 -0
- package/lib/local-endpoint.js +367 -352
- package/lib/manage-routes.js +1245 -652
- package/lib/model-catalog.js +57 -0
- package/lib/openai-gateway.js +776 -0
- package/lib/plan-client.js +942 -532
- package/lib/pool.js +771 -478
- package/lib/sse.js +429 -317
- package/lib/upstream-api.js +948 -611
- package/lib/upstream.js +294 -134
- package/lib/version-probe.js +110 -0
- package/package.json +84 -81
- package/scripts/analyze-grant-intervals.mjs +171 -0
- package/scripts/check-compat-rc3.mjs +21 -11
- package/scripts/debug-claimable.mjs +130 -0
- package/scripts/probe-marketing.mjs +203 -0
- package/scripts/restart-dsh.ps1 +119 -116
- package/scripts/test-claimed-pending.mjs +152 -0
- package/scripts/verify_compat.mjs +75 -64
- package/scripts/watch-marketing.mjs +155 -0
- package/tests/auto-claim.test.mjs +318 -0
- package/tests/claim-routes.test.mjs +438 -293
- package/tests/client-panel.test.mjs +1141 -356
- package/tests/distribution-keys.test.mjs +123 -0
- package/tests/e2e.test.mjs +388 -353
- package/tests/fallback-scrub.test.mjs +105 -0
- package/tests/launch-command.test.mjs +68 -0
- package/tests/openai-gateway.test.mjs +454 -0
- package/tests/pinned-models.test.mjs +505 -497
- package/tests/plan-client.test.mjs +582 -222
- package/tests/pool.test.mjs +319 -93
- package/tests/quota-blackhole.test.mjs +409 -0
- package/tests/restart-script.test.mjs +53 -0
- package/tests/routing-quota.test.mjs +251 -0
- package/tests/support/hanging-stream.mjs +48 -0
- package/tests/upstream-error-200.test.mjs +164 -125
- package/tests/upstream.test.mjs +330 -250
- package/tests/version-probe.test.mjs +172 -0
package/tests/upstream.test.mjs
CHANGED
|
@@ -1,250 +1,330 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
|
|
4
|
-
import { buildZcodeHeaders, nextUtc8DayStartMs, toEpochMs } from '../lib/upstream.js'
|
|
5
|
-
import {
|
|
6
|
-
classifyUpstreamError,
|
|
7
|
-
concurrencyRetryDelayMs,
|
|
8
|
-
describeUpstreamError,
|
|
9
|
-
isZcodeQuotaExhausted,
|
|
10
|
-
} from '../lib/errors.js'
|
|
11
|
-
|
|
12
|
-
test('buildZcodeHeaders: X-Device-Mid 与 anthropic-version 必定下发', () => {
|
|
13
|
-
const headers = buildZcodeHeaders({ device_mid: 'dev-1', app_version: '3.14.3' })
|
|
14
|
-
assert.equal(headers['X-Device-Mid'], 'dev-1')
|
|
15
|
-
assert.equal(headers['anthropic-version'], '2023-06-01')
|
|
16
|
-
assert.equal(headers['User-Agent'], 'ZCode/3.14.3')
|
|
17
|
-
assert.equal(headers['Authorization'], undefined)
|
|
18
|
-
assert.equal(headers['x-aliyun-captcha-verify-param'], undefined)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('buildZcodeHeaders: captcha 与 authorization 只在传入时出现', () => {
|
|
22
|
-
const headers = buildZcodeHeaders(
|
|
23
|
-
{ device_mid: 'dev-1' },
|
|
24
|
-
{ authorization: 'Bearer jwt', captcha: { param: 'p', region: 'cn' } },
|
|
25
|
-
)
|
|
26
|
-
assert.equal(headers['Authorization'], 'Bearer jwt')
|
|
27
|
-
assert.equal(headers['x-aliyun-captcha-verify-param'], 'p')
|
|
28
|
-
assert.equal(headers['x-aliyun-captcha-verify-region'], 'cn')
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test('classifyUpstreamError: 3009 是并发限流,不是额度用尽', () => {
|
|
32
|
-
const body = '{"code":3009,"msg":"model concurrency limit exceeded"}'
|
|
33
|
-
assert.equal(classifyUpstreamError(429, body), 'concurrency')
|
|
34
|
-
assert.equal(isZcodeQuotaExhausted(body), false)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test('classifyUpstreamError: 1005 / 1113 都算额度用尽', () => {
|
|
38
|
-
assert.equal(classifyUpstreamError(429, '{"code":1005,"msg":"exceed quota limit"}'), 'quota')
|
|
39
|
-
assert.equal(classifyUpstreamError(200, '{"code":1113,"msg":"余额不足"}'), 'quota')
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
test('classifyUpstreamError: 3007 换 captcha,3012 不重试,401 需重新登录', () => {
|
|
43
|
-
assert.equal(classifyUpstreamError(403, '{"code":3007,"msg":"captcha verify failed"}'), 'captcha')
|
|
44
|
-
assert.equal(classifyUpstreamError(403, '{"code":3012,"msg":"request has been blocked due to unusual activity."}'), 'risk')
|
|
45
|
-
assert.equal(classifyUpstreamError(401, ''), 'auth')
|
|
46
|
-
assert.equal(classifyUpstreamError(500, 'boom'), 'other')
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test('describeUpstreamError: 3012 必须带冷却惩罚告警', () => {
|
|
50
|
-
const text = describeUpstreamError(403, '{"code":3012,"msg":"unusual activity"}')
|
|
51
|
-
assert.match(text, /3012/)
|
|
52
|
-
assert.match(text, /请勿连续重试/)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('concurrencyRetryDelayMs: 线性退避 1500 / 3000', () => {
|
|
56
|
-
assert.equal(concurrencyRetryDelayMs(0), 1500)
|
|
57
|
-
assert.equal(concurrencyRetryDelayMs(1), 3000)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('toEpochMs: 秒级时间戳归一成毫秒(否则刚登录就报超时)', () => {
|
|
61
|
-
assert.equal(toEpochMs(1_790_746_234), 1_790_746_234_000)
|
|
62
|
-
assert.equal(toEpochMs(1_790_746_234_000), 1_790_746_234_000)
|
|
63
|
-
assert.equal(toEpochMs(0), 0)
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
test('nextUtc8DayStartMs: 返回下一个 UTC+8 零点', () => {
|
|
67
|
-
const at = new Date('2026-10-23T12:00:00Z') // = 2026-10-23 20:00 +08
|
|
68
|
-
const next = nextUtc8DayStartMs(at)
|
|
69
|
-
assert.equal(new Date(next).toISOString(), '2026-10-23T16:00:00.000Z')
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
/** fetchBalance 的假 fetch(固定响应)。 */
|
|
73
|
-
function fakeFetch(payload, status = 200) {
|
|
74
|
-
return async () => ({
|
|
75
|
-
ok: status >= 200 && status < 300,
|
|
76
|
-
status,
|
|
77
|
-
json: async () => payload,
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
test('fetchBalance: balances 汇总剩余/总量(现行行为不变)', async () => {
|
|
82
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
83
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
84
|
-
code: 0,
|
|
85
|
-
data: {
|
|
86
|
-
server_time: 1_793_021_836,
|
|
87
|
-
plans: [],
|
|
88
|
-
balances: [
|
|
89
|
-
{ show_name: 'GLM-5.3', unit_type: 'token', total_units: 8_000_000, used_units: 123_956, remaining_units: 7_876_044, available_units: 7_876_044 },
|
|
90
|
-
],
|
|
91
|
-
},
|
|
92
|
-
}))
|
|
93
|
-
assert.equal(balance.remaining, 7_876_044)
|
|
94
|
-
assert.equal(balance.total, 8_000_000)
|
|
95
|
-
assert.equal(balance.planName, 'GLM-5.3')
|
|
96
|
-
assert.equal(balance.pending, undefined, '无未生效权益时不下发 pending 键')
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
test('fetchBalance: 未生效的 plan entitlement 进 pending(23 点启用 3 亿包实测形状)', async () => {
|
|
100
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
101
|
-
// 2026-10-26 21:37 实测响应:balances 为空,额度在 plans[].entitlements 里,
|
|
102
|
-
// effective_at = 1793026800(23:00:00 +08)晚于 server_time。
|
|
103
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
104
|
-
code: 0,
|
|
105
|
-
data: {
|
|
106
|
-
server_time: 1_793_021_836,
|
|
107
|
-
plans: [
|
|
108
|
-
{
|
|
109
|
-
plan_id: 'zcode-v3-start-plan-nt-1026',
|
|
110
|
-
name: 'ZCODE GLOBAL BUILD',
|
|
111
|
-
status: 'active',
|
|
112
|
-
starts_at: 1_793_019_869,
|
|
113
|
-
ends_at: 1_793_062_800,
|
|
114
|
-
entitlements: [
|
|
115
|
-
{
|
|
116
|
-
entitlement_id: 'ent-1',
|
|
117
|
-
show_name: 'GLM-5.3-Flash',
|
|
118
|
-
meter: 'model_usage',
|
|
119
|
-
unit_type: 'token',
|
|
120
|
-
capabilities: ['model:glm-5.3-flash'],
|
|
121
|
-
grant_units: 300_000_000,
|
|
122
|
-
period: 'one_time',
|
|
123
|
-
priority: 101,
|
|
124
|
-
effective_at: 1_793_026_800,
|
|
125
|
-
},
|
|
126
|
-
],
|
|
127
|
-
},
|
|
128
|
-
],
|
|
129
|
-
balances: [],
|
|
130
|
-
},
|
|
131
|
-
}))
|
|
132
|
-
assert.equal(balance.remaining, 0)
|
|
133
|
-
assert.equal(balance.total, 0)
|
|
134
|
-
assert.equal(balance.pending?.length, 1)
|
|
135
|
-
assert.equal(balance.pending[0].showName, 'GLM-5.3-Flash')
|
|
136
|
-
assert.equal(balance.pending[0].grantUnits, 300_000_000)
|
|
137
|
-
assert.equal(balance.pending[0].effectiveAt, 1_793_026_800_000, '秒级时间戳必须归一成毫秒')
|
|
138
|
-
assert.equal(balance.pending[0].planId, 'zcode-v3-start-plan-nt-1026')
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
test('fetchBalance: 已生效的 entitlement 不进 pending(balances 已覆盖)', async () => {
|
|
142
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
143
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
144
|
-
code: 0,
|
|
145
|
-
data: {
|
|
146
|
-
server_time: 1_793_026_800, // 恰好等于 effective_at
|
|
147
|
-
plans: [
|
|
148
|
-
{
|
|
149
|
-
plan_id: 'p',
|
|
150
|
-
entitlements: [{ show_name: 'X', grant_units: 100, effective_at: 1_793_026_800 }],
|
|
151
|
-
},
|
|
152
|
-
],
|
|
153
|
-
balances: [{ show_name: 'X', total_units: 100, remaining_units: 100 }],
|
|
154
|
-
},
|
|
155
|
-
}))
|
|
156
|
-
assert.equal(balance.pending, undefined)
|
|
157
|
-
assert.equal(balance.remaining, 100)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
test('fetchBalance: grant_units 非正数 / 缺 effective_at 的条目跳过,多条按到点时间排序', async () => {
|
|
161
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
162
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
163
|
-
code: 0,
|
|
164
|
-
data: {
|
|
165
|
-
server_time: 1_000,
|
|
166
|
-
plans: [
|
|
167
|
-
{
|
|
168
|
-
plan_id: 'p',
|
|
169
|
-
entitlements: [
|
|
170
|
-
{ show_name: 'later', grant_units: 50, effective_at: 3_000 },
|
|
171
|
-
{ show_name: 'zero', grant_units: 0, effective_at: 2_000 },
|
|
172
|
-
{ show_name: 'no-time', grant_units: 70 },
|
|
173
|
-
{ show_name: 'earlier', grant_units: 10, effective_at: 2_000 },
|
|
174
|
-
],
|
|
175
|
-
},
|
|
176
|
-
],
|
|
177
|
-
balances: [],
|
|
178
|
-
},
|
|
179
|
-
}))
|
|
180
|
-
assert.equal(balance.pending?.length, 2, '零额度与缺时间的要跳过')
|
|
181
|
-
assert.deepEqual(balance.pending.map((item) => item.showName), ['earlier', 'later'])
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
test('fetchBalance: 企业版形态不变(不解析 pending)', async () => {
|
|
185
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
186
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
187
|
-
code: 0,
|
|
188
|
-
data: { displayMode: 'enterprise', plans: [{ entitlements: [{ grant_units: 5, effective_at: 9_999 }] }] },
|
|
189
|
-
}))
|
|
190
|
-
assert.equal(balance.enterprise, true)
|
|
191
|
-
assert.equal(balance.pending, undefined)
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
test('fetchBalance: expires_at 秒级归一成毫秒,且只汇总还有余额的桶', async () => {
|
|
195
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
196
|
-
// 2026-10-26 实测:两个桶的 expires_at 都是 1793030399(当天 23:59:59 +08)
|
|
197
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
198
|
-
code: 0,
|
|
199
|
-
data: {
|
|
200
|
-
server_time: 1_793_022_000,
|
|
201
|
-
plans: [],
|
|
202
|
-
balances: [
|
|
203
|
-
{ show_name: 'GLM-5.3', total_units: 3_000_000, used_units: 0, remaining_units: 3_000_000, available_units: 3_000_000, expires_at: 1_793_030_399 },
|
|
204
|
-
{ show_name: 'GLM-5.3-Flash', total_units: 5_000_000, used_units: 5_000_000, remaining_units: 0, available_units: 0, expires_at: 1_800_000_000 },
|
|
205
|
-
],
|
|
206
|
-
},
|
|
207
|
-
}))
|
|
208
|
-
assert.equal(balance.expiresAt, 1_793_030_399_000, '秒级必须归一成毫秒(否则界面显示 1970)')
|
|
209
|
-
/**
|
|
210
|
-
* 已用光的 Flash 桶(expires_at 更晚)不能参与顶层汇总 —— 否则会给出
|
|
211
|
-
* 「一个空桶的到期时间」,误导用户以为还有额度。
|
|
212
|
-
*/
|
|
213
|
-
assert.notEqual(balance.expiresAt, 1_800_000_000_000)
|
|
214
|
-
assert.equal(balance.buckets[1].expiresAt, 1_800_000_000_000, '每个桶仍保留自己的到期时间')
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
test('fetchBalance: 所有桶都用光时不给顶层 expiresAt', async () => {
|
|
218
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
219
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
220
|
-
code: 0,
|
|
221
|
-
data: {
|
|
222
|
-
server_time: 1_793_022_000,
|
|
223
|
-
plans: [],
|
|
224
|
-
balances: [
|
|
225
|
-
{ show_name: 'X', total_units: 100, used_units: 100, remaining_units: 0, available_units: 0, expires_at: 1_793_030_399 },
|
|
226
|
-
],
|
|
227
|
-
},
|
|
228
|
-
}))
|
|
229
|
-
assert.equal(balance.expiresAt, undefined)
|
|
230
|
-
assert.equal(balance.remaining, 0)
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
test('fetchBalance: 未生效权益带上 plan 的 ends_at 作为到期时间', async () => {
|
|
234
|
-
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
235
|
-
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
236
|
-
code: 0,
|
|
237
|
-
data: {
|
|
238
|
-
server_time: 1_793_021_836,
|
|
239
|
-
plans: [
|
|
240
|
-
{
|
|
241
|
-
plan_id: 'p1',
|
|
242
|
-
ends_at: 1_793_062_800,
|
|
243
|
-
entitlements: [{ show_name: 'GLM-5.3-Flash', grant_units: 300_000_000, effective_at: 1_793_026_800 }],
|
|
244
|
-
},
|
|
245
|
-
],
|
|
246
|
-
balances: [],
|
|
247
|
-
},
|
|
248
|
-
}))
|
|
249
|
-
assert.equal(balance.pending[0].expiresAt, 1_793_062_800_000)
|
|
250
|
-
})
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
|
|
4
|
+
import { buildZcodeHeaders, compareZcodeAppVersion, nextUtc8DayStartMs, toEpochMs, zcodeVersionGatedAppVersion, ZCODE_VERSION_GATED_MIN_APP_VERSION } from '../lib/upstream.js'
|
|
5
|
+
import {
|
|
6
|
+
classifyUpstreamError,
|
|
7
|
+
concurrencyRetryDelayMs,
|
|
8
|
+
describeUpstreamError,
|
|
9
|
+
isZcodeQuotaExhausted,
|
|
10
|
+
} from '../lib/errors.js'
|
|
11
|
+
|
|
12
|
+
test('buildZcodeHeaders: X-Device-Mid 与 anthropic-version 必定下发', () => {
|
|
13
|
+
const headers = buildZcodeHeaders({ device_mid: 'dev-1', app_version: '3.14.3' })
|
|
14
|
+
assert.equal(headers['X-Device-Mid'], 'dev-1')
|
|
15
|
+
assert.equal(headers['anthropic-version'], '2023-06-01')
|
|
16
|
+
assert.equal(headers['User-Agent'], 'ZCode/3.14.3')
|
|
17
|
+
assert.equal(headers['Authorization'], undefined)
|
|
18
|
+
assert.equal(headers['x-aliyun-captcha-verify-param'], undefined)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('buildZcodeHeaders: captcha 与 authorization 只在传入时出现', () => {
|
|
22
|
+
const headers = buildZcodeHeaders(
|
|
23
|
+
{ device_mid: 'dev-1' },
|
|
24
|
+
{ authorization: 'Bearer jwt', captcha: { param: 'p', region: 'cn' } },
|
|
25
|
+
)
|
|
26
|
+
assert.equal(headers['Authorization'], 'Bearer jwt')
|
|
27
|
+
assert.equal(headers['x-aliyun-captcha-verify-param'], 'p')
|
|
28
|
+
assert.equal(headers['x-aliyun-captcha-verify-region'], 'cn')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('classifyUpstreamError: 3009 是并发限流,不是额度用尽', () => {
|
|
32
|
+
const body = '{"code":3009,"msg":"model concurrency limit exceeded"}'
|
|
33
|
+
assert.equal(classifyUpstreamError(429, body), 'concurrency')
|
|
34
|
+
assert.equal(isZcodeQuotaExhausted(body), false)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('classifyUpstreamError: 1005 / 1113 都算额度用尽', () => {
|
|
38
|
+
assert.equal(classifyUpstreamError(429, '{"code":1005,"msg":"exceed quota limit"}'), 'quota')
|
|
39
|
+
assert.equal(classifyUpstreamError(200, '{"code":1113,"msg":"余额不足"}'), 'quota')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('classifyUpstreamError: 3007 换 captcha,3012 不重试,401 需重新登录', () => {
|
|
43
|
+
assert.equal(classifyUpstreamError(403, '{"code":3007,"msg":"captcha verify failed"}'), 'captcha')
|
|
44
|
+
assert.equal(classifyUpstreamError(403, '{"code":3012,"msg":"request has been blocked due to unusual activity."}'), 'risk')
|
|
45
|
+
assert.equal(classifyUpstreamError(401, ''), 'auth')
|
|
46
|
+
assert.equal(classifyUpstreamError(500, 'boom'), 'other')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('describeUpstreamError: 3012 必须带冷却惩罚告警', () => {
|
|
50
|
+
const text = describeUpstreamError(403, '{"code":3012,"msg":"unusual activity"}')
|
|
51
|
+
assert.match(text, /3012/)
|
|
52
|
+
assert.match(text, /请勿连续重试/)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('concurrencyRetryDelayMs: 线性退避 1500 / 3000', () => {
|
|
56
|
+
assert.equal(concurrencyRetryDelayMs(0), 1500)
|
|
57
|
+
assert.equal(concurrencyRetryDelayMs(1), 3000)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('toEpochMs: 秒级时间戳归一成毫秒(否则刚登录就报超时)', () => {
|
|
61
|
+
assert.equal(toEpochMs(1_790_746_234), 1_790_746_234_000)
|
|
62
|
+
assert.equal(toEpochMs(1_790_746_234_000), 1_790_746_234_000)
|
|
63
|
+
assert.equal(toEpochMs(0), 0)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('nextUtc8DayStartMs: 返回下一个 UTC+8 零点', () => {
|
|
67
|
+
const at = new Date('2026-10-23T12:00:00Z') // = 2026-10-23 20:00 +08
|
|
68
|
+
const next = nextUtc8DayStartMs(at)
|
|
69
|
+
assert.equal(new Date(next).toISOString(), '2026-10-23T16:00:00.000Z')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
/** fetchBalance 的假 fetch(固定响应)。 */
|
|
73
|
+
function fakeFetch(payload, status = 200) {
|
|
74
|
+
return async () => ({
|
|
75
|
+
ok: status >= 200 && status < 300,
|
|
76
|
+
status,
|
|
77
|
+
json: async () => payload,
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
test('fetchBalance: balances 汇总剩余/总量(现行行为不变)', async () => {
|
|
82
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
83
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
84
|
+
code: 0,
|
|
85
|
+
data: {
|
|
86
|
+
server_time: 1_793_021_836,
|
|
87
|
+
plans: [],
|
|
88
|
+
balances: [
|
|
89
|
+
{ show_name: 'GLM-5.3', unit_type: 'token', total_units: 8_000_000, used_units: 123_956, remaining_units: 7_876_044, available_units: 7_876_044 },
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
}))
|
|
93
|
+
assert.equal(balance.remaining, 7_876_044)
|
|
94
|
+
assert.equal(balance.total, 8_000_000)
|
|
95
|
+
assert.equal(balance.planName, 'GLM-5.3')
|
|
96
|
+
assert.equal(balance.pending, undefined, '无未生效权益时不下发 pending 键')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('fetchBalance: 未生效的 plan entitlement 进 pending(23 点启用 3 亿包实测形状)', async () => {
|
|
100
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
101
|
+
// 2026-10-26 21:37 实测响应:balances 为空,额度在 plans[].entitlements 里,
|
|
102
|
+
// effective_at = 1793026800(23:00:00 +08)晚于 server_time。
|
|
103
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
104
|
+
code: 0,
|
|
105
|
+
data: {
|
|
106
|
+
server_time: 1_793_021_836,
|
|
107
|
+
plans: [
|
|
108
|
+
{
|
|
109
|
+
plan_id: 'zcode-v3-start-plan-nt-1026',
|
|
110
|
+
name: 'ZCODE GLOBAL BUILD',
|
|
111
|
+
status: 'active',
|
|
112
|
+
starts_at: 1_793_019_869,
|
|
113
|
+
ends_at: 1_793_062_800,
|
|
114
|
+
entitlements: [
|
|
115
|
+
{
|
|
116
|
+
entitlement_id: 'ent-1',
|
|
117
|
+
show_name: 'GLM-5.3-Flash',
|
|
118
|
+
meter: 'model_usage',
|
|
119
|
+
unit_type: 'token',
|
|
120
|
+
capabilities: ['model:glm-5.3-flash'],
|
|
121
|
+
grant_units: 300_000_000,
|
|
122
|
+
period: 'one_time',
|
|
123
|
+
priority: 101,
|
|
124
|
+
effective_at: 1_793_026_800,
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
balances: [],
|
|
130
|
+
},
|
|
131
|
+
}))
|
|
132
|
+
assert.equal(balance.remaining, 0)
|
|
133
|
+
assert.equal(balance.total, 0)
|
|
134
|
+
assert.equal(balance.pending?.length, 1)
|
|
135
|
+
assert.equal(balance.pending[0].showName, 'GLM-5.3-Flash')
|
|
136
|
+
assert.equal(balance.pending[0].grantUnits, 300_000_000)
|
|
137
|
+
assert.equal(balance.pending[0].effectiveAt, 1_793_026_800_000, '秒级时间戳必须归一成毫秒')
|
|
138
|
+
assert.equal(balance.pending[0].planId, 'zcode-v3-start-plan-nt-1026')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('fetchBalance: 已生效的 entitlement 不进 pending(balances 已覆盖)', async () => {
|
|
142
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
143
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
144
|
+
code: 0,
|
|
145
|
+
data: {
|
|
146
|
+
server_time: 1_793_026_800, // 恰好等于 effective_at
|
|
147
|
+
plans: [
|
|
148
|
+
{
|
|
149
|
+
plan_id: 'p',
|
|
150
|
+
entitlements: [{ show_name: 'X', grant_units: 100, effective_at: 1_793_026_800 }],
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
balances: [{ show_name: 'X', total_units: 100, remaining_units: 100 }],
|
|
154
|
+
},
|
|
155
|
+
}))
|
|
156
|
+
assert.equal(balance.pending, undefined)
|
|
157
|
+
assert.equal(balance.remaining, 100)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test('fetchBalance: grant_units 非正数 / 缺 effective_at 的条目跳过,多条按到点时间排序', async () => {
|
|
161
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
162
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
163
|
+
code: 0,
|
|
164
|
+
data: {
|
|
165
|
+
server_time: 1_000,
|
|
166
|
+
plans: [
|
|
167
|
+
{
|
|
168
|
+
plan_id: 'p',
|
|
169
|
+
entitlements: [
|
|
170
|
+
{ show_name: 'later', grant_units: 50, effective_at: 3_000 },
|
|
171
|
+
{ show_name: 'zero', grant_units: 0, effective_at: 2_000 },
|
|
172
|
+
{ show_name: 'no-time', grant_units: 70 },
|
|
173
|
+
{ show_name: 'earlier', grant_units: 10, effective_at: 2_000 },
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
balances: [],
|
|
178
|
+
},
|
|
179
|
+
}))
|
|
180
|
+
assert.equal(balance.pending?.length, 2, '零额度与缺时间的要跳过')
|
|
181
|
+
assert.deepEqual(balance.pending.map((item) => item.showName), ['earlier', 'later'])
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
test('fetchBalance: 企业版形态不变(不解析 pending)', async () => {
|
|
185
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
186
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
187
|
+
code: 0,
|
|
188
|
+
data: { displayMode: 'enterprise', plans: [{ entitlements: [{ grant_units: 5, effective_at: 9_999 }] }] },
|
|
189
|
+
}))
|
|
190
|
+
assert.equal(balance.enterprise, true)
|
|
191
|
+
assert.equal(balance.pending, undefined)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('fetchBalance: expires_at 秒级归一成毫秒,且只汇总还有余额的桶', async () => {
|
|
195
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
196
|
+
// 2026-10-26 实测:两个桶的 expires_at 都是 1793030399(当天 23:59:59 +08)
|
|
197
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
198
|
+
code: 0,
|
|
199
|
+
data: {
|
|
200
|
+
server_time: 1_793_022_000,
|
|
201
|
+
plans: [],
|
|
202
|
+
balances: [
|
|
203
|
+
{ show_name: 'GLM-5.3', total_units: 3_000_000, used_units: 0, remaining_units: 3_000_000, available_units: 3_000_000, expires_at: 1_793_030_399 },
|
|
204
|
+
{ show_name: 'GLM-5.3-Flash', total_units: 5_000_000, used_units: 5_000_000, remaining_units: 0, available_units: 0, expires_at: 1_800_000_000 },
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
}))
|
|
208
|
+
assert.equal(balance.expiresAt, 1_793_030_399_000, '秒级必须归一成毫秒(否则界面显示 1970)')
|
|
209
|
+
/**
|
|
210
|
+
* 已用光的 Flash 桶(expires_at 更晚)不能参与顶层汇总 —— 否则会给出
|
|
211
|
+
* 「一个空桶的到期时间」,误导用户以为还有额度。
|
|
212
|
+
*/
|
|
213
|
+
assert.notEqual(balance.expiresAt, 1_800_000_000_000)
|
|
214
|
+
assert.equal(balance.buckets[1].expiresAt, 1_800_000_000_000, '每个桶仍保留自己的到期时间')
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('fetchBalance: 所有桶都用光时不给顶层 expiresAt', async () => {
|
|
218
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
219
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
220
|
+
code: 0,
|
|
221
|
+
data: {
|
|
222
|
+
server_time: 1_793_022_000,
|
|
223
|
+
plans: [],
|
|
224
|
+
balances: [
|
|
225
|
+
{ show_name: 'X', total_units: 100, used_units: 100, remaining_units: 0, available_units: 0, expires_at: 1_793_030_399 },
|
|
226
|
+
],
|
|
227
|
+
},
|
|
228
|
+
}))
|
|
229
|
+
assert.equal(balance.expiresAt, undefined)
|
|
230
|
+
assert.equal(balance.remaining, 0)
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('fetchBalance: 未生效权益带上 plan 的 ends_at 作为到期时间', async () => {
|
|
234
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
235
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
236
|
+
code: 0,
|
|
237
|
+
data: {
|
|
238
|
+
server_time: 1_793_021_836,
|
|
239
|
+
plans: [
|
|
240
|
+
{
|
|
241
|
+
plan_id: 'p1',
|
|
242
|
+
ends_at: 1_793_062_800,
|
|
243
|
+
entitlements: [{ show_name: 'GLM-5.3-Flash', grant_units: 300_000_000, effective_at: 1_793_026_800 }],
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
balances: [],
|
|
247
|
+
},
|
|
248
|
+
}))
|
|
249
|
+
assert.equal(balance.pending[0].expiresAt, 1_793_062_800_000)
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
test('fetchBalance: 多桶按到期时间升序、空桶沉底(逐桶到期时间供面板分条显示)', async () => {
|
|
253
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
254
|
+
const now = Math.floor(Date.now() / 1000)
|
|
255
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
256
|
+
code: 0,
|
|
257
|
+
data: {
|
|
258
|
+
server_time: now,
|
|
259
|
+
balances: [
|
|
260
|
+
{ plan_id: 'late', show_name: '晚到期', total_units: 1000, remaining_units: 500, expires_at: now + 7 * 86_400 },
|
|
261
|
+
{ plan_id: 'empty', show_name: '空桶', total_units: 1000, remaining_units: 0, expires_at: now + 86_400 },
|
|
262
|
+
{ plan_id: 'soon', show_name: '早到期', total_units: 2000, remaining_units: 800, expires_at: now + 3 * 86_400 },
|
|
263
|
+
],
|
|
264
|
+
},
|
|
265
|
+
}))
|
|
266
|
+
assert.deepEqual(
|
|
267
|
+
balance.buckets.map((b) => b.planId),
|
|
268
|
+
['soon', 'late', 'empty'],
|
|
269
|
+
'有余额的桶按到期升序,空桶沉底',
|
|
270
|
+
)
|
|
271
|
+
assert.equal(balance.expiresAt, (now + 3 * 86_400) * 1000, '顶层 expiresAt = 最早到期的有余额桶')
|
|
272
|
+
/** 每个桶带自己的数值与到期时间(面板分条用)。 */
|
|
273
|
+
for (const b of balance.buckets) {
|
|
274
|
+
assert.equal(typeof b.remainingUnits, 'number')
|
|
275
|
+
assert.equal(typeof b.totalUnits, 'number')
|
|
276
|
+
assert.ok(b.expiresAt > Date.now())
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
test('fetchBalance: 单桶时行为不变(buckets 仍下发,面板走汇总条)', async () => {
|
|
281
|
+
const { fetchBalance } = await import('../lib/upstream-api.js')
|
|
282
|
+
const now = Math.floor(Date.now() / 1000)
|
|
283
|
+
const balance = await fetchBalance({ zcode_jwt: 'j', device_mid: 'd' }, fakeFetch({
|
|
284
|
+
code: 0,
|
|
285
|
+
data: {
|
|
286
|
+
server_time: now,
|
|
287
|
+
balances: [{ plan_id: 'p', show_name: 'GLM', total_units: 100, remaining_units: 40, expires_at: now + 3600 }],
|
|
288
|
+
},
|
|
289
|
+
}))
|
|
290
|
+
assert.equal(balance.buckets.length, 1)
|
|
291
|
+
assert.equal(balance.remaining, 40)
|
|
292
|
+
assert.equal(balance.total, 100)
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
// ── 版本门控(服务端会随官方版本抬高下限,见 upstream.js 的观测表)──
|
|
296
|
+
|
|
297
|
+
test('版本门控: marketing/claim 的 app_version 与下限取较大者', () => {
|
|
298
|
+
/** 凭据旧版本 → 抬到门控下限(低版本投放恒空、claim 1004)。 */
|
|
299
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.14.3' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
300
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.16.2' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
301
|
+
/** 2026-11-10 实测:3.16.3 已被服务端判为「旧」(deliveries 恒空),必须抬到 3.17.2。 */
|
|
302
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.16.3' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
303
|
+
assert.equal(ZCODE_VERSION_GATED_MIN_APP_VERSION, '3.17.2')
|
|
304
|
+
/** 凭据更新 → 用它自己的版本(向前兼容,不降级)。 */
|
|
305
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.18.0' }), '3.18.0')
|
|
306
|
+
/** 缺失/脏值 → 门控下限(脏值还会让 Headers() 抛 TypeError,必须挡住)。 */
|
|
307
|
+
assert.equal(zcodeVersionGatedAppVersion({}), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
308
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.16.3-beta.1' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
309
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.016.3' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
310
|
+
assert.equal(zcodeVersionGatedAppVersion({ app_version: '3.16.3\r\nX-Injected: 1' }), ZCODE_VERSION_GATED_MIN_APP_VERSION)
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
test('版本门控: buildZcodeHeaders 的 appVersion 覆盖只影响本次请求头', () => {
|
|
314
|
+
const cred = { device_mid: 'dev-1', app_version: '3.14.3' }
|
|
315
|
+
/** 不传 = 凭据版本(推理路径维持既有消融矩阵口径)。 */
|
|
316
|
+
assert.equal(buildZcodeHeaders(cred)['X-ZCode-App-Version'], '3.14.3')
|
|
317
|
+
/** 门控路径显式传 = 抬版本,且不改凭据。 */
|
|
318
|
+
assert.equal(
|
|
319
|
+
buildZcodeHeaders(cred, { appVersion: zcodeVersionGatedAppVersion(cred) })['X-ZCode-App-Version'],
|
|
320
|
+
ZCODE_VERSION_GATED_MIN_APP_VERSION,
|
|
321
|
+
)
|
|
322
|
+
assert.equal(cred.app_version, '3.14.3', '凭据本身绝不被改写')
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
test('版本门控: 点分版本比较(缺失段按 0)', () => {
|
|
326
|
+
assert.equal(compareZcodeAppVersion('3.14.3', '3.16.3'), -1)
|
|
327
|
+
assert.equal(compareZcodeAppVersion('3.16.3', '3.16.3'), 0)
|
|
328
|
+
assert.equal(compareZcodeAppVersion('3.17', '3.16.3'), 1)
|
|
329
|
+
assert.equal(compareZcodeAppVersion('3.16', '3.16.3'), -1)
|
|
330
|
+
})
|