192 lines
4.3 KiB
Go
192 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestDeepSeekParser_ParseBytes(t *testing.T) {
|
|
// Create a logger for testing
|
|
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
|
|
|
// Create a new deepSeekParser
|
|
parser := NewDeepSeekParser(logger)
|
|
|
|
// Test case 1: Regular text response
|
|
t.Run("RegularTextResponse", func(t *testing.T) {
|
|
// Mock response JSON for a regular text response
|
|
responseJSON := `{
|
|
"id": "test-id",
|
|
"object": "text.completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek-chat",
|
|
"choices": [
|
|
{
|
|
"text": "This is a regular response",
|
|
"index": 0,
|
|
"finish_reason": "stop"
|
|
}
|
|
]
|
|
}`
|
|
|
|
result, err := parser.ParseBytes([]byte(responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
expected := "This is a regular response"
|
|
if result != expected {
|
|
t.Errorf("Expected %s, got %s", expected, result)
|
|
}
|
|
})
|
|
|
|
// Test case 2: Tool call response
|
|
t.Run("ToolCallResponse", func(t *testing.T) {
|
|
// Mock response JSON for a tool call response
|
|
responseJSON := `{
|
|
"id": "test-id",
|
|
"object": "chat.completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek-chat",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"finish_reason": "tool_calls",
|
|
"message": {
|
|
"role": "assistant",
|
|
"content": null,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_123",
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_current_timestamp",
|
|
"arguments": "{}"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}`
|
|
|
|
result, err := parser.ParseBytes([]byte(responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
expected := "[TOOL_CALL:get_current_timestamp]"
|
|
if result != expected {
|
|
t.Errorf("Expected %s, got %s", expected, result)
|
|
}
|
|
})
|
|
|
|
// Test case 3: Regular message response (OpenAI format)
|
|
t.Run("RegularMessageResponse", func(t *testing.T) {
|
|
// Mock response JSON for a regular message response
|
|
responseJSON := `{
|
|
"id": "test-id",
|
|
"object": "chat.completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek-chat",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"finish_reason": "stop",
|
|
"message": {
|
|
"role": "assistant",
|
|
"content": "This is a regular message response"
|
|
}
|
|
}
|
|
]
|
|
}`
|
|
|
|
result, err := parser.ParseBytes([]byte(responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
expected := "This is a regular message response"
|
|
if result != expected {
|
|
t.Errorf("Expected %s, got %s", expected, result)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestOpenRouterParser_ParseBytes(t *testing.T) {
|
|
// Create a logger for testing
|
|
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
|
|
|
// Create a new openRouterParser
|
|
parser := NewOpenRouterParser(logger)
|
|
|
|
// Test case 1: Regular text response
|
|
t.Run("RegularTextResponse", func(t *testing.T) {
|
|
// Mock response JSON for a regular text response
|
|
responseJSON := `{
|
|
"id": "test-id",
|
|
"object": "text.completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek-r1",
|
|
"choices": [
|
|
{
|
|
"text": "This is a regular response",
|
|
"index": 0,
|
|
"finish_reason": "stop"
|
|
}
|
|
]
|
|
}`
|
|
|
|
result, err := parser.ParseBytes([]byte(responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
expected := "This is a regular response"
|
|
if result != expected {
|
|
t.Errorf("Expected %s, got %s", expected, result)
|
|
}
|
|
})
|
|
|
|
// Test case 2: Tool call response
|
|
t.Run("ToolCallResponse", func(t *testing.T) {
|
|
// Mock response JSON for a tool call response
|
|
responseJSON := `{
|
|
"id": "test-id",
|
|
"object": "chat.completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek-r1",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"finish_reason": "tool_calls",
|
|
"message": {
|
|
"role": "assistant",
|
|
"content": null,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_123",
|
|
"type": "function",
|
|
"function": {
|
|
"name": "send_email",
|
|
"arguments": "{\"address\":\"test@example.com\",\"title\":\"Test\",\"body\":\"This is a test email\"}"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}`
|
|
|
|
result, err := parser.ParseBytes([]byte(responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
expected := "[TOOL_CALL:send_email]"
|
|
if result != expected {
|
|
t.Errorf("Expected %s, got %s", expected, result)
|
|
}
|
|
})
|
|
} |