dsh-zcode-startplan 0.4.5 → 1.0.2
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 -405
- package/README.md +88 -405
- package/bin/cli.mjs +509 -450
- package/client.js +2459 -716
- package/cordis.patch.yml +4 -2
- package/index.js +144 -14
- package/lib/adapter.js +476 -422
- package/lib/anthropic-body.js +513 -469
- package/lib/assemble.js +808 -420
- 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 -489
- 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 -570
- package/lib/upstream.js +294 -134
- package/lib/version-probe.js +110 -0
- package/package.json +84 -80
- 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 -76
- package/scripts/test-claimed-pending.mjs +152 -0
- package/scripts/verify_compat.mjs +75 -64
- package/scripts/watch-marketing.mjs +155 -0
- package/tests/anthropic-body.test.mjs +305 -0
- package/tests/assembler-contract.test.mjs +256 -0
- package/tests/auto-claim.test.mjs +318 -0
- package/tests/claim-routes.test.mjs +438 -0
- package/tests/client-panel.test.mjs +1141 -0
- package/tests/distribution-keys.test.mjs +123 -0
- package/tests/e2e.test.mjs +388 -0
- package/tests/fallback-scrub.test.mjs +105 -0
- package/tests/fixtures/quota-200.json +1 -0
- package/tests/launch-command.test.mjs +68 -0
- package/tests/login-channel.test.mjs +102 -0
- package/tests/openai-gateway.test.mjs +454 -0
- package/tests/pinned-models.test.mjs +505 -0
- package/tests/plan-client.test.mjs +582 -0
- package/tests/pool.test.mjs +319 -0
- 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/sse.test.mjs +86 -0
- package/tests/stream-translator.test.mjs +137 -0
- package/tests/support/hanging-stream.mjs +48 -0
- package/tests/support/resolve-profile-deps.mjs +52 -0
- package/tests/upstream-error-200.test.mjs +164 -0
- package/tests/upstream.test.mjs +330 -0
- package/tests/version-probe.test.mjs +172 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「HTTP 200 里包裹业务错误」的识别。
|
|
3
|
+
*
|
|
4
|
+
* 实测(2026-10-23,本渠道):额度用尽时上游返回
|
|
5
|
+
* `200 + application/json + {"code":1005,"msg":"exceed quota limit"}`。
|
|
6
|
+
*
|
|
7
|
+
* 若把它当成「空响应」处理,会:
|
|
8
|
+
* ① 丢掉真正的错误码,用户只看到误导性的「200 但零内容」;
|
|
9
|
+
* ② 无法据此正确决策(1005 该标号换号;3012 该直接失败不重试)。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import test from 'node:test'
|
|
13
|
+
import assert from 'node:assert/strict'
|
|
14
|
+
import { readFileSync } from 'node:fs'
|
|
15
|
+
import { fileURLToPath } from 'node:url'
|
|
16
|
+
import { dirname, join } from 'node:path'
|
|
17
|
+
|
|
18
|
+
import { extractUpstreamError, readContentHead } from '../lib/sse.js'
|
|
19
|
+
import { classifyUpstreamError, extractBusinessCode, hasBusinessCode } from '../lib/errors.js'
|
|
20
|
+
import { ZcodePlanClient } from '../lib/plan-client.js'
|
|
21
|
+
|
|
22
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
23
|
+
const fixture = (file) => readFileSync(join(here, 'fixtures', file))
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 回归:数字业务码必须按**结构**判定,不能裸子串匹配。
|
|
27
|
+
*
|
|
28
|
+
* 实测踩点:网关 502 页正文里带 `Request ID 3f8a-1005-b27c`,旧实现
|
|
29
|
+
* `body.includes('1005')` 判成「额度用尽」→ 把一个健康账号标限流到**当日结束**。
|
|
30
|
+
*/
|
|
31
|
+
test('classifyUpstreamError: 非 JSON 错误页里的数字片段不再误判为额度用尽', () => {
|
|
32
|
+
const html = '<html><head><title>502 Bad Gateway</title></head><body><p>Request ID 3f8a-1005-b27c</p></body></html>'
|
|
33
|
+
assert.equal(classifyUpstreamError(500, html), 'other')
|
|
34
|
+
assert.equal(hasBusinessCode(html, 1005), false)
|
|
35
|
+
const another = '<html><body>Error 1113 detected in upstream module</body></html>'
|
|
36
|
+
assert.equal(classifyUpstreamError(502, another), 'other')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('classifyUpstreamError: 结构化业务码(数字/字符串/嵌套)照旧识别', () => {
|
|
40
|
+
assert.equal(classifyUpstreamError(200, '{"code":1005,"msg":"exceed quota limit"}'), 'quota')
|
|
41
|
+
assert.equal(classifyUpstreamError(200, '{"code":"1005"}'), 'quota')
|
|
42
|
+
assert.equal(classifyUpstreamError(200, '{"error":{"code":3012}}'), 'risk')
|
|
43
|
+
assert.equal(classifyUpstreamError(200, '{"code":3009}'), 'concurrency')
|
|
44
|
+
assert.equal(classifyUpstreamError(200, '{"code":3007}'), 'captcha')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('extractBusinessCode: 只认 JSON 结构,纯文本/HTML 一律 undefined', () => {
|
|
48
|
+
assert.equal(extractBusinessCode('{"code":0}'), 0)
|
|
49
|
+
assert.equal(extractBusinessCode('{"msg":"ok"}'), undefined)
|
|
50
|
+
assert.equal(extractBusinessCode('<html>1005</html>'), undefined)
|
|
51
|
+
/** 被包裹的 JSON 片段仍能取出(上游偶有前后缀)。 */
|
|
52
|
+
assert.equal(extractBusinessCode('gateway: {"code":3007} trailing'), 3007)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('hasBusinessCode: 结构化 code 优先于兜底数字 token', () => {
|
|
56
|
+
/** 有结构化 code 时,出现别的数字串不算。 */
|
|
57
|
+
assert.equal(hasBusinessCode('{"code":3009,"msg":"1005 is a doc id"}', 1005), false)
|
|
58
|
+
assert.equal(hasBusinessCode('{"code":3009,"msg":"1005 is a doc id"}', 3009), true)
|
|
59
|
+
/** 无结构时裸业务码仍可识别(上游偶尔回纯文本)。 */
|
|
60
|
+
assert.equal(hasBusinessCode('1005 exceed quota limit', 1005), true)
|
|
61
|
+
assert.equal(hasBusinessCode('upstream 3009x timeout', 3009), false)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('extractUpstreamError: 200 + {code:1005} 被识别为业务错误', () => {
|
|
65
|
+
const error = extractUpstreamError({ code: 1005, msg: 'exceed quota limit' }, '')
|
|
66
|
+
assert.notEqual(error, undefined)
|
|
67
|
+
assert.match(error.text, /1005/)
|
|
68
|
+
assert.match(error.text, /exceed quota limit/)
|
|
69
|
+
/** 交给错误表后应判成 quota(而不是 other / 无权益)。 */
|
|
70
|
+
assert.equal(classifyUpstreamError(200, error.text), 'quota')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('extractUpstreamError: 3012 风控与 3009 并发同样能从 200 里认出来', () => {
|
|
74
|
+
assert.equal(
|
|
75
|
+
classifyUpstreamError(200, extractUpstreamError({ code: 3012, msg: 'unusual activity' }, '').text),
|
|
76
|
+
'risk',
|
|
77
|
+
)
|
|
78
|
+
assert.equal(
|
|
79
|
+
classifyUpstreamError(200, extractUpstreamError({ code: 3009, msg: 'concurrency limit' }, '').text),
|
|
80
|
+
'concurrency',
|
|
81
|
+
)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('extractUpstreamError: 成功码 0 与正常 message 不算错误', () => {
|
|
85
|
+
assert.equal(extractUpstreamError({ code: 0, msg: '' }, ''), undefined)
|
|
86
|
+
assert.equal(extractUpstreamError({ id: 'msg_1', type: 'message', content: [] }, ''), undefined)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('extractUpstreamError: Anthropic 标准错误载荷与裸 error 字段', () => {
|
|
90
|
+
const standard = extractUpstreamError({ type: 'error', error: { type: 'api_error', message: 'boom' } }, '')
|
|
91
|
+
assert.notEqual(standard, undefined)
|
|
92
|
+
assert.match(standard.text, /api_error/)
|
|
93
|
+
const bare = extractUpstreamError({ error: { message: 'boom' } }, '')
|
|
94
|
+
assert.notEqual(bare, undefined)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('readContentHead: 200 + 业务错误的 JSON 不再被当成「零内容」', async () => {
|
|
98
|
+
const body = new ReadableStream({
|
|
99
|
+
start(controller) {
|
|
100
|
+
controller.enqueue(fixture('quota-200.json'))
|
|
101
|
+
controller.close()
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
const head = await readContentHead(body, { contentType: 'application/json; charset=utf-8' })
|
|
105
|
+
assert.equal(head.hasContent, false)
|
|
106
|
+
assert.notEqual(head.upstreamError, undefined)
|
|
107
|
+
assert.match(head.upstreamError.text, /1005/)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('plan-client: 200 里的 1005 标记该账号当日用尽并换号(而不是「零内容」)', async () => {
|
|
111
|
+
const quotaBody = () =>
|
|
112
|
+
new ReadableStream({
|
|
113
|
+
start(controller) {
|
|
114
|
+
controller.enqueue(fixture('quota-200.json'))
|
|
115
|
+
controller.close()
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
const pool = {
|
|
119
|
+
state: [
|
|
120
|
+
{ id: 'acct-a', enabled: true, model_rate_limits: {}, credential: { zcode_jwt: 'jwt-a', device_mid: 'dev-a' } },
|
|
121
|
+
{ id: 'acct-b', enabled: true, model_rate_limits: {}, credential: { zcode_jwt: 'jwt-b', device_mid: 'dev-b' } },
|
|
122
|
+
],
|
|
123
|
+
list() {
|
|
124
|
+
return this.state
|
|
125
|
+
},
|
|
126
|
+
pick(_model, exclude) {
|
|
127
|
+
const hit = this.state.find((entry) => entry.enabled && !(exclude?.has(entry.id) ?? false))
|
|
128
|
+
return hit === undefined ? null : { entry: hit, credential: hit.credential }
|
|
129
|
+
},
|
|
130
|
+
markModelRateLimited(id, model) {
|
|
131
|
+
const hit = this.state.find((entry) => entry.id === id)
|
|
132
|
+
if (hit !== undefined) hit.model_rate_limits[model] = Date.now() + 60_000
|
|
133
|
+
},
|
|
134
|
+
noteError() {},
|
|
135
|
+
}
|
|
136
|
+
const marks = []
|
|
137
|
+
const originalMark = pool.markModelRateLimited.bind(pool)
|
|
138
|
+
pool.markModelRateLimited = (id, model, at) => {
|
|
139
|
+
marks.push({ id, code: 'quota' })
|
|
140
|
+
originalMark(id, model, at)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const client = new ZcodePlanClient({
|
|
144
|
+
pool,
|
|
145
|
+
minter: { async mint() { return { param: 'p', interactive: false } }, async fetchConfig() { return undefined } },
|
|
146
|
+
fetchImpl: async () => ({
|
|
147
|
+
ok: true,
|
|
148
|
+
status: 200,
|
|
149
|
+
headers: new Headers({ 'content-type': 'application/json; charset=utf-8' }),
|
|
150
|
+
body: quotaBody(),
|
|
151
|
+
}),
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
await assert.rejects(
|
|
155
|
+
() => client.call({ options: { model: 'GLM-5.3-Flash', messages: [{ role: 'user', content: [{ type: 'text', text: 'hi' }] }] } }),
|
|
156
|
+
(error) => {
|
|
157
|
+
assert.equal(error.code, 'QUOTA')
|
|
158
|
+
assert.match(error.message, /1005/)
|
|
159
|
+
return true
|
|
160
|
+
},
|
|
161
|
+
)
|
|
162
|
+
/** 两个账号都被按「额度用尽」标记过(而不是被当成无权益)。 */
|
|
163
|
+
assert.deepEqual(marks.map((mark) => mark.id), ['acct-a', 'acct-b'])
|
|
164
|
+
})
|
|
@@ -0,0 +1,330 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 版本探测(`lib/version-probe.js`)与门控下限(`lib/upstream.js`)的回归测试。
|
|
3
|
+
*
|
|
4
|
+
* 背景:服务端门控下限会随官方发布推进而抬高(实测 3.14.3 → 3.16.3 → 3.17.2,
|
|
5
|
+
* 每次抬高都让写死的常量失效,表现为「活动存在但探测不到」)。所以下限改为
|
|
6
|
+
* **运行期向官方首页取最新版本**再抬高,本文件锁三件事:
|
|
7
|
+
*
|
|
8
|
+
* 1. `extractLatestVersion` 从首页 HTML 里取**最高**版本(不是第一个、也不是最后一个);
|
|
9
|
+
* 2. `fetchLatestClientVersion` 任何失败都返回 `undefined`(探测失败不许阻断领取链路);
|
|
10
|
+
* 3. `setGatedMinAppVersion` **只抬高不降低**(探到更旧的版本 / 脏值一律忽略)。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import test from 'node:test'
|
|
14
|
+
import assert from 'node:assert/strict'
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
ZCODE_SITE_ORIGIN,
|
|
18
|
+
extractLatestVersion,
|
|
19
|
+
fetchLatestClientVersion,
|
|
20
|
+
resetVersionCache,
|
|
21
|
+
} from '../lib/version-probe.js'
|
|
22
|
+
import {
|
|
23
|
+
ZCODE_VERSION_GATED_MIN_APP_VERSION,
|
|
24
|
+
currentGatedMinAppVersion,
|
|
25
|
+
resetGatedMinAppVersion,
|
|
26
|
+
setGatedMinAppVersion,
|
|
27
|
+
} from '../lib/upstream.js'
|
|
28
|
+
|
|
29
|
+
/** 官方首页的真实形态:所有发布版本的下载链接铺在 HTML 里(顺序与版本高低无关)。 */
|
|
30
|
+
const releaseLink = (version, platform = 'win32', arch = 'x64', ext = 'exe') =>
|
|
31
|
+
`https://cdn-zcode.z.ai/zcode/electron/releases/${version}/${platform}/ZCode-${version}-${arch}.${ext}`
|
|
32
|
+
|
|
33
|
+
const htmlWith = (...versions) =>
|
|
34
|
+
`<html><body><script>window.__ASSETS__=${JSON.stringify(versions.map((v) => releaseLink(v)))}</script></body></html>`
|
|
35
|
+
|
|
36
|
+
test('extractLatestVersion: 取**最高**版本(不是首个/末个)', () => {
|
|
37
|
+
/** 刻意把 3.9.10 放在最后:字典序会误判成最高,点分比较不会。 */
|
|
38
|
+
const html = htmlWith('3.17.2', '3.14.3', '3.16.3', '3.9.10')
|
|
39
|
+
assert.equal(extractLatestVersion(html), '3.17.2')
|
|
40
|
+
/** 乱序、含重复:仍取最大,且不去重也不影响结果。 */
|
|
41
|
+
assert.equal(extractLatestVersion(htmlWith('3.18.0', '3.17.2', '3.18.0')), '3.18.0')
|
|
42
|
+
/** 段数不同时按缺失补 0 比较(3.10.0 > 3.9.9)。 */
|
|
43
|
+
assert.equal(extractLatestVersion(htmlWith('3.9.9', '3.10.0')), '3.10.0')
|
|
44
|
+
/** 主版本进位(10.0.0 > 9.99.99 而不是字符串比较)。 */
|
|
45
|
+
assert.equal(extractLatestVersion(htmlWith('9.99.99', '10.0.0')), '10.0.0')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('extractLatestVersion: 没有发布链接 / 非字符串 / 空串 → undefined(不猜)', () => {
|
|
49
|
+
assert.equal(extractLatestVersion('<html><body>no releases here</body></html>'), undefined)
|
|
50
|
+
assert.equal(extractLatestVersion(''), undefined)
|
|
51
|
+
assert.equal(extractLatestVersion(undefined), undefined)
|
|
52
|
+
assert.equal(extractLatestVersion(null), undefined)
|
|
53
|
+
/**
|
|
54
|
+
* 只认 `releases/<x.y.z>/` 这种路径段:裸版本号(如 `app_version: 3.99.0`)
|
|
55
|
+
* 不是发布链接,不能当成「官方最新」。
|
|
56
|
+
*/
|
|
57
|
+
assert.equal(extractLatestVersion('<script>const app_version = "3.99.0"</script>'), undefined)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('fetchLatestClientVersion: 抓到的 HTML 里取最高版本,并命中进程内缓存', async () => {
|
|
61
|
+
resetVersionCache()
|
|
62
|
+
const urls = []
|
|
63
|
+
const fetchImpl = async (url) => {
|
|
64
|
+
urls.push(url)
|
|
65
|
+
return { ok: true, status: 200, text: async () => htmlWith('3.16.3', '3.17.2') }
|
|
66
|
+
}
|
|
67
|
+
const version = await fetchLatestClientVersion({ fetchImpl, now: () => 1_000 })
|
|
68
|
+
assert.equal(version, '3.17.2')
|
|
69
|
+
assert.equal(urls.length, 1)
|
|
70
|
+
assert.equal(urls[0], `${ZCODE_SITE_ORIGIN}/`, '抓的是官方首页(公开页面,不带任何凭据)')
|
|
71
|
+
|
|
72
|
+
/** 24h 内走缓存:不再发请求(多刷只会被 CDN 限流)。 */
|
|
73
|
+
const cached = await fetchLatestClientVersion({ fetchImpl, now: () => 1_000 + 60_000 })
|
|
74
|
+
assert.equal(cached, '3.17.2')
|
|
75
|
+
assert.equal(urls.length, 1, '缓存窗口内不该重复抓取')
|
|
76
|
+
|
|
77
|
+
/** `force: true` 强制重抓。 */
|
|
78
|
+
await fetchLatestClientVersion({ fetchImpl, now: () => 1_000 + 60_000, force: true })
|
|
79
|
+
assert.equal(urls.length, 2)
|
|
80
|
+
resetVersionCache()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('fetchLatestClientVersion: 失败一律返回 undefined(绝不抛,绝不阻断领取链路)', async () => {
|
|
84
|
+
resetVersionCache()
|
|
85
|
+
/** ① fetch 自己抛错(DNS 失败 / 连接被拒 / 断网)。 */
|
|
86
|
+
const throwing = async () => {
|
|
87
|
+
throw new TypeError('fetch failed')
|
|
88
|
+
}
|
|
89
|
+
assert.equal(await fetchLatestClientVersion({ fetchImpl: throwing }), undefined)
|
|
90
|
+
|
|
91
|
+
/** ② 非 2xx(CDN 502 / 被限流)。 */
|
|
92
|
+
const notOk = async () => ({ ok: false, status: 502, text: async () => 'bad gateway' })
|
|
93
|
+
assert.equal(await fetchLatestClientVersion({ fetchImpl: notOk }), undefined)
|
|
94
|
+
|
|
95
|
+
/** ③ 200 但 HTML 结构变了(找不到 releases 路径)。 */
|
|
96
|
+
const noReleases = async () => ({ ok: true, status: 200, text: async () => '<html>redesigned</html>' })
|
|
97
|
+
assert.equal(await fetchLatestClientVersion({ fetchImpl: noReleases }), undefined)
|
|
98
|
+
|
|
99
|
+
/** ④ 读 body 时抛(连接中断)。 */
|
|
100
|
+
const brokenBody = async () => ({
|
|
101
|
+
ok: true,
|
|
102
|
+
status: 200,
|
|
103
|
+
text: async () => {
|
|
104
|
+
throw new Error('terminated')
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
assert.equal(await fetchLatestClientVersion({ fetchImpl: brokenBody }), undefined)
|
|
108
|
+
|
|
109
|
+
/** 失败不写缓存:下一次仍然真的去抓(否则一次抖动会固化一整天的「探不到」)。 */
|
|
110
|
+
let calls = 0
|
|
111
|
+
const recover = async () => {
|
|
112
|
+
calls += 1
|
|
113
|
+
return calls === 1 ? { ok: false, status: 500, text: async () => '' } : { ok: true, status: 200, text: async () => htmlWith('3.18.0') }
|
|
114
|
+
}
|
|
115
|
+
assert.equal(await fetchLatestClientVersion({ fetchImpl: recover }), undefined)
|
|
116
|
+
assert.equal(await fetchLatestClientVersion({ fetchImpl: recover }), '3.18.0', '失败不缓存,下一次能自愈')
|
|
117
|
+
resetVersionCache()
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('setGatedMinAppVersion: 只**抬高**不降低', () => {
|
|
121
|
+
resetGatedMinAppVersion()
|
|
122
|
+
const base = ZCODE_VERSION_GATED_MIN_APP_VERSION
|
|
123
|
+
assert.equal(currentGatedMinAppVersion(), base, '初值 = 编译期常量')
|
|
124
|
+
|
|
125
|
+
/** 抬高:生效。 */
|
|
126
|
+
assert.equal(setGatedMinAppVersion('3.18.0'), '3.18.0')
|
|
127
|
+
assert.equal(currentGatedMinAppVersion(), '3.18.0')
|
|
128
|
+
|
|
129
|
+
/** 更低的版本:忽略(降低会让服务端门控重新失效,等于退化)。 */
|
|
130
|
+
assert.equal(setGatedMinAppVersion('3.16.3'), '3.18.0', '探到更旧的版本绝不能把下限拉低')
|
|
131
|
+
assert.equal(currentGatedMinAppVersion(), '3.18.0')
|
|
132
|
+
|
|
133
|
+
/** 相等:保持。 */
|
|
134
|
+
assert.equal(setGatedMinAppVersion('3.18.0'), '3.18.0')
|
|
135
|
+
|
|
136
|
+
/** undefined / 空串 / 脏值:忽略(脏值会原样进 User-Agent,`Headers()` 对脏值抛 TypeError)。 */
|
|
137
|
+
for (const dirty of [undefined, null, '', ' ', 'latest', '3.18', '3.18.0-beta.1', '3.18.0\r\nX-Injected: 1', '03.18.0', 3.18]) {
|
|
138
|
+
assert.equal(setGatedMinAppVersion(dirty), '3.18.0', `脏值 ${JSON.stringify(dirty)} 必须被忽略`)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 再抬一档:继续生效(单调递增是唯一允许的移动方向)。 */
|
|
142
|
+
assert.equal(setGatedMinAppVersion('4.0.0'), '4.0.0')
|
|
143
|
+
|
|
144
|
+
/** 复位回常量(供测试与运行期重置用)。 */
|
|
145
|
+
resetGatedMinAppVersion()
|
|
146
|
+
assert.equal(currentGatedMinAppVersion(), base)
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('端到端:探测到的版本 → 抬高门控下限(失败则沿用内置常量)', async () => {
|
|
150
|
+
resetVersionCache()
|
|
151
|
+
resetGatedMinAppVersion()
|
|
152
|
+
const base = ZCODE_VERSION_GATED_MIN_APP_VERSION
|
|
153
|
+
/** 官方已发到比内置常量更新的版本。 */
|
|
154
|
+
const newer = '9.9.9'
|
|
155
|
+
const fetchImpl = async () => ({ ok: true, status: 200, text: async () => htmlWith(base, newer) })
|
|
156
|
+
const probed = await fetchLatestClientVersion({ fetchImpl })
|
|
157
|
+
assert.equal(probed, newer)
|
|
158
|
+
assert.equal(setGatedMinAppVersion(probed), newer, '探测成功即抬高下限(不必每次发版改常量)')
|
|
159
|
+
|
|
160
|
+
/** 探测失败(undefined):下限保持不动,链路照常可用。 */
|
|
161
|
+
resetGatedMinAppVersion()
|
|
162
|
+
/** 缓存里还留着上一次的成功值 —— 先清掉,否则走的是缓存而不是「探测失败」。 */
|
|
163
|
+
resetVersionCache()
|
|
164
|
+
const offline = async () => {
|
|
165
|
+
throw new Error('offline')
|
|
166
|
+
}
|
|
167
|
+
const failed = await fetchLatestClientVersion({ fetchImpl: offline })
|
|
168
|
+
assert.equal(failed, undefined)
|
|
169
|
+
assert.equal(setGatedMinAppVersion(failed), base, '探测失败沿用内置下限')
|
|
170
|
+
resetVersionCache()
|
|
171
|
+
resetGatedMinAppVersion()
|
|
172
|
+
})
|