providerService.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { createServer, type Server } from 'node:http';
  2. import { mkdtempSync } from 'node:fs';
  3. import { tmpdir } from 'node:os';
  4. import { join } from 'node:path';
  5. import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
  6. import type { CodexProviderSpec, CodexRuntime } from './codexRuntime';
  7. import {
  8. applyProvider,
  9. baseUrlHint,
  10. describeDroppedKeys,
  11. normalizeBaseUrl,
  12. probeEndpoint,
  13. toProviderSpec,
  14. } from './providerService';
  15. import type { ApplyProviderInput } from './types';
  16. /** applyProvider 会往 getCodexDataDir() 落 provider.json;挪到临时目录,绝不碰客户端真实数据目录 */
  17. vi.mock('./codexHome', async (importOriginal) => {
  18. const actual = await importOriginal<typeof import('./codexHome')>();
  19. // 目录只能算一次:providerFile() 每次都会调 getCodexDataDir(),给出不同路径就写不进同一处
  20. const dir = mkdtempSync(join(tmpdir(), 'zsjz-provider-test-'));
  21. return { ...actual, getCodexDataDir: () => dir };
  22. });
  23. let server: Server;
  24. let port = 0;
  25. let responsesStatus = 404;
  26. let chatStatus = 404;
  27. let lastRequest: { url: string | null; auth: string | null; body: string | null } = { url: null, auth: null, body: null };
  28. beforeAll(async () => {
  29. server = createServer((req, res) => {
  30. const chunks: Buffer[] = [];
  31. req.on('data', (chunk: Buffer) => chunks.push(chunk));
  32. req.on('end', () => {
  33. lastRequest = {
  34. url: req.url ?? null,
  35. auth: req.headers.authorization ?? null,
  36. body: Buffer.concat(chunks).toString('utf8') || null,
  37. };
  38. res.setHeader('content-type', 'application/json');
  39. const status = req.url?.endsWith('/responses')
  40. ? responsesStatus
  41. : req.url?.endsWith('/chat/completions')
  42. ? chatStatus
  43. : 404;
  44. res.writeHead(status).end(JSON.stringify({ status }));
  45. });
  46. });
  47. await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  48. const address = server.address();
  49. port = typeof address === 'object' && address ? address.port : 0;
  50. });
  51. afterAll(async () => {
  52. await new Promise<void>((resolve) => server.close(() => resolve()));
  53. });
  54. const openaiLike: ApplyProviderInput = {
  55. modelId: 'qwen3-max',
  56. name: '通义千问',
  57. baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1/',
  58. apiKey: 'sk-test-key',
  59. headersJson: JSON.stringify({ 'X-DashScope-WorkSpace': 'ws-1' }),
  60. config: JSON.stringify({ request_max_retries: 3, temperature: 0.2 }),
  61. };
  62. describe('normalizeBaseUrl / baseUrlHint', () => {
  63. it('去掉尾部斜杠,空值返回 null', () => {
  64. expect(normalizeBaseUrl('https://x.com/v1///')).toBe('https://x.com/v1');
  65. expect(normalizeBaseUrl(' ')).toBeNull();
  66. expect(normalizeBaseUrl(null)).toBeNull();
  67. });
  68. it('缺版本段时给非阻断提示', () => {
  69. expect(baseUrlHint('https://x.com')).toMatch(/v1/);
  70. expect(baseUrlHint('https://x.com/v1')).toBeNull();
  71. expect(baseUrlHint('ftp://x.com/v1')).toMatch(/http/);
  72. expect(baseUrlHint(null)).toMatch(/未配置/);
  73. });
  74. });
  75. describe('toProviderSpec', () => {
  76. it('OpenAI 兼容端点:写 model_providers.zsjz,key 只留给 env', () => {
  77. const spec = toProviderSpec(openaiLike);
  78. expect(spec).toMatchObject({
  79. id: 'zsjz',
  80. model: 'qwen3-max',
  81. baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
  82. apiKey: 'sk-test-key',
  83. envKey: 'ZSJZ_CODEX_API_KEY',
  84. });
  85. expect(spec.httpHeaders).toEqual({ 'X-DashScope-WorkSpace': 'ws-1' });
  86. // config 里只放行白名单键,temperature 必须被丢掉
  87. expect(spec.extra).toMatchObject({ request_max_retries: 3 });
  88. expect(spec.extra).not.toHaveProperty('temperature');
  89. });
  90. it('地址原样使用,尾部斜杠规整;没配 key 就不写 env_key', () => {
  91. const spec = toProviderSpec({ modelId: 'qwen', baseUrl: 'http://10.66.66.66:8080/v1/' });
  92. expect(spec).toMatchObject({ id: 'zsjz', model: 'qwen', baseUrl: 'http://10.66.66.66:8080/v1' });
  93. expect(spec.apiKey ?? null).toBeNull();
  94. });
  95. it('空闲窗口必须显著大于 Codex 默认的 300 秒,且不写等于默认值的无效项', () => {
  96. const spec = toProviderSpec({ modelId: 'm', baseUrl: 'http://10.66.66.66:8080/v1' });
  97. expect(spec.extra).toMatchObject({
  98. request_max_retries: 0,
  99. stream_max_retries: 0,
  100. // Codex 默认 300_000(model-provider-info/src/lib.rs:29);写 300_000 等于没写
  101. stream_idle_timeout_ms: 1_800_000,
  102. });
  103. expect(spec.extra).toBeTypeOf('object');
  104. // supports_websockets 的 serde 默认就是 false,用 -c 声明的 provider 根本不走 WS,
  105. // 写进默认值只会让下一位误以为这里防住了什么(15 秒那条说法来自 websocket 握手超时)
  106. expect(spec.extra).not.toHaveProperty('supports_websockets');
  107. });
  108. it('模型管理 config 里显式写的值优先于默认', () => {
  109. const spec = toProviderSpec({
  110. modelId: 'm',
  111. baseUrl: 'http://x/v1',
  112. config: { stream_idle_timeout_ms: 5_000, stream_max_retries: 3 },
  113. });
  114. expect(spec.extra).toMatchObject({
  115. stream_idle_timeout_ms: 5_000,
  116. stream_max_retries: 3,
  117. request_max_retries: 0,
  118. });
  119. });
  120. it('缺 modelId 或缺 baseUrl 直接报错,不做任何地址兜底', () => {
  121. expect(() => toProviderSpec({ modelId: ' ' })).toThrow(/modelId/);
  122. expect(() => toProviderSpec({ modelId: 'gpt' })).toThrow(/base_url/);
  123. expect(() => toProviderSpec({ modelId: 'gpt', baseUrl: ' ' })).toThrow(/base_url/);
  124. });
  125. it('headersJson 与 config 支持对象或 JSON 字符串,非法 JSON 忽略', () => {
  126. expect(toProviderSpec({ ...openaiLike, headersJson: { A: '1' } }).httpHeaders).toEqual({ A: '1' });
  127. expect(toProviderSpec({ ...openaiLike, headersJson: '{bad json' }).httpHeaders).toBeNull();
  128. expect(toProviderSpec({ ...openaiLike, config: { stream_max_retries: 5 } }).extra).toMatchObject({
  129. stream_max_retries: 5,
  130. });
  131. });
  132. it('describeDroppedKeys 报告被丢弃的非白名单键', () => {
  133. expect(describeDroppedKeys(openaiLike)).toEqual(['temperature']);
  134. expect(describeDroppedKeys({ modelId: 'x' })).toEqual([]);
  135. });
  136. });
  137. describe('probeEndpoint', () => {
  138. const base = () => `http://127.0.0.1:${port}/v1`;
  139. it('只探 Responses 路由存在性,请求体不带 input(不能触发推理)', async () => {
  140. responsesStatus = 400;
  141. const result = await probeEndpoint({
  142. baseUrl: `${base()}/`,
  143. modelId: 'qwen3-max',
  144. apiKey: 'sk-test-key',
  145. });
  146. expect(result.protocol).toBe('responses');
  147. expect(lastRequest.url).toBe('/v1/responses');
  148. expect(lastRequest.auth).toBe('Bearer sk-test-key');
  149. const body = JSON.parse(lastRequest.body ?? '{}') as Record<string, unknown>;
  150. expect(body.model).toBe('qwen3-max');
  151. // 一旦带上 input,单槽本地服务就会真的开始生成,实测一次要 14 秒
  152. expect(body.input).toBeUndefined();
  153. });
  154. it('2xx 与 4xx 只要有路由就判 responses 可用', async () => {
  155. for (const status of [200, 400, 401]) {
  156. responsesStatus = status;
  157. const result = await probeEndpoint({ baseUrl: base(), modelId: 'x' });
  158. expect(result).toMatchObject({ protocol: 'responses', responsesStatus: status });
  159. }
  160. });
  161. it('没有 Responses 但有 Chat → chat-only,并说明本客户端不做桥接', async () => {
  162. responsesStatus = 404;
  163. chatStatus = 400;
  164. const result = await probeEndpoint({ baseUrl: base(), modelId: 'x' });
  165. expect(result.protocol).toBe('chat-only');
  166. expect(result.detail).toMatch(/没有 \/responses/);
  167. expect(result.detail).toMatch(/不做桥接/);
  168. });
  169. it('两条路由都没有 → unsupported', async () => {
  170. responsesStatus = 404;
  171. chatStatus = 404;
  172. const result = await probeEndpoint({ baseUrl: base(), modelId: 'x' });
  173. expect(result.protocol).toBe('unsupported');
  174. });
  175. it('端点不可达时报错带上是哪个地址', async () => {
  176. const result = await probeEndpoint({ baseUrl: 'http://127.0.0.1:1/v1', modelId: 'x' });
  177. expect(result.protocol).toBe('unreachable');
  178. expect(result.responsesStatus).toBeNull();
  179. expect(result.detail).toMatch(/不可达/);
  180. expect(result.detail).toContain('http://127.0.0.1:1/v1/responses');
  181. });
  182. });
  183. /**
  184. * 客户端真正走的是 applyProvider:探端点 → 把 spec 交给运行时重启子进程。
  185. * 这里用假运行时把「交给 Codex 的最终参数」钉死 —— 真机 smoke 用的是手搓 spec,覆盖不到这一段。
  186. */
  187. describe('applyProvider 交给运行时的 spec', () => {
  188. class StubRuntime {
  189. spec: CodexProviderSpec | null | undefined;
  190. calls = 0;
  191. async applyProvider(spec: CodexProviderSpec | null): Promise<void> {
  192. this.calls += 1;
  193. this.spec = spec;
  194. }
  195. }
  196. it('Codex 直连上游真实地址,并带上有效的重试/空闲窗口参数', async () => {
  197. responsesStatus = 400;
  198. const stub = new StubRuntime();
  199. const upstream = `http://127.0.0.1:${port}/v1`;
  200. const applied = await applyProvider(stub as unknown as CodexRuntime, {
  201. modelId: 'qwen',
  202. name: '自部署 Qwen',
  203. baseUrl: upstream,
  204. modelRecordId: 7,
  205. });
  206. const spec = stub.spec;
  207. expect(spec).not.toBeNull();
  208. expect(spec?.id).toBe('zsjz');
  209. expect(spec?.model).toBe('qwen');
  210. // 不做任何翻译:Codex 连的就是模型记录里那个地址
  211. expect(spec?.baseUrl).toBe(upstream);
  212. expect(spec?.extra).toMatchObject({
  213. request_max_retries: 0,
  214. stream_max_retries: 0,
  215. stream_idle_timeout_ms: 1_800_000,
  216. });
  217. expect(applied).toMatchObject({ model: 'qwen', modelRecordId: '7', baseUrl: upstream });
  218. expect(applied).not.toHaveProperty('bridged');
  219. });
  220. it('端点只会 Chat 时直接报错,且不碰运行时', async () => {
  221. responsesStatus = 404;
  222. chatStatus = 400;
  223. const stub = new StubRuntime();
  224. await expect(
  225. applyProvider(stub as unknown as CodexRuntime, { modelId: 'qwen', baseUrl: `http://127.0.0.1:${port}/v1` }),
  226. ).rejects.toThrow(/没有 \/responses/);
  227. expect(stub.calls).toBe(0);
  228. expect(stub.spec).toBeUndefined();
  229. });
  230. });