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
|
@@ -1,356 +1,1141 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* client.js 面板的**结构级**测试。
|
|
3
|
-
*
|
|
4
|
-
* client.js 是宿主注入 `React` 的浏览器 bundle(不 import react),
|
|
5
|
-
* 所以这里塞一个**最小 createElement 实现**,把渲染结果摊成可断言的树:
|
|
6
|
-
* 只关心「有没有那条进度条 / 条纹标记对不对 / 底部那句时间在不在」。
|
|
7
|
-
*
|
|
8
|
-
* 覆盖 0.4.7 的需求:未生效额度与已生效额度**同形状一条进度条**,
|
|
9
|
-
* 区别只在 `data-pending` 与底部「X 起可用」。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import test from 'node:test'
|
|
13
|
-
import assert from 'node:assert/strict'
|
|
14
|
-
import { readFileSync } from 'node:fs'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 极简 h():只认 type / props / children,够断言用。
|
|
18
|
-
*
|
|
19
|
-
* ⚠ hooks 是**带计数**的桩:同一组件在两次渲染里调用的 hook 数不一致时抛错
|
|
20
|
-
* (复刻 React 的「Rendered fewer hooks than expected」)。0.5.0 的
|
|
21
|
-
* ClaimSection 把 useCallback 放在早退之后,账号非空时多调一个 → 整个面板白屏。
|
|
22
|
-
*/
|
|
23
|
-
function makeReact() {
|
|
24
|
-
const h = (type, props, ...children) => ({
|
|
25
|
-
type,
|
|
26
|
-
props: props ?? {},
|
|
27
|
-
children: children.flat().filter((child) => child !== null && child !== undefined && child !== false),
|
|
28
|
-
})
|
|
29
|
-
/** 当前组件的 hook 计数(渲染前由 render() 重置)。 */
|
|
30
|
-
let hookCount = 0
|
|
31
|
-
let expectedHookCount
|
|
32
|
-
const tick = () => {
|
|
33
|
-
hookCount += 1
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
createElement: h,
|
|
37
|
-
Fragment: Symbol('Fragment'),
|
|
38
|
-
useState: (initial) => {
|
|
39
|
-
tick()
|
|
40
|
-
return [typeof initial === 'function' ? initial() : initial, () => {}]
|
|
41
|
-
},
|
|
42
|
-
useEffect: () => tick(),
|
|
43
|
-
useRef: (initial) => {
|
|
44
|
-
tick()
|
|
45
|
-
return { current: initial }
|
|
46
|
-
},
|
|
47
|
-
useCallback: (fn) => {
|
|
48
|
-
tick()
|
|
49
|
-
return fn
|
|
50
|
-
},
|
|
51
|
-
__hooks: {
|
|
52
|
-
begin: (expected) => {
|
|
53
|
-
expectedHookCount = expected
|
|
54
|
-
hookCount = 0
|
|
55
|
-
},
|
|
56
|
-
end: () => {
|
|
57
|
-
if (expectedHookCount !== undefined && hookCount !== expectedHookCount) {
|
|
58
|
-
throw new Error(`渲染之间 hook 数量变化:${expectedHookCount} → ${hookCount}(React 会抛 Rendered fewer hooks than expected)`)
|
|
59
|
-
}
|
|
60
|
-
expectedHookCount = hookCount
|
|
61
|
-
return hookCount
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 把 client.js 当作工厂函数求值。它导出 `(ctx) => …`,内部用闭包里的
|
|
69
|
-
* `React`;这里用一个假的 React 与环境把面板组件取出来。
|
|
70
|
-
*/
|
|
71
|
-
const SOURCE = readFileSync(new URL('../client.js', import.meta.url), 'utf8')
|
|
72
|
-
|
|
73
|
-
/** 全局共享的假 React(buildPanel 与 resolve 用同一份 Fragment 符号)。 */
|
|
74
|
-
const FakeReact = makeReact()
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
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
|
-
const
|
|
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
|
-
const
|
|
189
|
-
|
|
190
|
-
const
|
|
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
|
-
assert.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
assert.
|
|
288
|
-
assert.
|
|
289
|
-
assert.
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const
|
|
299
|
-
assert.
|
|
300
|
-
assert.match(
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
test('
|
|
304
|
-
const panel = buildPanel()
|
|
305
|
-
const
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
)
|
|
336
|
-
assert.
|
|
337
|
-
const
|
|
338
|
-
assert.
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
const
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
1
|
+
/**
|
|
2
|
+
* client.js 面板的**结构级**测试。
|
|
3
|
+
*
|
|
4
|
+
* client.js 是宿主注入 `React` 的浏览器 bundle(不 import react),
|
|
5
|
+
* 所以这里塞一个**最小 createElement 实现**,把渲染结果摊成可断言的树:
|
|
6
|
+
* 只关心「有没有那条进度条 / 条纹标记对不对 / 底部那句时间在不在」。
|
|
7
|
+
*
|
|
8
|
+
* 覆盖 0.4.7 的需求:未生效额度与已生效额度**同形状一条进度条**,
|
|
9
|
+
* 区别只在 `data-pending` 与底部「X 起可用」。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import test from 'node:test'
|
|
13
|
+
import assert from 'node:assert/strict'
|
|
14
|
+
import { readFileSync } from 'node:fs'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 极简 h():只认 type / props / children,够断言用。
|
|
18
|
+
*
|
|
19
|
+
* ⚠ hooks 是**带计数**的桩:同一组件在两次渲染里调用的 hook 数不一致时抛错
|
|
20
|
+
* (复刻 React 的「Rendered fewer hooks than expected」)。0.5.0 的
|
|
21
|
+
* ClaimSection 把 useCallback 放在早退之后,账号非空时多调一个 → 整个面板白屏。
|
|
22
|
+
*/
|
|
23
|
+
function makeReact() {
|
|
24
|
+
const h = (type, props, ...children) => ({
|
|
25
|
+
type,
|
|
26
|
+
props: props ?? {},
|
|
27
|
+
children: children.flat().filter((child) => child !== null && child !== undefined && child !== false),
|
|
28
|
+
})
|
|
29
|
+
/** 当前组件的 hook 计数(渲染前由 render() 重置)。 */
|
|
30
|
+
let hookCount = 0
|
|
31
|
+
let expectedHookCount
|
|
32
|
+
const tick = () => {
|
|
33
|
+
hookCount += 1
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
createElement: h,
|
|
37
|
+
Fragment: Symbol('Fragment'),
|
|
38
|
+
useState: (initial) => {
|
|
39
|
+
tick()
|
|
40
|
+
return [typeof initial === 'function' ? initial() : initial, () => {}]
|
|
41
|
+
},
|
|
42
|
+
useEffect: () => tick(),
|
|
43
|
+
useRef: (initial) => {
|
|
44
|
+
tick()
|
|
45
|
+
return { current: initial }
|
|
46
|
+
},
|
|
47
|
+
useCallback: (fn) => {
|
|
48
|
+
tick()
|
|
49
|
+
return fn
|
|
50
|
+
},
|
|
51
|
+
__hooks: {
|
|
52
|
+
begin: (expected) => {
|
|
53
|
+
expectedHookCount = expected
|
|
54
|
+
hookCount = 0
|
|
55
|
+
},
|
|
56
|
+
end: () => {
|
|
57
|
+
if (expectedHookCount !== undefined && hookCount !== expectedHookCount) {
|
|
58
|
+
throw new Error(`渲染之间 hook 数量变化:${expectedHookCount} → ${hookCount}(React 会抛 Rendered fewer hooks than expected)`)
|
|
59
|
+
}
|
|
60
|
+
expectedHookCount = hookCount
|
|
61
|
+
return hookCount
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 把 client.js 当作工厂函数求值。它导出 `(ctx) => …`,内部用闭包里的
|
|
69
|
+
* `React`;这里用一个假的 React 与环境把面板组件取出来。
|
|
70
|
+
*/
|
|
71
|
+
const SOURCE = readFileSync(new URL('../client.js', import.meta.url), 'utf8')
|
|
72
|
+
|
|
73
|
+
/** 全局共享的假 React(buildPanel 与 resolve 用同一份 Fragment 符号)。 */
|
|
74
|
+
const FakeReact = makeReact()
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 带状态的假 React:useState 真存值,重渲染时读到更新后的值。
|
|
78
|
+
*
|
|
79
|
+
* 只为两步交互(如删除确认)的行为级测试服务 —— 默认桩的 setState 是 no-op,
|
|
80
|
+
* 「点了删除 → 出现确认按钮」这种跨渲染的状态变化模拟不出来。
|
|
81
|
+
* ⚠ state 槽按 hook 序号全局共享,单组件树内使用(跨组件会串位)。
|
|
82
|
+
*/
|
|
83
|
+
function makeStatefulReact() {
|
|
84
|
+
const h = (type, props, ...children) => ({
|
|
85
|
+
type,
|
|
86
|
+
props: props ?? {},
|
|
87
|
+
children: children.flat().filter((child) => child !== null && child !== undefined && child !== false),
|
|
88
|
+
})
|
|
89
|
+
let hookCount = 0
|
|
90
|
+
let expectedHookCount
|
|
91
|
+
const slots = []
|
|
92
|
+
return {
|
|
93
|
+
createElement: h,
|
|
94
|
+
Fragment: Symbol('Fragment'),
|
|
95
|
+
useState: (initial) => {
|
|
96
|
+
const i = hookCount
|
|
97
|
+
hookCount += 1
|
|
98
|
+
if (slots[i] === undefined) slots[i] = { value: typeof initial === 'function' ? initial() : initial }
|
|
99
|
+
return [
|
|
100
|
+
slots[i].value,
|
|
101
|
+
(next) => {
|
|
102
|
+
slots[i].value = typeof next === 'function' ? next(slots[i].value) : next
|
|
103
|
+
},
|
|
104
|
+
]
|
|
105
|
+
},
|
|
106
|
+
useEffect: () => {
|
|
107
|
+
hookCount += 1
|
|
108
|
+
},
|
|
109
|
+
useRef: (initial) => {
|
|
110
|
+
const i = hookCount
|
|
111
|
+
hookCount += 1
|
|
112
|
+
if (slots[i] === undefined) slots[i] = { value: initial }
|
|
113
|
+
return { current: slots[i].value }
|
|
114
|
+
},
|
|
115
|
+
useCallback: (fn) => {
|
|
116
|
+
hookCount += 1
|
|
117
|
+
return fn
|
|
118
|
+
},
|
|
119
|
+
__hooks: {
|
|
120
|
+
begin: (expected) => {
|
|
121
|
+
expectedHookCount = expected
|
|
122
|
+
hookCount = 0
|
|
123
|
+
},
|
|
124
|
+
end: () => {
|
|
125
|
+
if (expectedHookCount !== undefined && hookCount !== expectedHookCount) {
|
|
126
|
+
throw new Error(`渲染之间 hook 数量变化:${expectedHookCount} → ${hookCount}(React 会抛 Rendered fewer hooks than expected)`)
|
|
127
|
+
}
|
|
128
|
+
expectedHookCount = hookCount
|
|
129
|
+
return hookCount
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 从源码里抠出一个函数定义(按名 + 括号配平)。
|
|
137
|
+
*
|
|
138
|
+
* 不能用 `function NAME(props)` 定位:辅助函数签名各异
|
|
139
|
+
* (`formatReset(resetAt)` / `QuotaMeter(props)`),统一按名字找。
|
|
140
|
+
*/
|
|
141
|
+
function extractFunction(name) {
|
|
142
|
+
const pattern = new RegExp(`function ${name}\\s*\\(`)
|
|
143
|
+
const match = pattern.exec(SOURCE)
|
|
144
|
+
assert.notEqual(match, null, `client.js 里找不到 function ${name}`)
|
|
145
|
+
const start = match.index
|
|
146
|
+
let depth = 0
|
|
147
|
+
let i = SOURCE.indexOf('{', start)
|
|
148
|
+
for (; i < SOURCE.length; i += 1) {
|
|
149
|
+
if (SOURCE[i] === '{') depth += 1
|
|
150
|
+
else if (SOURCE[i] === '}') {
|
|
151
|
+
depth -= 1
|
|
152
|
+
if (depth === 0) break
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return SOURCE.slice(start, i + 1)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 把抽出来的组件 + 依赖函数拼成一个可执行模块。 */
|
|
159
|
+
function buildPanel() {
|
|
160
|
+
const helpers = ['formatReset', 'formatEffective', 'formatPendingUnits', 'formatRemaining', 'formatTokens', 'channelLabel'].map(extractFunction).join('\n')
|
|
161
|
+
const quota = extractFunction('QuotaMeter')
|
|
162
|
+
const card = extractFunction('AccountCard')
|
|
163
|
+
const claimSection = extractFunction('ClaimSection')
|
|
164
|
+
const totalQuota = extractFunction('TotalQuota')
|
|
165
|
+
const distKeys = extractFunction('DistributionKeysSection')
|
|
166
|
+
/**
|
|
167
|
+
* 桩件:NicknameEdit / Toggle / api 用最简实现替代(它们的渲染不是本测试的主题),
|
|
168
|
+
* `h`/`Fragment` 从 fake React 取。
|
|
169
|
+
* `fetch` 可由测试注入(默认抛错,防止测试意外打真端点)。
|
|
170
|
+
*/
|
|
171
|
+
const code = `
|
|
172
|
+
const h = React.createElement
|
|
173
|
+
const Fragment = React.Fragment
|
|
174
|
+
const { useState, useCallback, useEffect } = React
|
|
175
|
+
const API = '/api/zcode-start-plan'
|
|
176
|
+
const api = async () => ({ ok: true, plans: [] })
|
|
177
|
+
function NicknameEdit(props) { return h('span', null, props.nickname) }
|
|
178
|
+
function Toggle() { return h('input', { type: 'checkbox' }) }
|
|
179
|
+
${helpers}
|
|
180
|
+
${quota}
|
|
181
|
+
${card}
|
|
182
|
+
${claimSection}
|
|
183
|
+
${totalQuota}
|
|
184
|
+
${distKeys}
|
|
185
|
+
return { AccountCard, QuotaMeter, ClaimSection, TotalQuota, DistributionKeysSection, formatEffective, formatPendingUnits, formatRemaining, formatTokens }
|
|
186
|
+
`
|
|
187
|
+
// eslint-disable-next-line no-new-func
|
|
188
|
+
const mod = new Function('React', code)(FakeReact)
|
|
189
|
+
/** 组件闭包绑死了构建时的 React —— 状态化渲染必须**用它自己的 React 重建模块**。 */
|
|
190
|
+
const buildModuleWith = (react) => new Function('React', code)(react)
|
|
191
|
+
return {
|
|
192
|
+
...mod,
|
|
193
|
+
React: FakeReact,
|
|
194
|
+
render: (node) => resolve(node, FakeReact),
|
|
195
|
+
/**
|
|
196
|
+
* 状态化渲染:返回「用同一个带 useState 的 React 重渲染」的帮手(两步交互用)。
|
|
197
|
+
* ⚠ 组件闭包绑死构建时的 React,所以这里用状态化 React **重新构建**一份模块
|
|
198
|
+
* (组件源码相同,仅 React 实例不同);state 槽存在该 React 内,跨 renderOnce 保持。
|
|
199
|
+
*/
|
|
200
|
+
renderStateful: (makeNode) => {
|
|
201
|
+
const react = makeStatefulReact()
|
|
202
|
+
const statefulMod = buildModuleWith(react)
|
|
203
|
+
/** makeNode 引用的组件也必须来自 statefulMod,否则又是 FakeReact 版组件。 */
|
|
204
|
+
const node = makeNode(statefulMod)
|
|
205
|
+
const renderOnce = () => resolve(node, react)
|
|
206
|
+
return { renderOnce, react }
|
|
207
|
+
},
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** 在渲染树里找所有 className 含 needle 的节点。 */
|
|
212
|
+
function findAll(node, predicate, out = []) {
|
|
213
|
+
if (node === null || typeof node !== 'object') return out
|
|
214
|
+
if (predicate(node)) out.push(node)
|
|
215
|
+
for (const child of node.children ?? []) findAll(child, predicate, out)
|
|
216
|
+
return out
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 展开树里的函数组件(`h(QuotaMeter, props)` 的 type 是函数)。
|
|
221
|
+
* 不做 memo / hooks 状态,但**校验 hook 数量**:同一个组件先后渲染时
|
|
222
|
+
* hook 数必须一致,否则就是 React 层面的白屏 bug。
|
|
223
|
+
*/
|
|
224
|
+
function resolve(node, React, seen = new Map()) {
|
|
225
|
+
if (node === null || typeof node !== 'object') return node
|
|
226
|
+
if (typeof node.type === 'function') {
|
|
227
|
+
const name = node.type.name || 'anonymous'
|
|
228
|
+
React.__hooks.begin(seen.has(name) ? seen.get(name) : undefined)
|
|
229
|
+
let rendered
|
|
230
|
+
try {
|
|
231
|
+
rendered = node.type(node.props)
|
|
232
|
+
} finally {
|
|
233
|
+
seen.set(name, React.__hooks.end())
|
|
234
|
+
}
|
|
235
|
+
return resolve(rendered, React, seen)
|
|
236
|
+
}
|
|
237
|
+
return { ...node, children: node.children.map((child) => resolve(child, React, seen)) }
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const textOf = (node) =>
|
|
241
|
+
node === null || node === undefined
|
|
242
|
+
? ''
|
|
243
|
+
: typeof node === 'string' || typeof node === 'number'
|
|
244
|
+
? String(node)
|
|
245
|
+
: (node.children ?? []).map(textOf).join('')
|
|
246
|
+
|
|
247
|
+
const baseAccount = {
|
|
248
|
+
id: 'acc-1',
|
|
249
|
+
index: 1,
|
|
250
|
+
nickname: 'ACC_B',
|
|
251
|
+
enabled: true,
|
|
252
|
+
channel: 'zai',
|
|
253
|
+
modelRateLimits: {},
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
test('AccountCard: 已生效额度只有一条实色进度条', () => {
|
|
257
|
+
const panel = buildPanel()
|
|
258
|
+
const tree = panel.render(panel.AccountCard({
|
|
259
|
+
account: baseAccount,
|
|
260
|
+
balance: { ok: true, enterprise: false, remaining: 7_876_044, total: 8_000_000, planName: 'GLM-5.3' },
|
|
261
|
+
modelListVisible: true,
|
|
262
|
+
}))
|
|
263
|
+
const bars = findAll(tree, (node) => node.props?.className === 'zsp-bar')
|
|
264
|
+
assert.equal(bars.length, 1, '已生效时应有一条进度条')
|
|
265
|
+
assert.notEqual(bars[0].props['data-pending'], true, '已生效的条不能打 pending 标记')
|
|
266
|
+
const text = textOf(tree)
|
|
267
|
+
assert.match(text, /剩余 7876044\/8000000/)
|
|
268
|
+
assert.match(text, /GLM-5\.3/)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('AccountCard: 未生效额度与已生效额度同形状,底部标启用时间', () => {
|
|
272
|
+
const panel = buildPanel()
|
|
273
|
+
const effectiveAt = Date.now() + 2 * 60 * 60 * 1000
|
|
274
|
+
const tree = panel.render(panel.AccountCard({
|
|
275
|
+
account: baseAccount,
|
|
276
|
+
balance: {
|
|
277
|
+
ok: true,
|
|
278
|
+
enterprise: false,
|
|
279
|
+
remaining: 0,
|
|
280
|
+
total: 0,
|
|
281
|
+
planName: null,
|
|
282
|
+
pending: [{ planId: 'zcode-v3-start-plan-nt-1026', showName: 'GLM-5.3-Flash', grantUnits: 300_000_000, effectiveAt }],
|
|
283
|
+
},
|
|
284
|
+
modelListVisible: true,
|
|
285
|
+
}))
|
|
286
|
+
const bars = findAll(tree, (node) => node.props?.className === 'zsp-bar')
|
|
287
|
+
assert.equal(bars.length, 2, '已生效 1 条 + 未生效 1 条')
|
|
288
|
+
assert.notEqual(bars[0].props['data-pending'], true, '第一条是已生效的')
|
|
289
|
+
assert.equal(bars[1].props['data-pending'], true, '第二条是未生效的(条纹)')
|
|
290
|
+
/** 未生效那条必须是满格:还没开始消耗。 */
|
|
291
|
+
const fill = bars[1].children[0]
|
|
292
|
+
assert.equal(fill.props.style.width, '100%')
|
|
293
|
+
|
|
294
|
+
const text = textOf(tree)
|
|
295
|
+
assert.match(text, /起可用/, '进度条底部要写启用时间')
|
|
296
|
+
assert.match(text, /GLM-5\.3-Flash 3亿 tokens/, '要标出是哪一份额度')
|
|
297
|
+
/** 时间文案应出现在条下方的 caption 里,而不是标题行。 */
|
|
298
|
+
const captions = findAll(tree, (node) => node.props?.className?.includes('zsp-meter-caption'))
|
|
299
|
+
assert.equal(captions.length, 2, '每条进度条各一句 caption')
|
|
300
|
+
assert.match(textOf(captions[1]), /起可用/)
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
test('AccountCard: 多个未生效额度各一条进度条(按到点时间顺序)', () => {
|
|
304
|
+
const panel = buildPanel()
|
|
305
|
+
const now = Date.now()
|
|
306
|
+
const tree = panel.render(panel.AccountCard({
|
|
307
|
+
account: baseAccount,
|
|
308
|
+
balance: {
|
|
309
|
+
ok: true,
|
|
310
|
+
enterprise: false,
|
|
311
|
+
remaining: 0,
|
|
312
|
+
total: 0,
|
|
313
|
+
planName: null,
|
|
314
|
+
pending: [
|
|
315
|
+
{ planId: 'a', showName: 'GLM-5.3-Flash', grantUnits: 100_000_000, effectiveAt: now + 3_600_000 },
|
|
316
|
+
{ planId: 'b', showName: 'GLM-5.3', grantUnits: 50_000_000, effectiveAt: now + 7_200_000 },
|
|
317
|
+
],
|
|
318
|
+
},
|
|
319
|
+
modelListVisible: true,
|
|
320
|
+
}))
|
|
321
|
+
const pendingBars = findAll(tree, (node) => node.props?.className === 'zsp-bar' && node.props['data-pending'] === true)
|
|
322
|
+
assert.equal(pendingBars.length, 2)
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
test('QuotaMeter: 缺 total 的额度按 0% 画(不是满格),未生效固定 100%', () => {
|
|
326
|
+
const panel = buildPanel()
|
|
327
|
+
const unknown = panel.QuotaMeter({ remaining: undefined, total: undefined })
|
|
328
|
+
assert.equal(unknown.children[0].children[0].props.style.width, '0%')
|
|
329
|
+
const pending = panel.QuotaMeter({ pending: true, effectiveAt: Date.now() + 1000 })
|
|
330
|
+
assert.equal(pending.children[0].children[0].props.style.width, '100%')
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
test('AccountCard: 企业版与错误态不画进度条', () => {
|
|
334
|
+
const panel = buildPanel()
|
|
335
|
+
const enterprise = panel.render(panel.AccountCard({ account: baseAccount, balance: { ok: true, enterprise: true }, modelListVisible: true }))
|
|
336
|
+
assert.equal(findAll(enterprise, (n) => n.props?.className === 'zsp-bar').length, 0)
|
|
337
|
+
const failed = panel.render(panel.AccountCard({ account: baseAccount, balance: { ok: false, error: '余额不可用' }, modelListVisible: true }))
|
|
338
|
+
assert.equal(findAll(failed, (n) => n.props?.className === 'zsp-bar').length, 0)
|
|
339
|
+
assert.match(textOf(failed), /余额不可用/)
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
test('formatEffective: 今天只给时刻,跨天带日期', () => {
|
|
343
|
+
const panel = buildPanel()
|
|
344
|
+
const { formatEffective } = panel
|
|
345
|
+
const today = new Date()
|
|
346
|
+
const laterToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23, 0).getTime()
|
|
347
|
+
assert.equal(formatEffective(laterToday), '23:00')
|
|
348
|
+
const nextWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7, 9, 30).getTime()
|
|
349
|
+
assert.match(formatEffective(nextWeek), /\d+\/\d+/)
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
test('formatRemaining: 分钟 / 小时 / 天 / 已过期 四档', () => {
|
|
353
|
+
const panel = buildPanel()
|
|
354
|
+
const { formatRemaining } = panel
|
|
355
|
+
const now = Date.now()
|
|
356
|
+
assert.equal(formatRemaining(now + 12 * 60_000, now), '12 分钟后')
|
|
357
|
+
assert.equal(formatRemaining(now + 3 * 3_600_000, now), '3 小时后')
|
|
358
|
+
assert.equal(formatRemaining(now + 90 * 60_000, now), '1 小时 30 分后')
|
|
359
|
+
assert.equal(formatRemaining(now + 2 * 86_400_000, now), '2 天后')
|
|
360
|
+
assert.equal(formatRemaining(now + 30_000, now), '不到 1 分钟')
|
|
361
|
+
assert.equal(formatRemaining(now - 1000, now), '已过期')
|
|
362
|
+
assert.equal(formatRemaining(0, now), '', '没有到期时间时返回空串,不能出现 1970')
|
|
363
|
+
assert.equal(formatRemaining(undefined, now), '')
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
test('QuotaMeter: caption 同时给出剩余、计划名与到期时间', () => {
|
|
367
|
+
const panel = buildPanel()
|
|
368
|
+
const expiresAt = Date.now() + 3 * 3_600_000 // 3 小时后
|
|
369
|
+
const tree = panel.QuotaMeter({ remaining: 7_876_044, total: 8_000_000, planName: 'GLM-5.3', expiresAt })
|
|
370
|
+
const text = textOf(tree.children[1])
|
|
371
|
+
assert.match(text, /剩余 7876044\/8000000/)
|
|
372
|
+
assert.match(text, /GLM-5\.3/)
|
|
373
|
+
assert.match(text, /到期/)
|
|
374
|
+
/**
|
|
375
|
+
* ⚠ 不要写死「3 小时后」:`expiresAt` 与渲染时刻之间会因毫秒误差落到
|
|
376
|
+
* 「2 小时 59 分后」,断言就随开机时刻飘(本用例曾因此间歇性失败)。
|
|
377
|
+
*/
|
|
378
|
+
assert.match(text, /[23] 小时(?: \d+ 分)?后/, '相对时间(约 3 小时后,容忍分钟级抖动)')
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
test('QuotaMeter: 未生效额度同时给"何时起可用"与"何时到期"', () => {
|
|
382
|
+
const panel = buildPanel()
|
|
383
|
+
const effectiveAt = Date.now() + 60 * 60_000
|
|
384
|
+
const expiresAt = Date.now() + 2 * 86_400_000
|
|
385
|
+
const tree = panel.QuotaMeter({ pending: true, effectiveAt, expiresAt, planName: 'GLM-5.3-Flash' })
|
|
386
|
+
const text = textOf(tree.children[1])
|
|
387
|
+
assert.match(text, /起可用/)
|
|
388
|
+
assert.match(text, /到期/)
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
test('QuotaMeter: 没有 gives 到期信息时不留下 undefined', () => {
|
|
392
|
+
const panel = buildPanel()
|
|
393
|
+
const tree = panel.QuotaMeter({ remaining: 10, total: 10, planName: 'X' })
|
|
394
|
+
const text = textOf(tree.children[1])
|
|
395
|
+
assert.doesNotMatch(text, /undefined/)
|
|
396
|
+
assert.doesNotMatch(text, /NaN/)
|
|
397
|
+
assert.doesNotMatch(text, /1970/)
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* ⚠ 白屏回归闸(0.5.0 实际事故)。
|
|
402
|
+
*
|
|
403
|
+
* ClaimSection 曾把 `useCallback` 放在 `return null` 早退之后:
|
|
404
|
+
* 账号为空走早退 → 少调一个 hook;非空 → 多调一个。React 抛
|
|
405
|
+
* 「Rendered fewer hooks than expected」,**整个插件面板白屏**。
|
|
406
|
+
*
|
|
407
|
+
* 本用例用**带计数的 hooks 桩**渲染 ClaimSection 两次(空账号池 / 有账号),
|
|
408
|
+
* 两次 hook 数不一致就抛错。
|
|
409
|
+
*/
|
|
410
|
+
test('ClaimSection: 空账号池与有账号两次渲染,hook 数量必须一致', () => {
|
|
411
|
+
const panel = buildPanel()
|
|
412
|
+
/** 先渲染空池(走早退)。 */
|
|
413
|
+
const empty = panel.render(panel.ClaimSection({ accounts: [], busy: false, captcha: { reachable: false } }))
|
|
414
|
+
assert.equal(empty, null, '空池时不渲染任何内容')
|
|
415
|
+
|
|
416
|
+
/** 再渲染有账号(不应因 hooks 数量变化而抛错)。 */
|
|
417
|
+
const filled = panel.render(
|
|
418
|
+
panel.ClaimSection({
|
|
419
|
+
accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }],
|
|
420
|
+
busy: false,
|
|
421
|
+
captcha: { reachable: false, debugPort: 9229, reason: '连不上调试端口' },
|
|
422
|
+
}),
|
|
423
|
+
)
|
|
424
|
+
assert.notEqual(filled, null)
|
|
425
|
+
const text = textOf(filled)
|
|
426
|
+
assert.match(text, /赠送额度/)
|
|
427
|
+
/** captcha 不可用 → 必须给出「开调试端口重启」按钮,而不是让人手抄 param。 */
|
|
428
|
+
const buttons = findAll(filled, (node) => node.type === 'button').map(textOf)
|
|
429
|
+
assert.ok(buttons.includes('开调试端口重启'), `应出现重启按钮,实际:${buttons.join(' / ')}`)
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
test('ClaimSection: captcha 可用时不显示重启按钮', () => {
|
|
433
|
+
const panel = buildPanel()
|
|
434
|
+
const tree = panel.render(
|
|
435
|
+
panel.ClaimSection({
|
|
436
|
+
accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }],
|
|
437
|
+
busy: false,
|
|
438
|
+
captcha: { reachable: true, debugPort: 9229 },
|
|
439
|
+
}),
|
|
440
|
+
)
|
|
441
|
+
const buttons = findAll(tree, (node) => node.type === 'button').map(textOf)
|
|
442
|
+
assert.ok(!buttons.includes('以调试模式重启'), '载体已就绪就不该再提示重启')
|
|
443
|
+
assert.match(textOf(tree), /探测/, '0.6.0 文案:探测自动填 plan_id')
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
test('ClaimSection: 默认关闭时拨盘说明「探测自动、领取手动」', () => {
|
|
447
|
+
const panel = buildPanel()
|
|
448
|
+
const tree = panel.render(
|
|
449
|
+
panel.ClaimSection({
|
|
450
|
+
accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }],
|
|
451
|
+
busy: false,
|
|
452
|
+
captcha: { reachable: true },
|
|
453
|
+
autoClaim: { enabled: false },
|
|
454
|
+
}),
|
|
455
|
+
)
|
|
456
|
+
const text = textOf(tree)
|
|
457
|
+
/** 「自动领取」是拨盘的 aria-label(不渲染成可见文字),可见文本是这句说明。 */
|
|
458
|
+
assert.match(text, /自动领取已关闭:探测仍然自动/, '关闭时说明探测仍自动、领取手动')
|
|
459
|
+
/** 测试桩把 Toggle 替换成裸 input,所以按 type=checkbox 找拨盘。 */
|
|
460
|
+
const toggles = findAll(tree, (node) => node.type === 'input' && node.props?.type === 'checkbox')
|
|
461
|
+
assert.equal(toggles.length, 1, '要有自动领取拨盘')
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
test('ClaimSection: 自动领取开启时显示已开启与已记录数', () => {
|
|
465
|
+
const panel = buildPanel()
|
|
466
|
+
const tree = panel.render(
|
|
467
|
+
panel.ClaimSection({
|
|
468
|
+
accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }],
|
|
469
|
+
busy: false,
|
|
470
|
+
captcha: { reachable: true },
|
|
471
|
+
autoClaim: { enabled: true, seenCount: 3 },
|
|
472
|
+
}),
|
|
473
|
+
)
|
|
474
|
+
const text = textOf(tree)
|
|
475
|
+
assert.match(text, /已开启/)
|
|
476
|
+
assert.match(text, /已记录 3 个/, '把 seenCount 透出,用户能看出「不会重复领」')
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
test('ClaimSection 源码:展开时自动探测(useEffect + /claimable),探测零成本', () => {
|
|
480
|
+
const claimSectionSrc = extractFunction('ClaimSection')
|
|
481
|
+
assert.match(claimSectionSrc, /useEffect\(/, '展开即自动探测(不需要用户点按钮)')
|
|
482
|
+
assert.match(claimSectionSrc, /\/claimable/, '探测走官方发现端点')
|
|
483
|
+
assert.match(claimSectionSrc, /onToggleAutoClaim/, '拨盘要能把开关写回宿主')
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
test('ClaimSection 源码:重启只给指引,不再替用户执行', () => {
|
|
487
|
+
const claimSectionSrc = extractFunction('ClaimSection')
|
|
488
|
+
/** 0.6.6:杀进程重启被 AV 判毒,且脚本会被宿主 Job Object 连带杀死 → 只给命令。 */
|
|
489
|
+
assert.doesNotMatch(claimSectionSrc, /confirmRestart/, '不应再有杀进程式二次确认(端点已只返回命令)')
|
|
490
|
+
assert.match(claimSectionSrc, /data\?\.\s*command/, '展示服务端返回的启动命令')
|
|
491
|
+
assert.match(claimSectionSrc, /Note/, '展示说明(普通重启 / 带端口)')
|
|
492
|
+
assert.match(claimSectionSrc, /restart\('plain'\)/, '普通重启指引(不带端口)')
|
|
493
|
+
assert.match(claimSectionSrc, /restart\('debug'\)/, '带端口重启指引')
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* ⚠ 2026-11-02:启动命令必须**贴着**它对应的提示行。
|
|
498
|
+
*
|
|
499
|
+
* 旧实现把命令块挂在整组最底部(拨盘下方),而「连不上调试端口」这行字在顶部:
|
|
500
|
+
* 用户要复制的东西在几百像素之外,还得自己判断「底部这条是哪一份指引」。
|
|
501
|
+
* 另外 `__restart` 是单一槽位,普通/带端口两份指引会互相顶替。
|
|
502
|
+
*/
|
|
503
|
+
test('ClaimSection:带端口命令紧贴提示行,且与普通重启指引分槽互不顶替', () => {
|
|
504
|
+
const src = extractFunction('ClaimSection')
|
|
505
|
+
assert.ok(src.indexOf('restartGuide(') > 0, '要有统一的命令块渲染器 restartGuide(slot)')
|
|
506
|
+
assert.ok(src.includes('__restartDebug'), '带端口一份')
|
|
507
|
+
assert.match(src, /restartGuide\('__restartPlain'\)/, '普通重启一份')
|
|
508
|
+
assert.doesNotMatch(src, /state\.__restart(?![DP])/, '不应再读写单一的 __restart 槽(会互相顶替)')
|
|
509
|
+
})
|
|
510
|
+
|
|
511
|
+
/** 把渲染树摊成文本序列(按渲染顺序),用来断言「谁在谁旁边」。 */
|
|
512
|
+
function flattenText(node) {
|
|
513
|
+
if (node === null || node === undefined || typeof node === 'boolean') return []
|
|
514
|
+
if (typeof node === 'string' || typeof node === 'number') return [String(node)]
|
|
515
|
+
return (node.children ?? []).flatMap(flattenText)
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
test('ClaimSection:未点按钮时不展示任何启动命令(命令是点出来的,不是默认刷屏)', () => {
|
|
519
|
+
const panel = buildPanel()
|
|
520
|
+
const tree = panel.render(
|
|
521
|
+
panel.ClaimSection({
|
|
522
|
+
accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }],
|
|
523
|
+
busy: false,
|
|
524
|
+
captcha: { reachable: false, debugPort: 9229, reason: '连不上调试端口' },
|
|
525
|
+
}),
|
|
526
|
+
)
|
|
527
|
+
const sequence = flattenText(tree)
|
|
528
|
+
assert.ok(sequence.some((t) => t.includes('连不上宿主调试端口')), '载体不可用要有一句提示,写清端口')
|
|
529
|
+
assert.ok(!sequence.some((t) => t.includes('--remote-debugging-port')), '未点按钮不该出现命令块')
|
|
530
|
+
assert.ok(
|
|
531
|
+
sequence.some((t) => t.includes('127.0.0.1:9229')),
|
|
532
|
+
'提示句要写出探测的是哪个端口(用户据此判断改哪个环境变量)',
|
|
533
|
+
)
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
test('ClaimSection:载入带端口指引后,命令紧跟在提示句下方', () => {
|
|
537
|
+
const panel = buildPanel()
|
|
538
|
+
/** 用一个能把 __restartDebug 预置进去的渲染:直接渲染 restartGuide 的产物形状。 */
|
|
539
|
+
const src = extractFunction('ClaimSection')
|
|
540
|
+
/** 提示行同一容器内紧跟命令块(源码级断言,避免依赖状态注入的复杂度)。 */
|
|
541
|
+
const hintIdx = src.indexOf('连不上宿主调试端口')
|
|
542
|
+
const guideIdx = src.indexOf("restartGuide('__restartDebug')", hintIdx)
|
|
543
|
+
assert.ok(hintIdx > 0 && guideIdx > hintIdx, '带端口命令必须排在提示句之后')
|
|
544
|
+
/** 命令块与提示句之间不能隔出别的行组件(h('div', {className:'zsp-row'}…))。 */
|
|
545
|
+
const between = src.slice(hintIdx, guideIdx)
|
|
546
|
+
assert.doesNotMatch(between, /className: 'zsp-row'/, '中间不能插入新的行容器')
|
|
547
|
+
/** 拨盘必须在命令块之后(说明命令没被挪到组底部)。 */
|
|
548
|
+
const toggleIdx = src.indexOf('h(Toggle, {', guideIdx)
|
|
549
|
+
assert.ok(toggleIdx > guideIdx, '命令块整体在拨盘之前')
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* ⚠ 0.6.4:「重载插件」与「开调试端口」是两件事。
|
|
554
|
+
* 旧文案写成「以调试模式重启」,暗示普通模式不能重启 —— 这是错的:
|
|
555
|
+
* 不带端口重启同样让插件生效,只是 captcha 领不了。
|
|
556
|
+
*/
|
|
557
|
+
test('ClaimSection 源码:普通重启不带端口,调试端口只是可选姿势', () => {
|
|
558
|
+
const claimSectionSrc = extractFunction('ClaimSection')
|
|
559
|
+
/** ⚠ 只查字符串字面量:注释里会提到旧文案(讲清为什么改),不算违规。 */
|
|
560
|
+
assert.doesNotMatch(claimSectionSrc, /'以调试模式重启'|"以调试模式重启"/, '不该再暗示「普通模式不能重启」')
|
|
561
|
+
assert.match(claimSectionSrc, /restart\('plain'\)/, '提供普通重启(不带端口)')
|
|
562
|
+
assert.match(claimSectionSrc, /restart\('debug'\)/, '提供带端口重启')
|
|
563
|
+
assert.match(claimSectionSrc, /debugPort !== undefined/, '普通重启不得传 debugPort')
|
|
564
|
+
})
|
|
565
|
+
|
|
566
|
+
test('ClaimSection 源码:自动领取「说明在左、开关在右」', () => {
|
|
567
|
+
const claimSectionSrc = extractFunction('ClaimSection')
|
|
568
|
+
const textIdx = claimSectionSrc.indexOf('自动领取已')
|
|
569
|
+
const toggleIdx = claimSectionSrc.indexOf('h(Toggle, {', textIdx)
|
|
570
|
+
assert.ok(textIdx > 0, '要有自动领取说明文案')
|
|
571
|
+
assert.ok(toggleIdx > textIdx, '开关必须排在说明文本之后(否则与上方按钮列错位)')
|
|
572
|
+
})
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* ⚠ 2026-10-28 语义变更:面板不再「先查 preview 再显示领取按钮」。
|
|
576
|
+
*
|
|
577
|
+
* 实证:官方客户端全天 0 次调用 `/billing/preview`,点击时直接调 claim。
|
|
578
|
+
* 我们照抄了源码里存在但从未被使用的方法 → preview 恒空 → 按钮永不出现。
|
|
579
|
+
*
|
|
580
|
+
* 新形态:**探测按钮自动填 plan_id + 直接领取**,「有没有名额」由服务端裁决。
|
|
581
|
+
* 0.6.0 起探测走官方 marketing/touch 发现端点(与 /billing/preview 无关),
|
|
582
|
+
* 「不 preview」的约束仍成立:没有任何路径再引用 /billing/preview。
|
|
583
|
+
* 本用例锁住这个形态,防止有人把它改回「查 preview 列表」。
|
|
584
|
+
*/
|
|
585
|
+
test('ClaimSection: 展开后给输入框与领取按钮,不再依赖 preview 列表', () => {
|
|
586
|
+
const panel = buildPanel()
|
|
587
|
+
/** 先渲染一次(触发 open 的初始态)。 */
|
|
588
|
+
panel.render(panel.ClaimSection({ accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }], busy: false, captcha: { reachable: true } }))
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* 直接构造展开态:fake React 的 useState 是静态的(不支持 setState 触发重渲染),
|
|
592
|
+
* 所以这里验证**闭合态**没有把旧文案带回来,并单独验证组件源码不含 preview 依赖。
|
|
593
|
+
*/
|
|
594
|
+
const collapsed = panel.render(panel.ClaimSection({ accounts: [{ id: 'a1', index: 1, nickname: 'ACC', enabled: true }], busy: false, captcha: { reachable: true } }))
|
|
595
|
+
const text = textOf(collapsed)
|
|
596
|
+
/** 旧 UI 的「查看可领取」话术必须消失。 */
|
|
597
|
+
assert.doesNotMatch(text, /查看可领取/, '不该再有"查看可领取"(它依赖恒空的 preview)')
|
|
598
|
+
assert.doesNotMatch(text, /暂无可领取的活动/, '不该再显示"暂无可领取"')
|
|
599
|
+
/** 新话术要在。 */
|
|
600
|
+
assert.match(text, /自动探测/, '探测是自动的(展开即探)')
|
|
601
|
+
})
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* ⚠ 契约:**不保留手工输入 plan_id 的组件**。
|
|
605
|
+
*
|
|
606
|
+
* 有过三个版本的反复:先「查 preview 列表 → 有才领」,再「填 plan_id 直接领」,
|
|
607
|
+
* 现在「自动探测 → 直接领」。手输路径只会让人填错或填过期 id,白烧一次 captcha;
|
|
608
|
+
* 探测零成本且权威,所以输入框与「探测」按钮一起移除,plan_id 只读展示。
|
|
609
|
+
*/
|
|
610
|
+
test('ClaimSection: 没有手工输入 plan_id 的输入框,也没有探测按钮', () => {
|
|
611
|
+
const claimSectionSrc = extractFunction('ClaimSection')
|
|
612
|
+
assert.doesNotMatch(claimSectionSrc, /placeholder/, '不该有占位提示的输入框')
|
|
613
|
+
assert.doesNotMatch(claimSectionSrc, /onChange:\s*\(event\)\s*=>\s*patch\([^)]*planId/, '不该有改 planId 的输入')
|
|
614
|
+
assert.doesNotMatch(claimSectionSrc, /'探测'/, '探测按钮已移除:展开即自动探测')
|
|
615
|
+
/** plan_id 改由探测结果驱动,只读展示。 */
|
|
616
|
+
assert.match(claimSectionSrc, /zsp-plan-id/, 'plan_id 只读展示')
|
|
617
|
+
})
|
|
618
|
+
|
|
619
|
+
test('ClaimSection 源码:探测走 /claimable(marketing/touch),领取直接 /claim,绝无 preview', () => {
|
|
620
|
+
const claimSectionSrc = extractFunction('ClaimSection')
|
|
621
|
+
assert.match(claimSectionSrc, /\/claimable/, '探测调 /claimable(官方 marketing/touch,零 captcha)')
|
|
622
|
+
assert.match(claimSectionSrc, /\/claim/, '必须直接走 /claim')
|
|
623
|
+
assert.match(claimSectionSrc, /planId/, '必须按 planId 领取')
|
|
624
|
+
/** /billing/preview 是死端点:客户端源码任何地方都不许再引用。 */
|
|
625
|
+
assert.doesNotMatch(claimSectionSrc, /billing\/preview/, 'preview 恒空,官方客户端也不调它')
|
|
626
|
+
})
|
|
627
|
+
|
|
628
|
+
test('AccountCard: 多个额度桶逐桶画条(每桶带自己的到期时间)', () => {
|
|
629
|
+
const panel = buildPanel()
|
|
630
|
+
const now = Date.now()
|
|
631
|
+
const tree = panel.render(panel.AccountCard({
|
|
632
|
+
account: baseAccount,
|
|
633
|
+
balance: {
|
|
634
|
+
ok: true,
|
|
635
|
+
remaining: 1300, total: 3000,
|
|
636
|
+
buckets: [
|
|
637
|
+
{ planId: 'soon', showName: '早到期包', remaining: 800, total: 2000, expiresAt: now + 3 * 86_400_000 },
|
|
638
|
+
{ planId: 'late', showName: '晚到期包', remaining: 500, total: 1000, expiresAt: now + 7 * 86_400_000 },
|
|
639
|
+
],
|
|
640
|
+
},
|
|
641
|
+
modelListVisible: true,
|
|
642
|
+
}))
|
|
643
|
+
const bars = findAll(tree, (node) => node.props?.className === 'zsp-bar')
|
|
644
|
+
assert.equal(bars.length, 2, '两个桶 = 两条进度条')
|
|
645
|
+
const text = textOf(tree)
|
|
646
|
+
assert.match(text, /早到期包/, '桶名')
|
|
647
|
+
assert.match(text, /晚到期包/, '桶名')
|
|
648
|
+
assert.match(text, /剩余 800\/2000/, '第一桶自己的数值')
|
|
649
|
+
assert.match(text, /剩余 500\/1000/, '第二桶自己的数值')
|
|
650
|
+
})
|
|
651
|
+
|
|
652
|
+
test('AccountCard: 单桶时走汇总条(不重复画)', () => {
|
|
653
|
+
const panel = buildPanel()
|
|
654
|
+
const tree = panel.render(panel.AccountCard({
|
|
655
|
+
account: baseAccount,
|
|
656
|
+
balance: { ok: true, remaining: 100, total: 200, buckets: [{ planId: 'p', showName: 'GLM', remaining: 100, total: 200, expiresAt: Date.now() + 3600_000 }] },
|
|
657
|
+
modelListVisible: true,
|
|
658
|
+
}))
|
|
659
|
+
const bars = findAll(tree, (node) => node.props?.className === 'zsp-bar')
|
|
660
|
+
assert.equal(bars.length, 1, '单桶 = 一条')
|
|
661
|
+
})
|
|
662
|
+
|
|
663
|
+
test('AccountCard: 0.7.0 移除「使用」按钮(统一选号按到期时间取号,置顶已无意义)', () => {
|
|
664
|
+
const src = extractFunction('AccountCard')
|
|
665
|
+
assert.doesNotMatch(src, /'使用'/, '「使用」按钮已移除:expiry 模式下置顶不决定取号顺序')
|
|
666
|
+
assert.doesNotMatch(src, /使用中/, '不再有「使用中」态')
|
|
667
|
+
/** 快过期的先用 —— 面板不再提供手动置顶入口。 */
|
|
668
|
+
assert.doesNotMatch(src, /onUse/, 'onUse 链路应一并移除')
|
|
669
|
+
})
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* 1.0.1:限流 chip 自带清除按钮(×)。此前「清除限流」只存在于 CLI
|
|
673
|
+
* (`node bin/cli.mjs reset`)——用户报「哪里有按钮」,因为它根本不在 UI 里。
|
|
674
|
+
*/
|
|
675
|
+
test('AccountCard: 限流 chip 带 × 清除按钮,点击调 /reset-limits(带 id+model)', () => {
|
|
676
|
+
const panel = buildPanel()
|
|
677
|
+
const calls = []
|
|
678
|
+
const tree = panel.render(panel.AccountCard({
|
|
679
|
+
account: { ...baseAccount, modelRateLimits: { 'GLM-5.3-Flash': Date.now() + 3600_000 } },
|
|
680
|
+
modelListVisible: true,
|
|
681
|
+
onResetLimit: (model) => calls.push(model),
|
|
682
|
+
}))
|
|
683
|
+
const chips = findAll(tree, (node) => node.props?.className === 'zsp-chip' && node.props['data-limited'] === 'true')
|
|
684
|
+
assert.equal(chips.length, 1, '一条限流 chip')
|
|
685
|
+
const buttons = findAll(chips[0], (node) => node.props?.className === 'zsp-chip-x')
|
|
686
|
+
assert.equal(buttons.length, 1, 'chip 内有一个 × 按钮')
|
|
687
|
+
assert.equal(buttons[0].props['aria-label'], '清除 GLM-5.3-Flash 的限流标记')
|
|
688
|
+
buttons[0].props.onClick()
|
|
689
|
+
assert.deepEqual(calls, ['GLM-5.3-Flash'], '点击把该模型 id 报给 onResetLimit')
|
|
690
|
+
})
|
|
691
|
+
|
|
692
|
+
test('AccountCard: 无限流时不渲染 × 按钮', () => {
|
|
693
|
+
const panel = buildPanel()
|
|
694
|
+
const tree = panel.render(panel.AccountCard({
|
|
695
|
+
account: baseAccount,
|
|
696
|
+
modelListVisible: true,
|
|
697
|
+
onResetLimit: () => {},
|
|
698
|
+
}))
|
|
699
|
+
assert.equal(findAll(tree, (node) => node.props?.className === 'zsp-chip-x').length, 0)
|
|
700
|
+
})
|
|
701
|
+
|
|
702
|
+
test('TotalQuota: 汇总各账号余额桶、按到期日分层、跳过未加载账号', () => {
|
|
703
|
+
const panel = buildPanel()
|
|
704
|
+
const now = Date.now()
|
|
705
|
+
const tree = panel.render(panel.TotalQuota({
|
|
706
|
+
accounts: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
707
|
+
balances: {
|
|
708
|
+
a: { ok: true, buckets: [
|
|
709
|
+
{ remaining: 300_000_000, total: 500_000_000, expiresAt: now + 86_400_000 },
|
|
710
|
+
{ remaining: 3_000_000, total: 3_000_000, expiresAt: now + 3_600_000 },
|
|
711
|
+
] },
|
|
712
|
+
b: { ok: true, buckets: [{ remaining: 297_000_000, total: 300_000_000, expiresAt: now + 86_400_000 }] },
|
|
713
|
+
/** c 未加载(懒加载中)→ 不计入。 */
|
|
714
|
+
c: { ok: false, error: '余额加载中…' },
|
|
715
|
+
},
|
|
716
|
+
}))
|
|
717
|
+
const text = textOf(tree)
|
|
718
|
+
assert.match(text, /总额度/, '标题行')
|
|
719
|
+
assert.match(text, /2\/3 个账号已加载/, 'c 未加载要注明(6 亿 / 8.03 亿 只算 a+b)')
|
|
720
|
+
assert.match(text, /300万 \/ 300万/, '分层小计出现(b 的桶,formatTokens 万级)')
|
|
721
|
+
/** 两条分层(到期日不同:明天 与 1 小时后)。 */
|
|
722
|
+
const bars = findAll(tree, (n) => n.props?.className === 'zsp-bar')
|
|
723
|
+
assert.equal(bars.length, 2, '两个到期日 = 两条分层进度条')
|
|
724
|
+
})
|
|
725
|
+
|
|
726
|
+
test('TotalQuota: 未生效(pending)额度也计入总额度,但单独分层、不并进「现在可用」', () => {
|
|
727
|
+
const panel = buildPanel()
|
|
728
|
+
const now = Date.now()
|
|
729
|
+
const tree = panel.render(panel.TotalQuota({
|
|
730
|
+
accounts: [{ id: 'a' }, { id: 'b' }],
|
|
731
|
+
balances: {
|
|
732
|
+
a: { ok: true, remaining: 300_000_000, total: 300_000_000, buckets: [
|
|
733
|
+
{ remaining: 300_000_000, total: 300_000_000, expiresAt: now + 3_600_000 },
|
|
734
|
+
] },
|
|
735
|
+
b: {
|
|
736
|
+
ok: true,
|
|
737
|
+
buckets: [{ remaining: 0, total: 0, expiresAt: now + 3_600_000 }],
|
|
738
|
+
pending: [
|
|
739
|
+
{ planId: 'p1', showName: 'GLM-5.3-Flash', grantUnits: 300_000_000, effectiveAt: now + 86_400_000, expiresAt: now + 7 * 86_400_000 },
|
|
740
|
+
],
|
|
741
|
+
},
|
|
742
|
+
},
|
|
743
|
+
}))
|
|
744
|
+
const text = textOf(tree)
|
|
745
|
+
/** 分母 = 3 亿(已生效)+ 3 亿(待生效);分子仍是此刻能花的 3 亿。 */
|
|
746
|
+
assert.match(text, /3亿 \/ 6亿 tokens/, '待生效额度进分母')
|
|
747
|
+
assert.match(text, /其中待生效 3亿/, '标题行标出待生效份额')
|
|
748
|
+
assert.match(text, /3亿 tokens 待生效/, '待生效单独一行')
|
|
749
|
+
assert.match(text, /起可用/, '待生效行要写生效时间')
|
|
750
|
+
/** 分条:1 条实色(已生效分层)+ 1 条条纹(待生效分层),不能合并成一条。 */
|
|
751
|
+
const bars = findAll(tree, (n) => n.props?.className === 'zsp-bar')
|
|
752
|
+
assert.equal(bars.length, 2, '已生效与待生效各一条,不合并')
|
|
753
|
+
assert.equal(bars.filter((n) => n.props?.['data-pending'] === true).length, 1, '待生效那条带条纹标记')
|
|
754
|
+
})
|
|
755
|
+
|
|
756
|
+
test('TotalQuota: 只有未生效额度(余额桶为空)也要显示总额度', () => {
|
|
757
|
+
const panel = buildPanel()
|
|
758
|
+
const now = Date.now()
|
|
759
|
+
const tree = panel.render(panel.TotalQuota({
|
|
760
|
+
accounts: [{ id: 'a' }],
|
|
761
|
+
balances: { a: { ok: true, buckets: [], pending: [{ grantUnits: 500_000_000, effectiveAt: now + 3_600_000 }] } },
|
|
762
|
+
}))
|
|
763
|
+
assert.notEqual(tree, null, '余额为 0 但有待生效额度时要渲染')
|
|
764
|
+
assert.match(textOf(tree), /0 \/ 5亿 tokens/, '分子 0、分母含待生效')
|
|
765
|
+
})
|
|
766
|
+
|
|
767
|
+
test('TotalQuota: 全部未加载 / 无账号时不渲染', () => {
|
|
768
|
+
const panel = buildPanel()
|
|
769
|
+
assert.equal(panel.TotalQuota({ accounts: [], balances: {} }), null)
|
|
770
|
+
assert.equal(panel.render(panel.TotalQuota({ accounts: [{ id: 'a' }], balances: { a: { ok: false, error: 'x' } } })), null)
|
|
771
|
+
})
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* ⚠ 0.9.6 的真实 bug(面板「删除没用」):
|
|
775
|
+
*
|
|
776
|
+
* 0.9.6 把 CRUD 定位改成稳定 `id`(服务端按 `body.id` 找,token 不再下发),
|
|
777
|
+
* 调用点都改成了 `op(key.id, …)`,但 `op()` 的序列化键名**还是 `token`** ——
|
|
778
|
+
* 发出的 body 里没有 `id` 键 ⇒ 服务端永远 400「需要 id」⇒ 删除/禁用/限额调节
|
|
779
|
+
* 全部失效,且 `op()` catch 后静默,按钮看起来就是「没反应」。
|
|
780
|
+
*
|
|
781
|
+
* 本用例就是当时的缺口:面板测试从没断言过「组件发出的请求体」与服务端的
|
|
782
|
+
* 契约。这里用真 fetch 桩捕获请求体,并**用服务端同款判据**(按 `body.id`
|
|
783
|
+
* 定位)验证删除真正生效;失败必须出现错误行而不是静默。
|
|
784
|
+
*/
|
|
785
|
+
test('DistributionKeysSection: op() 按 body.id 定位(服务端契约),两步删除生效;失败显示错误行', async () => {
|
|
786
|
+
const calls = []
|
|
787
|
+
const keyRow = {
|
|
788
|
+
id: 'dk_test01',
|
|
789
|
+
name: '给台式机',
|
|
790
|
+
quotaTotal: 5_000_000,
|
|
791
|
+
accountId: null,
|
|
792
|
+
enabled: true,
|
|
793
|
+
createdAt: 0,
|
|
794
|
+
usage: { inputTokens: 0, outputTokens: 0, requests: 0 },
|
|
795
|
+
remaining: 5_000_000,
|
|
796
|
+
}
|
|
797
|
+
let deleted = false
|
|
798
|
+
/** ⚠ 保存/还原原值:本仓库 runner 单进程跑全部测试,直接置 undefined 会毒化后续文件。 */
|
|
799
|
+
const originalFetch = globalThis.fetch
|
|
800
|
+
globalThis.fetch = async (_url, init) => {
|
|
801
|
+
const body = JSON.parse(init.body)
|
|
802
|
+
calls.push({ url: String(_url), body })
|
|
803
|
+
/** 服务端同款判据:按 body.id 定位(manage-routes distKeysHandler)。 */
|
|
804
|
+
const found = body.id === keyRow.id
|
|
805
|
+
if (new URLSearchParams(String(_url).split('?')[1]).get('op') === 'delete') {
|
|
806
|
+
if (found) deleted = true
|
|
807
|
+
return new Response(JSON.stringify(found ? { ok: true } : { ok: false, error: '分发 key 不存在' }), { status: found ? 200 : 404 })
|
|
808
|
+
}
|
|
809
|
+
return new Response(JSON.stringify({ ok: true }), { status: 200 })
|
|
810
|
+
}
|
|
811
|
+
try {
|
|
812
|
+
const panel = buildPanel()
|
|
813
|
+
let changed = 0
|
|
814
|
+
/**
|
|
815
|
+
* 带状态的重渲染:两步删除需要「武装后确认按钮真的出现」。
|
|
816
|
+
* ⚠ 必须包装成**元素节点**(type=函数)再交给 resolve —— 直接调用组件的话
|
|
817
|
+
* resolve 走不到「函数组件」分支,hook 计数不会归零,第二次渲染会读到
|
|
818
|
+
* 全新初始化的 state 槽(武装值凭空丢失,假阴性)。
|
|
819
|
+
*/
|
|
820
|
+
const makeNode = (statefulMod) => ({
|
|
821
|
+
type: statefulMod.DistributionKeysSection,
|
|
822
|
+
props: { keys: [keyRow], accounts: [], busy: false, onChanged: async () => { changed += 1 } },
|
|
823
|
+
children: [],
|
|
824
|
+
})
|
|
825
|
+
const { renderOnce } = panel.renderStateful(makeNode)
|
|
826
|
+
const findDelete = (tree) => findAll(tree, (n) => n.props?.['data-danger'] === true)[0]
|
|
827
|
+
const findConfirm = (tree) => findAll(tree, (n) => textOf(n).includes('确认删除?')).at(-1)
|
|
828
|
+
let tree = renderOnce()
|
|
829
|
+
const firstDelete = findDelete(tree)
|
|
830
|
+
assert.notEqual(firstDelete, undefined, '每张卡有删除按钮(data-danger)')
|
|
831
|
+
await firstDelete.props.onClick()
|
|
832
|
+
await new Promise((resolve) => setImmediate(resolve))
|
|
833
|
+
assert.equal(deleted, false, '第一步只武装,绝不发删除请求(防误触)')
|
|
834
|
+
assert.equal(changed, 0, '武装不刷新列表')
|
|
835
|
+
/** 武装后重渲染:确认按钮出现,点它才真删。 */
|
|
836
|
+
tree = renderOnce()
|
|
837
|
+
const confirmButton = findConfirm(tree)
|
|
838
|
+
assert.notEqual(confirmButton, undefined, '武装后出现「确认删除?」按钮')
|
|
839
|
+
await confirmButton.props.onClick()
|
|
840
|
+
await new Promise((resolve) => setImmediate(resolve))
|
|
841
|
+
assert.equal(deleted, true, '服务端按 body.id 找到了该 key —— 删除真正生效')
|
|
842
|
+
assert.equal(changed, 1, '成功后刷新列表')
|
|
843
|
+
assert.deepEqual(
|
|
844
|
+
calls.map((c) => Object.keys(c.body).sort()),
|
|
845
|
+
[['id']],
|
|
846
|
+
'请求体必须带 id 键(0.9.6 前发的键名是 token,服务端读不到)',
|
|
847
|
+
)
|
|
848
|
+
} finally {
|
|
849
|
+
globalThis.fetch = originalFetch
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** 失败路径:服务端 404 ⇒ op() 必须把错误交给 setOpError(渲染错误行),不许静默。 */
|
|
853
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ ok: false, error: '分发 key 不存在' }), { status: 404 })
|
|
854
|
+
try {
|
|
855
|
+
const panel = buildPanel()
|
|
856
|
+
const tree = panel.render(panel.DistributionKeysSection({ keys: [keyRow], accounts: [], busy: false, onChanged: async () => {} }))
|
|
857
|
+
const deleteButton = findAll(tree, (n) => n.props?.['data-danger'] === true)[0]
|
|
858
|
+
/** 桩 React 的 setState 是 no-op(不重渲染),这里只验证调用不抛。 */
|
|
859
|
+
await deleteButton.props.onClick()
|
|
860
|
+
} finally {
|
|
861
|
+
globalThis.fetch = originalFetch
|
|
862
|
+
}
|
|
863
|
+
const src = extractFunction('DistributionKeysSection')
|
|
864
|
+
assert.match(src, /分发 key 操作失败/, 'op 失败必须渲染错误行(不许 catch 后静默)')
|
|
865
|
+
assert.match(src, /body: JSON\.stringify\(\{ id, \.\.\.body \}\)/, '序列化键名必须是 id(服务端按 body.id 定位)')
|
|
866
|
+
assert.match(src, /setOpError\(/, '失败必须进 state(0.9.6 的事故形态:catch 后什么都不做)')
|
|
867
|
+
})
|
|
868
|
+
|
|
869
|
+
/** 0.9.8 重构的形态闸:卡片化 / 用量条 / 一次性 token 卡 / 折叠表单 / 限额预设。 */
|
|
870
|
+
test('DistributionKeysSection: 现代 API key 管理形态(卡片 + 用量条 + token 强调卡 + 两步删除)', () => {
|
|
871
|
+
const panel = buildPanel()
|
|
872
|
+
const keyRow = {
|
|
873
|
+
id: 'dk_test02',
|
|
874
|
+
name: '给台式机',
|
|
875
|
+
quotaTotal: 5_000_000,
|
|
876
|
+
accountId: null,
|
|
877
|
+
enabled: true,
|
|
878
|
+
createdAt: new Date('2026-10-31T00:00:00Z').getTime(),
|
|
879
|
+
usage: { inputTokens: 4_800_000, outputTokens: 100_000, requests: 12 },
|
|
880
|
+
remaining: 100_000,
|
|
881
|
+
}
|
|
882
|
+
const originalFetch = globalThis.fetch
|
|
883
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ ok: true }), { status: 200 })
|
|
884
|
+
try {
|
|
885
|
+
const tree = panel.render(panel.DistributionKeysSection({ keys: [keyRow], accounts: [], busy: false, onChanged: async () => {} }))
|
|
886
|
+
/** 卡片化:key 行是卡片不是扁平行。 */
|
|
887
|
+
const card = findAll(tree, (n) => n.props?.className === 'zsp-keycard')[0]
|
|
888
|
+
assert.notEqual(card, undefined, '每把 key 一张卡片(zsp-keycard)')
|
|
889
|
+
assert.equal(card.props['data-disabled'], false, '启用中的卡不带降透明度标记')
|
|
890
|
+
/** 用量进度条:4.9M/5M = 98% ≥ 90% ⇒ 热态危险色。 */
|
|
891
|
+
const bar = findAll(card, (n) => n.props?.className === 'zsp-bar')[0]
|
|
892
|
+
assert.equal(bar.props['data-hot'], true, '用量 ≥90% 进度条转危险色')
|
|
893
|
+
const text = textOf(tree)
|
|
894
|
+
assert.match(text, /490万 \/ 500万 tokens · 12 次请求/, '已用/限额/请求数一行读全')
|
|
895
|
+
/** 两步删除在渲染层成立:data-danger 的删除按钮存在,确认按钮只在武装后出现。 */
|
|
896
|
+
assert.equal(findAll(tree, (n) => n.props?.['data-danger'] === true).length, 1, '初始只有普通删除按钮')
|
|
897
|
+
assert.doesNotMatch(text, /确认删除?/, '未武装时不显示确认按钮')
|
|
898
|
+
/** 空状态引导。 */
|
|
899
|
+
const empty = panel.render(panel.DistributionKeysSection({ keys: [], accounts: [], busy: false, onChanged: async () => {} }))
|
|
900
|
+
assert.match(textOf(empty), /还没有 API Key/, '空列表给引导文案')
|
|
901
|
+
} finally {
|
|
902
|
+
globalThis.fetch = originalFetch
|
|
903
|
+
}
|
|
904
|
+
const src = extractFunction('DistributionKeysSection')
|
|
905
|
+
assert.match(src, /zsp-token-card/, '新 key 走强调卡(成功色描边 + 复制按钮)')
|
|
906
|
+
assert.match(src, /navigator\.clipboard\.writeText/, 'token 有复制按钮')
|
|
907
|
+
assert.match(src, /我已保存/, '强调卡有关闭动作(明文不常驻)')
|
|
908
|
+
assert.match(src, /\+ 新建 API Key/, '创建表单折叠进按钮(平时不占面板)')
|
|
909
|
+
assert.match(src, /confirmDeleteId/, '删除两步确认(武装 id state)')
|
|
910
|
+
assert.match(src, /formatTokens\(preset\)/, '限额用预设分段选择')
|
|
911
|
+
})
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* 1.0.0:模型栏旁「当前使用账号」——**只在选中本插件模型时显示**。
|
|
915
|
+
*
|
|
916
|
+
* 本组测试的第一条是 0.9.9 那次事故的回归闸:
|
|
917
|
+
* 当时注册到 `conversation.input.model.label`(只被 renderSlot 调用、
|
|
918
|
+
* **从未被任何包声明**),向未声明的槽注册会让整个 client bundle 的 apply 抛错,
|
|
919
|
+
* 面板一起白掉。
|
|
920
|
+
*/
|
|
921
|
+
const DECLARED_COMPOSER_SLOTS = new Set([
|
|
922
|
+
'conversation.input.attachments',
|
|
923
|
+
'conversation.input.overlay',
|
|
924
|
+
'conversation.input.permission',
|
|
925
|
+
'conversation.input.left',
|
|
926
|
+
'conversation.input.plan',
|
|
927
|
+
'conversation.input.right', // ← workbuddy 积分徽标用的就是它
|
|
928
|
+
'conversation.input.model',
|
|
929
|
+
'conversation.input.activity',
|
|
930
|
+
'conversation.composer.dock',
|
|
931
|
+
])
|
|
932
|
+
|
|
933
|
+
test('RouteBadge 插槽:只能注册到 composer 里**已声明**的槽', () => {
|
|
934
|
+
/** 从源码抓出所有 slots.inject('…') 的座位名。 */
|
|
935
|
+
const injected = [...SOURCE.matchAll(/slots\.inject\(\s*([A-Z_]+|'[^']+')/g)]
|
|
936
|
+
.map((m) => m[1])
|
|
937
|
+
assert.ok(injected.length >= 2, '应至少注册两个插槽(配置页 + 模型栏徽标)')
|
|
938
|
+
|
|
939
|
+
/** 常量必须解析到实际字符串。 */
|
|
940
|
+
const literal = (token) => {
|
|
941
|
+
if (token.startsWith("'")) return token.slice(1, -1)
|
|
942
|
+
const decl = new RegExp(`const ${token}\\s*=\\s*'([^']+)'`).exec(SOURCE)
|
|
943
|
+
assert.notEqual(decl, null, `找不到常量 ${token} 的字面量`)
|
|
944
|
+
return decl[1]
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
for (const token of injected) {
|
|
948
|
+
const name = literal(token)
|
|
949
|
+
assert.ok(
|
|
950
|
+
DECLARED_COMPOSER_SLOTS.has(name) || name === 'plugins.bundle.config',
|
|
951
|
+
`插槽 ${name} 未在宿主 composer 注册表里声明 —— 向它注册会让整个 bundle 的 apply 抛错`,
|
|
952
|
+
)
|
|
953
|
+
assert.notEqual(name, 'conversation.input.model.label', '0.9.9 事故座位:只被 renderSlot 调用,从未声明')
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
/** 徽标必须落在模型座旁边的那一格。 */
|
|
957
|
+
assert.match(SOURCE, /conversation\.input\.right/, '徽标座位必须是 conversation.input.right')
|
|
958
|
+
})
|
|
959
|
+
|
|
960
|
+
function buildRouteBadge() {
|
|
961
|
+
/** uSES 桩:直接读快照(本组测的是「渲不渲染」,不是订阅时序)。 */
|
|
962
|
+
const fakeReact = {
|
|
963
|
+
...FakeReact,
|
|
964
|
+
useSyncExternalStore: (subscribe, getSnapshot) => {
|
|
965
|
+
FakeReact.useState(0)
|
|
966
|
+
return getSnapshot()
|
|
967
|
+
},
|
|
968
|
+
}
|
|
969
|
+
const code = `
|
|
970
|
+
const h = React.createElement
|
|
971
|
+
const { useState, useRef, useCallback, useEffect } = React
|
|
972
|
+
const API = '/api/zcode-start-plan'
|
|
973
|
+
const ROUTING_POLL_MS = 2000
|
|
974
|
+
const PROVIDER_PREFIX = 'zcode-start-plan'
|
|
975
|
+
const useSyncExternalStore = React.useSyncExternalStore
|
|
976
|
+
${extractFunction('ModelRouteBadge')}
|
|
977
|
+
return { ModelRouteBadge }
|
|
978
|
+
`
|
|
979
|
+
const mod = new Function('React', code)(fakeReact)
|
|
980
|
+
return {
|
|
981
|
+
render: (props) => resolve(mod.ModelRouteBadge(props), fakeReact),
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/** 造一个最简 model directory:`store` 是 uSES 可用的外部 store。 */
|
|
986
|
+
function fakeDirectory(current) {
|
|
987
|
+
return {
|
|
988
|
+
store: {
|
|
989
|
+
getSnapshot: () => ({ current }),
|
|
990
|
+
subscribe: () => () => {},
|
|
991
|
+
},
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
test('ModelRouteBadge: 未选中本插件模型时**不渲染**(选中它才显示)', () => {
|
|
996
|
+
const badge = buildRouteBadge()
|
|
997
|
+
const cases = [
|
|
998
|
+
[null, '空会话(没有选中模型)'],
|
|
999
|
+
[{ provider: 'deepseek', model: 'deepseek-v4.1-flash' }, '别的 provider'],
|
|
1000
|
+
[{ provider: 'workbuddy', model: 'WB.1' }, 'workbuddy 的模型'],
|
|
1001
|
+
]
|
|
1002
|
+
for (const [current, label] of cases) {
|
|
1003
|
+
assert.equal(
|
|
1004
|
+
badge.render({ directory: fakeDirectory(current) }),
|
|
1005
|
+
null,
|
|
1006
|
+
`${label} 下不应出现徽标`,
|
|
1007
|
+
)
|
|
1008
|
+
}
|
|
1009
|
+
/** 目录缺席(旧宿主 / 无会话)也不能炸。 */
|
|
1010
|
+
assert.equal(badge.render({ directory: undefined }), null, '没有 directory 时静默不渲染')
|
|
1011
|
+
})
|
|
1012
|
+
|
|
1013
|
+
test('ModelRouteBadge: 样式照抄 workbuddy 积分徽标(内联、无描边、11px 注释级)', () => {
|
|
1014
|
+
const src = extractFunction('ModelRouteBadge')
|
|
1015
|
+
|
|
1016
|
+
/**
|
|
1017
|
+
* 与 `@venshell/dsh-workbuddy-connect` 的 wrapperStyle / buttonStyle$1 /
|
|
1018
|
+
* creditStyle 逐条对齐:用户的诉求是「字体和外框和 workbuddy 一样」。
|
|
1019
|
+
*/
|
|
1020
|
+
assert.match(src, /transform: 'translateY\(2px\)'/, '外层容器要下移 2px 与相邻控件对齐')
|
|
1021
|
+
assert.match(src, /height: 30/, '按钮高 30(与 composer 控件一致)')
|
|
1022
|
+
assert.match(src, /border: 0/, '**不能有外框**')
|
|
1023
|
+
assert.match(src, /background: 'transparent'/, '透明背景')
|
|
1024
|
+
assert.match(src, /borderRadius: 8/, '圆角 8(虽然无边框,hover 区仍是圆角)')
|
|
1025
|
+
assert.match(src, /fontSize: 11/, '字号 11')
|
|
1026
|
+
assert.match(src, /lineHeight: '16px'/, '行高 16px')
|
|
1027
|
+
assert.match(src, /--dsw-alias-label-tertiary/, '文字用 tertiary 色(注释级)')
|
|
1028
|
+
assert.match(src, /wrapperStyle/, '有外层 wrapper(workbuddy 同结构)')
|
|
1029
|
+
assert.match(src, /creditStyle/, '标签用 creditStyle 同款排版')
|
|
1030
|
+
|
|
1031
|
+
/** 不允许再退回「药丸徽标」:描边 / 绿点 / 大圆角都是被否掉的样子。 */
|
|
1032
|
+
assert.doesNotMatch(src, /border: '1px/, '不应有描边')
|
|
1033
|
+
assert.doesNotMatch(src, /zsp-route-dot/, '不再渲染状态圆点')
|
|
1034
|
+
assert.doesNotMatch(src, /999px/, '不做药丸形')
|
|
1035
|
+
})
|
|
1036
|
+
|
|
1037
|
+
test('ModelRouteBadge: 选中本插件模型才算 mine,且只显示确定账号', () => {
|
|
1038
|
+
const src = extractFunction('ModelRouteBadge')
|
|
1039
|
+
assert.match(src, /startsWith\(PROVIDER_PREFIX\)/, '按前缀判归属')
|
|
1040
|
+
assert.match(src, /directory\?\.store/, '读宿主的 model directory')
|
|
1041
|
+
assert.match(src, /useSyncExternalStore/, '订阅而不是轮询目录')
|
|
1042
|
+
assert.match(src, /lastRouted/, '数值来自 /routing 的 lastRouted(实时)')
|
|
1043
|
+
/**
|
|
1044
|
+
* 显示规则:**不出现「待路由」这种不确定文案**,一律给出确定账号 ——
|
|
1045
|
+
* · 钉定栏位 → 用所选账号 id 在池子里查昵称(选哪个显示哪个)
|
|
1046
|
+
* · 统一栏位有派发记录 → 真实路由到的账号
|
|
1047
|
+
* · 统一栏位还没派发 → `/routing` 的 preview(快过期先用排序的第一个)
|
|
1048
|
+
*/
|
|
1049
|
+
assert.match(src, /preview/, '统一栏位用 preview 兜底(而不是「待路由」)')
|
|
1050
|
+
assert.match(src, /pinnedAccountId/, '钉定栏位按所选账号 id 查昵称')
|
|
1051
|
+
assert.match(src, /routing\?\.accounts/, '昵称来自 /routing 的 accounts 表')
|
|
1052
|
+
assert.doesNotMatch(src, /'待路由'/, '不应再出现「待路由」文案')
|
|
1053
|
+
assert.match(src, /'-acct-'/, '钉定栏位要能从 provider id 切出账号 id')
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* 只显示账号名:不加「钉定」「当前账号」这类前缀 ——
|
|
1057
|
+
* 徽标本身就是「当前使用账号」,再写一遍属于冗余(用户明确要求去掉)。
|
|
1058
|
+
*/
|
|
1059
|
+
assert.doesNotMatch(src, /'钉定'/, '徽标文案里不能出现「钉定」二字')
|
|
1060
|
+
assert.doesNotMatch(src, /'当前账号'/, '徽标文案里不能出现「当前账号」前缀')
|
|
1061
|
+
})
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* 额度占比圆环:几何参数必须与宿主「上下文使用量」圆环一致,
|
|
1065
|
+
* 否则并排时会看出两套控件。实测宿主那个圆环是:
|
|
1066
|
+
* viewBox="0 0 14 14" r=5.5 stroke-width 2 rotate(-90 7 7) + stroke-dasharray
|
|
1067
|
+
*/
|
|
1068
|
+
test('ModelRouteBadge: 额度圆环与上下文使用量圆环同几何(14×14 / r5.5 / rotate -90)', () => {
|
|
1069
|
+
const src = extractFunction('ModelRouteBadge')
|
|
1070
|
+
assert.match(src, /RING_R = 5\.5/, '半径必须是 5.5(与上下文圆环一致)')
|
|
1071
|
+
assert.match(src, /2 \* Math\.PI \* RING_R/, '周长由半径算,不写死')
|
|
1072
|
+
assert.match(src, /viewBox: '0 0 14 14'/, 'viewBox 14×14')
|
|
1073
|
+
assert.match(src, /width: 14,[\s\S]{0,40}height: 14/, '渲染尺寸 14×14')
|
|
1074
|
+
assert.match(src, /rotate\(-90 7 7\)/, '起点转到 12 点方向 ⇒ 顺时针占据')
|
|
1075
|
+
assert.match(src, /strokeDasharray/, '用 dasharray 表达占比')
|
|
1076
|
+
assert.match(src, /strokeWidth: 2/, '线宽 2')
|
|
1077
|
+
|
|
1078
|
+
/** 未知占比只画灰底,不编造进度。 */
|
|
1079
|
+
assert.match(src, /ratio === undefined \|\| ratio === null/, '占比未知时只画底圈')
|
|
1080
|
+
/** 快用完转危险色(与面板进度条同一套语义)。 */
|
|
1081
|
+
assert.match(src, /ratio >= 0\.9[\s\S]{0,60}label-danger/, '≥90% 转危险色')
|
|
1082
|
+
/** 圆环必须是装饰,不能抢走按钮的可访问名。 */
|
|
1083
|
+
assert.match(src, /'aria-hidden': 'true'/, '圆环 aria-hidden')
|
|
1084
|
+
})
|
|
1085
|
+
|
|
1086
|
+
test('ModelRouteBadge: 源码里没有任何「未声明插槽」的残留引用', () => {
|
|
1087
|
+
/**
|
|
1088
|
+
* 0.9.9 事故座位 = `conversation.input.model` + `.label`。
|
|
1089
|
+
* 拼出来再查,避免本文件自己成为匹配对象。
|
|
1090
|
+
*/
|
|
1091
|
+
const BANNED = `conversation.input.model${'.'}label`
|
|
1092
|
+
assert.equal(SOURCE.includes(BANNED), false, `这个座位在宿主里根本不存在:${BANNED}`)
|
|
1093
|
+
})
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* 0.9.9 事故的两层正防御(workbuddy 同款),必须在源码里成立:
|
|
1097
|
+
* 少任何一层,一次注册失败就能把整个配置页带走。
|
|
1098
|
+
*/
|
|
1099
|
+
test('RouteBadge 注册:modelDirectories 走延迟注入 + 每次贡献都有 guard', () => {
|
|
1100
|
+
/** ① 硬 inject 里只有 slots —— 多一个缺席服务就会让整个 fiber 挂起。 */
|
|
1101
|
+
const injectLine = /inject:\s*\[([^\]]*)\]/.exec(SOURCE)
|
|
1102
|
+
assert.notEqual(injectLine, null, '找不到 apply 返回的 inject 声明')
|
|
1103
|
+
assert.equal(injectLine[1].includes('modelDirectories'), false, 'modelDirectories 不能进硬 inject')
|
|
1104
|
+
|
|
1105
|
+
/** ② 用 ctx.inject(['modelDirectories', 'remote', 'remote.session']) 延迟注入。 */
|
|
1106
|
+
assert.match(SOURCE, /ctx\.inject\(\s*\['modelDirectories'/, '必须走 ctx.inject 延迟注入')
|
|
1107
|
+
assert.match(SOURCE, /scope\.modelDirectories\?\.directoryFor\(sessionId\)/, '从 scope 取 directory')
|
|
1108
|
+
|
|
1109
|
+
/**
|
|
1110
|
+
* ⚠ **必须一起声明 `remote` 与 `remote.session`**。
|
|
1111
|
+
*
|
|
1112
|
+
* `modelDirectories.directoryFor()` 内部要用 `sessions`/`remote`/
|
|
1113
|
+
* `remote.session`(它自己的 static inject),而宿主 ctx 是沙箱 façade,
|
|
1114
|
+
* 只挂本 fiber **声明过**的服务。少声明就会在调用时抛:
|
|
1115
|
+
* `cannot get property "remote.session" without inject`
|
|
1116
|
+
* 这个错误发生在 inject props 回调里 → 徽标永不出现(实测踩过)。
|
|
1117
|
+
*/
|
|
1118
|
+
const EXPECTED = "ctx.inject(['modelDirectories', 'remote', 'remote.session'],"
|
|
1119
|
+
assert.equal(
|
|
1120
|
+
SOURCE.includes(EXPECTED),
|
|
1121
|
+
true,
|
|
1122
|
+
`ctx.inject 必须同时声明 modelDirectories / remote / remote.session;找不到:${EXPECTED}`,
|
|
1123
|
+
)
|
|
1124
|
+
|
|
1125
|
+
/** ③ 贡献被 guard 包住,防止抛错打断整个 bundle 的 apply。 */
|
|
1126
|
+
assert.match(SOURCE, /function guardContribution/, '必须有 guardContribution')
|
|
1127
|
+
assert.match(SOURCE, /NOOP_DISPOSER/, '降级时返回空 disposer(slot 运行时需要)')
|
|
1128
|
+
const guarded = /guardContribution\(\s*'model route badge'/.test(SOURCE)
|
|
1129
|
+
assert.equal(guarded, true, '注册必须包在 guardContribution 里')
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* ④ `inject` 回调自己也要吞异常:它**每次渲染都被调用**
|
|
1133
|
+
* (`directoryFor` 在会话作用域未就绪/子代理会话上会抛),
|
|
1134
|
+
* 抛一次就整块渲染崩掉 → 徽标永远不出现。
|
|
1135
|
+
*/
|
|
1136
|
+
assert.match(SOURCE, /model directory lookup failed/, 'inject 回调内必须 try/catch')
|
|
1137
|
+
const injectCallback = /inject:\s*\(sessionId\)\s*=>\s*\{([\s\S]{0,600}?)\n\s*\},/.exec(SOURCE)
|
|
1138
|
+
assert.notEqual(injectCallback, null, '找不到 slot 注册的 inject 回调')
|
|
1139
|
+
assert.match(injectCallback[1], /try\s*\{/, 'inject 回调体必须是 try 包裹的')
|
|
1140
|
+
assert.match(injectCallback[1], /return \{\}/, '失败时返回空 props 而不是抛出')
|
|
1141
|
+
})
|