-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_test.go
More file actions
103 lines (88 loc) · 2.15 KB
/
prompt_test.go
File metadata and controls
103 lines (88 loc) · 2.15 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
package belaykit
import (
"strings"
"testing"
"testing/fstest"
"text/template"
)
func TestLoadPromptTemplate(t *testing.T) {
fsys := fstest.MapFS{
"test.md": &fstest.MapFile{
Data: []byte("Hello {{.Name}}, you have {{.Count}} items."),
},
}
pt, err := LoadPromptTemplate(fsys, "test.md", nil)
if err != nil {
t.Fatalf("LoadPromptTemplate failed: %v", err)
}
result, err := pt.Render(struct {
Name string
Count int
}{Name: "Alice", Count: 3})
if err != nil {
t.Fatalf("Render failed: %v", err)
}
expected := "Hello Alice, you have 3 items."
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestLoadPromptTemplateWithFuncMap(t *testing.T) {
fsys := fstest.MapFS{
"test.md": &fstest.MapFile{
Data: []byte("Items: {{join .Items \", \"}}"),
},
}
funcMap := template.FuncMap{
"join": func(items []string, sep string) string {
return strings.Join(items, sep)
},
}
pt, err := LoadPromptTemplate(fsys, "test.md", funcMap)
if err != nil {
t.Fatalf("LoadPromptTemplate failed: %v", err)
}
result, err := pt.Render(struct {
Items []string
}{Items: []string{"a", "b", "c"}})
if err != nil {
t.Fatalf("Render failed: %v", err)
}
expected := "Items: a, b, c"
if result != expected {
t.Errorf("got %q, want %q", result, expected)
}
}
func TestLoadPromptTemplateNotFound(t *testing.T) {
fsys := fstest.MapFS{}
_, err := LoadPromptTemplate(fsys, "missing.md", nil)
if err == nil {
t.Error("expected error for missing template")
}
}
func TestLoadPromptTemplateInvalidSyntax(t *testing.T) {
fsys := fstest.MapFS{
"bad.md": &fstest.MapFile{
Data: []byte("{{.Unclosed"),
},
}
_, err := LoadPromptTemplate(fsys, "bad.md", nil)
if err == nil {
t.Error("expected error for invalid template syntax")
}
}
func TestPromptTemplateRenderError(t *testing.T) {
fsys := fstest.MapFS{
"test.md": &fstest.MapFile{
Data: []byte("{{.Missing.Field}}"),
},
}
pt, err := LoadPromptTemplate(fsys, "test.md", nil)
if err != nil {
t.Fatalf("LoadPromptTemplate failed: %v", err)
}
_, err = pt.Render(struct{}{})
if err == nil {
t.Error("expected error for missing field in template")
}
}