|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/tigillo/githubmodels-go/models" |
| 11 | +) |
| 12 | + |
| 13 | +// Helper to create a test client pointing to a mock server |
| 14 | +func newTestClient(ts *httptest.Server) *Client { |
| 15 | + return &Client{ |
| 16 | + token: "test-token", |
| 17 | + BaseURL: ts.URL, |
| 18 | + Client: ts.Client(), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestListModels(t *testing.T) { |
| 23 | + // Mock response |
| 24 | + mockModels := []models.Model{ |
| 25 | + {ID: "github/code-chat", Name: "Code Chat", Description: "Chat with code model"}, |
| 26 | + } |
| 27 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | + if r.URL.Path != "/catalog/models" { |
| 29 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 30 | + } |
| 31 | + if r.Header.Get("Authorization") != "Bearer test-token" { |
| 32 | + t.Errorf("missing or wrong Authorization header") |
| 33 | + } |
| 34 | + w.Header().Set("Content-Type", "application/json") |
| 35 | + json.NewEncoder(w).Encode(mockModels) |
| 36 | + })) |
| 37 | + defer ts.Close() |
| 38 | + |
| 39 | + c := newTestClient(ts) |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + modelsList, err := c.ListModels(ctx) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("ListModels failed: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + if len(modelsList) != 1 { |
| 48 | + t.Fatalf("expected 1 model, got %d", len(modelsList)) |
| 49 | + } |
| 50 | + if modelsList[0].ID != "github/code-chat" { |
| 51 | + t.Errorf("unexpected model ID: %s", modelsList[0].ID) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestChatCompletion(t *testing.T) { |
| 56 | + mockResponse := models.ChatResponse{ |
| 57 | + ID: "chat-1", |
| 58 | + Choices: []models.Choice{ |
| 59 | + {Message: models.Message{Role: "assistant", Content: "Hello!"}}, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 64 | + if r.URL.Path != "/inference/chat/completions" { |
| 65 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 66 | + } |
| 67 | + w.Header().Set("Content-Type", "application/json") |
| 68 | + json.NewEncoder(w).Encode(mockResponse) |
| 69 | + })) |
| 70 | + defer ts.Close() |
| 71 | + |
| 72 | + c := newTestClient(ts) |
| 73 | + ctx := context.Background() |
| 74 | + |
| 75 | + req := models.ChatRequest{ |
| 76 | + Model: "github/code-chat", |
| 77 | + Messages: []models.Message{ |
| 78 | + {Role: "user", Content: "Hello"}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + resp, err := c.ChatCompletion(ctx, req) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("ChatCompletion failed: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + if resp.ID != "chat-1" { |
| 88 | + t.Errorf("unexpected response ID: %s", resp.ID) |
| 89 | + } |
| 90 | + if len(resp.Choices) != 1 { |
| 91 | + t.Errorf("expected 1 choice, got %d", len(resp.Choices)) |
| 92 | + } |
| 93 | + if resp.Choices[0].Message.Content != "Hello!" { |
| 94 | + t.Errorf("unexpected response content: %s", resp.Choices[0].Message.Content) |
| 95 | + } |
| 96 | +} |
0 commit comments