|
| 1 | +package openai_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/sashabaranov/go-openai" |
| 10 | +) |
| 11 | + |
| 12 | +// This file demonstrates how to create mock clients for go-openai streaming |
| 13 | +// functionality. This pattern is useful when testing code that depends on |
| 14 | +// go-openai streaming but you want to control the responses for testing. |
| 15 | + |
| 16 | +// MockOpenAIStreamClient demonstrates how to create a full mock client for go-openai. |
| 17 | +type MockOpenAIStreamClient struct { |
| 18 | + // Configure canned responses |
| 19 | + ChatCompletionResponse openai.ChatCompletionResponse |
| 20 | + ChatCompletionStreamErr error |
| 21 | + |
| 22 | + // Allow function overrides for more complex scenarios |
| 23 | + CreateChatCompletionStreamFn func( |
| 24 | + ctx context.Context, req openai.ChatCompletionRequest) (*openai.ChatCompletionStream, error) |
| 25 | +} |
| 26 | + |
| 27 | +func (m *MockOpenAIStreamClient) CreateChatCompletionStream( |
| 28 | + ctx context.Context, |
| 29 | + req openai.ChatCompletionRequest, |
| 30 | +) (*openai.ChatCompletionStream, error) { |
| 31 | + if m.CreateChatCompletionStreamFn != nil { |
| 32 | + return m.CreateChatCompletionStreamFn(ctx, req) |
| 33 | + } |
| 34 | + return nil, m.ChatCompletionStreamErr |
| 35 | +} |
| 36 | + |
| 37 | +// mockStreamReader creates specific responses for testing. |
| 38 | +type mockStreamReader struct { |
| 39 | + responses []openai.ChatCompletionStreamResponse |
| 40 | + index int |
| 41 | +} |
| 42 | + |
| 43 | +func (m *mockStreamReader) Recv() (openai.ChatCompletionStreamResponse, error) { |
| 44 | + if m.index >= len(m.responses) { |
| 45 | + return openai.ChatCompletionStreamResponse{}, io.EOF |
| 46 | + } |
| 47 | + resp := m.responses[m.index] |
| 48 | + m.index++ |
| 49 | + return resp, nil |
| 50 | +} |
| 51 | + |
| 52 | +func (m *mockStreamReader) Close() error { |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func TestMockOpenAIStreamClient_Demo(t *testing.T) { |
| 57 | + // Create expected responses that our mock stream will return |
| 58 | + expectedResponses := []openai.ChatCompletionStreamResponse{ |
| 59 | + { |
| 60 | + ID: "test-1", |
| 61 | + Object: "chat.completion.chunk", |
| 62 | + Model: "gpt-3.5-turbo", |
| 63 | + Choices: []openai.ChatCompletionStreamChoice{ |
| 64 | + { |
| 65 | + Index: 0, |
| 66 | + Delta: openai.ChatCompletionStreamChoiceDelta{ |
| 67 | + Role: "assistant", |
| 68 | + Content: "Hello", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + ID: "test-2", |
| 75 | + Object: "chat.completion.chunk", |
| 76 | + Model: "gpt-3.5-turbo", |
| 77 | + Choices: []openai.ChatCompletionStreamChoice{ |
| 78 | + { |
| 79 | + Index: 0, |
| 80 | + Delta: openai.ChatCompletionStreamChoiceDelta{ |
| 81 | + Content: " World", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + ID: "test-3", |
| 88 | + Object: "chat.completion.chunk", |
| 89 | + Model: "gpt-3.5-turbo", |
| 90 | + Choices: []openai.ChatCompletionStreamChoice{ |
| 91 | + { |
| 92 | + Index: 0, |
| 93 | + Delta: openai.ChatCompletionStreamChoiceDelta{}, |
| 94 | + FinishReason: "stop", |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + // Create mock client with custom stream function |
| 101 | + mockClient := &MockOpenAIStreamClient{ |
| 102 | + CreateChatCompletionStreamFn: func( |
| 103 | + _ context.Context, _ openai.ChatCompletionRequest, |
| 104 | + ) (*openai.ChatCompletionStream, error) { |
| 105 | + // Create a mock stream reader with our expected responses |
| 106 | + mockStreamReader := &mockStreamReader{ |
| 107 | + responses: expectedResponses, |
| 108 | + index: 0, |
| 109 | + } |
| 110 | + // Return a new ChatCompletionStream with our mock reader |
| 111 | + return openai.NewChatCompletionStream(mockStreamReader), nil |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + // Test the mock client |
| 116 | + stream, err := mockClient.CreateChatCompletionStream( |
| 117 | + context.Background(), |
| 118 | + openai.ChatCompletionRequest{ |
| 119 | + Model: openai.GPT3Dot5Turbo, |
| 120 | + Messages: []openai.ChatCompletionMessage{ |
| 121 | + { |
| 122 | + Role: openai.ChatMessageRoleUser, |
| 123 | + Content: "Hello!", |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + ) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("CreateChatCompletionStream returned error: %v", err) |
| 130 | + } |
| 131 | + defer stream.Close() |
| 132 | + |
| 133 | + // Verify we get back exactly the responses we configured |
| 134 | + fullResponse := "" |
| 135 | + for i, expectedResponse := range expectedResponses { |
| 136 | + receivedResponse, streamErr := stream.Recv() |
| 137 | + if streamErr != nil { |
| 138 | + t.Fatalf("stream.Recv() failed at index %d: %v", i, streamErr) |
| 139 | + } |
| 140 | + |
| 141 | + // Additional specific checks |
| 142 | + if receivedResponse.ID != expectedResponse.ID { |
| 143 | + t.Errorf("Response %d ID mismatch. Expected: %s, Got: %s", |
| 144 | + i, expectedResponse.ID, receivedResponse.ID) |
| 145 | + } |
| 146 | + if len(receivedResponse.Choices) > 0 && len(expectedResponse.Choices) > 0 { |
| 147 | + expectedContent := expectedResponse.Choices[0].Delta.Content |
| 148 | + receivedContent := receivedResponse.Choices[0].Delta.Content |
| 149 | + if receivedContent != expectedContent { |
| 150 | + t.Errorf("Response %d content mismatch. Expected: %s, Got: %s", |
| 151 | + i, expectedContent, receivedContent) |
| 152 | + } |
| 153 | + fullResponse += receivedContent |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Verify EOF at the end |
| 158 | + _, streamErr := stream.Recv() |
| 159 | + if !errors.Is(streamErr, io.EOF) { |
| 160 | + t.Errorf("Expected EOF at end of stream, got: %v", streamErr) |
| 161 | + } |
| 162 | + |
| 163 | + // Verify the full assembled response |
| 164 | + expectedFullResponse := "Hello World" |
| 165 | + if fullResponse != expectedFullResponse { |
| 166 | + t.Errorf("Full response mismatch. Expected: %s, Got: %s", expectedFullResponse, fullResponse) |
| 167 | + } |
| 168 | + |
| 169 | + t.Log("✅ Successfully demonstrated mock OpenAI client with streaming responses!") |
| 170 | + t.Logf(" Full response assembled: %q", fullResponse) |
| 171 | +} |
| 172 | + |
| 173 | +// TestMockOpenAIStreamClient_ErrorHandling demonstrates error handling. |
| 174 | +func TestMockOpenAIStreamClient_ErrorHandling(t *testing.T) { |
| 175 | + expectedError := errors.New("mock stream error") |
| 176 | + |
| 177 | + mockClient := &MockOpenAIStreamClient{ |
| 178 | + ChatCompletionStreamErr: expectedError, |
| 179 | + } |
| 180 | + |
| 181 | + _, err := mockClient.CreateChatCompletionStream( |
| 182 | + context.Background(), |
| 183 | + openai.ChatCompletionRequest{ |
| 184 | + Model: openai.GPT3Dot5Turbo, |
| 185 | + Messages: []openai.ChatCompletionMessage{ |
| 186 | + { |
| 187 | + Role: openai.ChatMessageRoleUser, |
| 188 | + Content: "Hello!", |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + ) |
| 193 | + |
| 194 | + if !errors.Is(err, expectedError) { |
| 195 | + t.Errorf("Expected error %v, got %v", expectedError, err) |
| 196 | + } |
| 197 | + |
| 198 | + t.Log("✅ Successfully demonstrated mock OpenAI client error handling!") |
| 199 | +} |
0 commit comments