-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
825 lines (736 loc) · 20.4 KB
/
config_test.go
File metadata and controls
825 lines (736 loc) · 20.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
// testCACertContent is a sample CA certificate content for testing
const testCACertContent = "-----BEGIN CERTIFICATE-----\nMIIDxTCCAq2gAwIBAgIQAqx...\n-----END CERTIFICATE-----"
func TestLoadConfig(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
expectError bool
validate func(*testing.T, *Config)
}{
{
name: "Valid config with all fields",
envVars: map[string]string{
"INPUT_BASE_URL": "http://localhost:8080/v1",
"INPUT_API_KEY": "test-key",
"INPUT_MODEL": "gpt-4",
"INPUT_SKIP_SSL_VERIFY": "true",
"INPUT_SYSTEM_PROMPT": "You are helpful",
"INPUT_INPUT_PROMPT": "Hello",
"INPUT_TEMPERATURE": "0.5",
"INPUT_MAX_TOKENS": "500",
},
expectError: false,
validate: func(t *testing.T, c *Config) {
if c.BaseURL != "http://localhost:8080/v1" {
t.Errorf("expected base_url 'http://localhost:8080/v1', got '%s'", c.BaseURL)
}
if c.APIKey != "test-key" {
t.Errorf("expected api_key 'test-key', got '%s'", c.APIKey)
}
if c.Model != "gpt-4" {
t.Errorf("expected model 'gpt-4', got '%s'", c.Model)
}
if !c.SkipSSLVerify {
t.Error("expected skip_ssl_verify to be true")
}
if c.SystemPrompt != "You are helpful" {
t.Errorf("expected system_prompt 'You are helpful', got '%s'", c.SystemPrompt)
}
if c.InputPrompt != "Hello" {
t.Errorf("expected input_prompt 'Hello', got '%s'", c.InputPrompt)
}
if c.Temperature != 0.5 {
t.Errorf("expected temperature 0.5, got %f", c.Temperature)
}
if c.MaxTokens != 500 {
t.Errorf("expected max_tokens 500, got %d", c.MaxTokens)
}
},
},
{
name: "Default values",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
"INPUT_INPUT_PROMPT": "Hello",
},
expectError: false,
validate: func(t *testing.T, c *Config) {
if c.BaseURL != "https://api.openai.com/v1" {
t.Errorf("expected default base_url, got '%s'", c.BaseURL)
}
if c.Temperature != 0.7 {
t.Errorf("expected default temperature 0.7, got %f", c.Temperature)
}
if c.MaxTokens != 1000 {
t.Errorf("expected default max_tokens 1000, got %d", c.MaxTokens)
}
if c.SkipSSLVerify {
t.Error("expected default skip_ssl_verify to be false")
}
},
},
{
name: "Missing API key",
envVars: map[string]string{
"INPUT_INPUT_PROMPT": "Hello",
},
expectError: true,
},
{
name: "Missing input prompt",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
},
expectError: true,
},
{
name: "Invalid temperature",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
"INPUT_INPUT_PROMPT": "Hello",
"INPUT_TEMPERATURE": "invalid",
},
expectError: true,
},
{
name: "Invalid max tokens",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
"INPUT_INPUT_PROMPT": "Hello",
"INPUT_MAX_TOKENS": "abc",
},
expectError: true,
},
{
name: "Negative max tokens",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
"INPUT_INPUT_PROMPT": "Hello",
"INPUT_MAX_TOKENS": "-100",
},
expectError: true,
},
{
name: "Invalid skip SSL verify",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
"INPUT_INPUT_PROMPT": "Hello",
"INPUT_SKIP_SSL_VERIFY": "invalid",
},
expectError: true,
},
{
name: "Multiline system prompt",
envVars: map[string]string{
"INPUT_API_KEY": "test-key",
"INPUT_INPUT_PROMPT": "Hello",
"INPUT_SYSTEM_PROMPT": "You are a helpful assistant.\nProvide clear responses.\n\nAlways be concise.",
},
expectError: false,
validate: func(t *testing.T, c *Config) {
expected := "You are a helpful assistant.\nProvide clear responses.\n\nAlways be concise."
if c.SystemPrompt != expected {
t.Errorf("expected multiline system_prompt, got '%s'", c.SystemPrompt)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear all env vars
clearEnvVars()
// Set test env vars
for key, value := range tt.envVars {
os.Setenv(key, value)
}
// Load config
config, err := LoadConfig()
// Check error expectation
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
// Validate config if no error expected
if !tt.expectError && tt.validate != nil {
tt.validate(t, config)
}
// Cleanup
clearEnvVars()
})
}
}
func TestConfigParseTemperature(t *testing.T) {
tests := []struct {
name string
input string
expected float64
expectError bool
}{
{"Valid temperature", "0.5", 0.5, false},
{"Max temperature", "2.0", 2.0, false},
{"Min temperature", "0.0", 0.0, false},
{"Empty string", "", 0.7, false}, // should keep default
{"Invalid temperature", "invalid", 0.0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &Config{Temperature: 0.7}
err := config.parseTemperature(tt.input)
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !tt.expectError && config.Temperature != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, config.Temperature)
}
})
}
}
func TestConfigParseMaxTokens(t *testing.T) {
tests := []struct {
name string
input string
expected int
expectError bool
}{
{"Valid tokens", "500", 500, false},
{"Large tokens", "4000", 4000, false},
{"Empty string", "", 1000, false}, // should keep default
{"Invalid tokens", "abc", 0, true},
{"Negative tokens", "-100", 0, true},
{"Zero tokens", "0", 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &Config{MaxTokens: 1000}
err := config.parseMaxTokens(tt.input)
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !tt.expectError && config.MaxTokens != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, config.MaxTokens)
}
})
}
}
// boolParseTestCase defines test cases for boolean parsing functions
type boolParseTestCase struct {
name string
input string
expected bool
expectError bool
}
// getBoolParseTestCases returns standard test cases for boolean parsing
func getBoolParseTestCases() []boolParseTestCase {
return []boolParseTestCase{
{"True lowercase", "true", true, false},
{"True uppercase", "TRUE", true, false},
{"False lowercase", "false", false, false},
{"Numeric true", "1", true, false},
{"Numeric false", "0", false, false},
{"Empty string", "", false, false}, // should keep default
{"Invalid value", "invalid", false, true},
}
}
func TestConfigParseSkipSSL(t *testing.T) {
for _, tt := range getBoolParseTestCases() {
t.Run(tt.name, func(t *testing.T) {
config := &Config{SkipSSLVerify: false}
err := config.parseSkipSSL(tt.input)
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !tt.expectError && config.SkipSSLVerify != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, config.SkipSSLVerify)
}
})
}
}
func TestLoadConfigWithPromptFromFile(t *testing.T) {
// Create temporary directory and files
tmpDir := t.TempDir()
systemPromptFile := filepath.Join(tmpDir, "system.txt")
inputPromptFile := filepath.Join(tmpDir, "input.txt")
systemContent := "You are a code reviewer"
inputContent := "Review this code:\nfunc main() {}"
if err := os.WriteFile(systemPromptFile, []byte(systemContent), 0o600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
if err := os.WriteFile(inputPromptFile, []byte(inputContent), 0o600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
// Test loading from file paths
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_SYSTEM_PROMPT", systemPromptFile)
os.Setenv("INPUT_INPUT_PROMPT", inputPromptFile)
config, err := LoadConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if config.SystemPrompt != systemContent {
t.Errorf("expected system_prompt '%s', got '%s'", systemContent, config.SystemPrompt)
}
if config.InputPrompt != inputContent {
t.Errorf("expected input_prompt '%s', got '%s'", inputContent, config.InputPrompt)
}
clearEnvVars()
}
func TestLoadConfigWithPromptFromURL(t *testing.T) {
// Create test servers
systemServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("System prompt from URL")); err != nil {
t.Errorf("failed to write response: %v", err)
}
}),
)
defer systemServer.Close()
inputServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("Input prompt from URL")); err != nil {
t.Errorf("failed to write response: %v", err)
}
}),
)
defer inputServer.Close()
// Test loading from URLs
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_SYSTEM_PROMPT", systemServer.URL)
os.Setenv("INPUT_INPUT_PROMPT", inputServer.URL)
config, err := LoadConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if config.SystemPrompt != "System prompt from URL" {
t.Errorf("expected system_prompt 'System prompt from URL', got '%s'", config.SystemPrompt)
}
if config.InputPrompt != "Input prompt from URL" {
t.Errorf("expected input_prompt 'Input prompt from URL', got '%s'", config.InputPrompt)
}
clearEnvVars()
}
func TestLoadConfigWithInvalidPromptFile(t *testing.T) {
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", "file:///nonexistent/file.txt")
_, err := LoadConfig()
if err == nil {
t.Error("expected error for nonexistent file, got nil")
}
clearEnvVars()
}
func TestLoadConfigWithInvalidPromptURL(t *testing.T) {
// Create a server that returns 404
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}),
)
defer server.Close()
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", server.URL)
_, err := LoadConfig()
if err == nil {
t.Error("expected error for 404 URL, got nil")
}
clearEnvVars()
}
// Helper function to clear all env vars
func clearEnvVars() {
os.Unsetenv("INPUT_BASE_URL")
os.Unsetenv("INPUT_API_KEY")
os.Unsetenv("INPUT_MODEL")
os.Unsetenv("INPUT_SKIP_SSL_VERIFY")
os.Unsetenv("INPUT_CA_CERT")
os.Unsetenv("INPUT_SYSTEM_PROMPT")
os.Unsetenv("INPUT_INPUT_PROMPT")
os.Unsetenv("INPUT_TOOL_SCHEMA")
os.Unsetenv("INPUT_TEMPERATURE")
os.Unsetenv("INPUT_MAX_TOKENS")
os.Unsetenv("INPUT_DEBUG")
os.Unsetenv("INPUT_HEADERS")
}
// contentLoadTestCase represents a test case for content loading (CA cert, tool schema, etc.)
type contentLoadTestCase struct {
name string
inputValue string
expected string
expectError bool
}
// runContentLoadTests runs table-driven tests for content loading fields
func runContentLoadTests(
t *testing.T,
testContent string,
testFile string,
envVarName string,
getConfigValue func(*Config) string,
) {
t.Helper()
tests := []contentLoadTestCase{
{
name: "From direct content",
inputValue: testContent,
expected: testContent,
expectError: false,
},
{
name: "From file path",
inputValue: testFile,
expected: testContent,
expectError: false,
},
{
name: "From file:// URI",
inputValue: "file://" + testFile,
expected: testContent,
expectError: false,
},
{
name: "No value",
inputValue: "",
expected: "",
expectError: false,
},
{
name: "Invalid file path",
inputValue: "file:///nonexistent/file.txt",
expected: "",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", "Hello")
if tt.inputValue != "" {
os.Setenv(envVarName, tt.inputValue)
}
config, err := LoadConfig()
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
clearEnvVars()
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
clearEnvVars()
return
}
actual := getConfigValue(config)
if actual != tt.expected {
t.Errorf("expected '%s', got '%s'", tt.expected, actual)
}
clearEnvVars()
})
}
}
func TestLoadConfigWithCACert(t *testing.T) {
tmpDir := t.TempDir()
caCertFile := filepath.Join(tmpDir, "ca-cert.pem")
if err := os.WriteFile(caCertFile, []byte(testCACertContent), 0o600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
runContentLoadTests(t, testCACertContent, caCertFile, "INPUT_CA_CERT", func(c *Config) string {
return c.CACert
})
}
func TestLoadConfigWithCACertFromURL(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(testCACertContent)); err != nil {
t.Errorf("failed to write response: %v", err)
}
}),
)
defer server.Close()
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", "Hello")
os.Setenv("INPUT_CA_CERT", server.URL)
config, err := LoadConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if config.CACert != testCACertContent {
t.Errorf("expected ca_cert '%s', got '%s'", testCACertContent, config.CACert)
}
clearEnvVars()
}
func TestConfigParseDebug(t *testing.T) {
for _, tt := range getBoolParseTestCases() {
t.Run(tt.name, func(t *testing.T) {
config := &Config{Debug: false}
err := config.parseDebug(tt.input)
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !tt.expectError && config.Debug != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, config.Debug)
}
})
}
}
// testToolSchemaContent is a sample tool schema JSON for testing
const testToolSchemaContent = `{
"name": "get_city_info",
"description": "Get information about a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
}`
func TestLoadConfigWithToolSchema(t *testing.T) {
tmpDir := t.TempDir()
toolSchemaFile := filepath.Join(tmpDir, "tool-schema.json")
if err := os.WriteFile(toolSchemaFile, []byte(testToolSchemaContent), 0o600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
runContentLoadTests(
t,
testToolSchemaContent,
toolSchemaFile,
"INPUT_TOOL_SCHEMA",
func(c *Config) string {
return c.ToolSchema
},
)
}
func TestLoadConfigWithToolSchemaFromURL(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(testToolSchemaContent)); err != nil {
t.Errorf("failed to write response: %v", err)
}
}),
)
defer server.Close()
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", "Hello")
os.Setenv("INPUT_TOOL_SCHEMA", server.URL)
config, err := LoadConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if config.ToolSchema != testToolSchemaContent {
t.Errorf("expected tool_schema '%s', got '%s'", testToolSchemaContent, config.ToolSchema)
}
clearEnvVars()
}
func TestLoadConfigWithToolSchemaTemplate(t *testing.T) {
// Create temporary file with template content
tmpDir := t.TempDir()
templateFile := filepath.Join(tmpDir, "tool-schema-template.json")
templateContent := `{
"name": "{{.FUNCTION_NAME}}",
"description": "Get information",
"parameters": {
"type": "object",
"properties": {}
}
}`
if err := os.WriteFile(templateFile, []byte(templateContent), 0o600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", "Hello")
os.Setenv("INPUT_TOOL_SCHEMA", templateFile)
os.Setenv("INPUT_FUNCTION_NAME", "my_custom_function")
config, err := LoadConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := `{
"name": "my_custom_function",
"description": "Get information",
"parameters": {
"type": "object",
"properties": {}
}
}`
if config.ToolSchema != expected {
t.Errorf("expected tool_schema '%s', got '%s'", expected, config.ToolSchema)
}
clearEnvVars()
os.Unsetenv("INPUT_FUNCTION_NAME")
}
func TestConfigParseHeaders(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]string
expectError bool
}{
{
name: "Empty string",
input: "",
expected: nil,
expectError: false,
},
{
name: "Single header",
input: "X-Custom-Header:value123",
expected: map[string]string{
"X-Custom-Header": "value123",
},
expectError: false,
},
{
name: "Multiple headers comma separated",
input: "X-Request-ID:abc123,X-Trace-ID:trace456",
expected: map[string]string{
"X-Request-ID": "abc123",
"X-Trace-ID": "trace456",
},
expectError: false,
},
{
name: "Multiple headers newline separated",
input: "X-Request-ID:abc123\nX-Trace-ID:trace456",
expected: map[string]string{
"X-Request-ID": "abc123",
"X-Trace-ID": "trace456",
},
expectError: false,
},
{
name: "Headers with spaces",
input: " X-Header1 : value1 , X-Header2 : value2 ",
expected: map[string]string{
"X-Header1": "value1",
"X-Header2": "value2",
},
expectError: false,
},
{
name: "Value with colon",
input: "Authorization:Bearer:token:with:colons",
expected: map[string]string{
"Authorization": "Bearer:token:with:colons",
},
expectError: false,
},
{
name: "Empty value",
input: "X-Empty-Value:",
expected: map[string]string{
"X-Empty-Value": "",
},
expectError: false,
},
{
name: "Invalid format no colon",
input: "InvalidHeader",
expected: nil,
expectError: true,
},
{
name: "Empty key",
input: ":value",
expected: nil,
expectError: true,
},
{
name: "Skip empty entries",
input: "X-Header1:value1,,X-Header2:value2,",
expected: map[string]string{
"X-Header1": "value1",
"X-Header2": "value2",
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &Config{}
err := config.parseHeaders(tt.input)
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if tt.expected == nil {
if config.Headers != nil {
t.Errorf("expected nil Headers, got %v", config.Headers)
}
return
}
if len(config.Headers) != len(tt.expected) {
t.Errorf("expected %d headers, got %d", len(tt.expected), len(config.Headers))
return
}
for key, expectedValue := range tt.expected {
if actualValue, ok := config.Headers[key]; !ok {
t.Errorf("missing header key: %s", key)
} else if actualValue != expectedValue {
t.Errorf("header %s: expected '%s', got '%s'", key, expectedValue, actualValue)
}
}
})
}
}
func TestLoadConfigWithHeaders(t *testing.T) {
clearEnvVars()
os.Setenv("INPUT_API_KEY", "test-key")
os.Setenv("INPUT_INPUT_PROMPT", "Hello")
os.Setenv("INPUT_HEADERS", "X-Request-ID:test123,X-Trace-ID:trace456")
config, err := LoadConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := map[string]string{
"X-Request-ID": "test123",
"X-Trace-ID": "trace456",
}
if len(config.Headers) != len(expected) {
t.Errorf("expected %d headers, got %d", len(expected), len(config.Headers))
}
for key, expectedValue := range expected {
if actualValue, ok := config.Headers[key]; !ok {
t.Errorf("missing header key: %s", key)
} else if actualValue != expectedValue {
t.Errorf("header %s: expected '%s', got '%s'", key, expectedValue, actualValue)
}
}
clearEnvVars()
}