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
@@ -1,93 +1,319 @@
1
- import test from 'node:test'
2
- import assert from 'node:assert/strict'
3
- import { mkdtempSync, rmSync } from 'node:fs'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
-
7
- import { AccountPool, SqliteStore, isUsableCredential } from '../lib/pool.js'
8
-
9
- const credential = (id) => ({ zcode_jwt: `jwt-${id}`, device_mid: `dev-${id}`, user_id: `u-${id}` })
10
-
11
- function withPool(fn) {
12
- return async () => {
13
- const dir = mkdtempSync(join(tmpdir(), 'zsp-pool-'))
14
- try {
15
- const store = await new SqliteStore(join(dir, 'accounts.db'))
16
- const pool = new AccountPool({ store })
17
- await fn(pool, store)
18
- store.close()
19
- } finally {
20
- rmSync(dir, { recursive: true, force: true })
21
- }
22
- }
23
- }
24
-
25
- test('add 以 user_id 去重:同账号再登录只更新凭据', withPool(async (pool, store) => {
26
- const first = pool.add(credential('a'), '账号A')
27
- assert.equal(first.created, true)
28
- const second = pool.add({ ...credential('a'), zcode_jwt: 'jwt-renewed' }, '账号A-改名')
29
- assert.equal(second.created, false)
30
- assert.equal(pool.list().length, 1)
31
- const stored = store.get(first.entry.id)
32
- assert.equal(stored.credential.zcode_jwt, 'jwt-renewed')
33
- assert.equal(stored.nickname, '账号A-改名')
34
- }))
35
-
36
- test('pick 遵守手动顺序,并跳过 exclude 中的账号', withPool(async (pool) => {
37
- pool.add(credential('a'), 'A')
38
- pool.add(credential('b'), 'B')
39
- const first = pool.pick('GLM-5.3-Flash')
40
- assert.equal(first.entry.nickname, 'A')
41
- const second = pool.pick('GLM-5.3-Flash', new Set([first.entry.id]))
42
- assert.equal(second.entry.nickname, 'B')
43
- assert.equal(pool.pick('GLM-5.3-Flash', new Set([first.entry.id, second.entry.id])), null)
44
- }))
45
-
46
- test('markModelRateLimited 只影响那一对「账号×模型」,且到期后恢复', withPool(async (pool, store) => {
47
- const { entry } = pool.add(credential('a'), 'A')
48
- const now = Date.now()
49
- pool.markModelRateLimited(entry.id, 'GLM-5.3-Flash', now + 60_000)
50
- assert.equal(pool.pick('GLM-5.3-Flash'), null)
51
- assert.notEqual(pool.pick('GLM-5.3'), null, '另一个模型不受影响')
52
-
53
- /** 模拟时间流逝:把标记改成已过期再 sweep。 */
54
- store.patch(entry.id, { model_rate_limits: { 'GLM-5.3-Flash': now - 1 } })
55
- pool.sweepExpired()
56
- assert.notEqual(pool.pick('GLM-5.3-Flash'), null)
57
- }))
58
-
59
- test('停用的账号不参与选号;reorder 改变优先级', withPool(async (pool) => {
60
- const a = pool.add(credential('a'), 'A').entry
61
- pool.add(credential('b'), 'B')
62
- pool.setEnabled(a.id, false)
63
- assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'B')
64
- pool.setEnabled(a.id, true)
65
-
66
- const ids = pool.list().map((entry) => entry.id)
67
- pool.reorder([...ids].reverse())
68
- assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'B', '反转后 B 排到最前')
69
- }))
70
-
71
- test('reorder 拒绝不同集合(而不是尽力而为)', withPool(async (pool) => {
72
- pool.add(credential('a'), 'A')
73
- assert.throws(() => pool.reorder(['not-an-id']), /账号列表已变化/)
74
- }))
75
-
76
- test('resolve 支持序号 / id 前缀 / 昵称片段', withPool(async (pool) => {
77
- const a = pool.add(credential('a'), '尾号1234').entry
78
- assert.equal(pool.resolve('1').id, a.id)
79
- assert.equal(pool.resolve(a.id.slice(0, 8)).id, a.id)
80
- assert.equal(pool.resolve('1234').id, a.id)
81
- assert.equal(pool.resolve('nope'), undefined)
82
- }))
83
-
84
- test('isUsableCredential: 缺 zcode_jwt 或 device_mid 一律不可用', () => {
85
- assert.equal(isUsableCredential({ zcode_jwt: 'x', device_mid: 'y' }), true)
86
- assert.equal(isUsableCredential({ zcode_jwt: '', device_mid: 'y' }), false)
87
- assert.equal(isUsableCredential({ zcode_jwt: 'x' }), false)
88
- assert.equal(isUsableCredential(null), false)
89
- })
90
-
91
- test('add 拒绝形状不合法的凭据(写入侧也要校验)', withPool(async (pool) => {
92
- assert.throws(() => pool.add({ zcode_jwt: 'x' }), /凭据形状不合法/)
93
- }))
1
+ import test from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { mkdtempSync, rmSync } from 'node:fs'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+
7
+ import { AccountPool, SqliteStore, isUsableCredential } from '../lib/pool.js'
8
+
9
+ const credential = (id) => ({ zcode_jwt: `jwt-${id}`, device_mid: `dev-${id}`, user_id: `u-${id}` })
10
+
11
+ function withPool(fn) {
12
+ return async () => {
13
+ const dir = mkdtempSync(join(tmpdir(), 'zsp-pool-'))
14
+ try {
15
+ const store = await new SqliteStore(join(dir, 'accounts.db'))
16
+ const pool = new AccountPool({ store })
17
+ await fn(pool, store)
18
+ store.close()
19
+ } finally {
20
+ rmSync(dir, { recursive: true, force: true })
21
+ }
22
+ }
23
+ }
24
+
25
+ test('add 以 user_id 去重:同账号再登录只更新凭据', withPool(async (pool, store) => {
26
+ const first = pool.add(credential('a'), '账号A')
27
+ assert.equal(first.created, true)
28
+ const second = pool.add({ ...credential('a'), zcode_jwt: 'jwt-renewed' }, '账号A-改名')
29
+ assert.equal(second.created, false)
30
+ assert.equal(pool.list().length, 1)
31
+ const stored = store.get(first.entry.id)
32
+ assert.equal(stored.credential.zcode_jwt, 'jwt-renewed')
33
+ assert.equal(stored.nickname, '账号A-改名')
34
+ }))
35
+
36
+ test('pick 遵守手动顺序,并跳过 exclude 中的账号', withPool(async (pool) => {
37
+ pool.setSelectionOrder('manual')
38
+ pool.add(credential('a'), 'A')
39
+ pool.add(credential('b'), 'B')
40
+ const first = pool.pick('GLM-5.3-Flash')
41
+ assert.equal(first.entry.nickname, 'A')
42
+ const second = pool.pick('GLM-5.3-Flash', new Set([first.entry.id]))
43
+ assert.equal(second.entry.nickname, 'B')
44
+ assert.equal(pool.pick('GLM-5.3-Flash', new Set([first.entry.id, second.entry.id])), null)
45
+ }))
46
+
47
+ test('默认 expiry 选号:快过期的先用,无到期数据的排后但不被跳过', withPool(async (pool) => {
48
+ assert.equal(pool.selectionOrder, 'expiry', '0.7.0 起默认 expiry')
49
+ const a = pool.add(credential('a'), 'A').entry
50
+ const b = pool.add(credential('b'), 'B').entry
51
+ const c = pool.add(credential('c'), 'C').entry
52
+
53
+ /** A 还没拉过余额(无缓存);B 7 天后到期;C 1 小时后到期 → C 最优先。 */
54
+ pool.noteExpiry(b.id, Date.now() + 7 * 86_400_000)
55
+ pool.noteExpiry(c.id, Date.now() + 3_600_000)
56
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'C', '快过期先用')
57
+ assert.equal(pool.pick('GLM-5.3-Flash', new Set([c.id])).entry.nickname, 'B')
58
+ assert.equal(pool.pick('GLM-5.3-Flash', new Set([c.id, b.id])).entry.nickname, 'A', '无到期数据仍参与(排最后)')
59
+
60
+ /** 全部用光(noteExpiry 0)= 回到手动顺序兜底。 */
61
+ pool.noteExpiry(c.id, 0)
62
+ pool.noteExpiry(b.id, 0)
63
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'A')
64
+ /** noteExpiry 负数/NaN 同样按「无数据」处理。 */
65
+ pool.noteExpiry(a.id, -5)
66
+ assert.equal(pool.expiryOf(a.id), undefined)
67
+ }))
68
+
69
+ test('expiry 模式下 /use 置顶不再决定取号顺序(两种策略互斥)', withPool(async (pool) => {
70
+ const a = pool.add(credential('a'), 'A').entry
71
+ const b = pool.add(credential('b'), 'B').entry
72
+ pool.noteExpiry(b.id, Date.now() + 3_600_000)
73
+ /** 置顶 A(模拟 /use:改 added_at 顺序)—— expiry 模式下 B 仍因快过期而优先。 */
74
+ pool.reorder([a.id, b.id])
75
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'B', '快过期优先于置顶')
76
+ /** 切回 manual:置顶立即生效。 */
77
+ pool.setSelectionOrder('manual')
78
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'A')
79
+ /** 非法值回落 expiry(默认)。 */
80
+ pool.setSelectionOrder('bogus')
81
+ assert.equal(pool.selectionOrder, 'expiry')
82
+ }))
83
+
84
+ test('markModelRateLimited 只影响那一对「账号×模型」,且到期后恢复', withPool(async (pool, store) => {
85
+ const { entry } = pool.add(credential('a'), 'A')
86
+ const now = Date.now()
87
+ pool.markModelRateLimited(entry.id, 'GLM-5.3-Flash', now + 60_000)
88
+ assert.equal(pool.pick('GLM-5.3-Flash'), null)
89
+ assert.notEqual(pool.pick('GLM-5.3'), null, '另一个模型不受影响')
90
+
91
+ /** 模拟时间流逝:把标记改成已过期再 sweep。 */
92
+ store.patch(entry.id, { model_rate_limits: { 'GLM-5.3-Flash': now - 1 } })
93
+ pool.sweepExpired()
94
+ assert.notEqual(pool.pick('GLM-5.3-Flash'), null)
95
+ }))
96
+
97
+ test('忙标记:pick 优先绕开忙账号;全部忙时回落排队', withPool(async (pool) => {
98
+ const a = pool.add(credential('a'), 'A').entry
99
+ const b = pool.add(credential('b'), 'B').entry
100
+
101
+ /** A 忙 → 统一选号直接路由到空闲的 B(修复的核心行为)。 */
102
+ pool.markModelBusy(a.id, 'GLM-5.3-Flash')
103
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, b.id)
104
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, b.id, '后续请求同样绕开 A')
105
+
106
+ /** B 也忙 → 回落到忙账号(软状态,排队好过报错)。 */
107
+ pool.markModelBusy(b.id, 'GLM-5.3-Flash')
108
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, a.id)
109
+
110
+ /** exclude 过的账号不会被第二档「捞回来」。 */
111
+ const picked = pool.pick('GLM-5.3-Flash', new Set([a.id]))
112
+ assert.equal(picked.entry.id, b.id)
113
+ assert.equal(pool.pick('GLM-5.3-Flash', new Set([a.id, b.id])), null)
114
+
115
+ /** 忙标记是「账号×模型」粒度:另一个模型不受影响。 */
116
+ assert.equal(pool.pick('GLM-5.3').entry.id, a.id)
117
+ }))
118
+
119
+ test('忙标记:过期自动恢复、clearModelBusy 与 busyCount', withPool(async (pool) => {
120
+ const a = pool.add(credential('a'), 'A').entry
121
+ pool.add(credential('b'), 'B')
122
+ pool.markModelBusy(a.id, 'GLM-5.3-Flash', Date.now() + 50)
123
+ assert.equal(pool.isModelBusy(a.id, 'GLM-5.3-Flash'), true)
124
+ assert.equal(pool.busyCount(), 1)
125
+
126
+ /** 到期后恢复:不 busy 的账号重新排回第一优先。 */
127
+ await new Promise((resolve) => setTimeout(resolve, 60))
128
+ assert.equal(pool.isModelBusy(a.id, 'GLM-5.3-Flash'), false)
129
+ assert.equal(pool.busyCount(), 0)
130
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, a.id)
131
+
132
+ /** clearModelBusy 单模型 / 全账号两种粒度。 */
133
+ pool.markModelBusy(a.id, 'GLM-5.3-Flash')
134
+ pool.markModelBusy(a.id, 'GLM-5.3')
135
+ pool.clearModelBusy(a.id, 'GLM-5.3-Flash')
136
+ assert.equal(pool.isModelBusy(a.id, 'GLM-5.3-Flash'), false)
137
+ assert.equal(pool.isModelBusy(a.id, 'GLM-5.3'), true)
138
+ pool.clearModelBusy(a.id)
139
+ assert.equal(pool.isModelBusy(a.id, 'GLM-5.3'), false)
140
+ }))
141
+
142
+ test('忙标记不影响额度标记:限流中的账号不会被第二档捞回', withPool(async (pool) => {
143
+ const a = pool.add(credential('a'), 'A').entry
144
+ const b = pool.add(credential('b'), 'B').entry
145
+ pool.markModelRateLimited(a.id, 'GLM-5.3-Flash', Date.now() + 60_000)
146
+ pool.markModelBusy(b.id, 'GLM-5.3-Flash')
147
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, b.id, 'A 额度尽是硬过滤;B 忙但第二档可回落')
148
+ pool.markModelBusy(a.id, 'GLM-5.3-Flash')
149
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, b.id, 'A 忙也不影响 —— 它本来就因限流被硬过滤')
150
+ assert.equal(pool.pick('GLM-5.3-Flash', new Set([b.id])), null, 'B 被排除后无可选账号(A 限流硬过滤,绝不捞回)')
151
+ }))
152
+
153
+ test('停用的账号不参与选号;reorder 改变优先级', withPool(async (pool) => {
154
+ const a = pool.add(credential('a'), 'A').entry
155
+ pool.add(credential('b'), 'B')
156
+ pool.setEnabled(a.id, false)
157
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'B')
158
+ pool.setEnabled(a.id, true)
159
+
160
+ const ids = pool.list().map((entry) => entry.id)
161
+ pool.reorder([...ids].reverse())
162
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.nickname, 'B', '反转后 B 排到最前')
163
+ }))
164
+
165
+ test('reorder 拒绝不同集合(而不是尽力而为)', withPool(async (pool) => {
166
+ pool.add(credential('a'), 'A')
167
+ assert.throws(() => pool.reorder(['not-an-id']), /账号列表已变化/)
168
+ }))
169
+
170
+ test('resolve 支持序号 / id 前缀 / 昵称片段', withPool(async (pool) => {
171
+ const a = pool.add(credential('a'), '尾号1234').entry
172
+ assert.equal(pool.resolve('1').id, a.id)
173
+ assert.equal(pool.resolve(a.id.slice(0, 8)).id, a.id)
174
+ assert.equal(pool.resolve('1234').id, a.id)
175
+ assert.equal(pool.resolve('nope'), undefined)
176
+ }))
177
+
178
+ test('isUsableCredential: 缺 zcode_jwt 或 device_mid 一律不可用', () => {
179
+ assert.equal(isUsableCredential({ zcode_jwt: 'x', device_mid: 'y' }), true)
180
+ assert.equal(isUsableCredential({ zcode_jwt: '', device_mid: 'y' }), false)
181
+ assert.equal(isUsableCredential({ zcode_jwt: 'x' }), false)
182
+ assert.equal(isUsableCredential(null), false)
183
+ })
184
+
185
+ test('add 拒绝形状不合法的凭据(写入侧也要校验)', withPool(async (pool) => {
186
+ assert.throws(() => pool.add({ zcode_jwt: 'x' }), /凭据形状不合法/)
187
+ }))
188
+
189
+ /**
190
+ * 1.0.0 实时路由:模型栏旁的「当前使用账号」读的是**每次真实派发**的记录,
191
+ * 而不是配置里的钉定账号 —— 统一选号会换号,配置值会立刻说谎。
192
+ */
193
+ test('noteRouted: 记录每次真实派发,换号跟着走,改名即时生效', withPool(async (pool) => {
194
+ assert.equal(pool.lastRouted(), undefined, '启动后未发请求 = undefined(不是「没有账号」)')
195
+ const a = pool.add(credential('a'), '账号A').entry
196
+ const b = pool.add(credential('b'), '账号B').entry
197
+
198
+ const first = pool.noteRouted(a.id, 'GLM-5.3-Flash')
199
+ assert.equal(first.accountId, a.id)
200
+ assert.equal(first.nickname, '账号A')
201
+ assert.equal(first.modelId, 'GLM-5.3-Flash')
202
+ assert.ok(first.at > 0)
203
+
204
+ /** 换号:记录必须跟着走(这正是「实时」的含义)。 */
205
+ assert.equal(pool.noteRouted(b.id, 'GLM-5.3').accountId, b.id)
206
+ assert.equal(pool.lastRouted().nickname, '账号B')
207
+
208
+ /** 改名后下一次记录带新昵称(不卡旧名)。 */
209
+ pool.patchAccount(b.id, { nickname: '改名后的B' })
210
+ assert.equal(pool.noteRouted(b.id, 'GLM-5.3').nickname, '改名后的B')
211
+ }))
212
+
213
+ test('noteRouted: 空入参不覆盖上一条;记录里绝不含凭据', withPool(async (pool) => {
214
+ const a = pool.add(credential('a'), 'A').entry
215
+ pool.noteRouted(a.id, 'GLM-5.3-Flash')
216
+ const before = pool.lastRouted()
217
+ pool.noteRouted('', 'GLM-5.3')
218
+ pool.noteRouted(undefined)
219
+ assert.equal(pool.lastRouted(), before, '非法入参不覆盖真实记录')
220
+ assert.equal(JSON.stringify(before).includes('jwt-a'), false, '路由记录不含 zcode_jwt')
221
+ assert.equal(JSON.stringify(before).includes('dev-a'), false, '路由记录不含 device_mid')
222
+ }))
223
+
224
+ /**
225
+ * 1.0.1 审计回归:`preview` 是「模型栏旁徽标在首次请求前」的唯一数据源,
226
+ * 只要它和 `pick` 的筛选不一致,徽标就必然显示一个**真实派发时会跳过**的账号。
227
+ */
228
+ test('previewOrder 与 pick 同筛选:限流 / 忙 / 凭据不可用的账号不会被预览', withPool(async (pool) => {
229
+ const a = pool.add(credential('a'), 'A').entry
230
+ const b = pool.add(credential('b'), 'B').entry
231
+ const c = pool.add(credential('c'), 'C').entry
232
+
233
+ /** 无任何标记时,预览第一 = pick 第一。 */
234
+ assert.equal(pool.previewOrder('GLM-5.3-Flash', 1)[0].accountId, pool.pick('GLM-5.3-Flash').entry.id)
235
+
236
+ /** A 在该模型上限流 → 预览必须跳过 A(而不是按 added_at 取第一个)。 */
237
+ pool.markModelRateLimited(a.id, 'GLM-5.3-Flash', Date.now() + 3_600_000)
238
+ assert.notEqual(pool.previewOrder('GLM-5.3-Flash', 1)[0].accountId, a.id, '限流账号不能出现在预览里')
239
+ assert.equal(pool.previewOrder('GLM-5.3-Flash', 1)[0].accountId, pool.pick('GLM-5.3-Flash').entry.id)
240
+ /** 另一个模型不受影响。 */
241
+ assert.equal(pool.previewOrder('GLM-5.3', 1)[0].accountId, a.id)
242
+
243
+ /**
244
+ * 忙标记(3009):第一档避开;全都忙时 pick 回落第二档,预览也必须回落。
245
+ */
246
+ pool.markModelBusy(b.id, 'GLM-5.3', 60_000)
247
+ pool.markModelBusy(c.id, 'GLM-5.3', 60_000)
248
+ assert.equal(pool.previewOrder('GLM-5.3', 1)[0].accountId, a.id, '忙账号在有空闲账号时不进预览')
249
+ pool.markModelBusy(a.id, 'GLM-5.3', 60_000)
250
+ assert.equal(
251
+ pool.previewOrder('GLM-5.3', 1)[0].accountId,
252
+ pool.pick('GLM-5.3').entry.id,
253
+ '全部忙时两者回落同一档(按排序取第一个)',
254
+ )
255
+ }))
256
+
257
+ test('previewOrder: 停用账号不参与(与 pick 一致)', withPool(async (pool) => {
258
+ const a = pool.add(credential('a'), 'A').entry
259
+ const b = pool.add(credential('b'), 'B').entry
260
+ /** `store.patch/upsert` 校验凭据形状,坏凭据写不进去 —— 这里覆盖可构造的停用路径。 */
261
+ pool.setEnabled(a.id, false)
262
+ assert.equal(pool.previewOrder('GLM-5.3-Flash', 1)[0].accountId, b.id)
263
+ assert.equal(pool.pick('GLM-5.3-Flash').entry.id, b.id, '两者必须一致')
264
+ pool.setEnabled(a.id, true)
265
+ assert.equal(pool.previewOrder('GLM-5.3-Flash', 1)[0].accountId, a.id, '重新启用后回到首选')
266
+ }))
267
+
268
+ /**
269
+ * 用户实测故障:**面板显示额度开启,对话却说额度耗尽**。
270
+ * 面板口径 = 上游余额接口里所有已激活桶;对话准入 = 当前生效桶 + 1005 标记。
271
+ * `quotaState` 必须把「准入已拒」显式暴露出来,否则徽标只能继续显示健康账号。
272
+ */
273
+ test('quotaState: 上游已判额度用尽时 available=false(解释面板/对话不一致)', withPool(async (pool) => {
274
+ const a = pool.add(credential('a'), 'A').entry
275
+ const model = 'GLM-5.3-Flash'
276
+
277
+ /** 没有否决位、也没有面板数据 → usable 未知(null),**不是**「有额度」。 */
278
+ assert.deepEqual(pool.quotaState(a.id, model, {}), {
279
+ available: null,
280
+ limited: false,
281
+ expired: false,
282
+ usable: null,
283
+ reason: null,
284
+ })
285
+ /** 面板确认有余量且无否决位 → 可用。 */
286
+ assert.equal(pool.quotaState(a.id, model, { available: true }).usable, true)
287
+
288
+ pool.markModelRateLimited(a.id, model, Date.now() + 3_600_000)
289
+ const limited = pool.quotaState(a.id, model, { available: true, expiresAt: null })
290
+ /** ⚠ 关键:面板口径 available=true 原样保留,冲突由 usable=false 表达。 */
291
+ assert.equal(limited.available, true, '面板口径不被准入否决洗掉')
292
+ assert.equal(limited.usable, false, '综合结论:不可用')
293
+ assert.equal(limited.limited, true)
294
+ assert.match(limited.reason, /额度用尽/)
295
+ /** 同一账号的其它模型不受影响。 */
296
+ assert.equal(pool.quotaState(a.id, 'GLM-5.3', { available: true }).usable, true)
297
+
298
+ /** 缓存里的生效桶已过期 → 同样不可用(提示为上游客口径的副作用)。 */
299
+ const expired = pool.quotaState(a.id, 'GLM-5.3', { available: true, expiresAt: Date.now() - 1 })
300
+ assert.equal(expired.usable, false)
301
+ assert.equal(expired.expired, true)
302
+ }))
303
+
304
+ test('noteRouted: outcome 记录「正在服务 / 已判耗尽」,换号后不停在失败账号上', withPool(async (pool) => {
305
+ const a = pool.add(credential('a'), 'A').entry
306
+ const b = pool.add(credential('b'), 'B').entry
307
+
308
+ pool.noteRouted(a.id, 'GLM-5.3-Flash', { outcome: 'attempting' })
309
+ assert.equal(pool.lastRouted().outcome, 'attempting')
310
+ /** 上游判 A 额度用尽(零内容 / 1005)→ 记录必须转成 exhausted。 */
311
+ pool.noteRouted(a.id, 'GLM-5.3-Flash', { outcome: 'exhausted', reason: 'HTTP 200 但零内容' })
312
+ assert.equal(pool.lastRouted().outcome, 'exhausted')
313
+ assert.match(pool.lastRouted().reason, /零内容/)
314
+ /** 换到 B 并命中 → 徽标显示 B(真实在服务的那个)。 */
315
+ pool.noteRouted(b.id, 'GLM-5.3-Flash', { outcome: 'ok' })
316
+ assert.equal(pool.lastRouted().accountId, b.id)
317
+ assert.equal(pool.lastRouted().outcome, 'ok')
318
+ assert.equal(pool.lastRouted().reason, undefined, '成功记录不带失败原因')
319
+ }))