package ai

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestExtractChatContent_String(t *testing.T) {
	raw := []byte(`{"model":"grok-3","choices":[{"finish_reason":"stop","message":{"content":"你好世界"}}]}`)
	text, meta, err := extractChatContent(raw)
	require.NoError(t, err)
	require.Equal(t, "你好世界", text)
	require.Equal(t, "stop", meta.FinishReason)
}

func TestExtractChatContent_ArrayParts(t *testing.T) {
	raw := []byte(`{"choices":[{"message":{"content":[{"type":"text","text":"第一段"},{"type":"text","text":"第二段"}]}}]}`)
	text, _, err := extractChatContent(raw)
	require.NoError(t, err)
	require.Contains(t, text, "第一段")
	require.Contains(t, text, "第二段")
}

func TestExtractChatContent_Empty(t *testing.T) {
	raw := []byte(`{"choices":[{"finish_reason":"length","message":{"content":""}}]}`)
	text, meta, err := extractChatContent(raw)
	require.NoError(t, err)
	require.Equal(t, "", text)
	require.Equal(t, "length", meta.FinishReason)
}

func TestExtractChatContent_IgnoresReasoningOnly(t *testing.T) {
	// deepseek / kimi 思考模型常只回 reasoning_content；不可當聊天回覆
	raw := []byte(`{"choices":[{"finish_reason":"length","message":{"content":null,"reasoning_content":"我們需要根據最近的對話來回應…"}}]}`)
	text, meta, err := extractChatContent(raw)
	require.NoError(t, err)
	require.Equal(t, "", text)
	require.Equal(t, "length", meta.FinishReason)
}
