(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func TestToolsE2E(t *testing.T) { |
| 17 | ctx := testharness.NewTestContext(t) |
| 18 | client := ctx.NewClient() |
| 19 | t.Cleanup(func() { client.ForceStop() }) |
| 20 | |
| 21 | t.Run("invokes built-in tools", func(t *testing.T) { |
| 22 | ctx.ConfigureForTest(t) |
| 23 | |
| 24 | // Write a test file |
| 25 | err := os.WriteFile(filepath.Join(ctx.WorkDir, "README.md"), []byte("# ELIZA, the only chatbot you'll ever need"), 0644) |
| 26 | if err != nil { |
| 27 | t.Fatalf("Failed to write test file: %v", err) |
| 28 | } |
| 29 | |
| 30 | session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 31 | OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 32 | }) |
| 33 | if err != nil { |
| 34 | t.Fatalf("Failed to create session: %v", err) |
| 35 | } |
| 36 | |
| 37 | _, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "What's the first line of README.md in this directory?"}) |
| 38 | if err != nil { |
| 39 | t.Fatalf("Failed to send message: %v", err) |
| 40 | } |
| 41 | |
| 42 | answer, err := testharness.GetFinalAssistantMessage(t.Context(), session) |
| 43 | if err != nil { |
| 44 | t.Fatalf("Failed to get assistant message: %v", err) |
| 45 | } |
| 46 | |
| 47 | if md, ok := answer.Data.(*copilot.AssistantMessageData); !ok || !strings.Contains(md.Content, "ELIZA") { |
| 48 | t.Errorf("Expected answer to contain 'ELIZA', got %v", answer.Data) |
| 49 | } |
| 50 | }) |
| 51 | |
| 52 | t.Run("invokes custom tool", func(t *testing.T) { |
| 53 | ctx.ConfigureForTest(t) |
| 54 | |
| 55 | type EncryptParams struct { |
| 56 | Input string `json:"input" jsonschema:"String to encrypt"` |
| 57 | } |
| 58 | |
| 59 | session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 60 | OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 61 | Tools: []copilot.Tool{ |
| 62 | copilot.DefineTool("encrypt_string", "Encrypts a string", |
| 63 | func(params EncryptParams, inv copilot.ToolInvocation) (string, error) { |
| 64 | return strings.ToUpper(params.Input), nil |
| 65 | }), |
| 66 | }, |
| 67 | }) |
| 68 | if err != nil { |
| 69 | t.Fatalf("Failed to create session: %v", err) |
| 70 | } |
| 71 | |
| 72 | _, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "Use encrypt_string to encrypt this string: Hello"}) |
| 73 | if err != nil { |
nothing calls this directly
no test coverage detected
searching dependent graphs…