|
| 1 | +import { describe, expect, it, mock } from 'bun:test' |
| 2 | + |
| 3 | +import { |
| 4 | + createDeepSeekRequest, |
| 5 | + normalizeDeepSeekRequestBody, |
| 6 | +} from '../deepseek' |
| 7 | + |
| 8 | +import type { ChatCompletionRequestBody } from '../types' |
| 9 | + |
| 10 | +describe('normalizeDeepSeekRequestBody', () => { |
| 11 | + it('converts multimodal user content into DeepSeek text content without mutating input', () => { |
| 12 | + const body: ChatCompletionRequestBody = { |
| 13 | + model: 'deepseek/deepseek-v4-pro', |
| 14 | + messages: [ |
| 15 | + { |
| 16 | + role: 'user', |
| 17 | + content: [ |
| 18 | + { type: 'text', text: 'What is in this image?' }, |
| 19 | + { |
| 20 | + type: 'image_url', |
| 21 | + image_url: { url: 'data:image/png;base64,AAECAw==' }, |
| 22 | + }, |
| 23 | + ], |
| 24 | + }, |
| 25 | + ], |
| 26 | + } |
| 27 | + |
| 28 | + const normalized = normalizeDeepSeekRequestBody(body) |
| 29 | + |
| 30 | + expect(normalized.messages[0].content).toBe( |
| 31 | + 'What is in this image?\n\n[1 image was omitted because the DeepSeek API does not support image input.]', |
| 32 | + ) |
| 33 | + expect(body.messages[0].content).toEqual([ |
| 34 | + { type: 'text', text: 'What is in this image?' }, |
| 35 | + { |
| 36 | + type: 'image_url', |
| 37 | + image_url: { url: 'data:image/png;base64,AAECAw==' }, |
| 38 | + }, |
| 39 | + ]) |
| 40 | + }) |
| 41 | + |
| 42 | + it('keeps text-only messages unchanged', () => { |
| 43 | + const body: ChatCompletionRequestBody = { |
| 44 | + model: 'deepseek/deepseek-v4-pro', |
| 45 | + messages: [{ role: 'user', content: 'Hello' }], |
| 46 | + } |
| 47 | + |
| 48 | + expect(normalizeDeepSeekRequestBody(body)).toEqual({ |
| 49 | + ...body, |
| 50 | + model: 'deepseek-v4-pro', |
| 51 | + }) |
| 52 | + }) |
| 53 | +}) |
| 54 | + |
| 55 | +describe('createDeepSeekRequest', () => { |
| 56 | + it('sends DeepSeek-compatible text content when the request contains an image attachment', async () => { |
| 57 | + let sentBody: Record<string, unknown> | null = null |
| 58 | + const mockFetch = mock( |
| 59 | + async (_url: string | URL | Request, init?: RequestInit) => { |
| 60 | + sentBody = JSON.parse(init?.body as string) |
| 61 | + return new Response(JSON.stringify({ ok: true }), { status: 200 }) |
| 62 | + }, |
| 63 | + ) as unknown as typeof globalThis.fetch |
| 64 | + |
| 65 | + const body: ChatCompletionRequestBody = { |
| 66 | + model: 'deepseek/deepseek-v4-pro', |
| 67 | + messages: [ |
| 68 | + { role: 'system', content: 'You are a coding assistant.' }, |
| 69 | + { |
| 70 | + role: 'user', |
| 71 | + content: [ |
| 72 | + { type: 'text', text: 'Please inspect this screenshot.' }, |
| 73 | + { |
| 74 | + type: 'image_url', |
| 75 | + image_url: { url: 'data:image/jpeg;base64,/9j/4AAQSkZJRg==' }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + ], |
| 80 | + stream: true, |
| 81 | + reasoning: { enabled: true, effort: 'medium' }, |
| 82 | + provider: { order: ['DeepSeek'] }, |
| 83 | + transforms: ['middle-out'], |
| 84 | + codebuff_metadata: { run_id: 'run-1', cost_mode: 'free' }, |
| 85 | + usage: { include: true }, |
| 86 | + } |
| 87 | + |
| 88 | + await createDeepSeekRequest({ |
| 89 | + body, |
| 90 | + originalModel: body.model, |
| 91 | + fetch: mockFetch, |
| 92 | + }) |
| 93 | + |
| 94 | + expect(sentBody).toMatchObject({ |
| 95 | + model: 'deepseek-v4-pro', |
| 96 | + stream: true, |
| 97 | + stream_options: { include_usage: true }, |
| 98 | + thinking: { type: 'enabled', reasoning_effort: 'high' }, |
| 99 | + }) |
| 100 | + expect(sentBody).not.toHaveProperty('reasoning') |
| 101 | + expect(sentBody).not.toHaveProperty('provider') |
| 102 | + expect(sentBody).not.toHaveProperty('transforms') |
| 103 | + expect(sentBody).not.toHaveProperty('codebuff_metadata') |
| 104 | + expect(sentBody).not.toHaveProperty('usage') |
| 105 | + |
| 106 | + const capturedBody = sentBody as unknown as Record<string, unknown> |
| 107 | + const messages = capturedBody.messages as Array<{ content: string }> |
| 108 | + expect(messages[1].content).toBe( |
| 109 | + 'Please inspect this screenshot.\n\n[1 image was omitted because the DeepSeek API does not support image input.]', |
| 110 | + ) |
| 111 | + expect(JSON.stringify(sentBody)).not.toContain('image_url') |
| 112 | + expect(JSON.stringify(body)).toContain('image_url') |
| 113 | + }) |
| 114 | +}) |
0 commit comments