|
| 1 | +package openai //nolint:testpackage // testing private field |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/sashabaranov/go-openai/internal/test" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + errTestUnmarshalerFailed = errors.New("test unmarshaler failed") |
| 14 | + errTestErrorAccumulatorWriteFailed = errors.New("test error accumulator failed") |
| 15 | +) |
| 16 | + |
| 17 | +type ( |
| 18 | + failingUnMarshaller struct{} |
| 19 | + failingErrorBuffer struct{} |
| 20 | +) |
| 21 | + |
| 22 | +func (b *failingErrorBuffer) Write(_ []byte) (n int, err error) { |
| 23 | + return 0, errTestErrorAccumulatorWriteFailed |
| 24 | +} |
| 25 | + |
| 26 | +func (b *failingErrorBuffer) Len() int { |
| 27 | + return 0 |
| 28 | +} |
| 29 | + |
| 30 | +func (b *failingErrorBuffer) Bytes() []byte { |
| 31 | + return []byte{} |
| 32 | +} |
| 33 | + |
| 34 | +func (*failingUnMarshaller) unmarshal(_ []byte, _ any) error { |
| 35 | + return errTestUnmarshalerFailed |
| 36 | +} |
| 37 | + |
| 38 | +func TestErrorAccumulatorReturnsUnmarshalerErrors(t *testing.T) { |
| 39 | + accumulator := &errorAccumulate{ |
| 40 | + buffer: &bytes.Buffer{}, |
| 41 | + unmarshaler: &failingUnMarshaller{}, |
| 42 | + } |
| 43 | + |
| 44 | + err := accumulator.write([]byte("{")) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("%+v", err) |
| 47 | + } |
| 48 | + _, err = accumulator.unmarshalError() |
| 49 | + if !errors.Is(err, errTestUnmarshalerFailed) { |
| 50 | + t.Fatalf("Did not return error when unmarshaler failed: %v", err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestErrorByteWriteErrors(t *testing.T) { |
| 55 | + accumulator := &errorAccumulate{ |
| 56 | + buffer: &failingErrorBuffer{}, |
| 57 | + unmarshaler: &jsonUnmarshaler{}, |
| 58 | + } |
| 59 | + err := accumulator.write([]byte("{")) |
| 60 | + if !errors.Is(err, errTestErrorAccumulatorWriteFailed) { |
| 61 | + t.Fatalf("Did not return error when write failed: %v", err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestErrorAccumulatorWriteErrors(t *testing.T) { |
| 66 | + var err error |
| 67 | + ts := test.NewTestServer().OpenAITestServer() |
| 68 | + ts.Start() |
| 69 | + defer ts.Close() |
| 70 | + |
| 71 | + config := DefaultConfig(test.GetTestToken()) |
| 72 | + config.BaseURL = ts.URL + "/v1" |
| 73 | + client := NewClientWithConfig(config) |
| 74 | + |
| 75 | + ctx := context.Background() |
| 76 | + |
| 77 | + stream, err := client.CreateChatCompletionStream(ctx, ChatCompletionRequest{}) |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + stream.errAccumulator = &errorAccumulate{ |
| 82 | + buffer: &failingErrorBuffer{}, |
| 83 | + unmarshaler: &jsonUnmarshaler{}, |
| 84 | + } |
| 85 | + |
| 86 | + _, err = stream.Recv() |
| 87 | + if !errors.Is(err, errTestErrorAccumulatorWriteFailed) { |
| 88 | + t.Fatalf("Did not return error when write failed: %v", err) |
| 89 | + } |
| 90 | +} |
0 commit comments