Files
Maven/internal/llm/client_test.go
T
kami f7442c3aea Run gofmt over the seven files that had drifted
Formatting only: import order, and statements that were packed onto one
line split out. `git diff -w` shows nothing but that.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CGeSZxh1DCtRxmFVSYVGvJ
2026-07-31 10:09:32 +04:00

60 lines
1.6 KiB
Go

package llm
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestComplete(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("method = %q, want POST", r.Method)
}
if !strings.HasSuffix(r.URL.Path, "/v1/chat/completions") {
t.Errorf("path = %q, want /v1/chat/completions", r.URL.Path)
}
var reqBody struct {
Messages []struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"messages"`
Grammar string `json:"grammar"`
MaxTokens int `json:"max_tokens"`
}
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
t.Fatalf("decode request body: %v", err)
}
if len(reqBody.Messages) < 2 {
t.Fatalf("expected at least 2 messages, got %d", len(reqBody.Messages))
}
if reqBody.Messages[0].Role != "system" || reqBody.Messages[1].Content != "hi" {
t.Errorf("unexpected messages: %+v", reqBody.Messages)
}
if reqBody.Grammar != `root ::= "x"` {
t.Errorf("grammar = %q, want root ::= \"x\"", reqBody.Grammar)
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"choices":[{"message":{"content":"ok"}}]}`))
}))
defer srv.Close()
c := New(srv.URL, 5*time.Second)
got, err := c.Complete(context.Background(), Req{
System: "be helpful",
User: "hi",
Grammar: `root ::= "x"`,
MaxTokens: 42,
})
if err != nil {
t.Fatalf("Complete: %v", err)
}
if got != "ok" {
t.Errorf("got %q, want %q", got, "ok")
}
}