-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt_loader_test.go
More file actions
621 lines (562 loc) · 14.3 KB
/
prompt_loader_test.go
File metadata and controls
621 lines (562 loc) · 14.3 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestLoadPrompt_PlainText(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "simple text",
input: "You are a helpful assistant",
expected: "You are a helpful assistant",
},
{
name: "multiline text",
input: "Line 1\nLine 2\nLine 3",
expected: "Line 1\nLine 2\nLine 3",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "text with special characters",
input: "You are a code reviewer! Check for: bugs, performance, security.",
expected: "You are a code reviewer! Check for: bugs, performance, security.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := LoadPrompt(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestLoadPrompt_File(t *testing.T) {
// Create a temporary directory
tmpDir := t.TempDir()
// Test cases
tests := []struct {
name string
fileContent string
fileName string
useFileURI bool
wantError bool
}{
{
name: "simple file",
fileContent: "You are a helpful assistant from file",
fileName: "prompt.txt",
useFileURI: false,
wantError: false,
},
{
name: "file with file:// prefix",
fileContent: "You are a helpful assistant with file:// prefix",
fileName: "prompt2.txt",
useFileURI: true,
wantError: false,
},
{
name: "multiline file",
fileContent: "Line 1\nLine 2\nLine 3",
fileName: "multiline.txt",
useFileURI: false,
wantError: false,
},
{
name: "file not found with file:// prefix",
fileContent: "",
fileName: "nonexistent.txt",
useFileURI: true,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filePath := filepath.Join(tmpDir, tt.fileName)
// Create file if content is provided
if tt.fileContent != "" {
err := os.WriteFile(filePath, []byte(tt.fileContent), 0o600)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
}
// Prepare input with or without file:// prefix
input := filePath
if tt.useFileURI {
input = "file://" + filePath
}
// Test LoadPrompt
result, err := LoadPrompt(input)
if tt.wantError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.fileContent {
t.Errorf("expected %q, got %q", tt.fileContent, result)
}
})
}
}
func TestLoadPrompt_URL(t *testing.T) {
tests := []struct {
name string
content string
statusCode int
wantError bool
errorString string
}{
{
name: "successful fetch",
content: "You are a helpful assistant from URL",
statusCode: http.StatusOK,
wantError: false,
},
{
name: "404 not found",
content: "",
statusCode: http.StatusNotFound,
wantError: true,
errorString: "status code 404",
},
{
name: "500 server error",
content: "",
statusCode: http.StatusInternalServerError,
wantError: true,
errorString: "status code 500",
},
{
name: "multiline content",
content: "Line 1\nLine 2\nLine 3",
statusCode: http.StatusOK,
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create test server
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check User-Agent header
if userAgent := r.Header.Get("User-Agent"); userAgent != "LLM-Action/1.0" {
t.Errorf("expected User-Agent 'LLM-Action/1.0', got %q", userAgent)
}
w.WriteHeader(tt.statusCode)
if tt.statusCode == http.StatusOK {
if _, err := w.Write([]byte(tt.content)); err != nil {
t.Errorf("failed to write response: %v", err)
}
}
}),
)
defer server.Close()
// Test LoadPrompt
result, err := LoadPrompt(server.URL)
if tt.wantError {
if err == nil {
t.Error("expected error, got nil")
} else if tt.errorString != "" && !strings.Contains(err.Error(), tt.errorString) {
t.Errorf("expected error to contain %q, got %q", tt.errorString, err.Error())
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.content {
t.Errorf("expected %q, got %q", tt.content, result)
}
})
}
}
func TestLoadPrompt_InvalidURL(t *testing.T) {
// Test with invalid URL
_, err := LoadPrompt("http://invalid-domain-that-does-not-exist-12345.com/prompt.txt")
if err == nil {
t.Error("expected error for invalid URL, got nil")
}
}
func TestIsURL(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"http://example.com", true},
{"https://example.com", true},
{"https://example.com/path/to/file.txt", true},
{"file://path/to/file", false},
{"/path/to/file", false},
{"plain text", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := isURL(tt.input)
if result != tt.expected {
t.Errorf("isURL(%q) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}
func TestIsFilePath(t *testing.T) {
// Create a temporary file
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.txt")
err := os.WriteFile(tmpFile, []byte("test"), 0o600)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
tests := []struct {
name string
input string
expected bool
}{
{
name: "existing file",
input: tmpFile,
expected: true,
},
{
name: "file:// with existing file",
input: "file://" + tmpFile,
expected: true,
},
{
name: "file:// with non-existing file",
input: "file:///non/existent/file.txt",
expected: true, // Returns true because of file:// prefix
},
{
name: "non-existing file without file://",
input: "/non/existent/file.txt",
expected: false,
},
{
name: "URL",
input: "https://example.com/file.txt",
expected: false,
},
{
name: "plain text",
input: "You are a helpful assistant",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isFilePath(tt.input)
if result != tt.expected {
t.Errorf("isFilePath(%q) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}
func TestLoadPrompt_WithTemplate(t *testing.T) {
tests := []struct {
name string
input string
envVars map[string]string
expected string
wantErr bool
}{
{
name: "plain text with template variable",
input: "Hello, {{.NAME}}!",
envVars: map[string]string{"NAME": "World"},
expected: "Hello, World!",
wantErr: false,
},
{
name: "template with INPUT_ prefix removal",
input: "Model: {{.MODEL}}, Temperature: {{.TEMPERATURE}}",
envVars: map[string]string{"INPUT_MODEL": "gpt-4", "INPUT_TEMPERATURE": "0.7"},
expected: "Model: gpt-4, Temperature: 0.7",
wantErr: false,
},
{
name: "template with GitHub Actions variables",
input: "Analyzing {{.GITHUB_REPOSITORY}} on branch {{.GITHUB_REF_NAME}}",
envVars: map[string]string{
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_REF_NAME": "main",
},
expected: "Analyzing owner/repo on branch main",
wantErr: false,
},
{
name: "template with conditional",
input: "{{if .DEBUG}}Debug enabled{{else}}Debug disabled{{end}}",
envVars: map[string]string{"DEBUG": "true"},
expected: "Debug enabled",
wantErr: false,
},
{
name: "invalid template syntax",
input: "{{.VAR",
envVars: map[string]string{},
expected: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up environment variables
for key, value := range tt.envVars {
os.Setenv(key, value)
}
defer func() {
for key := range tt.envVars {
os.Unsetenv(key)
}
}()
result, err := LoadPrompt(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("LoadPrompt() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && result != tt.expected {
t.Errorf("LoadPrompt() = %q, want %q", result, tt.expected)
}
})
}
}
func TestLoadPrompt_FileWithTemplate(t *testing.T) {
// Create a temporary directory
tmpDir := t.TempDir()
// Create a file with template content
templateContent := "Repository: {{.GITHUB_REPOSITORY}}\nModel: {{.MODEL}}"
filePath := filepath.Join(tmpDir, "template.txt")
err := os.WriteFile(filePath, []byte(templateContent), 0o600)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
// Set up environment variables
os.Setenv("GITHUB_REPOSITORY", "appleboy/LLM-action")
os.Setenv("INPUT_MODEL", "gpt-4o")
defer func() {
os.Unsetenv("GITHUB_REPOSITORY")
os.Unsetenv("INPUT_MODEL")
}()
// Test LoadPrompt with file path
result, err := LoadPrompt(filePath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "Repository: appleboy/LLM-action\nModel: gpt-4o"
if result != expected {
t.Errorf("LoadPrompt() = %q, want %q", result, expected)
}
}
func TestLoadPrompt_URLWithTemplate(t *testing.T) {
// Create test server that returns template content
templateContent := "URL Test: {{.TEST_VAR}}"
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(templateContent)); err != nil {
t.Errorf("failed to write response: %v", err)
}
}),
)
defer server.Close()
// Set up environment variable
os.Setenv("TEST_VAR", "success")
defer os.Unsetenv("TEST_VAR")
// Test LoadPrompt with URL
result, err := LoadPrompt(server.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "URL Test: success"
if result != expected {
t.Errorf("LoadPrompt() = %q, want %q", result, expected)
}
}
// Tests for LoadContent function (without template rendering)
func TestLoadContent_PlainText(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "simple text",
input: "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----",
expected: "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "text with template syntax should not be rendered",
input: "{{.SHOULD_NOT_RENDER}}",
expected: "{{.SHOULD_NOT_RENDER}}",
},
}
// Set up environment variable that should NOT be rendered
os.Setenv("SHOULD_NOT_RENDER", "rendered")
defer os.Unsetenv("SHOULD_NOT_RENDER")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := LoadContent(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestLoadContent_File(t *testing.T) {
// Create a temporary directory
tmpDir := t.TempDir()
// Create test file with certificate-like content
certContent := "-----BEGIN CERTIFICATE-----\nMIIDxTCCAq2gAwIBAgIQAqx...\n-----END CERTIFICATE-----"
filePath := filepath.Join(tmpDir, "ca-cert.pem")
err := os.WriteFile(filePath, []byte(certContent), 0o600)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
tests := []struct {
name string
input string
expected string
wantError bool
}{
{
name: "file path",
input: filePath,
expected: certContent,
wantError: false,
},
{
name: "file:// prefix",
input: "file://" + filePath,
expected: certContent,
wantError: false,
},
{
name: "non-existent file with file:// prefix",
input: "file:///non/existent/file.pem",
expected: "",
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := LoadContent(tt.input)
if tt.wantError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestLoadContent_URL(t *testing.T) {
certContent := "-----BEGIN CERTIFICATE-----\nMIIDxTCCAq2gAwIBAgIQAqx...\n-----END CERTIFICATE-----"
tests := []struct {
name string
content string
statusCode int
wantError bool
}{
{
name: "successful fetch",
content: certContent,
statusCode: http.StatusOK,
wantError: false,
},
{
name: "404 not found",
content: "",
statusCode: http.StatusNotFound,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.statusCode)
if tt.statusCode == http.StatusOK {
if _, err := w.Write([]byte(tt.content)); err != nil {
t.Errorf("failed to write response: %v", err)
}
}
}),
)
defer server.Close()
result, err := LoadContent(server.URL)
if tt.wantError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.content {
t.Errorf("expected %q, got %q", tt.content, result)
}
})
}
}
func TestLoadContent_NoTemplateRendering(t *testing.T) {
// Create a temporary file with template syntax
tmpDir := t.TempDir()
templateContent := "Content with {{.VAR}} that should not be rendered"
filePath := filepath.Join(tmpDir, "template.txt")
err := os.WriteFile(filePath, []byte(templateContent), 0o600)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
// Set environment variable
os.Setenv("VAR", "should-not-appear")
defer os.Unsetenv("VAR")
// LoadContent should NOT render the template
result, err := LoadContent(filePath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// The template syntax should remain as-is
if result != templateContent {
t.Errorf(
"LoadContent() should not render templates, got %q, want %q",
result,
templateContent,
)
}
}