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.
Files changed (51) hide show
  1. package/CHANGELOG.md +1963 -672
  2. package/README.md +92 -472
  3. package/bin/cli.mjs +509 -450
  4. package/client.js +2459 -1002
  5. package/cordis.patch.yml +4 -2
  6. package/index.js +109 -11
  7. package/lib/adapter.js +476 -449
  8. package/lib/anthropic-body.js +513 -469
  9. package/lib/assemble.js +808 -542
  10. package/lib/auto-claim.js +229 -0
  11. package/lib/captcha/minter.js +61 -7
  12. package/lib/distribution-keys.js +202 -0
  13. package/lib/errors.js +281 -125
  14. package/lib/fallback.js +120 -0
  15. package/lib/local-endpoint.js +367 -352
  16. package/lib/manage-routes.js +1245 -652
  17. package/lib/model-catalog.js +57 -0
  18. package/lib/openai-gateway.js +776 -0
  19. package/lib/plan-client.js +942 -532
  20. package/lib/pool.js +771 -478
  21. package/lib/sse.js +429 -317
  22. package/lib/upstream-api.js +948 -611
  23. package/lib/upstream.js +294 -134
  24. package/lib/version-probe.js +110 -0
  25. package/package.json +84 -81
  26. package/scripts/analyze-grant-intervals.mjs +171 -0
  27. package/scripts/check-compat-rc3.mjs +21 -11
  28. package/scripts/debug-claimable.mjs +130 -0
  29. package/scripts/probe-marketing.mjs +203 -0
  30. package/scripts/restart-dsh.ps1 +119 -116
  31. package/scripts/test-claimed-pending.mjs +152 -0
  32. package/scripts/verify_compat.mjs +75 -64
  33. package/scripts/watch-marketing.mjs +155 -0
  34. package/tests/auto-claim.test.mjs +318 -0
  35. package/tests/claim-routes.test.mjs +438 -293
  36. package/tests/client-panel.test.mjs +1141 -356
  37. package/tests/distribution-keys.test.mjs +123 -0
  38. package/tests/e2e.test.mjs +388 -353
  39. package/tests/fallback-scrub.test.mjs +105 -0
  40. package/tests/launch-command.test.mjs +68 -0
  41. package/tests/openai-gateway.test.mjs +454 -0
  42. package/tests/pinned-models.test.mjs +505 -497
  43. package/tests/plan-client.test.mjs +582 -222
  44. package/tests/pool.test.mjs +319 -93
  45. package/tests/quota-blackhole.test.mjs +409 -0
  46. package/tests/restart-script.test.mjs +53 -0
  47. package/tests/routing-quota.test.mjs +251 -0
  48. package/tests/support/hanging-stream.mjs +48 -0
  49. package/tests/upstream-error-200.test.mjs +164 -125
  50. package/tests/upstream.test.mjs +330 -250
  51. package/tests/version-probe.test.mjs +172 -0
@@ -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
+ })