82 lines
6.0 KiB
JavaScript
82 lines
6.0 KiB
JavaScript
'use strict'
|
||
// src/generation-endpoint.js 的行为契约(2026-11-24)
|
||
//
|
||
// 守的是什么:
|
||
// · joinEndpoint —— genPath 必须真正生效(配置项此前是死的),且尾斜杠/绝对地址/空路径都要对
|
||
// · buildAuthHeaders —— 缺省必须是 Bearer(老配置不能挂),header/query/none 各走各的
|
||
// · applyAuthQuery —— 同名参数覆盖,不能出现两个 key
|
||
// · shouldRetryNextAddress —— **结果未知不重试**(超时/hard-timeout),只换 5xx/429/连接失败
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
// 仓库风格:src/*.js 是 UMD 全局脚本(不认 CommonJS),测试用沙箱加载
|
||
const box = {}
|
||
new Function('window', 'globalThis', fs.readFileSync(path.join(__dirname, '..', 'src', 'generation-endpoint.js'), 'utf8'))(box, box)
|
||
const E = box.IWBGenerationEndpoint
|
||
assert.ok(E && typeof E.joinEndpoint === 'function', 'src/generation-endpoint.js 应导出 IWBGenerationEndpoint')
|
||
|
||
const eq = (a, b, msg) => assert.strictEqual(JSON.stringify(a), JSON.stringify(b), msg)
|
||
|
||
// ── joinEndpoint ──
|
||
assert.strictEqual(E.joinEndpoint('https://a.test/v1', '/custom/gen'), 'https://a.test/v1/custom/gen', '常规拼接')
|
||
assert.strictEqual(E.joinEndpoint('https://a.test/v1/', 'custom/gen'), 'https://a.test/v1/custom/gen', 'base 尾斜杠 + 无首斜杠')
|
||
assert.strictEqual(E.joinEndpoint('https://a.test/v1', ''), 'https://a.test/v1/images/generations', '空路径回落默认')
|
||
assert.strictEqual(E.joinEndpoint('https://a.test/v1', 'https://b.test/api'), 'https://b.test/api', 'path 是绝对 URL 时原样返回')
|
||
assert.strictEqual(E.joinEndpoint('', '/x'), '/x', 'base 为空时至少产出路径')
|
||
|
||
// ── normalizeAuth ──
|
||
eq(E.normalizeAuth(null), { type: 'bearer' }, '缺省=Bearer(老配置行为不变)')
|
||
eq(E.normalizeAuth({ type: 'bogus' }), { type: 'bearer' }, '未知类型回落 Bearer')
|
||
eq(E.normalizeAuth({ type: 'HEADER', headerName: 'x-api-key' }), { type: 'header', headerName: 'x-api-key' }, 'type 大小写归一')
|
||
eq(E.normalizeAuth({ type: 'header' }), { type: 'header', headerName: 'x-api-key' }, 'header 名缺省')
|
||
eq(E.normalizeAuth({ type: 'query' }), { type: 'query', queryName: 'key' }, 'query 名缺省')
|
||
|
||
// ── buildAuthHeaders ──
|
||
eq(E.buildAuthHeaders(null, 'sk-1', { 'Content-Type': 'application/json' }), { 'Content-Type': 'application/json', Authorization: 'Bearer sk-1' }, 'Bearer 是缺省')
|
||
eq(E.buildAuthHeaders({ type: 'header', headerName: 'x-api-key' }, 'sk-2'), { 'x-api-key': 'sk-2' }, '自定义 header')
|
||
eq(E.buildAuthHeaders({ type: 'none' }, 'sk-3'), {}, '无鉴权 不写头')
|
||
eq(E.buildAuthHeaders({ type: 'bearer' }, ''), {}, '空 key 不写头(让服务端报 401,而不是发空 Bearer)')
|
||
|
||
// ── applyAuthQuery ──
|
||
assert.strictEqual(E.applyAuthQuery('https://a.test/x', { type: 'query', queryName: 'key' }, 'k 1'), 'https://a.test/x?key=k%201', '新增参数并编码')
|
||
assert.strictEqual(E.applyAuthQuery('https://a.test/x?key=old', { type: 'query', queryName: 'key' }, 'new'), 'https://a.test/x?key=new', '同名参数覆盖')
|
||
assert.strictEqual(E.applyAuthQuery('https://a.test/x?a=1', { type: 'query', queryName: 'key' }, 'k'), 'https://a.test/x?a=1&key=k', '已有其他参数时用 &')
|
||
assert.strictEqual(E.applyAuthQuery('https://a.test/x', { type: 'bearer' }, 'k'), 'https://a.test/x', '非 query 鉴权不改 URL')
|
||
|
||
// ── shouldRetryNextAddress:结果未知绝不重试 ──
|
||
for (const t of ['server', 'limit', 'network']) assert.strictEqual(E.shouldRetryNextAddress({ iwbType: t }), true, t + ' 应换地址')
|
||
for (const t of ['param', 'auth', 'permission', 'timeout', 'hard-timeout', 'cancel', 'unknown', undefined]) assert.strictEqual(E.shouldRetryNextAddress({ iwbType: t }), false, String(t) + ' 不应换地址(结果未知/换也没用)')
|
||
|
||
|
||
// ── 接线契约(负数断言):防止有人把端点/鉴权又写死回 app.js ──
|
||
// 背景:genPath/auth 曾经「配置里有、代码里没用」,只靠模块单测发现不了这种回归。
|
||
const appSrc = fs.readFileSync(path.join(__dirname, '..', 'app.js'), 'utf8')
|
||
assert.ok(appSrc.indexOf("baseUrl + '/images/generations'") < 0, 'app.js 不得再硬编码 /images/generations(应用 genPath)')
|
||
// 生图/分层两条路径此前是 `const headers = { Authorization: 'Bearer ' + key.value }`;
|
||
// LLM 与 /models 探测仍是 Bearer(本次不改),所以只钉「生图那行的旧形状」
|
||
assert.ok(!/const headers = \{ Authorization: 'Bearer ' \+ key\.value \}/.test(appSrc), '生图不得再硬编码 Bearer 鉴权(应用 headersOf)')
|
||
assert.ok(appSrc.indexOf('urlOf(curBase)') >= 0, '生图请求应经 urlOf() 拼地址')
|
||
assert.ok(appSrc.indexOf('headersOf(') >= 0, '生图请求头应经 headersOf() 生成')
|
||
assert.ok(appSrc.indexOf('addrs') >= 0, '应使用候补地址列表(fallbacks 生效)')
|
||
// ── runWithAddresses:换地址策略的端到端行为 ──
|
||
;(async () => {
|
||
const seen = []
|
||
const addrs = ['https://a.test/v1', 'https://b.test/v1']
|
||
let n = 0
|
||
const ok = await E.runWithAddresses(addrs, async () => { n++; if (n === 1) { const e = new Error('503'); e.iwbType = 'server'; throw e } return 'done' }, (b) => seen.push(b))
|
||
assert.strictEqual(ok, 'done', '第一次失败(5xx)后应换地址并成功')
|
||
assert.strictEqual(n, 2, '应尝试两次')
|
||
eq(seen, addrs, 'onBase 应按顺序告知当前地址')
|
||
|
||
let m = 0
|
||
await assert.rejects(async () => E.runWithAddresses(addrs, async () => { m++; const e = new Error('timeout'); e.iwbType = 'timeout'; throw e }), /timeout/)
|
||
assert.strictEqual(m, 1, '超时属结果未知 ⇒ 只试一次(防重复计费)')
|
||
|
||
let k = 0
|
||
const one = await E.runWithAddresses([], async () => { k++; return 'x' })
|
||
assert.strictEqual(one, 'x', '无候选地址时也执行一次(用空 base)')
|
||
assert.strictEqual(k, 1, '空列表不循环')
|
||
|
||
console.log('generation endpoint checks passed(端点拼接 / 鉴权四态 / 地址回退判据 / 地址循环)')
|
||
})().catch((e) => { console.error(e); process.exit(1) })
|