|
| 1 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { spawnSync } from 'node:child_process' |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +function runCli(args: string[], home: string) { |
| 9 | + return spawnSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', ...args], { |
| 10 | + cwd: process.cwd(), |
| 11 | + env: { |
| 12 | + ...process.env, |
| 13 | + CLAUDE_CONFIG_DIR: join(home, '.claude'), |
| 14 | + HOME: home, |
| 15 | + TZ: 'UTC', |
| 16 | + }, |
| 17 | + encoding: 'utf-8', |
| 18 | + timeout: 30_000, |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +function userLine(content: string, timestamp: string): string { |
| 23 | + return JSON.stringify({ |
| 24 | + type: 'user', |
| 25 | + sessionId: 'deepseek-v4-session', |
| 26 | + timestamp, |
| 27 | + cwd: '/tmp/deepseek-v4-validation', |
| 28 | + message: { role: 'user', content }, |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +function assistantLine(model: string, timestamp: string, messageId: string, usage: Record<string, number>): string { |
| 33 | + return JSON.stringify({ |
| 34 | + type: 'assistant', |
| 35 | + sessionId: 'deepseek-v4-session', |
| 36 | + timestamp, |
| 37 | + cwd: '/tmp/deepseek-v4-validation', |
| 38 | + message: { |
| 39 | + id: messageId, |
| 40 | + type: 'message', |
| 41 | + role: 'assistant', |
| 42 | + model, |
| 43 | + content: [ |
| 44 | + { type: 'text', text: 'updated pricing code' }, |
| 45 | + { type: 'tool_use', id: `tu-${messageId}`, name: 'Edit', input: { file_path: '/tmp/deepseek-v4-validation/pricing.ts', old_string: 'old', new_string: 'new' } }, |
| 46 | + ], |
| 47 | + usage, |
| 48 | + }, |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +describe('CLI DeepSeek v4 Claude pricing regression', () => { |
| 53 | + it('prices DeepSeek v4 Claude sessions even when the runtime LiteLLM cache lacks those models', async () => { |
| 54 | + const home = await mkdtemp(join(tmpdir(), 'codeburn-deepseek-v4-cli-')) |
| 55 | + |
| 56 | + try { |
| 57 | + const projectDir = join(home, '.claude', 'projects', 'deepseek-v4-validation') |
| 58 | + const cacheDir = join(home, '.cache', 'codeburn') |
| 59 | + await mkdir(projectDir, { recursive: true }) |
| 60 | + await mkdir(cacheDir, { recursive: true }) |
| 61 | + |
| 62 | + await writeFile(join(cacheDir, 'litellm-pricing.json'), JSON.stringify({ |
| 63 | + timestamp: Date.now(), |
| 64 | + data: { |
| 65 | + 'gpt-4o-mini': { |
| 66 | + inputCostPerToken: 1.5e-7, |
| 67 | + outputCostPerToken: 6e-7, |
| 68 | + cacheWriteCostPerToken: 0, |
| 69 | + cacheReadCostPerToken: 7.5e-8, |
| 70 | + webSearchCostPerRequest: 0.01, |
| 71 | + fastMultiplier: 1, |
| 72 | + }, |
| 73 | + }, |
| 74 | + })) |
| 75 | + |
| 76 | + await writeFile( |
| 77 | + join(projectDir, 'session.jsonl'), |
| 78 | + [ |
| 79 | + userLine('Use DeepSeek v4 through the Claude-compatible endpoint.', '2026-05-20T10:00:00.000Z'), |
| 80 | + assistantLine('deepseek-v4-pro', '2026-05-20T10:01:00.000Z', 'deepseek-v4-pro', { |
| 81 | + input_tokens: 2_477_914, |
| 82 | + output_tokens: 762_994, |
| 83 | + cache_read_input_tokens: 258_556_928, |
| 84 | + cache_creation_input_tokens: 0, |
| 85 | + }), |
| 86 | + userLine('Validate the flash model path too.', '2026-05-20T10:02:00.000Z'), |
| 87 | + assistantLine('deepseek-v4-flash', '2026-05-20T10:03:00.000Z', 'deepseek-v4-flash', { |
| 88 | + input_tokens: 1_552_573, |
| 89 | + output_tokens: 353_914, |
| 90 | + cache_read_input_tokens: 48_388_608, |
| 91 | + cache_creation_input_tokens: 0, |
| 92 | + }), |
| 93 | + ].join('\n') + '\n', |
| 94 | + ) |
| 95 | + |
| 96 | + const result = runCli([ |
| 97 | + '--format', 'json', |
| 98 | + '--from', '2026-05-20', |
| 99 | + '--to', '2026-05-20', |
| 100 | + '--provider', 'claude', |
| 101 | + ], home) |
| 102 | + |
| 103 | + expect(result.status, `stderr: ${result.stderr}`).toBe(0) |
| 104 | + |
| 105 | + const report = JSON.parse(result.stdout) as { |
| 106 | + overview: { cost: number; calls: number; tokens: { cacheRead: number } } |
| 107 | + models: Array<{ name: string; cost: number; calls: number; inputTokens: number; outputTokens: number; cacheReadTokens: number }> |
| 108 | + } |
| 109 | + const pro = report.models.find(m => m.name === 'DeepSeek v4 Pro') |
| 110 | + const flash = report.models.find(m => m.name === 'DeepSeek v4 Flash') |
| 111 | + |
| 112 | + expect(report.overview.calls).toBe(2) |
| 113 | + expect(report.overview.tokens.cacheRead).toBe(306_945_536) |
| 114 | + expect(report.overview.cost).toBeCloseTo(3.13091, 5) |
| 115 | + |
| 116 | + expect(pro).toBeDefined() |
| 117 | + expect(pro!.calls).toBe(1) |
| 118 | + expect(pro!.inputTokens).toBe(2_477_914) |
| 119 | + expect(pro!.outputTokens).toBe(762_994) |
| 120 | + expect(pro!.cacheReadTokens).toBe(258_556_928) |
| 121 | + expect(pro!.cost).toBeCloseTo(2.678966, 6) |
| 122 | + |
| 123 | + expect(flash).toBeDefined() |
| 124 | + expect(flash!.calls).toBe(1) |
| 125 | + expect(flash!.inputTokens).toBe(1_552_573) |
| 126 | + expect(flash!.outputTokens).toBe(353_914) |
| 127 | + expect(flash!.cacheReadTokens).toBe(48_388_608) |
| 128 | + expect(flash!.cost).toBeCloseTo(0.451944, 6) |
| 129 | + } finally { |
| 130 | + await rm(home, { recursive: true, force: true }) |
| 131 | + } |
| 132 | + }) |
| 133 | +}) |
0 commit comments