File size: 9,269 Bytes
7def60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package openai

import (
	"encoding/json"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"

	"github.com/rs/zerolog/log"

	"github.com/mudler/LocalAI/core/config"
	"github.com/mudler/LocalAI/core/schema"

	"github.com/gofiber/fiber/v2"
	utils2 "github.com/mudler/LocalAI/pkg/utils"
	"github.com/stretchr/testify/assert"

	"testing"
)

func startUpApp() (app *fiber.App, option *config.ApplicationConfig, loader *config.BackendConfigLoader) {
	// Preparing the mocked objects
	loader = &config.BackendConfigLoader{}

	option = &config.ApplicationConfig{
		UploadLimitMB: 10,
		UploadDir:     "test_dir",
	}

	_ = os.RemoveAll(option.UploadDir)

	app = fiber.New(fiber.Config{
		BodyLimit: 20 * 1024 * 1024, // sets the limit to 20MB.
	})

	// Create a Test Server
	app.Post("/files", UploadFilesEndpoint(loader, option))
	app.Get("/files", ListFilesEndpoint(loader, option))
	app.Get("/files/:file_id", GetFilesEndpoint(loader, option))
	app.Delete("/files/:file_id", DeleteFilesEndpoint(loader, option))
	app.Get("/files/:file_id/content", GetFilesContentsEndpoint(loader, option))

	return
}

func TestUploadFileExceedSizeLimit(t *testing.T) {
	// Preparing the mocked objects
	loader := &config.BackendConfigLoader{}

	option := &config.ApplicationConfig{
		UploadLimitMB: 10,
		UploadDir:     "test_dir",
	}

	_ = os.RemoveAll(option.UploadDir)

	app := fiber.New(fiber.Config{
		BodyLimit: 20 * 1024 * 1024, // sets the limit to 20MB.
	})

	// Create a Test Server
	app.Post("/files", UploadFilesEndpoint(loader, option))
	app.Get("/files", ListFilesEndpoint(loader, option))
	app.Get("/files/:file_id", GetFilesEndpoint(loader, option))
	app.Delete("/files/:file_id", DeleteFilesEndpoint(loader, option))
	app.Get("/files/:file_id/content", GetFilesContentsEndpoint(loader, option))

	t.Run("UploadFilesEndpoint file size exceeds limit", func(t *testing.T) {
		t.Cleanup(tearDown())
		resp, err := CallFilesUploadEndpoint(t, app, "foo.txt", "file", "fine-tune", 11, option)
		assert.NoError(t, err)

		assert.Equal(t, fiber.StatusBadRequest, resp.StatusCode)
		assert.Contains(t, bodyToString(resp, t), "exceeds upload limit")
	})
	t.Run("UploadFilesEndpoint purpose not defined", func(t *testing.T) {
		t.Cleanup(tearDown())
		resp, _ := CallFilesUploadEndpoint(t, app, "foo.txt", "file", "", 5, option)

		assert.Equal(t, fiber.StatusBadRequest, resp.StatusCode)
		assert.Contains(t, bodyToString(resp, t), "Purpose is not defined")
	})
	t.Run("UploadFilesEndpoint file already exists", func(t *testing.T) {
		t.Cleanup(tearDown())
		f1 := CallFilesUploadEndpointWithCleanup(t, app, "foo.txt", "file", "fine-tune", 5, option)

		resp, err := CallFilesUploadEndpoint(t, app, "foo.txt", "file", "fine-tune", 5, option)
		fmt.Println(f1)
		fmt.Printf("ERror: %v\n", err)
		fmt.Printf("resp: %+v\n", resp)

		assert.Equal(t, fiber.StatusBadRequest, resp.StatusCode)
		assert.Contains(t, bodyToString(resp, t), "File already exists")
	})
	t.Run("UploadFilesEndpoint file uploaded successfully", func(t *testing.T) {
		t.Cleanup(tearDown())
		file := CallFilesUploadEndpointWithCleanup(t, app, "test.txt", "file", "fine-tune", 5, option)

		// Check if file exists in the disk
		testName := strings.Split(t.Name(), "/")[1]
		fileName := testName + "-test.txt"
		filePath := filepath.Join(option.UploadDir, utils2.SanitizeFileName(fileName))
		_, err := os.Stat(filePath)

		assert.False(t, os.IsNotExist(err))
		assert.Equal(t, file.Bytes, 5242880)
		assert.NotEmpty(t, file.CreatedAt)
		assert.Equal(t, file.Filename, fileName)
		assert.Equal(t, file.Purpose, "fine-tune")
	})
	t.Run("ListFilesEndpoint without purpose parameter", func(t *testing.T) {
		t.Cleanup(tearDown())
		resp, err := CallListFilesEndpoint(t, app, "")
		assert.NoError(t, err)

		assert.Equal(t, 200, resp.StatusCode)

		listFiles := responseToListFile(t, resp)
		if len(listFiles.Data) != len(UploadedFiles) {
			t.Errorf("Expected %v files, got %v files", len(UploadedFiles), len(listFiles.Data))
		}
	})
	t.Run("ListFilesEndpoint with valid purpose parameter", func(t *testing.T) {
		t.Cleanup(tearDown())
		_ = CallFilesUploadEndpointWithCleanup(t, app, "test.txt", "file", "fine-tune", 5, option)

		resp, err := CallListFilesEndpoint(t, app, "fine-tune")
		assert.NoError(t, err)

		listFiles := responseToListFile(t, resp)
		if len(listFiles.Data) != 1 {
			t.Errorf("Expected 1 file, got %v files", len(listFiles.Data))
		}
	})
	t.Run("ListFilesEndpoint with invalid query parameter", func(t *testing.T) {
		t.Cleanup(tearDown())
		resp, err := CallListFilesEndpoint(t, app, "not-so-fine-tune")
		assert.NoError(t, err)
		assert.Equal(t, 200, resp.StatusCode)

		listFiles := responseToListFile(t, resp)

		if len(listFiles.Data) != 0 {
			t.Errorf("Expected 0 file, got %v files", len(listFiles.Data))
		}
	})
	t.Run("GetFilesContentsEndpoint get file content", func(t *testing.T) {
		t.Cleanup(tearDown())
		req := httptest.NewRequest("GET", "/files", nil)
		resp, _ := app.Test(req)
		assert.Equal(t, 200, resp.StatusCode)

		var listFiles schema.ListFiles
		if err := json.Unmarshal(bodyToByteArray(resp, t), &listFiles); err != nil {
			t.Errorf("Failed to decode response: %v", err)
			return
		}

		if len(listFiles.Data) != 0 {
			t.Errorf("Expected 0 file, got %v files", len(listFiles.Data))
		}
	})
}

