120 lines
3.5 KiB
Go
120 lines
3.5 KiB
Go
// Unit and integration tests for q38max
|
|||
|
|
// Created by Luxferre in 2026, released into the public domain
|
||
|
|
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestChatMessageGetContentString(t *testing.T) {
|
||
|
|
msg1 := ChatMessage{Role: "user", Content: "Hello world"}
|
||
|
|
if msg1.GetContentString() != "Hello world" {
|
||
|
|
t.Fatalf("expected 'Hello world', got %q", msg1.GetContentString())
|
||
|
|
}
|
||
|
|
|
||
|
|
msg2 := ChatMessage{
|
||
|
|
Role: "user",
|
||
|
|
Content: []interface{}{
|
||
|
|
map[string]interface{}{"type": "text", "text": "Part 1 "},
|
||
|
|
map[string]interface{}{"type": "text", "text": "Part 2"},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
if msg2.GetContentString() != "Part 1 Part 2" {
|
||
|
|
t.Fatalf("expected 'Part 1 Part 2', got %q", msg2.GetContentString())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestExtractThinkingContent(t *testing.T) {
|
||
|
|
raw := "<think>\nAnalyzing the user's request...\n</think>\nHere is the answer."
|
||
|
|
thinking, clean := ExtractThinkingContent(raw)
|
||
|
|
if thinking != "Analyzing the user's request..." {
|
||
|
|
t.Fatalf("unexpected thinking extraction: %q", thinking)
|
||
|
|
}
|
||
|
|
if clean != "Here is the answer." {
|
||
|
|
t.Fatalf("unexpected clean text: %q", clean)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectToolCalls(t *testing.T) {
|
||
|
|
xmlInput := "Let me check the weather.\n<tool_call>{\"name\": \"get_weather\", \"arguments\": {\"location\": \"Tokyo\"}}</tool_call>"
|
||
|
|
calls, rem := DetectToolCalls(xmlInput)
|
||
|
|
if len(calls) != 1 {
|
||
|
|
t.Fatalf("expected 1 tool call, got %d", len(calls))
|
||
|
|
}
|
||
|
|
if calls[0].Function.Name != "get_weather" {
|
||
|
|
t.Fatalf("expected function name 'get_weather', got %q", calls[0].Function.Name)
|
||
|
|
}
|
||
|
|
if !strings.Contains(calls[0].Function.Arguments, "Tokyo") {
|
||
|
|
t.Fatalf("expected argument with Tokyo, got %q", calls[0].Function.Arguments)
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(rem) != "Let me check the weather." {
|
||
|
|
t.Fatalf("unexpected remaining text: %q", rem)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPrepareConversation(t *testing.T) {
|
||
|
|
req := ChatCompletionRequest{
|
||
|
|
Model: "qwen-3.8-max",
|
||
|
|
Messages: []ChatMessage{
|
||
|
|
{Role: "system", Content: "You are a helpful assistant."},
|
||
|
|
{Role: "user", Content: "Tell me a joke."},
|
||
|
|
{Role: "assistant", Content: "Why did the chicken cross the road?"},
|
||
|
|
{Role: "user", Content: "Why?"},
|
||
|
|
},
|
||
|
|
ReasoningEffort: "medium",
|
||
|
|
}
|
||
|
|
|
||
|
|
history, question, thinkingMode := PrepareConversation(req)
|
||
|
|
if thinkingMode != "true" {
|
||
|
|
t.Fatalf("expected thinkingMode 'true', got %q", thinkingMode)
|
||
|
|
}
|
||
|
|
if len(history) != 2 {
|
||
|
|
t.Fatalf("expected 2 history items, got %d", len(history))
|
||
|
|
}
|
||
|
|
if question != "Why?" {
|
||
|
|
t.Fatalf("expected question 'Why?', got %q", question)
|
||
|
|
}
|
||
|
|
if !strings.Contains(history[0]["content"].(string), "You are a helpful assistant.") {
|
||
|
|
t.Fatalf("expected system prompt inside first turn, got %v", history[0]["content"])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestModelsHandler(t *testing.T) {
|
||
|
|
gw := NewQ38Gateway("https://mock.hf.space", "/dummy/path.jpg", "qwen-3.8-max", 5*time.Second)
|
||
|
|
|
||
|
|
req := httptest.NewRequest("GET", "/v1/models", nil)
|
||
|
|
w := httptest.NewRecorder()
|
||
|
|
|
||
|
|
gw.HandleModels(w, req)
|
||
|
|
|
||
|
|
if w.Code != http.StatusOK {
|
||
|
|
t.Fatalf("expected 200 OK, got %d", w.Code)
|
||
|
|
}
|
||
|
|
|
||
|
|
var res ModelsResponse
|
||
|
|
if err := json.NewDecoder(w.Body).Decode(&res); err != nil {
|
||
|
|
t.Fatalf("failed to decode response: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(res.Data) == 0 {
|
||
|
|
t.Fatalf("expected at least 1 model in response")
|
||
|
|
}
|
||
|
|
|
||
|
|
found := false
|
||
|
|
for _, m := range res.Data {
|
||
|
|
if m.ID == "qwen-3.8-max" {
|
||
|
|
found = true
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !found {
|
||
|
|
t.Fatalf("qwen-3.8-max not found in models list")
|
||
|
|
}
|
||
|
|
}
|