8
8
"runtime"
9
9
"strings"
10
10
"testing"
11
+
12
+ "github.com/getkin/kin-openapi/openapi3"
11
13
)
12
14
13
15
func TestMain (m * testing.M ) {
@@ -77,7 +79,7 @@ func TestExecFileChdir(t *testing.T) {
77
79
}
78
80
79
81
func TestExecComplexTool (t * testing.T ) {
80
- tool := & Tool {
82
+ tool := & SimpleTool {
81
83
JSONResponse : true ,
82
84
Instructions : `
83
85
Create three short graphic artist descriptions and their muses.
@@ -110,11 +112,11 @@ func TestExecWithToolList(t *testing.T) {
110
112
shebang = "#!/usr/bin/env powershell.exe"
111
113
}
112
114
tools := []fmt.Stringer {
113
- & Tool {
115
+ & SimpleTool {
114
116
Tools : []string {"echo" },
115
117
Instructions : "echo hello there" ,
116
118
},
117
- & Tool {
119
+ & SimpleTool {
118
120
Name : "echo" ,
119
121
Tools : []string {"sys.exec" },
120
122
Description : "Echoes the input" ,
@@ -141,16 +143,16 @@ func TestExecWithToolListAndSubTool(t *testing.T) {
141
143
shebang = "#!/usr/bin/env powershell.exe"
142
144
}
143
145
tools := []fmt.Stringer {
144
- & Tool {
146
+ & SimpleTool {
145
147
Tools : []string {"echo" },
146
148
Instructions : "echo hello there" ,
147
149
},
148
- & Tool {
150
+ & SimpleTool {
149
151
Name : "other" ,
150
152
Tools : []string {"echo" },
151
153
Instructions : "echo hello somewhere else" ,
152
154
},
153
- & Tool {
155
+ & SimpleTool {
154
156
Name : "echo" ,
155
157
Tools : []string {"sys.exec" },
156
158
Description : "Echoes the input" ,
@@ -296,3 +298,205 @@ func TestStreamExecFileWithEvents(t *testing.T) {
296
298
t .Error ("No events output" )
297
299
}
298
300
}
301
+
302
+ func TestParseSimpleFile (t * testing.T ) {
303
+ tools , err := Parse (context .Background (), "./test/test.gpt" , Opts {})
304
+ if err != nil {
305
+ t .Errorf ("Error parsing file: %v" , err )
306
+ }
307
+
308
+ if len (tools ) != 1 {
309
+ t .Errorf ("Unexpected number of tools: %d" , len (tools ))
310
+ }
311
+
312
+ if tools [0 ].ToolNode == nil {
313
+ t .Error ("No tool node found" )
314
+ }
315
+
316
+ if tools [0 ].ToolNode .Tool .Instructions != "Respond with a hello, in a random language. Also include the language in the response." {
317
+ t .Errorf ("Unexpected instructions: %s" , tools [0 ].ToolNode .Tool .Instructions )
318
+ }
319
+ }
320
+
321
+ func TestParseSimpleFileWithChdir (t * testing.T ) {
322
+ tools , err := Parse (context .Background (), "./test.gpt" , Opts {Chdir : "./test" })
323
+ if err != nil {
324
+ t .Errorf ("Error parsing file: %v" , err )
325
+ }
326
+
327
+ if len (tools ) != 1 {
328
+ t .Errorf ("Unexpected number of tools: %d" , len (tools ))
329
+ }
330
+
331
+ if tools [0 ].ToolNode == nil {
332
+ t .Error ("No tool node found" )
333
+ }
334
+
335
+ if tools [0 ].ToolNode .Tool .Instructions != "Respond with a hello, in a random language. Also include the language in the response." {
336
+ t .Errorf ("Unexpected instructions: %s" , tools [0 ].ToolNode .Tool .Instructions )
337
+ }
338
+ }
339
+
340
+ func TestParseTool (t * testing.T ) {
341
+ tools , err := ParseTool (context .Background (), "echo hello" )
342
+ if err != nil {
343
+ t .Errorf ("Error parsing tool: %v" , err )
344
+ }
345
+
346
+ if len (tools ) != 1 {
347
+ t .Errorf ("Unexpected number of tools: %d" , len (tools ))
348
+ }
349
+
350
+ if tools [0 ].ToolNode == nil {
351
+ t .Error ("No tool node found" )
352
+ }
353
+
354
+ if tools [0 ].ToolNode .Tool .Instructions != "echo hello" {
355
+ t .Errorf ("Unexpected instructions: %s" , tools [0 ].ToolNode .Tool .Instructions )
356
+ }
357
+ }
358
+
359
+ func TestParseToolWithTextNode (t * testing.T ) {
360
+ tools , err := ParseTool (context .Background (), "echo hello\n ---\n !markdown\n hello" )
361
+ if err != nil {
362
+ t .Errorf ("Error parsing tool: %v" , err )
363
+ }
364
+
365
+ if len (tools ) != 2 {
366
+ t .Errorf ("Unexpected number of tools: %d" , len (tools ))
367
+ }
368
+
369
+ if tools [0 ].ToolNode == nil {
370
+ t .Error ("No tool node found" )
371
+ }
372
+
373
+ if tools [0 ].ToolNode .Tool .Instructions != "echo hello" {
374
+ t .Errorf ("Unexpected instructions: %s" , tools [0 ].ToolNode .Tool .Instructions )
375
+ }
376
+
377
+ if tools [1 ].TextNode == nil {
378
+ t .Error ("No text node found" )
379
+ }
380
+
381
+ if tools [1 ].TextNode .Text != "!markdown\n hello\n " {
382
+ t .Errorf ("Unexpected text: %s" , tools [1 ].TextNode .Text )
383
+ }
384
+ }
385
+
386
+ func TestFmt (t * testing.T ) {
387
+ nodes := []Node {
388
+ {
389
+ ToolNode : & ToolNode {
390
+ Tool : Tool {
391
+ Parameters : Parameters {
392
+ Tools : []string {"echo" },
393
+ },
394
+ Instructions : "echo hello there" ,
395
+ },
396
+ },
397
+ },
398
+ {
399
+ ToolNode : & ToolNode {
400
+ Tool : Tool {
401
+ Parameters : Parameters {
402
+ Name : "echo" ,
403
+ Arguments : & openapi3.Schema {
404
+ Type : "object" ,
405
+ Properties : map [string ]* openapi3.SchemaRef {
406
+ "input" : {
407
+ Value : & openapi3.Schema {
408
+ Description : "The string input to echo" ,
409
+ Type : "string" ,
410
+ },
411
+ },
412
+ },
413
+ },
414
+ },
415
+ Instructions : "#!/bin/bash\n echo hello there" ,
416
+ },
417
+ },
418
+ },
419
+ }
420
+
421
+ out , err := Fmt (context .Background (), nodes )
422
+ if err != nil {
423
+ t .Errorf ("Error formatting nodes: %v" , err )
424
+ }
425
+
426
+ if out != `Tools: echo
427
+
428
+ echo hello there
429
+
430
+ ---
431
+ Name: echo
432
+ Args: input: The string input to echo
433
+
434
+ #!/bin/bash
435
+ echo hello there
436
+ ` {
437
+ t .Errorf ("Unexpected output: %s" , out )
438
+ }
439
+ }
440
+
441
+ func TestFmtWithTextNode (t * testing.T ) {
442
+ nodes := []Node {
443
+ {
444
+ ToolNode : & ToolNode {
445
+ Tool : Tool {
446
+ Parameters : Parameters {
447
+ Tools : []string {"echo" },
448
+ },
449
+ Instructions : "echo hello there" ,
450
+ },
451
+ },
452
+ },
453
+ {
454
+ TextNode : & TextNode {
455
+ Text : "!markdown\n We now echo hello there\n " ,
456
+ },
457
+ },
458
+ {
459
+ ToolNode : & ToolNode {
460
+ Tool : Tool {
461
+ Parameters : Parameters {
462
+ Name : "echo" ,
463
+ Arguments : & openapi3.Schema {
464
+ Type : "object" ,
465
+ Properties : map [string ]* openapi3.SchemaRef {
466
+ "input" : {
467
+ Value : & openapi3.Schema {
468
+ Description : "The string input to echo" ,
469
+ Type : "string" ,
470
+ },
471
+ },
472
+ },
473
+ },
474
+ },
475
+ Instructions : "#!/bin/bash\n echo hello there" ,
476
+ },
477
+ },
478
+ },
479
+ }
480
+
481
+ out , err := Fmt (context .Background (), nodes )
482
+ if err != nil {
483
+ t .Errorf ("Error formatting nodes: %v" , err )
484
+ }
485
+
486
+ if out != `Tools: echo
487
+
488
+ echo hello there
489
+
490
+ ---
491
+ !markdown
492
+ We now echo hello there
493
+ ---
494
+ Name: echo
495
+ Args: input: The string input to echo
496
+
497
+ #!/bin/bash
498
+ echo hello there
499
+ ` {
500
+ t .Errorf ("Unexpected output: %s" , out )
501
+ }
502
+ }
0 commit comments