func CallListFilesEndpoint(t *testing.T, app *fiber.App, purpose string) (*http.Response, error) {
	var target string
	if purpose != "" {
		target = fmt.Sprintf("/files?purpose=%s", purpose)
	} else {
		target = "/files"
	}
	req := httptest.NewRequest("GET", target, nil)
	return app.Test(req)
}

func CallFilesContentEndpoint(t *testing.T, app *fiber.App, fileId string) (*http.Response, error) {
	request := httptest.NewRequest("GET", "/files?file_id="+fileId, nil)
	return app.Test(request)
}

func CallFilesUploadEndpoint(t *testing.T, app *fiber.App, fileName, tag, purpose string, fileSize int, appConfig *config.ApplicationConfig) (*http.Response, error) {
	testName := strings.Split(t.Name(), "/")[1]

	// Create a file that exceeds the limit
	file := createTestFile(t, testName+"-"+fileName, fileSize, appConfig)

	// Creating a new HTTP Request
	body, writer := newMultipartFile(file.Name(), tag, purpose)

	req := httptest.NewRequest(http.MethodPost, "/files", body)
	req.Header.Set(fiber.HeaderContentType, writer.FormDataContentType())
	return app.Test(req)
}

func CallFilesUploadEndpointWithCleanup(t *testing.T, app *fiber.App, fileName, tag, purpose string, fileSize int, appConfig *config.ApplicationConfig) schema.File {
	// Create a file that exceeds the limit
	testName := strings.Split(t.Name(), "/")[1]
	file := createTestFile(t, testName+"-"+fileName, fileSize, appConfig)

	// Creating a new HTTP Request
	body, writer := newMultipartFile(file.Name(), tag, purpose)

	req := httptest.NewRequest(http.MethodPost, "/files", body)
	req.Header.Set(fiber.HeaderContentType, writer.FormDataContentType())
	resp, err := app.Test(req)
	assert.NoError(t, err)
	f := responseToFile(t, resp)

	//id := f.ID
	//t.Cleanup(func() {
	//	_, err := CallFilesDeleteEndpoint(t, app, id)
	//	assert.NoError(t, err)
	//	assert.Empty(t, UploadedFiles)
	//})

	return f

}

func CallFilesDeleteEndpoint(t *testing.T, app *fiber.App, fileId string) (*http.Response, error) {
	target := fmt.Sprintf("/files/%s", fileId)
	req := httptest.NewRequest(http.MethodDelete, target, nil)
	return app.Test(req)
}

// Helper to create multi-part file
func newMultipartFile(filePath, tag, purpose string) (*strings.Reader, *multipart.Writer) {
	body := new(strings.Builder)
	writer := multipart.NewWriter(body)
	file, _ := os.Open(filePath)
	defer file.Close()
	part, _ := writer.CreateFormFile(tag, filepath.Base(filePath))
	io.Copy(part, file)

	if purpose != "" {
		_ = writer.WriteField("purpose", purpose)
	}

	writer.Close()
	return strings.NewReader(body.String()), writer
}

// Helper to create test files
func createTestFile(t *testing.T, name string, sizeMB int, option *config.ApplicationConfig) *os.File {
	err := os.MkdirAll(option.UploadDir, 0750)
	if err != nil {

		t.Fatalf("Error MKDIR: %v", err)
	}

	file, err := os.Create(name)
	assert.NoError(t, err)
	file.WriteString(strings.Repeat("a", sizeMB*1024*1024)) // sizeMB MB File

	t.Cleanup(func() {
		os.Remove(name)
		os.RemoveAll(option.UploadDir)
	})
	return file
}

func bodyToString(resp *http.Response, t *testing.T) string {
	return string(bodyToByteArray(resp, t))
}

func bodyToByteArray(resp *http.Response, t *testing.T) []byte {
	bodyBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}
	return bodyBytes
}

func responseToFile(t *testing.T, resp *http.Response) schema.File {
	var file schema.File
	responseToString := bodyToString(resp, t)

	err := json.NewDecoder(strings.NewReader(responseToString)).Decode(&file)
	if err != nil {
		t.Errorf("Failed to decode response: %s", err)
	}

	return file
}

func responseToListFile(t *testing.T, resp *http.Response) schema.ListFiles {
	var listFiles schema.ListFiles
	responseToString := bodyToString(resp, t)

	err := json.NewDecoder(strings.NewReader(responseToString)).Decode(&listFiles)
	if err != nil {
		log.Error().Err(err).Msg("failed to decode response")
	}

	return listFiles
}