Files
grailbench/parser_test.go
2025-09-06 15:12:07 +03:00

216 lines
5.1 KiB
Go

package main
import (
"grailbench/config"
"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 default config for testing
cfg := config.LoadConfigOrDefault("")
// Create a new deepSeekParser
parser := NewDeepSeekParser(logger, cfg)
// 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/deepseek-chat-v3-0324:free",
"choices": [
{
"text": "This is a regular response",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15
}
}`
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/deepseek-chat-v3-0324:free",
"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/deepseek-chat-v3-0324:free",
"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 default config for testing
cfg := config.LoadConfigOrDefault("")
// Test case 1: Regular text response (completion API)
t.Run("RegularTextResponse", func(t *testing.T) {
// Create a new openRouterParser with completion API
parser := NewOpenRouterParser(logger, cfg)
parser.useChatAPI = false // Use completion API for this test
// Mock response JSON for a regular text response
responseJSON := `{
"id": "test-id",
"object": "text_completion",
"created": 1234567890,
"model": "deepseek/deepseek-chat-v3-0324:free",
"choices": [
{
"text": "This is a regular response",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15
}
}`
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 (chat API)
t.Run("ToolCallResponse", func(t *testing.T) {
// Create a new openRouterParser with chat API
parser := NewOpenRouterParser(logger, cfg)
parser.useChatAPI = true // Use chat API for this test
// Mock response JSON for a tool call response
responseJSON := `{
"id": "test-id",
"object": "chat.completion",
"created": 1234567890,
"model": "deepseek/deepseek-chat-v3-0324:free",
"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)
}
})
}