ScuttleBot

scuttlebot / internal / llm / anthropic_test.go
Source Blame History 77 lines
5ac549c… lmata 1 package llm
5ac549c… lmata 2
5ac549c… lmata 3 import (
5ac549c… lmata 4 "context"
5ac549c… lmata 5 "encoding/json"
5ac549c… lmata 6 "net/http"
5ac549c… lmata 7 "net/http/httptest"
5ac549c… lmata 8 "testing"
5ac549c… lmata 9 )
5ac549c… lmata 10
5ac549c… lmata 11 func TestAnthropicSummarize(t *testing.T) {
5ac549c… lmata 12 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5ac549c… lmata 13 if r.Method != "POST" {
5ac549c… lmata 14 t.Errorf("expected POST request, got %s", r.Method)
5ac549c… lmata 15 }
5ac549c… lmata 16 if r.URL.Path != "/v1/messages" {
5ac549c… lmata 17 t.Errorf("unexpected path: %s", r.URL.Path)
5ac549c… lmata 18 }
5ac549c… lmata 19 if r.Header.Get("x-api-key") != "test-api-key" {
5ac549c… lmata 20 t.Errorf("expected api key test-api-key, got %s", r.Header.Get("x-api-key"))
5ac549c… lmata 21 }
5ac549c… lmata 22 if r.Header.Get("anthropic-version") != "2023-06-01" {
5ac549c… lmata 23 t.Errorf("expected anthropic-version 2023-06-01, got %s", r.Header.Get("anthropic-version"))
5ac549c… lmata 24 }
5ac549c… lmata 25
5ac549c… lmata 26 resp := map[string]any{
5ac549c… lmata 27 "content": []map[string]any{
5ac549c… lmata 28 {
5ac549c… lmata 29 "type": "text",
5ac549c… lmata 30 "text": "anthropic response",
5ac549c… lmata 31 },
5ac549c… lmata 32 },
5ac549c… lmata 33 }
5ac549c… lmata 34 _ = json.NewEncoder(w).Encode(resp)
5ac549c… lmata 35 }))
5ac549c… lmata 36 defer srv.Close()
5ac549c… lmata 37
5ac549c… lmata 38 p := newAnthropicProvider(BackendConfig{
5ac549c… lmata 39 Backend: "anthropic",
5ac549c… lmata 40 APIKey: "test-api-key",
5ac549c… lmata 41 BaseURL: srv.URL,
5ac549c… lmata 42 }, srv.Client())
5ac549c… lmata 43
5ac549c… lmata 44 got, err := p.Summarize(context.Background(), "test prompt")
5ac549c… lmata 45 if err != nil {
5ac549c… lmata 46 t.Fatalf("Summarize failed: %v", err)
5ac549c… lmata 47 }
5ac549c… lmata 48 if got != "anthropic response" {
5ac549c… lmata 49 t.Errorf("got %q, want %q", got, "anthropic response")
5ac549c… lmata 50 }
5ac549c… lmata 51 }
5ac549c… lmata 52
5ac549c… lmata 53 func TestAnthropicDiscoverModels(t *testing.T) {
5ac549c… lmata 54 p := newAnthropicProvider(BackendConfig{
5ac549c… lmata 55 Backend: "anthropic",
5ac549c… lmata 56 APIKey: "test-api-key",
5ac549c… lmata 57 }, http.DefaultClient)
5ac549c… lmata 58
5ac549c… lmata 59 models, err := p.DiscoverModels(context.Background())
5ac549c… lmata 60 if err != nil {
5ac549c… lmata 61 t.Fatalf("DiscoverModels failed: %v", err)
5ac549c… lmata 62 }
5ac549c… lmata 63
5ac549c… lmata 64 if len(models) == 0 {
5ac549c… lmata 65 t.Error("expected non-empty model list")
5ac549c… lmata 66 }
5ac549c… lmata 67 found := false
5ac549c… lmata 68 for _, m := range models {
5ac549c… lmata 69 if m.ID == "claude-3-5-sonnet-20241022" {
5ac549c… lmata 70 found = true
5ac549c… lmata 71 break
5ac549c… lmata 72 }
5ac549c… lmata 73 }
5ac549c… lmata 74 if !found {
5ac549c… lmata 75 t.Error("expected to find claude-3-5-sonnet-20241022 in model list")
5ac549c… lmata 76 }
5ac549c… lmata 77 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button