forked from hra42/openrouter-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_utils_test.go
More file actions
432 lines (371 loc) · 10.4 KB
/
Copy pathtext_utils_test.go
File metadata and controls
432 lines (371 loc) · 10.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package openrouter
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestReadTextFile(t *testing.T) {
tests := []struct {
name string
extension string
content string
wantErr bool
errContains string
}{
{
name: "Valid TXT file",
extension: ".txt",
content: "Hello, World!",
wantErr: false,
},
{
name: "Valid Markdown file",
extension: ".md",
content: "# Heading\n\nParagraph",
wantErr: false,
},
{
name: "Valid JSON file",
extension: ".json",
content: `{"key": "value"}`,
wantErr: false,
},
{
name: "Valid Python file",
extension: ".py",
content: "def hello():\n print('Hello')",
wantErr: false,
},
{
name: "Valid Go file",
extension: ".go",
content: "package main\n\nfunc main() {}",
wantErr: false,
},
{
name: "Valid JavaScript file",
extension: ".js",
content: "console.log('hello');",
wantErr: false,
},
{
name: "Valid YAML file",
extension: ".yaml",
content: "key: value",
wantErr: false,
},
{
name: "Unsupported format .exe",
extension: ".exe",
content: "binary data",
wantErr: true,
errContains: "unsupported text file format",
},
{
name: "Unsupported format .bin",
extension: ".bin",
content: "binary data",
wantErr: true,
errContains: "unsupported text file format",
},
{
name: "Uppercase extension .TXT",
extension: ".TXT",
content: "Hello",
wantErr: false,
},
{
name: "Uppercase extension .PY",
extension: ".PY",
content: "print('hello')",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test"+tt.extension)
if err := os.WriteFile(tmpFile, []byte(tt.content), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
content, err := ReadTextFile(tmpFile)
if (err != nil) != tt.wantErr {
t.Errorf("ReadTextFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if tt.errContains != "" && err != nil {
if !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("ReadTextFile() error = %v, should contain %v", err, tt.errContains)
}
}
return
}
if content != tt.content {
t.Errorf("ReadTextFile() content = %v, want %v", content, tt.content)
}
})
}
}
func TestReadTextFile_InvalidUTF8(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.txt")
// Write invalid UTF-8 bytes
invalidUTF8 := []byte{0xff, 0xfe, 0xfd}
if err := os.WriteFile(tmpFile, invalidUTF8, 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
_, err := ReadTextFile(tmpFile)
if err == nil {
t.Error("Expected error for invalid UTF-8, got nil")
}
if !strings.Contains(err.Error(), "invalid UTF-8") {
t.Errorf("Error should mention invalid UTF-8, got: %v", err)
}
}
func TestReadTextFileWithFilename(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.py")
expectedContent := "print('hello')"
if err := os.WriteFile(tmpFile, []byte(expectedContent), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
content, filename, err := ReadTextFileWithFilename(tmpFile)
if err != nil {
t.Fatalf("ReadTextFileWithFilename() error = %v", err)
}
if content != expectedContent {
t.Errorf("content = %v, want %v", content, expectedContent)
}
if filename != "test.py" {
t.Errorf("filename = %v, want test.py", filename)
}
}
func TestCreateUserMessageWithTextFile(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "code.js")
code := "console.log('hello');"
if err := os.WriteFile(tmpFile, []byte(code), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
msg, err := CreateUserMessageWithTextFile("Review this code:", tmpFile)
if err != nil {
t.Fatalf("CreateUserMessageWithTextFile() error = %v", err)
}
if msg.Role != "user" {
t.Errorf("Role = %v, want user", msg.Role)
}
content, ok := msg.Content.(string)
if !ok {
t.Fatalf("Content is not a string, got %T", msg.Content)
}
if !strings.Contains(content, "Review this code:") {
t.Error("Content should contain user text")
}
if !strings.Contains(content, "code.js") {
t.Error("Content should contain filename")
}
if !strings.Contains(content, code) {
t.Error("Content should contain file content")
}
if !strings.Contains(content, "===") {
t.Error("Content should contain filename header markers")
}
}
func TestCreateUserMessageWithTextFiles(t *testing.T) {
tmpDir := t.TempDir()
file1 := filepath.Join(tmpDir, "file1.txt")
file2 := filepath.Join(tmpDir, "file2.txt")
_ = os.WriteFile(file1, []byte("Content 1"), 0644)
_ = os.WriteFile(file2, []byte("Content 2"), 0644)
msg, err := CreateUserMessageWithTextFiles("Review these files:", file1, file2)
if err != nil {
t.Fatalf("CreateUserMessageWithTextFiles() error = %v", err)
}
content, ok := msg.Content.(string)
if !ok {
t.Fatalf("Content is not a string, got %T", msg.Content)
}
if !strings.Contains(content, "file1.txt") {
t.Error("Content should contain first filename")
}
if !strings.Contains(content, "file2.txt") {
t.Error("Content should contain second filename")
}
if !strings.Contains(content, "Content 1") {
t.Error("Content should contain first file content")
}
if !strings.Contains(content, "Content 2") {
t.Error("Content should contain second file content")
}
}
func TestCreateUserMessageWithTextContent(t *testing.T) {
content := "SELECT * FROM users;"
text := "Here's a query:"
label := "query.sql"
msg := CreateUserMessageWithTextContent(text, content, label)
if msg.Role != "user" {
t.Errorf("Role = %v, want user", msg.Role)
}
msgContent, ok := msg.Content.(string)
if !ok {
t.Fatalf("Content is not a string, got %T", msg.Content)
}
if !strings.Contains(msgContent, text) {
t.Error("Content should contain text")
}
if !strings.Contains(msgContent, label) {
t.Error("Content should contain label")
}
if !strings.Contains(msgContent, content) {
t.Error("Content should contain query content")
}
}
func TestCreateUserMessageWithTextContent_NoLabel(t *testing.T) {
content := "print('hello')"
text := "Here's code:"
msg := CreateUserMessageWithTextContent(text, content, "")
msgContent, ok := msg.Content.(string)
if !ok {
t.Fatalf("Content is not a string, got %T", msg.Content)
}
if !strings.Contains(msgContent, text) {
t.Error("Content should contain text")
}
if !strings.Contains(msgContent, content) {
t.Error("Content should contain code content")
}
}
func TestContentBuilder_AddTextFile(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.go")
goCode := "package main\n\nfunc main() {}"
if err := os.WriteFile(tmpFile, []byte(goCode), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
cb := NewContentBuilder()
cb.AddText("Here's my code:")
cb, err := cb.AddTextFile(tmpFile)
if err != nil {
t.Fatalf("AddTextFile() error = %v", err)
}
parts := cb.Build()
if len(parts) != 2 {
t.Fatalf("Expected 2 parts, got %d", len(parts))
}
if parts[0].Type != "text" || parts[0].Text != "Here's my code:" {
t.Error("First part should be user text")
}
if parts[1].Type != "text" {
t.Error("Second part should be text type")
}
if !strings.Contains(parts[1].Text, "test.go") {
t.Error("Second part should contain filename")
}
if !strings.Contains(parts[1].Text, goCode) {
t.Error("Second part should contain code")
}
}
func TestContentBuilder_AddTextFileWithLabel(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.md")
content := "# Title\n\nContent"
if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
cb := NewContentBuilder()
cb, err := cb.AddTextFileWithLabel(tmpFile, "custom_label")
if err != nil {
t.Fatalf("AddTextFileWithLabel() error = %v", err)
}
parts := cb.Build()
if len(parts) != 1 {
t.Fatalf("Expected 1 part, got %d", len(parts))
}
if !strings.Contains(parts[0].Text, "custom_label") {
t.Error("Should contain custom label")
}
if !strings.Contains(parts[0].Text, content) {
t.Error("Should contain file content")
}
}
func TestContentBuilder_AddTextContent(t *testing.T) {
cb := NewContentBuilder()
content := "SELECT * FROM users;"
cb.AddText("Here's a query:")
cb.AddTextContent(content, "query.sql")
parts := cb.Build()
if len(parts) != 2 {
t.Fatalf("Expected 2 parts, got %d", len(parts))
}
if !strings.Contains(parts[1].Text, "query.sql") {
t.Error("Should contain label")
}
if !strings.Contains(parts[1].Text, content) {
t.Error("Should contain content")
}
}
func TestContentBuilder_AddTextContent_NoLabel(t *testing.T) {
cb := NewContentBuilder()
content := "print('hello')"
cb.AddText("Code:")
cb.AddTextContent(content, "")
parts := cb.Build()
if len(parts) != 2 {
t.Fatalf("Expected 2 parts, got %d", len(parts))
}
if !strings.Contains(parts[1].Text, content) {
t.Error("Should contain content")
}
}
func TestValidateTextContent(t *testing.T) {
tests := []struct {
name string
content string
wantErr bool
}{
{
name: "Valid UTF-8",
content: "Hello, 世界! 🌍",
wantErr: false,
},
{
name: "Simple ASCII",
content: "Hello World",
wantErr: false,
},
{
name: "Invalid UTF-8",
content: string([]byte{0xff, 0xfe}),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateTextContent(tt.content)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateTextContent() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestReadTextFile_FileNotFound(t *testing.T) {
_, err := ReadTextFile("/nonexistent/path/file.txt")
if err == nil {
t.Error("Expected error for nonexistent file")
}
if !strings.Contains(err.Error(), "failed to read") {
t.Errorf("Error should mention read failure, got: %v", err)
}
}
func TestCreateUserMessageWithTextFiles_MissingFile(t *testing.T) {
tmpDir := t.TempDir()
existingFile := filepath.Join(tmpDir, "exists.txt")
nonexistentFile := filepath.Join(tmpDir, "nonexistent.txt")
_ = os.WriteFile(existingFile, []byte("content"), 0644)
_, err := CreateUserMessageWithTextFiles("text", existingFile, nonexistentFile)
if err == nil {
t.Error("Expected error for missing file")
}
}