|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | + |
| 3 | +import { addKimiToolCompatibilityFields, isKimiModel } from '../kimi-tool-compat' |
| 4 | + |
| 5 | +import type { ChatCompletionRequestBody } from '../types' |
| 6 | + |
| 7 | +describe('addKimiToolCompatibilityFields', () => { |
| 8 | + it('adds declaration ids and tool-result names without mutating input', () => { |
| 9 | + const body: ChatCompletionRequestBody = { |
| 10 | + model: 'moonshotai/kimi-k2.6', |
| 11 | + messages: [ |
| 12 | + { |
| 13 | + role: 'assistant', |
| 14 | + content: '', |
| 15 | + tool_calls: [ |
| 16 | + { |
| 17 | + id: 'call_123', |
| 18 | + type: 'function', |
| 19 | + function: { |
| 20 | + name: 'read_files', |
| 21 | + arguments: JSON.stringify({ paths: ['README.md'] }), |
| 22 | + }, |
| 23 | + }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + { |
| 27 | + role: 'tool', |
| 28 | + tool_call_id: 'call_123', |
| 29 | + content: JSON.stringify({ message: 'ok' }), |
| 30 | + }, |
| 31 | + ], |
| 32 | + tools: [ |
| 33 | + { |
| 34 | + type: 'function', |
| 35 | + function: { |
| 36 | + name: 'read_files', |
| 37 | + description: 'Read files', |
| 38 | + parameters: { type: 'object' }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + } |
| 43 | + |
| 44 | + const result = addKimiToolCompatibilityFields(body) |
| 45 | + |
| 46 | + expect(result.tools?.[0]).toEqual({ |
| 47 | + id: 'tool_1', |
| 48 | + type: 'function', |
| 49 | + function: { |
| 50 | + name: 'read_files', |
| 51 | + description: 'Read files', |
| 52 | + parameters: { type: 'object' }, |
| 53 | + }, |
| 54 | + }) |
| 55 | + expect(result.messages[1]).toEqual({ |
| 56 | + role: 'tool', |
| 57 | + tool_call_id: 'call_123', |
| 58 | + name: 'read_files', |
| 59 | + content: JSON.stringify({ message: 'ok' }), |
| 60 | + }) |
| 61 | + expect(body.tools?.[0]).not.toHaveProperty('id') |
| 62 | + expect(body.messages[1]).not.toHaveProperty('name') |
| 63 | + }) |
| 64 | + |
| 65 | + it('preserves existing ids and names', () => { |
| 66 | + const body: ChatCompletionRequestBody = { |
| 67 | + model: 'moonshotai/kimi-k2.6', |
| 68 | + messages: [ |
| 69 | + { |
| 70 | + role: 'assistant', |
| 71 | + content: '', |
| 72 | + tool_calls: [ |
| 73 | + { |
| 74 | + id: 'call_456', |
| 75 | + type: 'function', |
| 76 | + function: { |
| 77 | + name: 'write_todos', |
| 78 | + arguments: JSON.stringify({ todos: [] }), |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + { |
| 84 | + role: 'tool', |
| 85 | + tool_call_id: 'call_456', |
| 86 | + name: 'existing_name', |
| 87 | + content: '{}', |
| 88 | + }, |
| 89 | + ], |
| 90 | + tools: [ |
| 91 | + { |
| 92 | + id: 'existing_tool_id', |
| 93 | + type: 'function', |
| 94 | + function: { |
| 95 | + name: 'write_todos', |
| 96 | + parameters: { type: 'object' }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + ], |
| 100 | + } |
| 101 | + |
| 102 | + expect(addKimiToolCompatibilityFields(body)).toEqual(body) |
| 103 | + }) |
| 104 | +}) |
| 105 | + |
| 106 | +describe('isKimiModel', () => { |
| 107 | + it('matches only Moonshot model ids', () => { |
| 108 | + expect(isKimiModel('moonshotai/kimi-k2.6')).toBe(true) |
| 109 | + expect(isKimiModel('anthropic/claude-sonnet-4.5')).toBe(false) |
| 110 | + expect(isKimiModel(undefined)).toBe(false) |
| 111 | + }) |
| 112 | +}) |
0 commit comments