import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; import { Hono } from 'hono'; import { createServer } from '../../src/server.js'; import { Logger } from '../../src/logger.js'; import { DetailLogger } from '../../src/detail-logger.js'; import type { ProviderConfig, ProxyConfig } from '../../src/config.js'; import { tmpdir } from 'os'; import { join } from 'path'; // Mock 上游 API 响应 const createMockAnthropicStreamResponse = (text: string) => { const data = [ `data: {"type":"message_start","message":{"id":"msg_123","type":"message","role":"assistant","model":"claude-3-5-sonnet-20241022","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":10,"output_tokens":0}}}`, `data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}`, ...text.split(' ').map((word, i) => `data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"${word}${i === text.split(' ').length - 1 ? '' : ' '}"}}` ), `data: {"type":"content_block_stop","index":0}`, `data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":${text.split(' ').length}}}`, `data: {"type":"message_stop"}` ].join('\n'); return new Response(data, { status: 200, headers: { 'Content-Type': 'text/event-stream' } }); }; const createMockOpenAINonStreamResponse = (text: string) => { return new Response(JSON.stringify({ id: 'chatcmpl-123', object: 'chat.completion', created: Math.floor(Date.now() / 1000), model: 'gpt-4', choices: [{ index: 0, message: { role: 'assistant', content: text }, finish_reason: 'stop' }], usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } }), { status: 200, headers: { 'Content-Type': 'application/json' } }); }; describe('proxy e2e', () => { let app: Hono; let testLogDir: string; let originalFetch: typeof fetch; beforeAll(() => { testLogDir = join(tmpdir(), 'test-e2e-logs-' + Date.now()); const logger = new Logger(testLogDir); const detailLogger = new DetailLogger(testLogDir); // 创建测试模型配置 - 使用不存在的 provider 来避免真实调用 const testModels: ProviderConfig[] = [ { customModel: 'test-openai', realModel: 'gpt-4', apiKey: 'sk-test-openai-key', baseUrl: 'https://api.openai.com/v1', provider: 'openai' }, { customModel: 'test-anthropic', realModel: 'claude-3-5-sonnet-20241022', apiKey: 'sk-ant-test-key', baseUrl: 'https://api.anthropic.com/v1', provider: 'anthropic' } ]; // 创建测试 ProxyConfig 对象 const testConfig: ProxyConfig = { models: testModels }; app = createServer(testConfig, logger, detailLogger, 30000); // 保存原始 fetch originalFetch = globalThis.fetch; }); afterAll(() => { // 恢复原始 fetch globalThis.fetch = originalFetch; }); describe('OpenAI passthrough (non-stream)', () => { it('should pass through OpenAI non-stream response', async () => { // Mock fetch globalThis.fetch = vi.fn().mockResolvedValue(createMockOpenAINonStreamResponse('Hello from OpenAI')); const response = await app.request('/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer test-openai' }, body: JSON.stringify({ model: 'test-openai', messages: [{ role: 'user', content: 'Say hello' }], stream: false }) }); expect(response.status).toBe(200); const json = await response.json() as any; expect(json.choices[0].message.content).toBe('Hello from OpenAI'); expect(json.usage).toBeDefined(); }); }); describe('OpenAI to Anthropic conversion (non-stream)', () => { it('should convert OpenAI non-stream request to Anthropic and return OpenAI format', async () => { // Mock Anthropic API 返回非流式响应 const anthropicResponse = new Response(JSON.stringify({ id: 'msg_123', type: 'message', role: 'assistant', model: 'claude-3-5-sonnet-20241022', content: [{ type: 'text', text: 'Hello from Anthropic non-stream' }], stop_reason: 'end_turn', stop_sequence: null, usage: { input_tokens: 10, output_tokens: 5 } }), { status: 200, headers: { 'Content-Type': 'application/json' } }); globalThis.fetch = vi.fn().mockResolvedValue(anthropicResponse); const response = await app.request('/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer test-anthropic' }, body: JSON.stringify({ model: 'test-anthropic', messages: [{ role: 'user', content: 'Say hello' }], stream: false }) }); expect(response.status).toBe(200); const json = await response.json() as any; expect(json.choices[0].message.content).toBe('Hello from Anthropic non-stream'); expect(json.usage).toBeDefined(); }); }); describe('Error handling', () => { it('should pass through error response from upstream', async () => { // Mock 上游返回 401 错误 globalThis.fetch = vi.fn().mockResolvedValue( new Response(JSON.stringify({ error: { message: 'Invalid API key' } }), { status: 401, headers: { 'Content-Type': 'application/json' } }) ); const response = await app.request('/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer invalid-key' }, body: JSON.stringify({ model: 'test-openai', messages: [{ role: 'user', content: 'Hello' }] }) }); // 当前实现:透传响应体但可能不保留状态码 // 这是一个已知的潜在问题 const json = await response.json() as any; expect(json.error).toBeDefined(); expect(json.error.message).toBe('Invalid API key'); }); it('should return 404 for unknown model', async () => { const response = await app.request('/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'unknown-model', messages: [{ role: 'user', content: 'Hello' }] }) }); expect(response.status).toBe(404); const json = await response.json() as any; expect(json.error.message).toBe('Model not found'); }); }); describe('Request conversion', () => { it('should convert OpenAI request to Anthropic format', async () => { let capturedUrl: string | null = null; globalThis.fetch = vi.fn().mockImplementation((url: string, options?: any) => { capturedUrl = url.toString(); return createMockAnthropicStreamResponse('Test response'); }); await app.request('/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer test-anthropic' }, body: JSON.stringify({ model: 'test-anthropic', messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Hello' } ], stream: true }) }); // 验证请求被发送到了正确的 URL expect(capturedUrl).not.toBeNull(); expect(capturedUrl).toContain('anthropic.com'); }); }); });