MCPcopy Index your code
hub / github.com/coder/coder / TestAttachFile

Function TestAttachFile

coderd/x/chatd/chattool/attachfile_test.go:31–269  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func 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

Callers

nothing calls this directly

Calls 14

EXPECTMethod · 0.95
NewMockAgentConnFunction · 0.92
AttachmentsFromMetadataFunction · 0.92
newAttachFileToolFunction · 0.85
decodeAttachFileResponseFunction · 0.85
FatalMethod · 0.80
RunMethod · 0.65
ReadFileMethod · 0.65
NewMethod · 0.65
ContainsMethod · 0.45
EqualMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected