chatBridgeService.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { createServer, type Server } from 'node:http';
  2. import { afterEach, beforeEach, describe, expect, it } from 'vitest';
  3. import { ChatBridgeService } from './chatBridgeService';
  4. /** mock 上游:记录最近一次 /chat/completions 请求,按配置返回非流式或 SSE 流式 */
  5. let upstream: Server;
  6. let upstreamPort = 0;
  7. let lastUpstream: { url: string | null; auth: string | null; extra: string | null; body: Record<string, unknown> | null };
  8. let upstreamMode: 'json' | 'sse' | 'error' = 'json';
  9. const SSE_BODY = [
  10. `data: ${JSON.stringify({ choices: [{ delta: { content: '你好' }, finish_reason: null }] })}`,
  11. '',
  12. `data: ${JSON.stringify({ choices: [{ delta: { content: '世界' }, finish_reason: null }] })}`,
  13. '',
  14. `data: ${JSON.stringify({ choices: [{ delta: {}, finish_reason: 'stop' }], usage: { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 } })}`,
  15. '',
  16. 'data: [DONE]',
  17. '',
  18. '',
  19. ].join('\n');
  20. beforeEach(async () => {
  21. upstreamMode = 'json';
  22. lastUpstream = { url: null, auth: null, extra: null, body: null };
  23. upstream = createServer((req, res) => {
  24. const chunks: Buffer[] = [];
  25. req.on('data', (chunk: Buffer) => chunks.push(chunk));
  26. req.on('end', () => {
  27. lastUpstream = {
  28. url: req.url ?? null,
  29. auth: req.headers.authorization ?? null,
  30. extra: (req.headers['x-extra'] as string) ?? null,
  31. body: JSON.parse(Buffer.concat(chunks).toString('utf8') || 'null'),
  32. };
  33. if (upstreamMode === 'error') {
  34. res.writeHead(500, { 'content-type': 'application/json' });
  35. res.end(JSON.stringify({ error: 'boom' }));
  36. return;
  37. }
  38. if (upstreamMode === 'sse') {
  39. res.writeHead(200, { 'content-type': 'text/event-stream' });
  40. res.end(SSE_BODY);
  41. return;
  42. }
  43. res.writeHead(200, { 'content-type': 'application/json' });
  44. res.end(
  45. JSON.stringify({
  46. id: 'chatcmpl-1',
  47. created: 1700000000,
  48. model: 'qwen',
  49. choices: [{ finish_reason: 'stop', message: { role: 'assistant', content: '非流式回复' } }],
  50. usage: { prompt_tokens: 3, completion_tokens: 4, total_tokens: 7 },
  51. }),
  52. );
  53. });
  54. });
  55. await new Promise<void>((resolve) => upstream.listen(0, '127.0.0.1', resolve));
  56. const address = upstream.address();
  57. upstreamPort = typeof address === 'object' && address ? address.port : 0;
  58. });
  59. afterEach(async () => {
  60. await new Promise<void>((resolve) => upstream.close(() => resolve()));
  61. });
  62. function makeBridge(): Promise<ChatBridgeService> {
  63. return Promise.resolve(new ChatBridgeService());
  64. }
  65. describe('ChatBridgeService', () => {
  66. it('非流式:请求翻译后转发上游,响应翻译回 Responses 形状,头原样透传', async () => {
  67. const bridge = await makeBridge();
  68. const info = await bridge.start({
  69. upstreamBaseUrl: `http://127.0.0.1:${upstreamPort}/v1`,
  70. headers: { 'X-Extra': 'yes' },
  71. });
  72. try {
  73. const response = await fetch(`${info.url}/responses`, {
  74. method: 'POST',
  75. headers: { 'content-type': 'application/json', authorization: 'Bearer sk-live' },
  76. body: JSON.stringify({
  77. model: 'qwen',
  78. instructions: '你是助手',
  79. input: '你好',
  80. max_output_tokens: 64,
  81. }),
  82. });
  83. expect(response.status).toBe(200);
  84. // 上游收到的是 chat 形状
  85. expect(lastUpstream.url).toBe('/v1/chat/completions');
  86. expect(lastUpstream.auth).toBe('Bearer sk-live');
  87. expect(lastUpstream.extra).toBe('yes');
  88. expect(lastUpstream.body).toMatchObject({
  89. model: 'qwen',
  90. max_tokens: 64,
  91. messages: [
  92. { role: 'developer', content: '你是助手' },
  93. { role: 'user', content: '你好' },
  94. ],
  95. });
  96. // 下游拿到的是 responses 形状
  97. const body = (await response.json()) as Record<string, unknown>;
  98. expect(body).toMatchObject({
  99. object: 'response',
  100. status: 'completed',
  101. usage: { input_tokens: 3, output_tokens: 4, total_tokens: 7 },
  102. });
  103. const output = body.output as Array<Record<string, unknown>>;
  104. expect((output[0].content as Array<{ text: string }>)[0].text).toBe('非流式回复');
  105. } finally {
  106. await bridge.stop();
  107. }
  108. });
  109. it('流式:上游 SSE 逐事件翻译下发,completed 带 usage', async () => {
  110. upstreamMode = 'sse';
  111. const bridge = await makeBridge();
  112. const info = await bridge.start({ upstreamBaseUrl: `http://127.0.0.1:${upstreamPort}/v1` });
  113. try {
  114. const response = await fetch(`${info.url}/responses`, {
  115. method: 'POST',
  116. headers: { 'content-type': 'application/json' },
  117. body: JSON.stringify({ model: 'qwen', input: '你好', stream: true }),
  118. });
  119. expect(response.status).toBe(200);
  120. expect(response.headers.get('content-type')).toContain('text/event-stream');
  121. const text = await response.text();
  122. const events = text
  123. .split('\n\n')
  124. .filter((block) => block.trim())
  125. .map((block) => {
  126. const eventMatch = block.match(/^event: (.+)$/mu);
  127. const dataMatch = block.match(/^data: (.+)$/mu);
  128. return { event: eventMatch?.[1], data: JSON.parse(dataMatch?.[1] ?? '{}') as Record<string, unknown> };
  129. });
  130. const types = events.map((item) => item.event);
  131. expect(types[0]).toBe('response.created');
  132. expect(types.at(-1)).toBe('response.completed');
  133. // 请求侧补了 include_usage
  134. expect(lastUpstream.body?.stream_options).toEqual({ include_usage: true });
  135. const deltas = events
  136. .filter((item) => item.event === 'response.output_text.delta')
  137. .map((item) => item.data.delta);
  138. expect(deltas.join('')).toBe('你好世界');
  139. const completed = events.at(-1)?.data.response as Record<string, unknown>;
  140. expect(completed.usage).toEqual({ input_tokens: 5, output_tokens: 2, total_tokens: 7 });
  141. } finally {
  142. await bridge.stop();
  143. }
  144. });
  145. it('上游非 2xx:状态码与错误体透回', async () => {
  146. upstreamMode = 'error';
  147. const bridge = await makeBridge();
  148. const info = await bridge.start({ upstreamBaseUrl: `http://127.0.0.1:${upstreamPort}/v1` });
  149. try {
  150. const response = await fetch(`${info.url}/responses`, {
  151. method: 'POST',
  152. headers: { 'content-type': 'application/json' },
  153. body: JSON.stringify({ model: 'qwen', input: 'x' }),
  154. });
  155. expect(response.status).toBe(500);
  156. const body = (await response.json()) as { error: { message: string } };
  157. expect(body.error.message).toContain('500');
  158. } finally {
  159. await bridge.stop();
  160. }
  161. });
  162. it('只接 POST /v1/responses;stop 后端口释放', async () => {
  163. const bridge = await makeBridge();
  164. const info = await bridge.start({ upstreamBaseUrl: `http://127.0.0.1:${upstreamPort}/v1` });
  165. const wrong = await fetch(`${info.url}/models`);
  166. expect(wrong.status).toBe(404);
  167. await bridge.stop();
  168. await expect(fetch(`${info.url}/responses`, { method: 'POST' })).rejects.toThrow();
  169. });
  170. });