(t *testing.T)
| 29 | } |
| 30 | |
| 31 | func TestAttachFile(t *testing.T) { |
| 32 | t.Parallel() |
| 33 | |
| 34 | t.Run("EmptyPathReturnsError", func(t *testing.T) { |
| 35 | t.Parallel() |
| 36 | ctrl := gomock.NewController(t) |
| 37 | mockConn := agentconnmock.NewMockAgentConn(ctrl) |
| 38 | tool := newAttachFileTool(t, mockConn, func(_ context.Context, _ string, _ string, _ []byte) (chattool.AttachmentMetadata, error) { |
| 39 | return chattool.AttachmentMetadata{}, nil |
| 40 | }) |
| 41 | resp, err := tool.Run(context.Background(), fantasy.ToolCall{ |
| 42 | ID: "call-1", Name: "attach_file", Input: `{"path":""}`, |
| 43 | }) |
| 44 | require.NoError(t, err) |
| 45 | assert.True(t, resp.IsError) |
| 46 | assert.Contains(t, resp.Content, "path is required") |
| 47 | }) |
| 48 | |
| 49 | t.Run("RelativePathErrorComesFromAgent", func(t *testing.T) { |
| 50 | t.Parallel() |
| 51 | ctrl := gomock.NewController(t) |
| 52 | mockConn := agentconnmock.NewMockAgentConn(ctrl) |
| 53 | mockConn.EXPECT(). |
| 54 | ReadFile(gomock.Any(), "notes.txt", int64(0), int64(10<<20+1)). |
| 55 | Return(nil, "", xerrors.New(`file path must be absolute: "notes.txt"`)) |
| 56 | tool := newAttachFileTool(t, mockConn, func(_ context.Context, _ string, _ string, _ []byte) (chattool.AttachmentMetadata, error) { |
| 57 | return chattool.AttachmentMetadata{}, nil |
| 58 | }) |
| 59 | resp, err := tool.Run(context.Background(), fantasy.ToolCall{ |
| 60 | ID: "call-1", Name: "attach_file", Input: `{"path":"notes.txt"}`, |
| 61 | }) |
| 62 | require.NoError(t, err) |
| 63 | assert.True(t, resp.IsError) |
| 64 | assert.Contains(t, resp.Content, `file path must be absolute: "notes.txt"`) |
| 65 | }) |
| 66 | |
| 67 | t.Run("ValidTextFileStoresAttachment", func(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | ctrl := gomock.NewController(t) |
| 70 | mockConn := agentconnmock.NewMockAgentConn(ctrl) |
| 71 | content := "build succeeded\n" |
| 72 | mockConn.EXPECT(). |
| 73 | ReadFile(gomock.Any(), "/home/coder/build.log", int64(0), int64(10<<20+1)). |
| 74 | Return(io.NopCloser(strings.NewReader(content)), "text/plain", nil) |
| 75 | |
| 76 | var storedName string |
| 77 | var storedType string |
| 78 | var storedData []byte |
| 79 | tool := newAttachFileTool(t, mockConn, func(_ context.Context, name string, detectName string, data []byte) (chattool.AttachmentMetadata, error) { |
| 80 | storedName = name |
| 81 | require.Equal(t, "/home/coder/build.log", detectName) |
| 82 | storedType = "text/plain" |
| 83 | storedData = append([]byte(nil), data...) |
| 84 | return chattool.AttachmentMetadata{ |
| 85 | FileID: uuid.MustParse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), |
| 86 | MediaType: storedType, |
| 87 | Name: name, |
| 88 | }, nil |
nothing calls this directly
no test coverage detected