|
| 1 | +/* |
| 2 | +Copyright 2024 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package openai |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/dapr/components-contrib/conversation" |
| 20 | + "github.com/dapr/components-contrib/metadata" |
| 21 | + "github.com/dapr/kit/logger" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestInit(t *testing.T) { |
| 28 | + testCases := []struct { |
| 29 | + name string |
| 30 | + metadata map[string]string |
| 31 | + testFn func(*testing.T, *OpenAI, error) |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "with default endpoint", |
| 35 | + metadata: map[string]string{ |
| 36 | + "key": "test-key", |
| 37 | + "model": "gpt-4", |
| 38 | + }, |
| 39 | + testFn: func(t *testing.T, o *OpenAI, err error) { |
| 40 | + require.NoError(t, err) |
| 41 | + assert.NotNil(t, o.llm) |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "with custom endpoint", |
| 46 | + metadata: map[string]string{ |
| 47 | + "key": "test-key", |
| 48 | + "model": "gpt-4", |
| 49 | + "endpoint": "https://api.openai.com/v1", |
| 50 | + }, |
| 51 | + testFn: func(t *testing.T, o *OpenAI, err error) { |
| 52 | + require.NoError(t, err) |
| 53 | + assert.NotNil(t, o.llm) |
| 54 | + // Since we can't directly access the client's baseURL, |
| 55 | + // we're mainly testing that initialization succeeds |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tc := range testCases { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + o := NewOpenAI(logger.NewLogger("openai test")) |
| 63 | + err := o.Init(t.Context(), conversation.Metadata{ |
| 64 | + Base: metadata.Base{ |
| 65 | + Properties: tc.metadata, |
| 66 | + }, |
| 67 | + }) |
| 68 | + tc.testFn(t, o.(*OpenAI), err) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestEndpointInMetadata(t *testing.T) { |
| 74 | + // Create an instance of OpenAI component |
| 75 | + o := &OpenAI{} |
| 76 | + |
| 77 | + // This test relies on the metadata tag |
| 78 | + md := o.GetComponentMetadata() |
| 79 | + if len(md) == 0 { |
| 80 | + t.Skip("Metadata is not enabled, skipping test") |
| 81 | + } |
| 82 | + |
| 83 | + // Print all available metadata keys for debugging |
| 84 | + t.Logf("Available metadata keys: %v", func() []string { |
| 85 | + keys := make([]string, 0, len(md)) |
| 86 | + for k := range md { |
| 87 | + keys = append(keys, k) |
| 88 | + } |
| 89 | + return keys |
| 90 | + }()) |
| 91 | + |
| 92 | + // Verify endpoint field exists (note: field names are capitalized in metadata) |
| 93 | + _, exists := md["Endpoint"] |
| 94 | + assert.True(t, exists, "Endpoint field should exist in metadata") |
| 95 | +} |
0 commit comments