test: 添加测试支持

This commit is contained in:
claude-code-best
2026-04-14 22:31:38 +08:00
parent e601557716
commit 3c4fa38b19
7 changed files with 497 additions and 0 deletions

View File

@@ -435,6 +435,54 @@ describe('DeepSeek thinking mode (enableThinking)', () => {
expect(assistant.reasoning_content).toBeUndefined()
})
// ── fix: reorder tool and user messages for OpenAI API compatibility (#168) ──
test('tool messages come BEFORE user text when mixed in same turn', () => {
// OpenAI requires: assistant(tool_calls) → tool → user
// Bug: previously user text was emitted before tool messages
const result = anthropicMessagesToOpenAI(
[
makeUserMsg('run ls'),
makeAssistantMsg([
{ type: 'tool_use' as const, id: 'toolu_1', name: 'bash', input: { command: 'ls' } },
]),
makeUserMsg([
{ type: 'tool_result' as const, tool_use_id: 'toolu_1', content: 'file.txt' },
{ type: 'text' as const, text: 'looks good' },
]),
],
[] as any,
)
// Find the tool message and the user text message
const toolIdx = result.findIndex(m => m.role === 'tool')
const userTextIdx = result.findIndex(
m => m.role === 'user' && typeof m.content === 'string' && m.content.includes('looks good'),
)
expect(toolIdx).toBeGreaterThanOrEqual(0)
expect(userTextIdx).toBeGreaterThanOrEqual(0)
// Tool MUST come before user text
expect(toolIdx).toBeLessThan(userTextIdx)
})
test('tool message immediately follows assistant tool_calls (no user message in between)', () => {
const result = anthropicMessagesToOpenAI(
[
makeUserMsg('do something'),
makeAssistantMsg([
{ type: 'tool_use' as const, id: 'toolu_2', name: 'bash', input: { command: 'pwd' } },
]),
makeUserMsg([
{ type: 'tool_result' as const, tool_use_id: 'toolu_2', content: '/home/user' },
]),
],
[] as any,
)
const assistantIdx = result.findIndex(m => m.role === 'assistant' && (m as any).tool_calls)
const toolIdx = result.findIndex(m => m.role === 'tool')
expect(assistantIdx).toBeGreaterThanOrEqual(0)
expect(toolIdx).toBe(assistantIdx + 1)
})
test('sets content to null when only thinking and tool_calls present', () => {
const result = anthropicMessagesToOpenAI(
[makeUserMsg('question'), makeAssistantMsg([