|
| 1 | +// Live integration tests against real api.deepseek.com. |
| 2 | +// Skipped automatically unless DEEPSEEK_API_KEY (or stored credentials) is available. |
| 3 | +// |
| 4 | +// These were used in fact to validate M1's mock-based unit tests against real |
| 5 | +// wire behaviour 2026-05-28 — they confirmed: |
| 6 | +// · text streaming chunk shape matches our mock |
| 7 | +// · tool_calls streaming with incremental arguments accumulation matches our mock |
| 8 | +// · reasoning_content streaming on deepseek-reasoner is captured into thinking blocks |
| 9 | +// · /v1/models returns deepseek-v4-flash + deepseek-v4-pro; deepseek-chat / |
| 10 | +// deepseek-reasoner are stable aliases (still accepted at the API layer) |
| 11 | +// |
| 12 | +// To run: DEEPSEEK_API_KEY=sk-... pnpm --filter @deepcode/core test live |
| 13 | +// Or: place a key in ~/.deepcode/credentials.json (the CLI does this on onboard). |
| 14 | + |
| 15 | +import { promises as fs } from 'node:fs'; |
| 16 | +import { homedir } from 'node:os'; |
| 17 | +import { join } from 'node:path'; |
| 18 | +import { describe, expect, it } from 'vitest'; |
| 19 | +import { DeepSeekProvider } from './deepseek.js'; |
| 20 | + |
| 21 | +async function resolveTestKey(): Promise<string | null> { |
| 22 | + if (process.env.DEEPSEEK_API_KEY) return process.env.DEEPSEEK_API_KEY; |
| 23 | + try { |
| 24 | + const raw = await fs.readFile(join(homedir(), '.deepcode', 'credentials.json'), 'utf8'); |
| 25 | + const parsed = JSON.parse(raw) as { apiKey?: string }; |
| 26 | + return parsed.apiKey ?? null; |
| 27 | + } catch { |
| 28 | + return null; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Live tests cost real API tokens. They only run when DEEPCODE_LIVE_TESTS=1 is set, |
| 33 | +// even if credentials are available locally — protects against accidental burns |
| 34 | +// on every `pnpm test`. |
| 35 | +const enabled = process.env.DEEPCODE_LIVE_TESTS === '1'; |
| 36 | +const apiKey = enabled ? await resolveTestKey() : null; |
| 37 | +const live = enabled && apiKey ? describe : describe.skip; |
| 38 | + |
| 39 | +live('DeepSeekProvider — live API', () => { |
| 40 | + it('streams text deltas from deepseek-chat', async () => { |
| 41 | + const p = new DeepSeekProvider({ apiKey: apiKey! }); |
| 42 | + const out: string[] = []; |
| 43 | + const result = await p.runTurn({ |
| 44 | + model: 'deepseek-chat', |
| 45 | + systemPrompt: 'Reply only with: ok', |
| 46 | + tools: [], |
| 47 | + messages: [{ role: 'user', content: [{ type: 'text', text: 'Ready?' }] }], |
| 48 | + maxTokens: 10, |
| 49 | + handlers: { onTextDelta: (t) => out.push(t) }, |
| 50 | + }); |
| 51 | + expect(out.join('').length).toBeGreaterThan(0); |
| 52 | + expect(result.stopReason).toBe('end_turn'); |
| 53 | + expect(result.content.find((b) => b.type === 'text')).toBeDefined(); |
| 54 | + expect(result.usage.inputTokens).toBeGreaterThan(0); |
| 55 | + expect(result.usage.outputTokens).toBeGreaterThan(0); |
| 56 | + }, 30_000); |
| 57 | + |
| 58 | + it('emits tool_use block when the model invokes a tool', async () => { |
| 59 | + const p = new DeepSeekProvider({ apiKey: apiKey! }); |
| 60 | + const result = await p.runTurn({ |
| 61 | + model: 'deepseek-chat', |
| 62 | + systemPrompt: 'You must use the Echo tool when asked.', |
| 63 | + tools: [ |
| 64 | + { |
| 65 | + name: 'Echo', |
| 66 | + description: 'Echo back the input text.', |
| 67 | + inputSchema: { |
| 68 | + type: 'object', |
| 69 | + properties: { text: { type: 'string' } }, |
| 70 | + required: ['text'], |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + messages: [ |
| 75 | + { |
| 76 | + role: 'user', |
| 77 | + content: [{ type: 'text', text: 'Call the Echo tool with text "hello".' }], |
| 78 | + }, |
| 79 | + ], |
| 80 | + maxTokens: 100, |
| 81 | + }); |
| 82 | + const toolUse = result.content.find((b) => b.type === 'tool_use'); |
| 83 | + expect(toolUse).toBeDefined(); |
| 84 | + if (toolUse?.type === 'tool_use') { |
| 85 | + expect(toolUse.name).toBe('Echo'); |
| 86 | + expect(toolUse.input).toMatchObject({ text: expect.any(String) }); |
| 87 | + expect(toolUse.id).toMatch(/call_/); |
| 88 | + } |
| 89 | + expect(result.stopReason).toBe('tool_use'); |
| 90 | + }, 30_000); |
| 91 | + |
| 92 | + it('captures reasoning_content into thinking blocks for deepseek-reasoner', async () => { |
| 93 | + const p = new DeepSeekProvider({ apiKey: apiKey! }); |
| 94 | + let thinkingChunks = 0; |
| 95 | + const result = await p.runTurn({ |
| 96 | + model: 'deepseek-reasoner', |
| 97 | + systemPrompt: 'Solve briefly. Show one line of reasoning.', |
| 98 | + tools: [], |
| 99 | + messages: [ |
| 100 | + { |
| 101 | + role: 'user', |
| 102 | + content: [{ type: 'text', text: 'What is 17 * 23? Just the number.' }], |
| 103 | + }, |
| 104 | + ], |
| 105 | + maxTokens: 400, |
| 106 | + handlers: { |
| 107 | + onThinkingDelta: () => { |
| 108 | + thinkingChunks++; |
| 109 | + }, |
| 110 | + }, |
| 111 | + }); |
| 112 | + // reasoner should stream reasoning_content and produce a thinking block |
| 113 | + expect(thinkingChunks).toBeGreaterThan(0); |
| 114 | + expect(result.content.find((b) => b.type === 'thinking')).toBeDefined(); |
| 115 | + expect(result.usage.reasoningTokens).toBeGreaterThan(0); |
| 116 | + }, 60_000); |
| 117 | +}); |
0 commit comments