| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- import { createServer, type Server } from 'node:http';
- import { mkdtempSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { join } from 'node:path';
- import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
- import { chatBridge } from './chatBridgeService';
- import type { CodexProviderSpec, CodexRuntime } from './codexRuntime';
- import {
- applyProvider,
- baseUrlHint,
- describeDroppedKeys,
- normalizeBaseUrl,
- probeEndpoint,
- toProviderSpec,
- } from './providerService';
- import type { ApplyProviderInput } from './types';
- /** applyProvider 会往 getCodexDataDir() 落 provider.json;挪到临时目录,绝不碰客户端真实数据目录 */
- vi.mock('./codexHome', async (importOriginal) => {
- const actual = await importOriginal<typeof import('./codexHome')>();
- // 目录只能算一次:providerFile() 每次都会调 getCodexDataDir(),给出不同路径就写不进同一处
- const dir = mkdtempSync(join(tmpdir(), 'zsjz-provider-test-'));
- return { ...actual, getCodexDataDir: () => dir };
- });
- let server: Server;
- let port = 0;
- let chatStatus = 404;
- let lastRequest: { url: string | null; auth: string | null; body: string | null } = { url: null, auth: null, body: null };
- beforeAll(async () => {
- server = createServer((req, res) => {
- const chunks: Buffer[] = [];
- req.on('data', (chunk: Buffer) => chunks.push(chunk));
- req.on('end', () => {
- lastRequest = {
- url: req.url ?? null,
- auth: req.headers.authorization ?? null,
- body: Buffer.concat(chunks).toString('utf8') || null,
- };
- res.setHeader('content-type', 'application/json');
- const status = req.url?.endsWith('/chat/completions') ? chatStatus : 404;
- res.writeHead(status).end(JSON.stringify({ status }));
- });
- });
- await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
- const address = server.address();
- port = typeof address === 'object' && address ? address.port : 0;
- });
- afterAll(async () => {
- await new Promise<void>((resolve) => server.close(() => resolve()));
- });
- const openaiLike: ApplyProviderInput = {
- modelId: 'qwen3-max',
- name: '通义千问',
- baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1/',
- apiKey: 'sk-test-key',
- headersJson: JSON.stringify({ 'X-DashScope-WorkSpace': 'ws-1' }),
- config: JSON.stringify({ request_max_retries: 3, temperature: 0.2 }),
- };
- describe('normalizeBaseUrl / baseUrlHint', () => {
- it('去掉尾部斜杠,空值返回 null', () => {
- expect(normalizeBaseUrl('https://x.com/v1///')).toBe('https://x.com/v1');
- expect(normalizeBaseUrl(' ')).toBeNull();
- expect(normalizeBaseUrl(null)).toBeNull();
- });
- it('缺版本段时给非阻断提示', () => {
- expect(baseUrlHint('https://x.com')).toMatch(/v1/);
- expect(baseUrlHint('https://x.com/v1')).toBeNull();
- expect(baseUrlHint('ftp://x.com/v1')).toMatch(/http/);
- expect(baseUrlHint(null)).toMatch(/未配置/);
- });
- });
- describe('toProviderSpec', () => {
- it('OpenAI 兼容端点:写 model_providers.zsjz,key 只留给 env', () => {
- const spec = toProviderSpec(openaiLike);
- expect(spec).toMatchObject({
- id: 'zsjz',
- model: 'qwen3-max',
- baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
- apiKey: 'sk-test-key',
- envKey: 'ZSJZ_CODEX_API_KEY',
- });
- expect(spec.httpHeaders).toEqual({ 'X-DashScope-WorkSpace': 'ws-1' });
- // config 里只放行白名单键,temperature 必须被丢掉
- expect(spec.extra).toMatchObject({ request_max_retries: 3 });
- expect(spec.extra).not.toHaveProperty('temperature');
- });
- it('地址原样使用,尾部斜杠规整;没配 key 就不写 env_key', () => {
- const spec = toProviderSpec({ modelId: 'qwen', baseUrl: 'http://10.66.66.66:8080/v1/' });
- expect(spec).toMatchObject({ id: 'zsjz', model: 'qwen', baseUrl: 'http://10.66.66.66:8080/v1' });
- expect(spec.apiKey ?? null).toBeNull();
- });
- it('自部署端点默认关掉重试放大、放宽空闲超时、不走 websocket', () => {
- const spec = toProviderSpec({ modelId: 'm', baseUrl: 'http://10.66.66.66:8080/v1' });
- // 单槽推理上一次 prefill 要几分钟,Codex 默认 5 次重试 = 同样几分钟的活儿重复五遍
- expect(spec.extra).toMatchObject({
- request_max_retries: 0,
- stream_max_retries: 0,
- stream_idle_timeout_ms: 300_000,
- supports_websockets: false,
- });
- });
- it('模型管理 config 里显式写的值优先于默认', () => {
- const spec = toProviderSpec({
- modelId: 'm',
- baseUrl: 'http://x/v1',
- config: { stream_idle_timeout_ms: 5_000, stream_max_retries: 3 },
- });
- expect(spec.extra).toMatchObject({
- stream_idle_timeout_ms: 5_000,
- stream_max_retries: 3,
- request_max_retries: 0,
- });
- });
- it('缺 modelId 或缺 baseUrl 直接报错,不做任何地址兜底', () => {
- expect(() => toProviderSpec({ modelId: ' ' })).toThrow(/modelId/);
- expect(() => toProviderSpec({ modelId: 'gpt' })).toThrow(/base_url/);
- expect(() => toProviderSpec({ modelId: 'gpt', baseUrl: ' ' })).toThrow(/base_url/);
- });
- it('headersJson 与 config 支持对象或 JSON 字符串,非法 JSON 忽略', () => {
- expect(toProviderSpec({ ...openaiLike, headersJson: { A: '1' } }).httpHeaders).toEqual({ A: '1' });
- expect(toProviderSpec({ ...openaiLike, headersJson: '{bad json' }).httpHeaders).toBeNull();
- expect(toProviderSpec({ ...openaiLike, config: { stream_max_retries: 5 } }).extra).toMatchObject({
- stream_max_retries: 5,
- });
- });
- it('describeDroppedKeys 报告被丢弃的非白名单键', () => {
- expect(describeDroppedKeys(openaiLike)).toEqual(['temperature']);
- expect(describeDroppedKeys({ modelId: 'x' })).toEqual([]);
- });
- });
- describe('probeEndpoint', () => {
- const base = () => `http://127.0.0.1:${port}/v1`;
- it('只探 Chat 路由存在性,请求体不带 messages(不能触发推理)', async () => {
- chatStatus = 400;
- const result = await probeEndpoint({
- baseUrl: `${base()}/`,
- modelId: 'qwen3-max',
- apiKey: 'sk-test-key',
- });
- expect(result.protocol).toBe('chat');
- expect(lastRequest.url).toBe('/v1/chat/completions');
- expect(lastRequest.auth).toBe('Bearer sk-test-key');
- const body = JSON.parse(lastRequest.body ?? '{}') as Record<string, unknown>;
- expect(body.model).toBe('qwen3-max');
- // 一旦带上 messages,单槽本地服务就会真的开始生成,实测一次要 14 秒
- expect(body.messages).toBeUndefined();
- });
- it('2xx 与 4xx 只要有路由就判 chat 可用', async () => {
- for (const status of [200, 400, 401]) {
- chatStatus = status;
- const result = await probeEndpoint({ baseUrl: base(), modelId: 'x' });
- expect(result).toMatchObject({ protocol: 'chat', chatStatus: status });
- }
- });
- it('没有 Chat 路由判 unsupported', async () => {
- chatStatus = 404;
- const result = await probeEndpoint({ baseUrl: base(), modelId: 'x' });
- expect(result.protocol).toBe('unsupported');
- expect(result.detail).toMatch(/没有 \/chat\/completions/);
- });
- it('端点不可达时报错带上是哪个地址', async () => {
- const result = await probeEndpoint({ baseUrl: 'http://127.0.0.1:1/v1', modelId: 'x' });
- expect(result.protocol).toBe('unreachable');
- expect(result.chatStatus).toBeNull();
- expect(result.detail).toMatch(/不可达/);
- expect(result.detail).toContain('http://127.0.0.1:1/v1/chat/completions');
- });
- });
- /**
- * 客户端真正走的是 applyProvider:探测 → 起桥接 → 把 spec 交给运行时重启子进程。
- * 这里用假运行时把「交给 Codex 的最终参数」钉死 —— 真机 smoke 用的是手搓 spec,覆盖不到这一段。
- */
- describe('applyProvider 交给运行时的 spec', () => {
- class StubRuntime {
- spec: CodexProviderSpec | null | undefined;
- calls = 0;
- async applyProvider(spec: CodexProviderSpec | null): Promise<void> {
- this.calls += 1;
- this.spec = spec;
- }
- }
- it('一律自定义 provider + 桥接地址,并带上单槽端点该有的重试/超时开关', async () => {
- chatStatus = 200;
- const stub = new StubRuntime();
- const upstream = `http://127.0.0.1:${port}/v1`;
- const applied = await applyProvider(stub as unknown as CodexRuntime, {
- modelId: 'qwen',
- name: '自部署 Qwen',
- baseUrl: upstream,
- modelRecordId: 7,
- });
- try {
- const spec = stub.spec;
- expect(spec).not.toBeNull();
- expect(spec?.id).toBe('zsjz');
- expect(spec?.model).toBe('qwen');
- // Codex 连的是本地桥接,不是上游
- expect(spec?.baseUrl).toBe(chatBridge.info?.url);
- expect(spec?.baseUrl).not.toBe(upstream);
- expect(spec?.extra).toMatchObject({
- request_max_retries: 0,
- stream_max_retries: 0,
- stream_idle_timeout_ms: 300_000,
- supports_websockets: false,
- });
- // 落盘的是上游真实地址,便于页面展示
- expect(applied).toMatchObject({ model: 'qwen', modelRecordId: '7', baseUrl: upstream, bridged: true });
- } finally {
- await chatBridge.stop();
- }
- });
- it('上游没有 Chat 路由时直接报错,且不碰运行时', async () => {
- chatStatus = 404;
- const stub = new StubRuntime();
- await expect(
- applyProvider(stub as unknown as CodexRuntime, { modelId: 'qwen', baseUrl: `http://127.0.0.1:${port}/v1` }),
- ).rejects.toThrow(/没有 \/chat\/completions/);
- expect(stub.calls).toBe(0);
- expect(stub.spec).toBeUndefined();
- });
- });
|