@johorion/magpie-marvis 0.2.57 → 0.2.58

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.
@@ -1,306 +1,319 @@
1
- // tests/legacy-model-loader.test.mjs —— 旧真实槽位必须继续出网(0.2.21 allowlist 误伤回归锁)
2
- //
3
- // 事故现场:0.2.21 的 loader 用 TABLE allowlist 拦模型。TABLE 是**对外展示目录**,
4
- // 不是「上游可用集合」:`app-auto` / `file-auto` / `computer-auto` / `browser-auto`
5
- // 上游仍然 200,却被本地 404,一次 fetch 都没发 —— 旧路由整批废掉,
6
- // 而文案还说它们「已失效或未知」。
7
- //
8
- // 新策略(0.2.22):
9
- // · 只用 context.mjs 的 `isUnavailableModel` 拦**明确不可用**的模型(当前只有 `kimi-k2.5`);
10
- // · 其余模型(含 TABLE 外的旧真实槽位、随手编的 ID)一律出网,由上游响应决定;
11
- // · 上游 `4130001` / `无效模型id` → 模型级 404(此时 fetch 已经是 1 次:真的问过上游);
12
- // · `5104002` 是 RPC 故障,**不**做全局 404 映射;
13
- // · TABLE 8 条 + 旧数字下标 0..7 不回归;`main-auto` 仍是内部槽位,显示名不变。
14
- // · denylist 判定对首尾空白 / 大小写不敏感:`"kimi-k2.5 "` / `"Kimi-K2.5"` 这类
15
- // 手工配置写法同样本地 404、fetch 0(`isUnavailableModel` 先 trim + toLowerCase)。
16
- //
17
- // 全部离线:mock 掉 globalThis.fetch,不需要真实凭据、不碰真实上游。
18
- //
19
- // 这个文件先于实现落地(红):旧实现把 TABLE 外全部本地 404、fetch 0 次。
20
-
21
- import test from "node:test"
22
- import assert from "node:assert/strict"
23
-
24
- import { loaderHook, DEFAULT_MODEL } from "../src/hooks/loader.mjs"
25
- import { DISPLAY_BY_SLOT, MODEL_ALIASES } from "../src/context.mjs"
26
-
27
- const CHAT_URL = "https://yybadaccess.3g.qq.com/v1/chat/completions"
28
-
29
- /** 假凭据:只是形状对,值全部是测试用的假串,不会发到任何真实上游 */
30
- const AUTH = Object.freeze({
31
- access: "mv_test_offline_only",
32
- uid: "0123456789abcdef0123456789abcdef",
33
- metadata: {
34
- openid: "fedcba9876543210fedcba9876543210",
35
- guid: "0".repeat(32),
36
- loginType: "1",
37
- },
38
- })
39
-
40
- /** 替换 globalThis.fetch 并记录调用;返回 { calls, restore } */
41
- function stubFetch(handler) {
42
- const calls = []
43
- const real = globalThis.fetch
44
- globalThis.fetch = async (input, init) => {
45
- calls.push({ url: typeof input === "string" ? input : String(input), init: init ?? {} })
46
- return handler(input, init ?? {})
47
- }
48
- return {
49
- calls,
50
- restore: () => {
51
- globalThis.fetch = real
52
- },
53
- }
54
- }
55
-
56
- /** 取 loader 描述符(loaderHook 的形状是 getAuth => { fetch, baseURL, apiKey }) */
57
- async function loaderOf() {
58
- const describe = await loaderHook()(async () => AUTH)
59
- assert.equal(typeof describe.fetch, "function", "loader 必须返回 fetch")
60
- return describe
61
- }
62
-
63
- /** 组一个 /v1/chat/completions 请求(OpenAI 形状,stream 由参数决定) */
64
- function requestFor(model, stream = false) {
65
- return {
66
- url: CHAT_URL,
67
- init: {
68
- method: "POST",
69
- headers: { "content-type": "application/json" },
70
- body: JSON.stringify({
71
- model,
72
- stream,
73
- messages: [{ role: "user", content: "ping" }],
74
- }),
75
- },
76
- }
77
- }
78
-
79
- /** 上游正常回包(200,非流式 JSON) */
80
- function upstreamOk(model = "deepseek-v4-1-flash-261017") {
81
- return new Response(
82
- JSON.stringify({
83
- id: "chatcmpl-test",
84
- object: "chat.completion",
85
- model,
86
- choices: [{ index: 0, message: { role: "assistant", content: "pong" }, finish_reason: "stop" }],
87
- }),
88
- { status: 200, headers: { "content-type": "application/json" } },
89
- )
90
- }
91
-
92
- /** 上游错误回包:status 是 HTTP 状态,body 里带业务码 / 文案 */
93
- function upstreamError(status, payload) {
94
- return new Response(JSON.stringify(payload), {
95
- status,
96
- headers: { "content-type": "application/json" },
97
- })
98
- }
99
-
100
- /** 断言「本地 404、没有出网」并检查引导文案(只对明确不可用名单生效) */
101
- async function assertLocalModelUnavailable(res, calls) {
102
- assert.equal(res.status, 404, "明确不可用的模型必须在本地回 404")
103
- const body = await res.json()
104
- assert.equal(body.error.code, "model_unavailable")
105
- assert.equal(body.error.type, "upstream_error")
106
- assert.match(body.error.message, /不可用/, "文案要说明这个模型持续不可用")
107
- assert.match(body.error.message, /重新选择/, "文案要引导用户回模型列表重新选")
108
- assert.match(body.error.message, /真实模型名/, "文案要指明选「真实模型名」条目")
109
- assert.equal(calls.length, 0, "明确不可用模型一次 upstream 都不能访问")
110
- }
111
-
112
- // ─────────────────────────────────────────────────────────────
113
- // ① allowlist 误伤回归:TABLE 外的旧真实槽位仍然出网
114
- // (上游实测 200;旧实现本地 404、fetch 0 —— 这个用例就是它的红)
115
- // ─────────────────────────────────────────────────────────────
116
-
117
- const LEGACY_REAL_SLOTS = ["app-auto", "file-auto", "computer-auto", "browser-auto"]
118
-
119
- test("旧真实槽位 app-auto/file-auto/computer-auto/browser-auto 上游 200 → 出网 1 次、返回 200", async () => {
120
- for (const model of LEGACY_REAL_SLOTS) {
121
- const s = stubFetch(() => upstreamOk(model))
122
- try {
123
- const req = requestFor(model, false)
124
- const res = await (await loaderOf()).fetch(req.url, req.init)
125
- assert.equal(res.status, 200, `${model} 上游 200,loader 必须透出 200`)
126
- assert.equal(s.calls.length, 1, `${model} 必须出网 1 次(旧实现被 allowlist 本地拦掉,fetch 0)`)
127
- assert.equal(JSON.parse(s.calls[0].init.body).model, model, `${model} 是真实槽位名,原样发给上游`)
128
- } finally {
129
- s.restore()
130
- }
131
- }
132
- })
133
-
134
- test("旧真实槽位 stream:true 同样出网 1 次(流式与非流式走同一条路)", async () => {
135
- for (const model of LEGACY_REAL_SLOTS) {
136
- const s = stubFetch(() => upstreamOk(model))
137
- try {
138
- const req = requestFor(model, true)
139
- const res = await (await loaderOf()).fetch(req.url, req.init)
140
- assert.equal(res.status, 200)
141
- assert.equal(s.calls.length, 1, `${model} stream 也必须出网`)
142
- assert.equal(JSON.parse(s.calls[0].init.body).stream, true)
143
- } finally {
144
- s.restore()
145
- }
146
- }
147
- })
148
-
149
- // ─────────────────────────────────────────────────────────────
150
- // ② 明确不可用名单(当前只有 kimi-k2.5):两种 stream 都本地 404、fetch 0
151
- // ─────────────────────────────────────────────────────────────
152
-
153
- test("kimi-k2.5(stream:true)→ 本地 404,fetch 0 次", async () => {
154
- const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
155
- try {
156
- const req = requestFor("kimi-k2.5", true)
157
- const res = await (await loaderOf()).fetch(req.url, req.init)
158
- await assertLocalModelUnavailable(res, s.calls)
159
- } finally {
160
- s.restore()
161
- }
162
- })
163
-
164
- test("kimi-k2.5(stream:false)→ 本地 404,fetch 0 次", async () => {
165
- const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
166
- try {
167
- const req = requestFor("kimi-k2.5", false)
168
- const res = await (await loaderOf()).fetch(req.url, req.init)
169
- await assertLocalModelUnavailable(res, s.calls)
170
- } finally {
171
- s.restore()
172
- }
173
- })
174
-
175
- test("kimi-k2.5 上游即使 200 也不出网(名单是硬的,不靠上游兜底)", async () => {
176
- const s = stubFetch(() => upstreamOk("kimi-k2.5"))
177
- try {
178
- const req = requestFor("kimi-k2.5", false)
179
- const res = await (await loaderOf()).fetch(req.url, req.init)
180
- await assertLocalModelUnavailable(res, s.calls)
181
- } finally {
182
- s.restore()
183
- }
184
- })
185
-
186
- // ─────────────────────────────────────────────────────────────
187
- // ③ 随机不存在的模型:出网 1 次,上游 4130001 → 模型级 404
188
- // ─────────────────────────────────────────────────────────────
189
-
190
- test("随手编的模型出网 1 次;上游 4130001 → 404 model_unavailable(不是本地拦)", async () => {
191
- const model = "totally-made-up-model-xyz"
192
- const s = stubFetch(() => upstreamError(400, { code: 4130001, msg: "无效模型id" }))
193
- try {
194
- const req = requestFor(model, false)
195
- const res = await (await loaderOf()).fetch(req.url, req.init)
196
- assert.equal(s.calls.length, 1, "未知模型必须先问上游,由上游响应决定")
197
- assert.equal(JSON.parse(s.calls[0].init.body).model, model, "未知模型 ID 原样发出")
198
- assert.equal(res.status, 404, "上游 4130001 归入模型级 404")
199
- const body = await res.json()
200
- assert.equal(body.error.code, "model_unavailable")
201
- } finally {
202
- s.restore()
203
- }
204
- })
205
-
206
- test("上游只说「无效模型id」没有业务码时也归入 404(fetch 仍是 1 次)", async () => {
207
- const s = stubFetch(() => upstreamError(400, { error: { message: "无效模型id: xxx" } }))
208
- try {
209
- const req = requestFor("another-unknown-model", false)
210
- const res = await (await loaderOf()).fetch(req.url, req.init)
211
- assert.equal(s.calls.length, 1)
212
- assert.equal(res.status, 404)
213
- assert.equal((await res.json()).error.code, "model_unavailable")
214
- } finally {
215
- s.restore()
216
- }
217
- })
218
-
219
- // ─────────────────────────────────────────────────────────────
220
- // ④ TABLE 8 条 + 旧数字 0..7 不回归;main-auto 仍是内部槽位
221
- // ─────────────────────────────────────────────────────────────
222
-
223
- test("TABLE 8 条槽位 / 显示名 / 旧下标 0..7 全部放行且出网", async () => {
224
- assert.equal(Object.keys(DISPLAY_BY_SLOT).length, 8)
225
- assert.equal(Object.keys(MODEL_ALIASES).length, 8)
226
- assert.equal(DEFAULT_MODEL, "main-auto", "main-auto 仍是内部槽位")
227
- assert.notEqual(DISPLAY_BY_SLOT["main-auto"], "main-auto", "卡片显示名仍是真实模型名")
228
-
229
- const cases = []
230
- for (const [slot, display] of Object.entries(DISPLAY_BY_SLOT)) {
231
- cases.push([slot, slot], [display, slot])
232
- }
233
- for (const [index, slot] of Object.entries(MODEL_ALIASES)) {
234
- cases.push([index, slot])
235
- }
236
-
237
- for (const [model, expectedSlot] of cases) {
238
- const s = stubFetch(() => upstreamOk(DISPLAY_BY_SLOT[expectedSlot]))
239
- try {
240
- const req = requestFor(model, false)
241
- const res = await (await loaderOf()).fetch(req.url, req.init)
242
- assert.equal(res.status, 200, `模型 ${model} 应放行`)
243
- assert.equal(s.calls.length, 1, `模型 ${model} 应出网`)
244
- assert.equal(JSON.parse(s.calls[0].init.body).model, expectedSlot, `模型 ${model} 应归一成 ${expectedSlot}`)
245
- } finally {
246
- s.restore()
247
- }
248
- }
249
- })
250
-
251
- // ─────────────────────────────────────────────────────────────
252
- // ⑤ 5104002 不做全局 404 映射
253
- // ─────────────────────────────────────────────────────────────
254
-
255
- test("上游 5104002 RPC 500 原样透出(不全局映射成 404)", async () => {
256
- const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
257
- try {
258
- const req = requestFor("main-auto", false)
259
- const res = await (await loaderOf()).fetch(req.url, req.init)
260
- assert.equal(s.calls.length, 1)
261
- assert.equal(res.status, 500, "5104002 是 RPC 故障,不能当模型失效")
262
- assert.equal((await res.json()).code, 5104002)
263
- } finally {
264
- s.restore()
265
- }
266
- })
267
-
268
- // ─────────────────────────────────────────────────────────────
269
- // ⑥ denylist 变体:首尾空白 / 大小写也必须本地 404、fetch 0(F2)
270
- // isUnavailableModel 先 trim + toLowerCase 再 canonical
271
- // ─────────────────────────────────────────────────────────────
272
-
273
- const UNAVAILABLE_VARIANTS = [
274
- "kimi-k2.5 ",
275
- " kimi-k2.5",
276
- "\tkimi-k2.5\n",
277
- "Kimi-K2.5",
278
- "KIMI-K2.5",
279
- "Kimi-K2.5 ",
280
- ]
281
-
282
- test("kimi-k2.5 的空白 / 大小写变体(stream:true)→ 本地 404,fetch 0 次", async () => {
283
- for (const model of UNAVAILABLE_VARIANTS) {
284
- const s = stubFetch(() => upstreamOk(model))
285
- try {
286
- const req = requestFor(model, true)
287
- const res = await (await loaderOf()).fetch(req.url, req.init)
288
- await assertLocalModelUnavailable(res, s.calls)
289
- } finally {
290
- s.restore()
291
- }
292
- }
293
- })
294
-
295
- test("kimi-k2.5 的空白 / 大小写变体(stream:false)→ 本地 404,fetch 0 次", async () => {
296
- for (const model of UNAVAILABLE_VARIANTS) {
297
- const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
298
- try {
299
- const req = requestFor(model, false)
300
- const res = await (await loaderOf()).fetch(req.url, req.init)
301
- await assertLocalModelUnavailable(res, s.calls)
302
- } finally {
303
- s.restore()
304
- }
305
- }
1
+ // tests/legacy-model-loader.test.mjs —— 旧真实槽位必须继续出网(0.2.21 allowlist 误伤回归锁)
2
+ //
3
+ // 事故现场:0.2.21 的 loader 用 TABLE allowlist 拦模型。TABLE 是**对外展示目录**,
4
+ // 不是「上游可用集合」:`app-auto` / `file-auto` / `computer-auto` / `browser-auto`
5
+ // 上游仍然 200,却被本地 404,一次 fetch 都没发 —— 旧路由整批废掉,
6
+ // 而文案还说它们「已失效或未知」。
7
+ //
8
+ // 新策略(0.2.22):
9
+ // · 只用 context.mjs 的 `isUnavailableModel` 拦**明确不可用**的模型(当前只有 `kimi-k2.5`);
10
+ // · 其余模型(含 TABLE 外的旧真实槽位、随手编的 ID)一律出网,由上游响应决定;
11
+ // · 上游 `4130001` / `无效模型id` → 模型级 404(此时 fetch 已经是 1 次:真的问过上游);
12
+ // · `5104002` 是 RPC 故障,**不**做全局 404 映射;
13
+ // · TABLE 8 条 + 旧数字下标 0..7 不回归;`main-auto` 仍是内部槽位,显示名不变。
14
+ // · denylist 判定对首尾空白 / 大小写不敏感:`"kimi-k2.5 "` / `"Kimi-K2.5"` 这类
15
+ // 手工配置写法同样本地 404、fetch 0(`isUnavailableModel` 先 trim + toLowerCase)。
16
+ //
17
+ // 全部离线:mock 掉 globalThis.fetch,不需要真实凭据、不碰真实上游。
18
+ //
19
+ // 这个文件先于实现落地(红):旧实现把 TABLE 外全部本地 404、fetch 0 次。
20
+
21
+ import test from "node:test"
22
+ import assert from "node:assert/strict"
23
+
24
+ import { loaderHook, DEFAULT_MODEL } from "../src/hooks/loader.mjs"
25
+ import { DISPLAY_BY_SLOT, MODEL_ALIASES, isUnavailableModel } from "../src/context.mjs"
26
+
27
+ const CHAT_URL = "https://yybadaccess.3g.qq.com/v1/chat/completions"
28
+
29
+ /** 假凭据:只是形状对,值全部是测试用的假串,不会发到任何真实上游 */
30
+ const AUTH = Object.freeze({
31
+ access: "mv_test_offline_only",
32
+ uid: "0123456789abcdef0123456789abcdef",
33
+ metadata: {
34
+ openid: "fedcba9876543210fedcba9876543210",
35
+ guid: "0".repeat(32),
36
+ loginType: "1",
37
+ },
38
+ })
39
+
40
+ /** 替换 globalThis.fetch 并记录调用;返回 { calls, restore } */
41
+ function stubFetch(handler) {
42
+ const calls = []
43
+ const real = globalThis.fetch
44
+ globalThis.fetch = async (input, init) => {
45
+ calls.push({ url: typeof input === "string" ? input : String(input), init: init ?? {} })
46
+ return handler(input, init ?? {})
47
+ }
48
+ return {
49
+ calls,
50
+ restore: () => {
51
+ globalThis.fetch = real
52
+ },
53
+ }
54
+ }
55
+
56
+ /** 取 loader 描述符(loaderHook 的形状是 getAuth => { fetch, baseURL, apiKey }) */
57
+ async function loaderOf() {
58
+ const describe = await loaderHook()(async () => AUTH)
59
+ assert.equal(typeof describe.fetch, "function", "loader 必须返回 fetch")
60
+ return describe
61
+ }
62
+
63
+ /** 组一个 /v1/chat/completions 请求(OpenAI 形状,stream 由参数决定) */
64
+ function requestFor(model, stream = false) {
65
+ return {
66
+ url: CHAT_URL,
67
+ init: {
68
+ method: "POST",
69
+ headers: { "content-type": "application/json" },
70
+ body: JSON.stringify({
71
+ model,
72
+ stream,
73
+ messages: [{ role: "user", content: "ping" }],
74
+ }),
75
+ },
76
+ }
77
+ }
78
+
79
+ /** 上游正常回包(200,非流式 JSON) */
80
+ function upstreamOk(model = "deepseek-v4-1-flash-261017") {
81
+ return new Response(
82
+ JSON.stringify({
83
+ id: "chatcmpl-test",
84
+ object: "chat.completion",
85
+ model,
86
+ choices: [{ index: 0, message: { role: "assistant", content: "pong" }, finish_reason: "stop" }],
87
+ }),
88
+ { status: 200, headers: { "content-type": "application/json" } },
89
+ )
90
+ }
91
+
92
+ /** 上游错误回包:status 是 HTTP 状态,body 里带业务码 / 文案 */
93
+ function upstreamError(status, payload) {
94
+ return new Response(JSON.stringify(payload), {
95
+ status,
96
+ headers: { "content-type": "application/json" },
97
+ })
98
+ }
99
+
100
+ /** 断言「本地 404、没有出网」并检查引导文案(只对明确不可用名单生效) */
101
+ async function assertLocalModelUnavailable(res, calls) {
102
+ assert.equal(res.status, 404, "明确不可用的模型必须在本地回 404")
103
+ const body = await res.json()
104
+ assert.equal(body.error.code, "model_unavailable")
105
+ assert.equal(body.error.type, "upstream_error")
106
+ assert.match(body.error.message, /不可用/, "文案要说明这个模型持续不可用")
107
+ assert.match(body.error.message, /重新选择/, "文案要引导用户回模型列表重新选")
108
+ assert.match(body.error.message, /真实模型名/, "文案要指明选「真实模型名」条目")
109
+ assert.equal(calls.length, 0, "明确不可用模型一次 upstream 都不能访问")
110
+ }
111
+
112
+ // ─────────────────────────────────────────────────────────────
113
+ // ① allowlist 误伤回归:TABLE 外的旧真实槽位仍然出网
114
+ // (上游实测 200;旧实现本地 404、fetch 0 —— 这个用例就是它的红)
115
+ // ─────────────────────────────────────────────────────────────
116
+
117
+ const LEGACY_REAL_SLOTS = ["app-auto", "file-auto", "computer-auto", "browser-auto"]
118
+
119
+ test("旧真实槽位 app-auto/file-auto/computer-auto/browser-auto 上游 200 → 出网 1 次、返回 200", async () => {
120
+ for (const model of LEGACY_REAL_SLOTS) {
121
+ const s = stubFetch(() => upstreamOk(model))
122
+ try {
123
+ const req = requestFor(model, false)
124
+ const res = await (await loaderOf()).fetch(req.url, req.init)
125
+ assert.equal(res.status, 200, `${model} 上游 200,loader 必须透出 200`)
126
+ assert.equal(s.calls.length, 1, `${model} 必须出网 1 次(旧实现被 allowlist 本地拦掉,fetch 0)`)
127
+ assert.equal(JSON.parse(s.calls[0].init.body).model, model, `${model} 是真实槽位名,原样发给上游`)
128
+ } finally {
129
+ s.restore()
130
+ }
131
+ }
132
+ })
133
+
134
+ test("旧真实槽位 stream:true 同样出网 1 次(流式与非流式走同一条路)", async () => {
135
+ for (const model of LEGACY_REAL_SLOTS) {
136
+ const s = stubFetch(() => upstreamOk(model))
137
+ try {
138
+ const req = requestFor(model, true)
139
+ const res = await (await loaderOf()).fetch(req.url, req.init)
140
+ assert.equal(res.status, 200)
141
+ assert.equal(s.calls.length, 1, `${model} stream 也必须出网`)
142
+ assert.equal(JSON.parse(s.calls[0].init.body).stream, true)
143
+ } finally {
144
+ s.restore()
145
+ }
146
+ }
147
+ })
148
+
149
+ // ─────────────────────────────────────────────────────────────
150
+ // ② 明确不可用名单(当前只有 kimi-k2.5):两种 stream 都本地 404、fetch 0
151
+ // ─────────────────────────────────────────────────────────────
152
+
153
+ test("kimi-k2.5(stream:true)→ 本地 404,fetch 0 次", async () => {
154
+ const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
155
+ try {
156
+ const req = requestFor("kimi-k2.5", true)
157
+ const res = await (await loaderOf()).fetch(req.url, req.init)
158
+ await assertLocalModelUnavailable(res, s.calls)
159
+ } finally {
160
+ s.restore()
161
+ }
162
+ })
163
+
164
+ test("kimi-k2.5(stream:false)→ 本地 404,fetch 0 次", async () => {
165
+ const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
166
+ try {
167
+ const req = requestFor("kimi-k2.5", false)
168
+ const res = await (await loaderOf()).fetch(req.url, req.init)
169
+ await assertLocalModelUnavailable(res, s.calls)
170
+ } finally {
171
+ s.restore()
172
+ }
173
+ })
174
+
175
+ test("kimi-k2.5 上游即使 200 也不出网(名单是硬的,不靠上游兜底)", async () => {
176
+ const s = stubFetch(() => upstreamOk("kimi-k2.5"))
177
+ try {
178
+ const req = requestFor("kimi-k2.5", false)
179
+ const res = await (await loaderOf()).fetch(req.url, req.init)
180
+ await assertLocalModelUnavailable(res, s.calls)
181
+ } finally {
182
+ s.restore()
183
+ }
184
+ })
185
+
186
+ // ─────────────────────────────────────────────────────────────
187
+ // ③ 随机不存在的模型:出网 1 次,上游 4130001 → 模型级 404
188
+ // ─────────────────────────────────────────────────────────────
189
+
190
+ test("随手编的模型出网 1 次;上游 4130001 → 404 model_unavailable(不是本地拦)", async () => {
191
+ const model = "totally-made-up-model-xyz"
192
+ const s = stubFetch(() => upstreamError(400, { code: 4130001, msg: "无效模型id" }))
193
+ try {
194
+ const req = requestFor(model, false)
195
+ const res = await (await loaderOf()).fetch(req.url, req.init)
196
+ assert.equal(s.calls.length, 1, "未知模型必须先问上游,由上游响应决定")
197
+ assert.equal(JSON.parse(s.calls[0].init.body).model, model, "未知模型 ID 原样发出")
198
+ assert.equal(res.status, 404, "上游 4130001 归入模型级 404")
199
+ const body = await res.json()
200
+ assert.equal(body.error.code, "model_unavailable")
201
+ } finally {
202
+ s.restore()
203
+ }
204
+ })
205
+
206
+ test("上游只说「无效模型id」没有业务码时也归入 404(fetch 仍是 1 次)", async () => {
207
+ const s = stubFetch(() => upstreamError(400, { error: { message: "无效模型id: xxx" } }))
208
+ try {
209
+ const req = requestFor("another-unknown-model", false)
210
+ const res = await (await loaderOf()).fetch(req.url, req.init)
211
+ assert.equal(s.calls.length, 1)
212
+ assert.equal(res.status, 404)
213
+ assert.equal((await res.json()).error.code, "model_unavailable")
214
+ } finally {
215
+ s.restore()
216
+ }
217
+ })
218
+
219
+ // ─────────────────────────────────────────────────────────────
220
+ // ④ TABLE 8 条 + 旧数字 0..7 不回归;main-auto 仍是内部槽位
221
+ // ─────────────────────────────────────────────────────────────
222
+
223
+ test("表里每条槽位:活着的放行出网,实测已下线的本地挡掉", async () => {
224
+ // 2026-12-06 实测(清空重定向后逐槽位直发上游,对照组 zzz-not-a-model 同样 4130001):
225
+ // 活着 6 个:main-auto · deepseek-v4-pro-external · hy3-preview-agent
226
+ // · hy3-preview-agent-real · hunyuan-turbos-vision · hy4
227
+ // 已下线 3 个:claude-sonnet-4-6 · claude-opus-4-6 · gpt-5.4
228
+ // 条数不再写死,改由表推导;两组的期望行为分别是 200 出网 / 404 不出网。
229
+ assert.ok(Object.keys(DISPLAY_BY_SLOT).length >= 6, "表至少要覆盖实测活着的 6 个槽位")
230
+ assert.equal(Object.keys(MODEL_ALIASES).length, Object.keys(DISPLAY_BY_SLOT).length)
231
+ assert.equal(DEFAULT_MODEL, "main-auto", "main-auto 仍是内部槽位")
232
+ assert.notEqual(DISPLAY_BY_SLOT["main-auto"], "main-auto", "卡片显示名仍是真实模型名")
233
+
234
+ const cases = []
235
+ for (const [slot, display] of Object.entries(DISPLAY_BY_SLOT)) {
236
+ cases.push([slot, slot], [display, slot])
237
+ }
238
+ for (const [index, slot] of Object.entries(MODEL_ALIASES)) {
239
+ cases.push([index, slot])
240
+ }
241
+
242
+ for (const [model, expectedSlot] of cases) {
243
+ const dead = isUnavailableModel(expectedSlot)
244
+ const s = stubFetch(() => upstreamOk(DISPLAY_BY_SLOT[expectedSlot]))
245
+ try {
246
+ const req = requestFor(model, false)
247
+ const res = await (await loaderOf()).fetch(req.url, req.init)
248
+ if (dead) {
249
+ // 已下线槽位必须 404(搁置该模型)且**不出网**:
250
+ // 用 401 会把账号标成需重新登录并 failover 到每个账号,用 503 会被当成可重试风控。
251
+ assert.equal(res.status, 404, `已下线的 ${model} 应本地 404`)
252
+ assert.equal(s.calls.length, 0, `已下线的 ${model} 不该出网`)
253
+ continue
254
+ }
255
+ assert.equal(res.status, 200, `模型 ${model} 应放行`)
256
+ assert.equal(s.calls.length, 1, `模型 ${model} 应出网`)
257
+ assert.equal(JSON.parse(s.calls[0].init.body).model, expectedSlot, `模型 ${model} 应归一成 ${expectedSlot}`)
258
+ } finally {
259
+ s.restore()
260
+ }
261
+ }
262
+ })
263
+
264
+ // ─────────────────────────────────────────────────────────────
265
+ // ⑤ 5104002 不做全局 404 映射
266
+ // ─────────────────────────────────────────────────────────────
267
+
268
+ test("上游 5104002 RPC 500 原样透出(不全局映射成 404)", async () => {
269
+ const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
270
+ try {
271
+ const req = requestFor("main-auto", false)
272
+ const res = await (await loaderOf()).fetch(req.url, req.init)
273
+ assert.equal(s.calls.length, 1)
274
+ assert.equal(res.status, 500, "5104002 是 RPC 故障,不能当模型失效")
275
+ assert.equal((await res.json()).code, 5104002)
276
+ } finally {
277
+ s.restore()
278
+ }
279
+ })
280
+
281
+ // ─────────────────────────────────────────────────────────────
282
+ // ⑥ denylist 变体:首尾空白 / 大小写也必须本地 404、fetch 0(F2)
283
+ // isUnavailableModel 先 trim + toLowerCase 再 canonical
284
+ // ─────────────────────────────────────────────────────────────
285
+
286
+ const UNAVAILABLE_VARIANTS = [
287
+ "kimi-k2.5 ",
288
+ " kimi-k2.5",
289
+ "\tkimi-k2.5\n",
290
+ "Kimi-K2.5",
291
+ "KIMI-K2.5",
292
+ "Kimi-K2.5 ",
293
+ ]
294
+
295
+ test("kimi-k2.5 的空白 / 大小写变体(stream:true)→ 本地 404,fetch 0 次", async () => {
296
+ for (const model of UNAVAILABLE_VARIANTS) {
297
+ const s = stubFetch(() => upstreamOk(model))
298
+ try {
299
+ const req = requestFor(model, true)
300
+ const res = await (await loaderOf()).fetch(req.url, req.init)
301
+ await assertLocalModelUnavailable(res, s.calls)
302
+ } finally {
303
+ s.restore()
304
+ }
305
+ }
306
+ })
307
+
308
+ test("kimi-k2.5 的空白 / 大小写变体(stream:false)→ 本地 404,fetch 0 次", async () => {
309
+ for (const model of UNAVAILABLE_VARIANTS) {
310
+ const s = stubFetch(() => upstreamError(500, { code: 5104002, msg: "RPC error" }))
311
+ try {
312
+ const req = requestFor(model, false)
313
+ const res = await (await loaderOf()).fetch(req.url, req.init)
314
+ await assertLocalModelUnavailable(res, s.calls)
315
+ } finally {
316
+ s.restore()
317
+ }
318
+ }
306
319
  })