191 lines
4.5 KiB
Go
191 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"grailbench/config"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// TestParserToolDetection tests the parser implementations with mock responses
|
|
func TestParserToolDetection(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 cases with different response types
|
|
testCases := []struct {
|
|
name string
|
|
responseJSON string
|
|
expectedOutput string
|
|
isToolCall bool
|
|
expectedTool string
|
|
}{
|
|
{
|
|
name: "RegularTextResponse",
|
|
responseJSON: `{
|
|
"id": "test-id",
|
|
"object": "text_completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek/deepseek-chat-v3-0324:free",
|
|
"choices": [
|
|
{
|
|
"text": "The capital of France is Paris.",
|
|
"index": 0,
|
|
"logprobs": null,
|
|
"finish_reason": "stop"
|
|
}
|
|
],
|
|
"usage": {
|
|
"prompt_tokens": 10,
|
|
"completion_tokens": 5,
|
|
"total_tokens": 15
|
|
}
|
|
}`,
|
|
expectedOutput: "The capital of France is Paris.",
|
|
isToolCall: false,
|
|
},
|
|
{
|
|
name: "ToolCallResponse",
|
|
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": "{}"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}`,
|
|
expectedOutput: "[TOOL_CALL:get_current_timestamp]",
|
|
isToolCall: true,
|
|
expectedTool: "get_current_timestamp",
|
|
},
|
|
{
|
|
name: "RegularMessageResponse",
|
|
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": "Hello, how can I help you today?"
|
|
}
|
|
}
|
|
]
|
|
}`,
|
|
expectedOutput: "Hello, how can I help you today?",
|
|
isToolCall: false,
|
|
},
|
|
}
|
|
|
|
// Test DeepSeekParser
|
|
t.Run("DeepSeekParser", func(t *testing.T) {
|
|
parser := NewDeepSeekParser(logger, cfg)
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
result, err := parser.ParseBytes([]byte(tc.responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
if result != tc.expectedOutput {
|
|
t.Errorf("Expected %s, got %s", tc.expectedOutput, result)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
// Test OpenRouterParser
|
|
t.Run("OpenRouterParser", func(t *testing.T) {
|
|
// Test RegularTextResponse with completion API
|
|
t.Run("RegularTextResponse", func(t *testing.T) {
|
|
parser := NewOpenRouterParser(logger, cfg)
|
|
parser.useChatAPI = false // Use completion API for this test
|
|
|
|
tc := struct {
|
|
name string
|
|
responseJSON string
|
|
expectedOutput string
|
|
isToolCall bool
|
|
expectedTool string
|
|
}{
|
|
name: "RegularTextResponse",
|
|
responseJSON: `{
|
|
"id": "test-id",
|
|
"object": "text_completion",
|
|
"created": 1234567890,
|
|
"model": "deepseek/deepseek-chat-v3-0324:free",
|
|
"choices": [
|
|
{
|
|
"text": "The capital of France is Paris.",
|
|
"index": 0,
|
|
"logprobs": null,
|
|
"finish_reason": "stop"
|
|
}
|
|
],
|
|
"usage": {
|
|
"prompt_tokens": 10,
|
|
"completion_tokens": 5,
|
|
"total_tokens": 15
|
|
}
|
|
}`,
|
|
expectedOutput: "The capital of France is Paris.",
|
|
isToolCall: false,
|
|
}
|
|
|
|
result, err := parser.ParseBytes([]byte(tc.responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
if result != tc.expectedOutput {
|
|
t.Errorf("Expected %s, got %s", tc.expectedOutput, result)
|
|
}
|
|
})
|
|
|
|
// Test ToolCallResponse and RegularMessageResponse with chat API
|
|
parser := NewOpenRouterParser(logger, cfg)
|
|
parser.useChatAPI = true // Use chat API for these tests
|
|
|
|
for _, tc := range testCases {
|
|
if tc.name == "RegularTextResponse" {
|
|
continue // Skip this one as we tested it above
|
|
}
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
result, err := parser.ParseBytes([]byte(tc.responseJSON))
|
|
if err != nil {
|
|
t.Errorf("ParseBytes returned an error: %v", err)
|
|
}
|
|
|
|
if result != tc.expectedOutput {
|
|
t.Errorf("Expected %s, got %s", tc.expectedOutput, result)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
} |