dsh-zcode-startplan 0.2.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,497 @@
1
+ /**
2
+ * 「每账号一个 provider 栏位」(workbuddy 模式)的契约测试:
3
+ *
4
+ * 1. id 编解码(lib/pinned-model.js,0.3.0 形态仍向后兼容);
5
+ * 2. adapter 多路由:统一栏位 + 每账号栏位,可见性 = 空目录;
6
+ * 宿主契约(rc.3 源码核实):
7
+ * - `prepareRoutes` 要求 `providerInfo(provider).id === provider` 且 name 非空;
8
+ * - `listModels` 只要求 id 非空且全表不重复(空数组 = 隐藏分组);
9
+ * - `normalizeModelInfo` 要求 `resolveModel` 返回的 `id` 与请求原样一致;
10
+ * 3. plan-client:路由级钉定(pinnedAccountId)只消耗指定账号;
11
+ * `@acct:` 后缀仍生效;额度尽报 QUOTA 不换号。
12
+ */
13
+
14
+ import test from 'node:test'
15
+ import assert from 'node:assert/strict'
16
+ import { mkdtempSync, rmSync } from 'node:fs'
17
+ import { tmpdir } from 'node:os'
18
+ import { join } from 'node:path'
19
+
20
+ import { makePinnedModelId, parsePinnedModelId, isPinnedModelId } from '../lib/pinned-model.js'
21
+ import { AccountPool, SqliteStore } from '../lib/pool.js'
22
+ import { ZcodePlanClient } from '../lib/plan-client.js'
23
+ import { NativeZcodeStartPlanAdapter } from '../lib/adapter.js'
24
+ import { resolveConfig, Config } from '../index.js'
25
+
26
+ const cred = (tag) => ({ zcode_jwt: `jwt-${tag}`, device_mid: `dev-${tag}`, user_id: `u-${tag}` })
27
+
28
+ const sseStream = (events) =>
29
+ new ReadableStream({
30
+ start(controller) {
31
+ const encoder = new TextEncoder()
32
+ for (const event of events) controller.enqueue(encoder.encode(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`))
33
+ controller.close()
34
+ },
35
+ })
36
+
37
+ const okResponse = (body) => ({
38
+ ok: true,
39
+ status: 200,
40
+ headers: new Headers({ 'content-type': 'text/event-stream' }),
41
+ body,
42
+ })
43
+
44
+ const textStream = () =>
45
+ sseStream([
46
+ { type: 'message_start', message: { model: 'GLM-5.3-Flash', usage: { input_tokens: 5 } } },
47
+ { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } },
48
+ { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: '好的' } },
49
+ { type: 'content_block_stop', index: 0 },
50
+ { type: 'message_delta', delta: { stop_reason: 'end_turn' }, usage: { output_tokens: 1 } },
51
+ { type: 'message_stop' },
52
+ ])
53
+
54
+ async function makePool(t, tag) {
55
+ const dir = mkdtempSync(join(tmpdir(), `zsp-${tag}-`))
56
+ const store = await new SqliteStore(join(dir, 'accounts.db'))
57
+ t.after(() => {
58
+ store.close()
59
+ rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 })
60
+ })
61
+ return new AccountPool({ store })
62
+ }
63
+
64
+ const baseModels = async () => [
65
+ { id: 'GLM-5.3-Flash', name: 'GLM-5.3-Flash', contextWindow: 1_000_000, maxTokens: 65_536, supportsImage: true },
66
+ { id: 'GLM-5.3', name: 'GLM-5.3', contextWindow: 1_000_000, maxTokens: 65_536, supportsImage: false },
67
+ ]
68
+
69
+ test('pinned-model:编解码往返与边界(0.3.0 形态向后兼容)', () => {
70
+ const id = makePinnedModelId('GLM-5.3-Flash', 'abc-123')
71
+ assert.equal(id, 'GLM-5.3-Flash@acct:abc-123')
72
+ assert.deepEqual(parsePinnedModelId(id), { baseModelId: 'GLM-5.3-Flash', accountId: 'abc-123' })
73
+ assert.equal(isPinnedModelId(id), true)
74
+ assert.equal(parsePinnedModelId('GLM-5.3'), null)
75
+ assert.equal(parsePinnedModelId('GLM-5.3@acct:'), null)
76
+ assert.equal(parsePinnedModelId('@acct:x'), null)
77
+ assert.equal(parsePinnedModelId(undefined), null)
78
+ })
79
+
80
+ test('adapter:账号路由派生互逆', async (t) => {
81
+ const pool = await makePool(t, 'route')
82
+ const adapter = new NativeZcodeStartPlanAdapter({
83
+ client: {},
84
+ models: baseModels,
85
+ provider: 'zcode-start-plan',
86
+ pool,
87
+ })
88
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', 'abc')
89
+ assert.equal(route, 'zcode-start-plan-acct-abc')
90
+ assert.equal(NativeZcodeStartPlanAdapter.accountIdFromRoute('zcode-start-plan', route), 'abc')
91
+ /** 统一路由不是账号路由。 */
92
+ assert.equal(NativeZcodeStartPlanAdapter.accountIdFromRoute('zcode-start-plan', 'zcode-start-plan'), null)
93
+ assert.equal(NativeZcodeStartPlanAdapter.accountIdFromRoute('zcode-start-plan', 'zcode-start-plan-acct-'), null)
94
+ /** accountRoutes:只列启用账号。 */
95
+ const first = pool.add(cred('a'), '主号')
96
+ const second = pool.add(cred('b'), '国际号')
97
+ pool.setEnabled(second.entry.id, false)
98
+ const routes = await adapter.accountRoutes()
99
+ assert.equal(routes.length, 1)
100
+ assert.equal(routes[0].accountId, first.entry.id)
101
+ assert.equal(routes[0].provider, NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', first.entry.id))
102
+ assert.equal(routes[0].nickname, '主号')
103
+ })
104
+
105
+ test('adapter.providerInfo:账号栏位名「ZCode Start Plan · 昵称」,prepareRoutes 契约全过', async (t) => {
106
+ const pool = await makePool(t, 'info')
107
+ const first = pool.add(cred('a'), '主号')
108
+ const adapter = new NativeZcodeStartPlanAdapter({
109
+ client: {},
110
+ models: baseModels,
111
+ provider: 'zcode-start-plan',
112
+ pool,
113
+ })
114
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', first.entry.id)
115
+ const info = adapter.providerInfo(route)
116
+ assert.equal(info.id, route, '宿主 prepareRoutes 要求 info.id === provider')
117
+ assert.equal(info.name, 'ZCode Start Plan · 主号')
118
+ const unified = adapter.providerInfo('zcode-start-plan')
119
+ assert.equal(unified.id, 'zcode-start-plan')
120
+ assert.ok(unified.name.length > 0)
121
+ /** 已删除的账号也要有合法 name(路由残留期间不炸)。 */
122
+ pool.remove(first.entry.id)
123
+ const orphan = adapter.providerInfo(route)
124
+ assert.equal(orphan.id, route)
125
+ assert.ok(orphan.name.length > 0)
126
+ })
127
+
128
+ test('adapter.listModels:账号栏位只列该账号;停用/删除 = 空目录(隐藏分组)', async (t) => {
129
+ const pool = await makePool(t, 'list')
130
+ const first = pool.add(cred('a'), '主号')
131
+ const adapter = new NativeZcodeStartPlanAdapter({
132
+ client: {},
133
+ models: baseModels,
134
+ provider: 'zcode-start-plan',
135
+ pool,
136
+ })
137
+
138
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', first.entry.id)
139
+ const models = await adapter.listModels(route)
140
+ assert.equal(models.length, 2, '该账号栏位 = 基础模型目录')
141
+ const seen = new Set()
142
+ for (const model of models) {
143
+ assert.equal(model.provider, route)
144
+ assert.ok(model.id.length > 0)
145
+ assert.ok(model.name.length > 0)
146
+ assert.equal(seen.has(model.id), false)
147
+ seen.add(model.id)
148
+ }
149
+ assert.ok(models[0].description.includes('主号'))
150
+ assert.equal(models[0].context.contextWindow, 1_000_000, '继承基础模型元数据')
151
+
152
+ /** 停用 = 空目录 = 宿主隐藏分组(workbuddy 可见性机制)。 */
153
+ pool.setEnabled(first.entry.id, false)
154
+ assert.deepEqual(await adapter.listModels(route), [])
155
+
156
+ /** 统一栏位默认可见。 */
157
+ const unified = await adapter.listModels('zcode-start-plan')
158
+ assert.equal(unified.length, 2)
159
+ assert.deepEqual(unified.map((model) => model.id), ['GLM-5.3-Flash', 'GLM-5.3'])
160
+ /** 关掉统一栏位 = 空目录,路由保留。 */
161
+ adapter.setUnifiedVisible(false)
162
+ assert.deepEqual(await adapter.listModels('zcode-start-plan'), [])
163
+ adapter.setUnifiedVisible(true)
164
+ assert.equal((await adapter.listModels('zcode-start-plan')).length, 2)
165
+ })
166
+
167
+ test('adapter.resolveModel:返回 id 与请求原样一致(INVALID_MODEL_INFO 契约)', async (t) => {
168
+ const pool = await makePool(t, 'resolve')
169
+ pool.add(cred('a'), '主号')
170
+ const adapter = new NativeZcodeStartPlanAdapter({
171
+ client: {},
172
+ models: baseModels,
173
+ provider: 'zcode-start-plan',
174
+ pool,
175
+ })
176
+ const resolved = await adapter.resolveModel('zcode-start-plan', 'GLM-5.3-Flash')
177
+ assert.equal(resolved.id, 'GLM-5.3-Flash')
178
+ assert.equal(resolved.provider, 'zcode-start-plan')
179
+ /** 账号栏位上解析基础模型 id:id 原样(宿主 normalizeModelInfo 硬契约)。 */
180
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', pool.list()[0].id)
181
+ const pinnedResolved = await adapter.resolveModel(route, 'GLM-5.3-Flash')
182
+ assert.equal(pinnedResolved.id, 'GLM-5.3-Flash')
183
+ assert.equal(pinnedResolved.provider, route)
184
+ /** 0.3.0 的 @acct: 后缀也原样放行。 */
185
+ const legacy = makePinnedModelId('GLM-5.3-Flash', 'any-account')
186
+ assert.equal((await adapter.resolveModel('zcode-start-plan', legacy)).id, legacy)
187
+ })
188
+
189
+ test('plan-client:路由级钉定只消耗指定账号(不按置顶顺序),上游见基础模型 id', async (t) => {
190
+ const pool = await makePool(t, 'pin')
191
+ const first = pool.add(cred('a'), '主号')
192
+ pool.add(cred('b'), '国际号')
193
+
194
+ let capturedBody
195
+ let capturedAuth = ''
196
+ const fetchImpl = async (_url, init) => {
197
+ capturedBody = JSON.parse(init.body)
198
+ /** fetch 头大小写不敏感,但假实现拿到的是普通对象 —— 逐键找。 */
199
+ for (const [key, value] of Object.entries(init.headers ?? {})) {
200
+ if (key.toLowerCase() === 'authorization') capturedAuth = value
201
+ }
202
+ return okResponse(textStream())
203
+ }
204
+ const client = new ZcodePlanClient({
205
+ pool,
206
+ minter: { async mint() { return { param: 'p', interactive: false } }, async fetchConfig() { return undefined } },
207
+ fetchImpl,
208
+ })
209
+ const adapter = new NativeZcodeStartPlanAdapter({
210
+ client,
211
+ models: baseModels,
212
+ provider: 'zcode-start-plan',
213
+ pool,
214
+ })
215
+
216
+ /** 置顶「国际号」,请求走「主号」的路由 → 发出去的必须是主号的凭据。 */
217
+ const ids = pool.list().map((entry) => entry.id)
218
+ pool.reorder([ids[1], ids[0]])
219
+
220
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', first.entry.id)
221
+ const chunks = []
222
+ for await (const chunk of adapter.stream({
223
+ provider: route,
224
+ model: 'GLM-5.3-Flash',
225
+ maxTokens: 64,
226
+ messages: [{ role: 'user', content: [{ type: 'text', text: '你好' }] }],
227
+ })) {
228
+ chunks.push(chunk)
229
+ }
230
+ assert.equal(chunks[chunks.length - 1].type, 'finish')
231
+ assert.equal(chunks[chunks.length - 1].reason.kind, 'stop')
232
+ assert.equal(capturedBody.model, 'GLM-5.3-Flash', '上游只见基础模型 id')
233
+ assert.equal(capturedAuth, 'Bearer jwt-a', '路由钉定生效:用的是主号的凭据')
234
+ })
235
+
236
+ test('adapter:setAccountVisible 隐藏栏位 = 空目录(每卡片开关的落点)', async (t) => {
237
+ const pool = await makePool(t, 'vis')
238
+ const first = pool.add(cred('a'), '主号')
239
+ const adapter = new NativeZcodeStartPlanAdapter({
240
+ client: {},
241
+ models: baseModels,
242
+ provider: 'zcode-start-plan',
243
+ pool,
244
+ })
245
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', first.entry.id)
246
+
247
+ /** 默认显示。 */
248
+ assert.equal((await adapter.listModels(route)).length, 2)
249
+ assert.equal(adapter.isAccountVisible(first.entry.id), true)
250
+
251
+ /** 面板开关关掉 = 空目录(路由保留),与统一栏位同一机制。 */
252
+ adapter.setAccountVisible(first.entry.id, false)
253
+ assert.deepEqual(await adapter.listModels(route), [])
254
+ assert.equal(adapter.isAccountVisible(first.entry.id), false)
255
+
256
+ /** 打开恢复显示;对未知 id 打开也无害。 */
257
+ adapter.setAccountVisible(first.entry.id, true)
258
+ assert.equal((await adapter.listModels(route)).length, 2)
259
+ adapter.setAccountVisible('unknown-id', true)
260
+ assert.equal((await adapter.listModels(route)).length, 2)
261
+
262
+ /** 构造时也可以带初始隐藏集合(settings.json 恢复)。 */
263
+ const hiddenAtBoot = new NativeZcodeStartPlanAdapter({
264
+ client: {},
265
+ models: baseModels,
266
+ provider: 'zcode-start-plan',
267
+ pool,
268
+ hiddenAccounts: [first.entry.id],
269
+ })
270
+ assert.deepEqual(await hiddenAtBoot.listModels(route), [])
271
+ /** 隐藏只影响目录,不影响钉定请求(老会话仍能走该路由)。 */
272
+ const client = new ZcodePlanClient({
273
+ pool,
274
+ minter: { async mint() { return { param: 'p', interactive: false } }, async fetchConfig() { return undefined } },
275
+ fetchImpl: async () => okResponse(textStream()),
276
+ })
277
+ const hiddenAdapter = new NativeZcodeStartPlanAdapter({ client, models: baseModels, provider: 'zcode-start-plan', pool, hiddenAccounts: [first.entry.id] })
278
+ const chunks = []
279
+ for await (const chunk of hiddenAdapter.stream({
280
+ provider: route,
281
+ model: 'GLM-5.3-Flash',
282
+ maxTokens: 64,
283
+ messages: [{ role: 'user', content: [{ type: 'text', text: '你好' }] }],
284
+ })) {
285
+ chunks.push(chunk)
286
+ }
287
+ assert.equal(chunks[chunks.length - 1].reason.kind, 'stop', '隐藏栏位不阻断已选它的会话')
288
+ })
289
+
290
+ test('plan-client:@acct: 后缀仍生效(0.3.0 兼容)', async (t) => { const pool = await makePool(t, 'legacy')
291
+ const first = pool.add(cred('a'), '主号')
292
+ pool.add(cred('b'), '国际号')
293
+
294
+ let capturedAuth = ''
295
+ const fetchImpl = async (_url, init) => {
296
+ for (const [key, value] of Object.entries(init.headers ?? {})) {
297
+ if (key.toLowerCase() === 'authorization') capturedAuth = value
298
+ }
299
+ return okResponse(textStream())
300
+ }
301
+ const client = new ZcodePlanClient({
302
+ pool,
303
+ minter: { async mint() { return { param: 'p', interactive: false } }, async fetchConfig() { return undefined } },
304
+ fetchImpl,
305
+ })
306
+ const adapter = new NativeZcodeStartPlanAdapter({ client, models: baseModels, provider: 'zcode-start-plan', pool })
307
+
308
+ const ids = pool.list().map((entry) => entry.id)
309
+ pool.reorder([ids[1], ids[0]])
310
+
311
+ const chunks = []
312
+ for await (const chunk of adapter.stream({
313
+ provider: 'zcode-start-plan',
314
+ model: makePinnedModelId('GLM-5.3-Flash', first.entry.id),
315
+ maxTokens: 64,
316
+ messages: [{ role: 'user', content: [{ type: 'text', text: '你好' }] }],
317
+ })) {
318
+ chunks.push(chunk)
319
+ }
320
+ assert.equal(chunks[chunks.length - 1].reason.kind, 'stop')
321
+ assert.equal(capturedAuth, 'Bearer jwt-a')
322
+ })
323
+
324
+ test('plan-client:钉定账号额度耗尽 → 报 QUOTA 不换号,且标记限流', async (t) => {
325
+ const pool = await makePool(t, 'quota')
326
+ const first = pool.add(cred('a'), '主号')
327
+ pool.add(cred('b'), '国际号')
328
+
329
+ let calls = 0
330
+ const fetchImpl = async () => {
331
+ calls += 1
332
+ return {
333
+ ok: true,
334
+ status: 200,
335
+ headers: new Headers({ 'content-type': 'application/json' }),
336
+ body: new ReadableStream({
337
+ start(controller) {
338
+ controller.enqueue(new TextEncoder().encode('{"code":1005,"msg":"exceed quota limit"}'))
339
+ controller.close()
340
+ },
341
+ }),
342
+ }
343
+ }
344
+ const client = new ZcodePlanClient({
345
+ pool,
346
+ minter: { async mint() { return { param: 'p', interactive: false } }, async fetchConfig() { return undefined } },
347
+ fetchImpl,
348
+ })
349
+ const adapter = new NativeZcodeStartPlanAdapter({ client, models: baseModels, provider: 'zcode-start-plan', pool })
350
+
351
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', first.entry.id)
352
+ const chunks = []
353
+ for await (const chunk of adapter.stream({
354
+ provider: route,
355
+ model: 'GLM-5.3-Flash',
356
+ maxTokens: 64,
357
+ messages: [{ role: 'user', content: [{ type: 'text', text: '你好' }] }],
358
+ })) {
359
+ chunks.push(chunk)
360
+ }
361
+ const finish = chunks[chunks.length - 1]
362
+ assert.equal(finish.type, 'finish')
363
+ assert.equal(finish.reason.kind, 'error')
364
+ assert.equal(finish.reason.failure.code, 'QUOTA', '钉定账号额度尽 = 明确报错,不静默换号')
365
+ assert.match(finish.reason.failure.message, /主号/)
366
+ assert.equal(calls, 1, '只打这一发:换号会吃「国际号」的额度')
367
+ const entry = pool.get(first.entry.id)
368
+ assert.ok(entry.model_rate_limits['GLM-5.3-Flash'] > 0)
369
+ })
370
+
371
+ test('plan-client:钉定账号已删除 → 明确报错不回退', async (t) => {
372
+ const pool = await makePool(t, 'gone')
373
+ const only = pool.add(cred('a'), '主号')
374
+ pool.add(cred('b'), '国际号')
375
+
376
+ let calls = 0
377
+ const client = new ZcodePlanClient({
378
+ pool,
379
+ minter: { async mint() { return { param: 'p', interactive: false } }, async fetchConfig() { return undefined } },
380
+ fetchImpl: async () => {
381
+ calls += 1
382
+ return okResponse(textStream())
383
+ },
384
+ })
385
+ const adapter = new NativeZcodeStartPlanAdapter({ client, models: baseModels, provider: 'zcode-start-plan', pool })
386
+ const route = NativeZcodeStartPlanAdapter.accountRoute('zcode-start-plan', only.entry.id)
387
+ pool.remove(only.entry.id)
388
+
389
+ const chunks = []
390
+ for await (const chunk of adapter.stream({
391
+ provider: route,
392
+ model: 'GLM-5.3-Flash',
393
+ maxTokens: 64,
394
+ messages: [{ role: 'user', content: [{ type: 'text', text: '你好' }] }],
395
+ })) {
396
+ chunks.push(chunk)
397
+ }
398
+ const finish = chunks[chunks.length - 1]
399
+ assert.equal(finish.reason.kind, 'error')
400
+ assert.equal(finish.reason.failure.code, 'QUOTA')
401
+ assert.match(finish.reason.failure.message, /已不存在/)
402
+ assert.equal(calls, 0, '账号不存在时根本不该发请求')
403
+ })
404
+
405
+ test('config:accountProviders 默认开启、pinnedAccounts 旧名映射、非法值被拒', () => {
406
+ const config = resolveConfig(undefined)
407
+ assert.equal(config.accountProviders, true)
408
+ assert.equal(resolveConfig({ pinnedAccounts: false }).accountProviders, false)
409
+ assert.equal(resolveConfig({ pinnedAccounts: false, accountProviders: true }).accountProviders, true)
410
+ assert.throws(() => Config({ accountProviders: 'yes' }), /accountProviders/)
411
+ assert.equal(Config({ accountProviders: false }).accountProviders, false)
412
+ })
413
+
414
+ test('manage-routes:/unified-list 开关回调生效、非法 body 被拒', async (t) => {
415
+ const pool = await makePool(t, 'mgmt')
416
+ const first = pool.add(cred('a'), '主号')
417
+ const { makeManageRoutes } = await import('../lib/manage-routes.js')
418
+
419
+ const seen = []
420
+ const seenAccount = []
421
+ const settings = { unifiedModelList: true, hiddenModelLists: [] }
422
+ const routes = makeManageRoutes({
423
+ pool,
424
+ settings,
425
+ onSetUnifiedVisible: (visible) => {
426
+ seen.push(visible)
427
+ settings.unifiedModelList = visible
428
+ },
429
+ onSetAccountVisible: (accountId, visible) => {
430
+ seenAccount.push([accountId, visible])
431
+ const hidden = new Set(settings.hiddenModelLists)
432
+ if (visible === true) hidden.delete(accountId)
433
+ else hidden.add(accountId)
434
+ settings.hiddenModelLists = [...hidden]
435
+ },
436
+ })
437
+ const unified = routes.find((route) => route.path.endsWith('/unified-list'))
438
+ assert.notEqual(unified, undefined)
439
+ const perAccount = routes.find((route) => route.path.endsWith('/model-list-visible'))
440
+ assert.notEqual(perAccount, undefined)
441
+
442
+ const makeReqRes = (body) => [
443
+ {
444
+ socket: { remoteAddress: '127.0.0.1' },
445
+ headers: { host: 'localhost' },
446
+ on(event, cb) {
447
+ if (event === 'data') cb(Buffer.from(JSON.stringify(body)))
448
+ if (event === 'end') cb()
449
+ },
450
+ },
451
+ {
452
+ writeHead() {},
453
+ end() {},
454
+ writableEnded: false,
455
+ destroyed: false,
456
+ },
457
+ ]
458
+
459
+ /** 合法调用:回调收到 true/false。 */
460
+ let [req, res] = makeReqRes({ visible: false })
461
+ await unified.handler(req, res)
462
+ assert.deepEqual(seen, [false])
463
+ assert.equal(settings.unifiedModelList, false)
464
+
465
+ ;[req, res] = makeReqRes({ visible: true })
466
+ await unified.handler(req, res)
467
+ assert.deepEqual(seen, [false, true])
468
+
469
+ /** 非法 body(缺 visible)→ 400,回调不触发。 */
470
+ const responses = []
471
+ const badRes = {
472
+ writeHead() {},
473
+ end(text) {
474
+ responses.push(JSON.parse(text))
475
+ },
476
+ writableEnded: false,
477
+ destroyed: false,
478
+ }
479
+ await unified.handler(makeReqRes({})[0], badRes)
480
+ assert.equal(responses[0].ok, false)
481
+ assert.deepEqual(seen, [false, true])
482
+
483
+ /** 每账号开关:隐藏 → 回调收到 (id, false);再显示 → (id, true)。 */
484
+ await perAccount.handler(...makeReqRes({ id: first.entry.id, visible: false }))
485
+ assert.deepEqual(seenAccount, [[first.entry.id, false]])
486
+ assert.deepEqual(settings.hiddenModelLists, [first.entry.id])
487
+ await perAccount.handler(...makeReqRes({ id: first.entry.id, visible: true }))
488
+ assert.deepEqual(seenAccount, [[first.entry.id, false], [first.entry.id, true]])
489
+ assert.deepEqual(settings.hiddenModelLists, [])
490
+
491
+ /** 未知账号 / 非法 body 被拒。 */
492
+ await perAccount.handler(makeReqRes({ id: 'nope', visible: false })[0], badRes)
493
+ assert.equal(responses[responses.length - 1].ok, false)
494
+ await perAccount.handler(makeReqRes({ visible: true })[0], badRes)
495
+ assert.equal(responses[responses.length - 1].ok, false)
496
+ assert.deepEqual(seenAccount, [[first.entry.id, false], [first.entry.id, true]])
497
+ })