|
| 1 | +package githubmodels |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + openai "github.com/sashabaranov/go-openai" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGithubModelsProvider_Name(t *testing.T) { |
| 13 | + provider := &Provider{} |
| 14 | + |
| 15 | + name := provider.Name() |
| 16 | + |
| 17 | + assert.Equal(t, "githubmodels", name) |
| 18 | +} |
| 19 | + |
| 20 | +func TestNewProvider(t *testing.T) { |
| 21 | + t.Run("with parameters", func(t *testing.T) { |
| 22 | + provider := NewProvider("test_api_key", "test_model") |
| 23 | + |
| 24 | + assert.Equal(t, "test_api_key", provider.APIKey) |
| 25 | + assert.Equal(t, "test_model", provider.Model) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("with environment variables", func(t *testing.T) { |
| 29 | + os.Setenv("GITHUB_TOKEN", "env_api_key") |
| 30 | + |
| 31 | + provider := NewProvider("", "test_model") |
| 32 | + |
| 33 | + assert.Equal(t, "env_api_key", provider.APIKey) |
| 34 | + assert.Equal(t, "test_model", provider.Model) |
| 35 | + |
| 36 | + os.Unsetenv("GITHUB_TOKEN") |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func TestGithubModelsProvider_GetChatCompletions(t *testing.T) { |
| 41 | + config := openai.DefaultConfig("test_api_key") |
| 42 | + config.BaseURL = "https://models.inference.ai.azure.com" |
| 43 | + client := openai.NewClientWithConfig(config) |
| 44 | + |
| 45 | + provider := &Provider{ |
| 46 | + APIKey: "test_api_key", |
| 47 | + Model: "test_model", |
| 48 | + client: client, |
| 49 | + } |
| 50 | + |
| 51 | + msgs := []openai.ChatCompletionMessage{ |
| 52 | + { |
| 53 | + Role: "user", |
| 54 | + Content: "hello", |
| 55 | + }, |
| 56 | + { |
| 57 | + Role: "system", |
| 58 | + Content: "I'm a bot", |
| 59 | + }, |
| 60 | + } |
| 61 | + req := openai.ChatCompletionRequest{ |
| 62 | + Messages: msgs, |
| 63 | + } |
| 64 | + |
| 65 | + _, err := provider.GetChatCompletions(context.TODO(), req, nil) |
| 66 | + assert.Error(t, err) |
| 67 | + assert.Contains(t, err.Error(), "401") |
| 68 | + assert.Contains(t, err.Error(), "Bad credentials") |
| 69 | +} |
| 70 | + |
| 71 | +func TestGithubModelsProvider_GetChatCompletionsStream(t *testing.T) { |
| 72 | + config := openai.DefaultConfig("test_api_key") |
| 73 | + config.BaseURL = "https://models.inference.ai.azure.com" |
| 74 | + client := openai.NewClientWithConfig(config) |
| 75 | + |
| 76 | + provider := &Provider{ |
| 77 | + APIKey: "test_api_key", |
| 78 | + Model: "test_model", |
| 79 | + client: client, |
| 80 | + } |
| 81 | + |
| 82 | + msgs := []openai.ChatCompletionMessage{ |
| 83 | + { |
| 84 | + Role: "user", |
| 85 | + Content: "hello", |
| 86 | + }, |
| 87 | + { |
| 88 | + Role: "system", |
| 89 | + Content: "I'm a bot", |
| 90 | + }, |
| 91 | + } |
| 92 | + req := openai.ChatCompletionRequest{ |
| 93 | + Messages: msgs, |
| 94 | + } |
| 95 | + |
| 96 | + _, err := provider.GetChatCompletionsStream(context.TODO(), req, nil) |
| 97 | + assert.Error(t, err) |
| 98 | + assert.Contains(t, err.Error(), "401") |
| 99 | + assert.Contains(t, err.Error(), "Bad credentials") |
| 100 | +} |
0 commit comments