-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt_test.go
More file actions
426 lines (393 loc) · 11.6 KB
/
prompt_test.go
File metadata and controls
426 lines (393 loc) · 11.6 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
package syndicate
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
func TestCreateAndBuildSimplePrompt(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Introduction")
pb.AddText("Introduction", " Welcome to the system! ")
result := pb.Build()
expected := "<Introduction>\nWelcome to the system!\n</Introduction>"
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddSubSectionAndBuild(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Main")
pb.AddText("Main", "This is the main section.")
pb.AddSubSection("Details", "Main")
pb.AddText("Details", "Some details here.")
result := pb.Build()
expected := "<Main>\nThis is the main section.\n <Details>\n Some details here.\n </Details>\n</Main>"
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddTextFWithJSONConversion(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Data")
data := map[string]int{"count": 10}
pb.AddTextF("Data", data)
result := pb.Build()
jsonData, _ := json.Marshal(data)
expected := fmt.Sprintf("<Data>\n%s\n</Data>", string(jsonData))
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddListItemAndListItemF(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("ListSection")
pb.AddListItem("ListSection", "first item")
pb.AddListItem("ListSection", "second item")
pb.AddListItemF("ListSection", 123)
result := pb.Build()
expectedLines := []string{
"<ListSection>",
"1. first item",
"2. second item",
"3. 123",
"</ListSection>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestMultipleNestedSubsections(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Section1")
pb.AddText("Section1", "Line in Section1")
pb.AddSubSection("Sub1", "Section1")
pb.AddText("Sub1", "Line in Sub1")
pb.AddSubSection("SubSub1", "Sub1")
pb.AddText("SubSub1", "Deep line")
result := pb.Build()
expected := "<Section1>\nLine in Section1\n <Sub1>\n Line in Sub1\n <SubSub1>\n Deep line\n </SubSub1>\n </Sub1>\n</Section1>"
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestCreateDuplicateSection(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("DupSection")
pb.CreateSection("DupSection")
pb.AddText("DupSection", "Only once.")
result := pb.Build()
expected := "<DupSection>\nOnly once.\n</DupSection>"
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddTextToNonExistentSection(t *testing.T) {
pb := NewPromptBuilder()
pb.AddText("NonExistent", "Should not appear")
result := pb.Build()
if result != "" {
t.Errorf("Expected empty prompt, got: %s", result)
}
}
func TestBuildWithNoSections(t *testing.T) {
pb := NewPromptBuilder()
result := pb.Build()
if result != "" {
t.Errorf("Expected empty prompt for no sections, got: %s", result)
}
}
// New tests for markdown features
func TestAddBulletItem(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("BulletList")
pb.AddBulletItem("BulletList", "First bullet")
pb.AddBulletItem("BulletList", "Second bullet")
result := pb.Build()
expectedLines := []string{
"<BulletList>",
"- First bullet",
"- Second bullet",
"</BulletList>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddBulletItemF(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("BulletList")
pb.AddBulletItem("BulletList", "Text bullet")
pb.AddBulletItemF("BulletList", 456)
pb.AddBulletItemF("BulletList", map[string]string{"key": "value"})
result := pb.Build()
jsonData, _ := json.Marshal(map[string]string{"key": "value"})
expectedLines := []string{
"<BulletList>",
"- Text bullet",
"- 456",
fmt.Sprintf("- %s", string(jsonData)),
"</BulletList>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddCodeBlock(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("CodeSection")
pb.AddCodeBlock("CodeSection", "func main() {\n\tfmt.Println(\"Hello\")\n}", "go")
result := pb.Build()
expectedLines := []string{
"<CodeSection>",
"```go",
"func main() {",
"\tfmt.Println(\"Hello\")",
"}",
"```",
"</CodeSection>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddCodeBlockNoLanguage(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("CodeSection")
pb.AddCodeBlock("CodeSection", "plain text code", "")
result := pb.Build()
expectedLines := []string{
"<CodeSection>",
"```",
"plain text code",
"```",
"</CodeSection>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddCodeBlockF(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("CodeSection")
data := struct {
Name string
Age int
}{"John", 30}
pb.AddCodeBlockF("CodeSection", data, "json")
result := pb.Build()
jsonData, _ := json.Marshal(data)
expectedLines := []string{
"<CodeSection>",
"```json",
string(jsonData),
"```",
"</CodeSection>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddBoldText(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Formatting")
pb.AddBoldText("Formatting", "This is bold text")
result := pb.Build()
expectedLines := []string{
"<Formatting>",
"**This is bold text**",
"</Formatting>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddItalicText(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Formatting")
pb.AddItalicText("Formatting", "This is italic text")
result := pb.Build()
expectedLines := []string{
"<Formatting>",
"*This is italic text*",
"</Formatting>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddHeader(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Headers")
pb.AddHeader("Headers", "Level 1 Header", 1)
pb.AddHeader("Headers", "Level 3 Header", 3)
pb.AddHeader("Headers", "Too Large Level", 8) // Should limit to level 6
pb.AddHeader("Headers", "Too Small Level", 0) // Should default to level 1
result := pb.Build()
expectedLines := []string{
"<Headers>",
"# Level 1 Header",
"### Level 3 Header",
"###### Too Large Level",
"# Too Small Level",
"</Headers>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddBlockquote(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Quotes")
pb.AddBlockquote("Quotes", "This is a blockquote")
pb.AddBlockquote("Quotes", "Multi-line\nblockquote\nwith three lines")
result := pb.Build()
expectedLines := []string{
"<Quotes>",
"> This is a blockquote",
"> Multi-line",
"> blockquote",
"> with three lines",
"</Quotes>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddLink(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Links")
pb.AddLink("Links", "Go to Google", "https://www.google.com")
result := pb.Build()
expectedLines := []string{
"<Links>",
"[Go to Google](https://www.google.com)",
"</Links>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddHorizontalRule(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Divider")
pb.AddText("Divider", "Before divider")
pb.AddHorizontalRule("Divider")
pb.AddText("Divider", "After divider")
result := pb.Build()
expectedLines := []string{
"<Divider>",
"Before divider",
"---",
"After divider",
"</Divider>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddTable(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Tables")
headers := []string{"Name", "Age", "Role"}
rows := [][]string{
{"John", "30", "Developer"},
{"Jane", "28", "Designer"},
{"Bob", "35", "Manager"},
}
pb.AddTable("Tables", headers, rows)
result := pb.Build()
expectedLines := []string{
"<Tables>",
"| Name | Age | Role |",
"| --- | --- | --- |",
"| John | 30 | Developer |",
"| Jane | 28 | Designer |",
"| Bob | 35 | Manager |",
"</Tables>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestAddTableWithFewerColumns(t *testing.T) {
pb := NewPromptBuilder()
pb.CreateSection("Tables")
headers := []string{"Name", "Age", "Role"}
rows := [][]string{
{"John", "30"}, // Missing Role
{"Jane"}, // Missing Age and Role
{"Bob", "35", "Manager", "Extra"}, // Extra column
}
pb.AddTable("Tables", headers, rows)
result := pb.Build()
expectedLines := []string{
"<Tables>",
"| Name | Age | Role |",
"| --- | --- | --- |",
"| John | 30 | |",
"| Jane | | |",
"| Bob | 35 | Manager |",
"</Tables>",
}
expected := strings.Join(expectedLines, "\n")
if result != expected {
t.Errorf("Expected:\n%s\ngot:\n%s", expected, result)
}
}
func TestComplexPromptBuilder(t *testing.T) {
pb := NewPromptBuilder()
// Create a comprehensive prompt with various markdown features
pb.CreateSection("Instructions")
pb.AddHeader("Instructions", "Task Instructions", 2)
pb.AddText("Instructions", "Please follow these instructions carefully:")
pb.AddBulletItem("Instructions", "Review the provided code")
pb.AddBulletItem("Instructions", "Identify any bugs or issues")
pb.AddBulletItem("Instructions", "Suggest improvements")
pb.CreateSection("CodeExample")
pb.AddText("CodeExample", "Here's the code to review:")
pb.AddCodeBlock("CodeExample", "func sum(a, b int) int {\n return a - b // Bug: This should be a + b\n}", "go")
pb.CreateSection("Guidelines")
pb.AddBoldText("Guidelines", "Important points to consider:")
pb.AddListItem("Guidelines", "Check for off-by-one errors")
pb.AddListItem("Guidelines", "Ensure proper error handling")
pb.AddBlockquote("Guidelines", "Remember that clean code is more important than clever code")
pb.CreateSection("References")
pb.AddLink("References", "Go Style Guide", "https://golang.org/doc/effective_go")
pb.AddHorizontalRule("References")
pb.AddTable("References",
[]string{"Resource", "URL", "Notes"},
[][]string{
{"Go Documentation", "https://golang.org/doc", "Official docs"},
{"Go by Example", "https://gobyexample.com", "Practical examples"},
})
result := pb.Build()
// This test ensures that a complex prompt with all features compiles
// without error and has the expected number of sections
if !strings.Contains(result, "<Instructions>") ||
!strings.Contains(result, "<CodeExample>") ||
!strings.Contains(result, "<Guidelines>") ||
!strings.Contains(result, "<References>") {
t.Errorf("Complex prompt missing expected sections")
}
// Check for specific markdown elements
if !strings.Contains(result, "## Task Instructions") ||
!strings.Contains(result, "```go") ||
!strings.Contains(result, "**Important points to consider:**") ||
!strings.Contains(result, "> Remember that clean code") {
t.Errorf("Complex prompt missing expected markdown elements")
}
}