File size: 3,933 Bytes
7cd5cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package openai

import (
	"bytes"
	"errors"
	"io"
	"net/http"
	"testing"
	"time"

	"github.com/QuantumNous/new-api/common"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestConvertOpenAIImageURLsToBase64Disabled(t *testing.T) {
	t.Setenv("CODEX_IMAGE_URL_TO_B64", "false")
	body := []byte(`{"created":1,"data":[{"url":"https://example.com/image.png"}]}`)

	converted, stats, err := convertOpenAIImageURLsToBase64(body, func(string, ...string) (*http.Response, error) {
		t.Fatal("download must not run while compatibility is disabled")
		return nil, nil
	})

	require.NoError(t, err)
	assert.Equal(t, body, converted)
	assert.Zero(t, stats.Converted)
}

func TestConvertOpenAIImageURLsToBase64PreservesResponseFields(t *testing.T) {
	t.Setenv("CODEX_IMAGE_URL_TO_B64", "true")
	image := []byte("test-image")
	body := []byte(`{"created":1,"data":[{"url":"https://example.com/image.png","revised_prompt":"kept"}],"usage":{"total_tokens":7},"custom":{"kept":true}}`)

	converted, stats, err := convertOpenAIImageURLsToBase64(body, func(rawURL string, reason ...string) (*http.Response, error) {
		assert.Equal(t, "https://example.com/image.png", rawURL)
		assert.Equal(t, []string{"OpenAI image URL compatibility"}, reason)
		return &http.Response{
			StatusCode:    http.StatusOK,
			ContentLength: int64(len(image)),
			Body:          io.NopCloser(bytes.NewReader(image)),
		}, nil
	})

	require.NoError(t, err)
	assert.Equal(t, 1, stats.Converted)
	assert.Equal(t, int64(len(image)), stats.DownloadedBytes)

	var payload map[string]any
	require.NoError(t, common.Unmarshal(converted, &payload))
	data := payload["data"].([]any)
	item := data[0].(map[string]any)
	assert.Equal(t, "dGVzdC1pbWFnZQ==", item["b64_json"])
	assert.Equal(t, "kept", item["revised_prompt"])
	assert.NotContains(t, item, "url")
	assert.Equal(t, float64(7), payload["usage"].(map[string]any)["total_tokens"])
	assert.Equal(t, true, payload["custom"].(map[string]any)["kept"])
	assert.Contains(t, stats.ServerTiming(), "image-url-compat")
}

func TestReadImageURLCompatResponseRejectsOversize(t *testing.T) {
	response := &http.Response{
		StatusCode:    http.StatusOK,
		ContentLength: maxImageURLCompatBytes + 1,
		Body:          io.NopCloser(bytes.NewReader([]byte("unused"))),
	}

	_, err := readImageURLCompatResponse(response)

	require.Error(t, err)
	assert.Contains(t, err.Error(), "exceeds")
}

func TestDownloadOpenAIImageURLCompatRejectsPrivateLiteral(t *testing.T) {
	_, err := downloadOpenAIImageURLCompat("https://127.0.0.1/image.png")

	require.Error(t, err)
	assert.Contains(t, err.Error(), "private IP address not allowed")
}

func TestRetryImageURLCompatDownloadRecoversFromTransientError(t *testing.T) {
	attempts := 0
	waits := 0

	response, err := retryImageURLCompatDownload(func() (*http.Response, error) {
		attempts++
		if attempts == 1 {
			return nil, errors.New("EOF")
		}
		return &http.Response{
			StatusCode: http.StatusOK,
			Body:       io.NopCloser(bytes.NewBufferString("image")),
		}, nil
	}, func(time.Duration) {
		waits++
	})

	require.NoError(t, err)
	require.NotNil(t, response)
	assert.Equal(t, 2, attempts)
	assert.Equal(t, 1, waits)
}

func TestRetryImageURLCompatDownloadStopsAfterLimit(t *testing.T) {
	attempts := 0

	response, err := retryImageURLCompatDownload(func() (*http.Response, error) {
		attempts++
		return nil, errors.New("EOF")
	}, func(time.Duration) {})

	require.Error(t, err)
	assert.Nil(t, response)
	assert.Equal(t, maxImageURLCompatDownloadAttempts, attempts)
}

func TestRetryImageURLCompatDownloadRejectsEmptyResponse(t *testing.T) {
	attempts := 0

	response, err := retryImageURLCompatDownload(func() (*http.Response, error) {
		attempts++
		return nil, nil
	}, func(time.Duration) {})

	require.Error(t, err)
	assert.Nil(t, response)
	assert.Contains(t, err.Error(), "empty response")
	assert.Equal(t, maxImageURLCompatDownloadAttempts, attempts)
}