Skip to content

Commit

Permalink
chore: add test for prompt with metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Aug 20, 2024
1 parent 14a861d commit 5924712
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions gptscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,68 @@ func TestPrompt(t *testing.T) {
}
}

func TestPromptWithMetadata(t *testing.T) {
run, err := g.Run(context.Background(), "sys.prompt", Options{IncludeEvents: true, Prompt: true, Input: `{"fields":"first name","metadata":{"key":"value"}}`})
if err != nil {
t.Errorf("Error executing tool: %v", err)
}

// Wait for the prompt event
var promptFrame *PromptFrame
for e := range run.Events() {
if e.Prompt != nil {
if e.Prompt.Type == EventTypePrompt {
promptFrame = e.Prompt
break
}
}
}

if promptFrame == nil {
t.Fatalf("No prompt call event")
}

if promptFrame.Sensitive {
t.Errorf("Unexpected sensitive prompt event: %v", promptFrame.Sensitive)
}

if len(promptFrame.Fields) != 1 {
t.Fatalf("Unexpected number of fields: %d", len(promptFrame.Fields))
}

if promptFrame.Fields[0] != "first name" {
t.Errorf("Unexpected field: %s", promptFrame.Fields[0])
}

if promptFrame.Metadata["key"] != "value" {
t.Errorf("Unexpected metadata: %v", promptFrame.Metadata)
}

if err = g.PromptResponse(context.Background(), PromptResponse{
ID: promptFrame.ID,
Responses: map[string]string{promptFrame.Fields[0]: "Clicky"},
}); err != nil {
t.Errorf("Error responding: %v", err)
}

// Read the remainder of the events
for range run.Events() {

Check failure on line 1155 in gptscript_test.go

View workflow job for this annotation

GitHub Actions / run-tests / test-linux

empty-block: this block is empty, you can remove it (revive)
}

out, err := run.Text()
if err != nil {
t.Errorf("Error reading output: %v", err)
}

if !strings.Contains(out, "Clicky") {
t.Errorf("Unexpected output: %s", out)
}

if len(run.ErrorOutput()) != 0 {
t.Errorf("Should have no stderr output: %v", run.ErrorOutput())
}
}

func TestPromptWithoutPromptAllowed(t *testing.T) {
tools := ToolDef{
Instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
Expand Down

0 comments on commit 5924712

Please sign in to comment.