Skip to content

Commit 2a5d3be

Browse files
committed
feat: add integration tests for gpt-4o-audio-preview
1 parent 628b88a commit 2a5d3be

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

api_integration_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ func TestAPI(t *testing.T) {
108108
},
109109
)
110110
checks.NoError(t, err, "CreateChatCompletion (with functions) returned error")
111+
112+
response, err := c.CreateChatCompletion(
113+
ctx,
114+
openai.ChatCompletionRequest{
115+
Model: openai.GPT4oAudioPreview,
116+
Messages: []openai.ChatCompletionMessage{
117+
{
118+
Role: openai.ChatMessageRoleUser,
119+
Content: "hi",
120+
},
121+
},
122+
Audio: &openai.AudioOutput{
123+
Voice: openai.AudioVoiceAlloy,
124+
Format: openai.AudioFormatPCM16,
125+
},
126+
Modalities: []openai.Modality{openai.ModalityText, openai.ModalityAudio},
127+
},
128+
)
129+
checks.NoError(t, err, "CreateChatCompletion (with audio) returned error")
130+
if response.Choices[0].Message.Audio == nil {
131+
t.Fatal("Audio response is nil")
132+
}
133+
if len(response.Choices[0].Message.Audio.Data) == 0 {
134+
t.Fatal("Audio response data is empty")
135+
}
136+
if response.Choices[0].Message.Audio.Transcript == "" {
137+
t.Fatal("Audio response transcript is empty")
138+
}
139+
if response.Usage.PromptTokens == 0 || response.Usage.CompletionTokens == 0 || response.Usage.TotalTokens == 0 {
140+
t.Fatal("Usage is zero")
141+
}
142+
t.Logf("Usage: %+v", response.Usage)
111143
}
112144

113145
func TestCompletionStream(t *testing.T) {
@@ -145,6 +177,60 @@ func TestCompletionStream(t *testing.T) {
145177
}
146178
}
147179

180+
func TestChatCompletionStream(t *testing.T) {
181+
apiToken := os.Getenv("OPENAI_TOKEN")
182+
if apiToken == "" {
183+
t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.")
184+
}
185+
186+
c := openai.NewClient(apiToken)
187+
ctx := context.Background()
188+
189+
stream, err := c.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
190+
Model: openai.GPT4oAudioPreview,
191+
Messages: []openai.ChatCompletionMessage{
192+
{
193+
Role: openai.ChatMessageRoleUser,
194+
Content: "hi",
195+
},
196+
},
197+
Audio: &openai.AudioOutput{
198+
Voice: openai.AudioVoiceAlloy,
199+
Format: openai.AudioFormatPCM16,
200+
},
201+
Modalities: []openai.Modality{openai.ModalityText, openai.ModalityAudio},
202+
StreamOptions: &openai.StreamOptions{
203+
IncludeUsage: true,
204+
},
205+
})
206+
checks.NoError(t, err, "CreateCompletionStream returned error")
207+
defer stream.Close()
208+
209+
var usage *openai.Usage
210+
counter := 0
211+
for {
212+
response, err := stream.Recv()
213+
if err != nil {
214+
if errors.Is(err, io.EOF) {
215+
break
216+
}
217+
t.Errorf("Stream error: %v", err)
218+
} else {
219+
counter++
220+
}
221+
if response.Usage != nil {
222+
usage = response.Usage
223+
t.Logf("Usage: %+v", usage)
224+
}
225+
}
226+
if counter == 0 {
227+
t.Error("Stream did not return any responses")
228+
}
229+
if usage == nil {
230+
t.Error("Usage is nil")
231+
}
232+
}
233+
148234
func TestAPIError(t *testing.T) {
149235
apiToken := os.Getenv("OPENAI_TOKEN")
150236
if apiToken == "" {

internal/test/checks/checks.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type tHelper interface {
7373
// Pointer variable equality is determined based on the equality of the
7474
// referenced values (as opposed to the memory addresses). Function equality
7575
// cannot be determined and will always fail.
76-
func Equal(t TestingT, expected, actual interface{}) bool {
76+
func Equal(t TestingT, expected, actual any) bool {
7777
if h, ok := t.(tHelper); ok {
7878
h.Helper()
7979
}
@@ -97,7 +97,7 @@ func JSONEq(t TestingT, expected string, actual string) bool {
9797
if h, ok := t.(tHelper); ok {
9898
h.Helper()
9999
}
100-
var expectedJSONAsInterface, actualJSONAsInterface interface{}
100+
var expectedJSONAsInterface, actualJSONAsInterface any
101101

102102
if err := json.Unmarshal([]byte(expected), &expectedJSONAsInterface); err != nil {
103103
t.Fatalf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error())
@@ -112,7 +112,7 @@ func JSONEq(t TestingT, expected string, actual string) bool {
112112

113113
// validateEqualArgs checks whether provided arguments can be safely used in the
114114
// Equal/NotEqual functions.
115-
func validateEqualArgs(expected, actual interface{}) error {
115+
func validateEqualArgs(expected, actual any) error {
116116
if expected == nil && actual == nil {
117117
return nil
118118
}
@@ -123,7 +123,7 @@ func validateEqualArgs(expected, actual interface{}) error {
123123
return nil
124124
}
125125

126-
func isFunction(arg interface{}) bool {
126+
func isFunction(arg any) bool {
127127
if arg == nil {
128128
return false
129129
}
@@ -137,7 +137,7 @@ func isFunction(arg interface{}) bool {
137137
// ObjectsAreEqual determines if two objects are considered equal.
138138
//
139139
// This function does no assertion of any kind.
140-
func ObjectsAreEqual(expected, actual interface{}) bool {
140+
func ObjectsAreEqual(expected, actual any) bool {
141141
if expected == nil || actual == nil {
142142
return expected == actual
143143
}

0 commit comments

Comments
 (0)